app3

Dependencies:   mbed

Fork of Code_APP1 by Louis Beaulieu

Committer:
ericbisson
Date:
Mon Jan 16 21:37:15 2017 +0000
Revision:
6:2ec2a74c807f
Parent:
5:1a60144f7163
Child:
7:a57409bdf777
merge

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LouBe4 0:52fba6f1554b 1 #include "mbed.h"
ericbisson 5:1a60144f7163 2 #include "header.h"
LouBe4 0:52fba6f1554b 3
LouBe4 0:52fba6f1554b 4 Serial pc(USBTX, USBRX);
LouBe4 0:52fba6f1554b 5 SPI spi(p11, p12, p13);
LouBe4 0:52fba6f1554b 6 DigitalOut cs(p14);
LouBe4 0:52fba6f1554b 7 I2C i2c(p28, p27);
LouBe4 0:52fba6f1554b 8 PwmOut led1(LED1);
LouBe4 1:3430643e8ed4 9
LouBe4 3:5dcf9b78f3ad 10 void calculer_angle(char bufferAngle[], int accZ)
LouBe4 1:3430643e8ed4 11 {
ericbisson 5:1a60144f7163 12 float angle = abs(cos(static_cast<float>(moyenne_mobile(accZ)*90/64)*PI/180)*90);
LouBe4 3:5dcf9b78f3ad 13 int angleInt = (int)(angle*100);
LouBe4 4:ffd7cef72270 14 snprintf(bufferAngle, 5, "%d", angleInt);
LouBe4 3:5dcf9b78f3ad 15
LouBe4 3:5dcf9b78f3ad 16 if(angle < 10)
LouBe4 3:5dcf9b78f3ad 17 {
LouBe4 3:5dcf9b78f3ad 18 bufferAngle[3] = bufferAngle[2];
LouBe4 3:5dcf9b78f3ad 19 bufferAngle[2] = bufferAngle[1];
LouBe4 3:5dcf9b78f3ad 20 bufferAngle[1] = bufferAngle[0];
LouBe4 3:5dcf9b78f3ad 21 bufferAngle[0] = '0';
ericbisson 5:1a60144f7163 22 }
LouBe4 1:3430643e8ed4 23 }
LouBe4 0:52fba6f1554b 24
LouBe4 0:52fba6f1554b 25 int main() {
ericbisson 5:1a60144f7163 26 int addrChip = 0x3A;
ericbisson 5:1a60144f7163 27 char buffer[3] = {0,0,0};
ericbisson 5:1a60144f7163 28
ericbisson 6:2ec2a74c807f 29 *power_ptr = PCUART0;
ericbisson 6:2ec2a74c807f 30 *enableDataFlow_ptr = TXE0;
ericbisson 6:2ec2a74c807f 31 *clk_ptr = PCLK_UART0;
ericbisson 6:2ec2a74c807f 32 *baudRate_ptr = DLAB_ACTIVATE;
ericbisson 6:2ec2a74c807f 33 *baudRate_ptr = DLMSB;
ericbisson 6:2ec2a74c807f 34 *baudRate_ptr = DLLSB;
ericbisson 6:2ec2a74c807f 35 *enableFIFO_ptr = FIFO_ACTIVATE;
ericbisson 6:2ec2a74c807f 36 *pinSelect_ptr = PIN9_TX;
ericbisson 6:2ec2a74c807f 37
ericbisson 5:1a60144f7163 38 SetClockAndMode(250000, 0);
ericbisson 5:1a60144f7163 39 change_dots(0x02);
ericbisson 5:1a60144f7163 40
ericbisson 5:1a60144f7163 41 //Activer l'accéléromètre pour lecture 8 bits
ericbisson 5:1a60144f7163 42 char activation[2] = {0x2A, 0x03};
ericbisson 5:1a60144f7163 43 char fullScale[2] = {0x0E, 0x00};
ericbisson 5:1a60144f7163 44 char resultat[5] = {0};
ericbisson 5:1a60144f7163 45 i2c.write(addrChip, activation, 2, true);
ericbisson 5:1a60144f7163 46 i2c.write(addrChip, fullScale, 2, true);
ericbisson 5:1a60144f7163 47
LouBe4 3:5dcf9b78f3ad 48 while(1)
ericbisson 5:1a60144f7163 49 {
LouBe4 3:5dcf9b78f3ad 50 //Aller lire les valeurs d'accélération
LouBe4 3:5dcf9b78f3ad 51 buffer[0] = 0x01;
LouBe4 3:5dcf9b78f3ad 52 i2c.write(addrChip, buffer, 1, true);
LouBe4 3:5dcf9b78f3ad 53 i2c.read(addrChip, buffer, 3);
LouBe4 3:5dcf9b78f3ad 54 calculer_angle(resultat, buffer[2]);
LouBe4 3:5dcf9b78f3ad 55
ericbisson 5:1a60144f7163 56 chip_select(false);
ericbisson 5:1a60144f7163 57 write_to_7segment(resultat[0],resultat[1],resultat[2],resultat[3]);
ericbisson 5:1a60144f7163 58 chip_select(true);
ericbisson 5:1a60144f7163 59 wait(0.1);
ericbisson 5:1a60144f7163 60 }
ericbisson 5:1a60144f7163 61 }
ericbisson 5:1a60144f7163 62
ericbisson 5:1a60144f7163 63 void SetClockAndMode(int Speed, char Mode)
ericbisson 5:1a60144f7163 64 {
ericbisson 5:1a60144f7163 65 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 66 {
ericbisson 5:1a60144f7163 67 spi.format(8,Mode);
ericbisson 5:1a60144f7163 68 spi.frequency(Speed);
ericbisson 5:1a60144f7163 69 }
ericbisson 5:1a60144f7163 70 else
ericbisson 5:1a60144f7163 71 {
ericbisson 5:1a60144f7163 72 // TODO
ericbisson 5:1a60144f7163 73 }
ericbisson 5:1a60144f7163 74 }
ericbisson 5:1a60144f7163 75
ericbisson 5:1a60144f7163 76 // function to change displayed dots
ericbisson 5:1a60144f7163 77 void change_dots(char dot)
ericbisson 5:1a60144f7163 78 {
ericbisson 5:1a60144f7163 79 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 80 {
LouBe4 3:5dcf9b78f3ad 81 spi.write(0x77);
ericbisson 5:1a60144f7163 82 spi.write(dot);
ericbisson 5:1a60144f7163 83 }
ericbisson 5:1a60144f7163 84 else
ericbisson 5:1a60144f7163 85 {
ericbisson 5:1a60144f7163 86 // TODO
ericbisson 5:1a60144f7163 87 }
ericbisson 5:1a60144f7163 88 }
ericbisson 5:1a60144f7163 89
ericbisson 5:1a60144f7163 90 void ResetCursor()
ericbisson 5:1a60144f7163 91 {
ericbisson 5:1a60144f7163 92 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 93 {
ericbisson 5:1a60144f7163 94 spi.write(0x79);
ericbisson 5:1a60144f7163 95 spi.write(0);
ericbisson 5:1a60144f7163 96 }
ericbisson 5:1a60144f7163 97 else
ericbisson 5:1a60144f7163 98 {
ericbisson 5:1a60144f7163 99 // TODO
LouBe4 3:5dcf9b78f3ad 100 }
LouBe4 0:52fba6f1554b 101 }
ericbisson 5:1a60144f7163 102
ericbisson 5:1a60144f7163 103 // function used to write numbers to all four digits
ericbisson 5:1a60144f7163 104 void write_to_7segment(char d1, char d2, char d3, char d4)
ericbisson 5:1a60144f7163 105 {
ericbisson 5:1a60144f7163 106 ResetCursor();
ericbisson 5:1a60144f7163 107 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 108 {
ericbisson 5:1a60144f7163 109 spi.write(d1);
ericbisson 5:1a60144f7163 110 spi.write(d2);
ericbisson 5:1a60144f7163 111 spi.write(d3);
ericbisson 5:1a60144f7163 112 spi.write(d4);
ericbisson 5:1a60144f7163 113 }
ericbisson 5:1a60144f7163 114 else
ericbisson 5:1a60144f7163 115 {
ericbisson 5:1a60144f7163 116 // TODO
ericbisson 5:1a60144f7163 117 }
ericbisson 5:1a60144f7163 118 }
ericbisson 5:1a60144f7163 119
ericbisson 5:1a60144f7163 120 void chip_select(bool bSet)
ericbisson 5:1a60144f7163 121 {
ericbisson 5:1a60144f7163 122 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 123 {
ericbisson 5:1a60144f7163 124 if (!bSet)
ericbisson 5:1a60144f7163 125 {
ericbisson 5:1a60144f7163 126 wait_us(25);
ericbisson 5:1a60144f7163 127 }
ericbisson 5:1a60144f7163 128 cs = bSet;
ericbisson 5:1a60144f7163 129 if (bSet)
ericbisson 5:1a60144f7163 130 {
ericbisson 5:1a60144f7163 131 wait_us(25);
ericbisson 5:1a60144f7163 132 }
ericbisson 5:1a60144f7163 133 }
ericbisson 5:1a60144f7163 134 }
ericbisson 5:1a60144f7163 135
ericbisson 5:1a60144f7163 136 // function used to calculate and return the new value of a moving average
ericbisson 5:1a60144f7163 137 int moyenne_mobile(int newData)
ericbisson 5:1a60144f7163 138 {
ericbisson 5:1a60144f7163 139 int sum = 0;
ericbisson 5:1a60144f7163 140 MovingAverage.buffer[MovingAverage.cursor] = newData;
ericbisson 5:1a60144f7163 141 MovingAverage.cursor++;
ericbisson 5:1a60144f7163 142 if (MovingAverage.cursor >= MOVING_AVG_SIZE)
ericbisson 5:1a60144f7163 143 {
ericbisson 5:1a60144f7163 144 MovingAverage.cursor = 0;
ericbisson 5:1a60144f7163 145 MovingAverage.bFilled = true;
ericbisson 5:1a60144f7163 146 }
ericbisson 5:1a60144f7163 147
ericbisson 5:1a60144f7163 148 if (MovingAverage.bFilled)
ericbisson 5:1a60144f7163 149 {
ericbisson 5:1a60144f7163 150 for (int i = 0; i < MOVING_AVG_SIZE; i++)
ericbisson 5:1a60144f7163 151 {
ericbisson 5:1a60144f7163 152 sum += MovingAverage.buffer[i];
ericbisson 5:1a60144f7163 153 }
ericbisson 5:1a60144f7163 154 sum = sum / MOVING_AVG_SIZE;
ericbisson 5:1a60144f7163 155 }
ericbisson 5:1a60144f7163 156 else
ericbisson 5:1a60144f7163 157 {
ericbisson 5:1a60144f7163 158 for (int i = 0; i < MovingAverage.cursor; i++)
ericbisson 5:1a60144f7163 159 {
ericbisson 5:1a60144f7163 160 sum += MovingAverage.buffer[i];
ericbisson 5:1a60144f7163 161 }
ericbisson 5:1a60144f7163 162 sum = sum / MovingAverage.cursor;
ericbisson 5:1a60144f7163 163 }
ericbisson 5:1a60144f7163 164
ericbisson 5:1a60144f7163 165 return sum;
ericbisson 5:1a60144f7163 166 }