app3

Dependencies:   mbed

Fork of Code_APP1 by Louis Beaulieu

Committer:
ericbisson
Date:
Mon Jan 16 22:23:21 2017 +0000
Revision:
7:a57409bdf777
Parent:
6:2ec2a74c807f
Child:
8:6f2b7f9b0d0d
scrap

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 7:a57409bdf777 28 //*clk_ptr |= PCLK_UART0;
ericbisson 7:a57409bdf777 29 pc.printf("main() clk_ptr");
ericbisson 7:a57409bdf777 30 //*baudRate_ptr |= DLAB_ACTIVATE;
ericbisson 7:a57409bdf777 31 pc.printf("main() DLAB_ACTIVATE");
ericbisson 7:a57409bdf777 32 //*baudRate_ptr |= DLMSB;
ericbisson 7:a57409bdf777 33 pc.printf("main() DLMSB");
ericbisson 7:a57409bdf777 34 //*baudRate_ptr |= DLLSB;
ericbisson 7:a57409bdf777 35 pc.printf("main() DLLSB");
ericbisson 7:a57409bdf777 36 //*enableFIFO_ptr |= FIFO_ACTIVATE;
ericbisson 7:a57409bdf777 37 pc.printf("main() FIFO_ACTIVATE");
ericbisson 6:2ec2a74c807f 38
ericbisson 5:1a60144f7163 39 SetClockAndMode(250000, 0);
ericbisson 5:1a60144f7163 40 change_dots(0x02);
ericbisson 5:1a60144f7163 41
ericbisson 5:1a60144f7163 42 //Activer l'accéléromètre pour lecture 8 bits
ericbisson 5:1a60144f7163 43 char activation[2] = {0x2A, 0x03};
ericbisson 5:1a60144f7163 44 char fullScale[2] = {0x0E, 0x00};
ericbisson 5:1a60144f7163 45 char resultat[5] = {0};
ericbisson 5:1a60144f7163 46 i2c.write(addrChip, activation, 2, true);
ericbisson 5:1a60144f7163 47 i2c.write(addrChip, fullScale, 2, true);
ericbisson 5:1a60144f7163 48
LouBe4 3:5dcf9b78f3ad 49 while(1)
ericbisson 5:1a60144f7163 50 {
LouBe4 3:5dcf9b78f3ad 51 //Aller lire les valeurs d'accélération
LouBe4 3:5dcf9b78f3ad 52 buffer[0] = 0x01;
LouBe4 3:5dcf9b78f3ad 53 i2c.write(addrChip, buffer, 1, true);
LouBe4 3:5dcf9b78f3ad 54 i2c.read(addrChip, buffer, 3);
LouBe4 3:5dcf9b78f3ad 55 calculer_angle(resultat, buffer[2]);
LouBe4 3:5dcf9b78f3ad 56
ericbisson 5:1a60144f7163 57 chip_select(false);
ericbisson 5:1a60144f7163 58 write_to_7segment(resultat[0],resultat[1],resultat[2],resultat[3]);
ericbisson 5:1a60144f7163 59 chip_select(true);
ericbisson 5:1a60144f7163 60 wait(0.1);
ericbisson 5:1a60144f7163 61 }
ericbisson 5:1a60144f7163 62 }
ericbisson 5:1a60144f7163 63
ericbisson 5:1a60144f7163 64 void SetClockAndMode(int Speed, char Mode)
ericbisson 5:1a60144f7163 65 {
ericbisson 5:1a60144f7163 66 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 67 {
ericbisson 5:1a60144f7163 68 spi.format(8,Mode);
ericbisson 5:1a60144f7163 69 spi.frequency(Speed);
ericbisson 5:1a60144f7163 70 }
ericbisson 5:1a60144f7163 71 else
ericbisson 5:1a60144f7163 72 {
ericbisson 5:1a60144f7163 73 // TODO
ericbisson 5:1a60144f7163 74 }
ericbisson 5:1a60144f7163 75 }
ericbisson 5:1a60144f7163 76
ericbisson 7:a57409bdf777 77 void write_uart(char value)
ericbisson 7:a57409bdf777 78 {
ericbisson 7:a57409bdf777 79 /*
ericbisson 7:a57409bdf777 80 *U0THR |= value;
ericbisson 7:a57409bdf777 81 *pinSelect_ptr |= PIN9_TX;
ericbisson 7:a57409bdf777 82 *enableDataFlow_ptr |= TXE0;
ericbisson 7:a57409bdf777 83 */
ericbisson 7:a57409bdf777 84 }
ericbisson 7:a57409bdf777 85
ericbisson 5:1a60144f7163 86 // function to change displayed dots
ericbisson 5:1a60144f7163 87 void change_dots(char dot)
ericbisson 5:1a60144f7163 88 {
ericbisson 5:1a60144f7163 89 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 90 {
LouBe4 3:5dcf9b78f3ad 91 spi.write(0x77);
ericbisson 5:1a60144f7163 92 spi.write(dot);
ericbisson 5:1a60144f7163 93 }
ericbisson 5:1a60144f7163 94 else
ericbisson 5:1a60144f7163 95 {
ericbisson 7:a57409bdf777 96 write_uart(0x77);
ericbisson 7:a57409bdf777 97 write_uart(dot);
ericbisson 5:1a60144f7163 98 }
ericbisson 5:1a60144f7163 99 }
ericbisson 5:1a60144f7163 100
ericbisson 5:1a60144f7163 101 void ResetCursor()
ericbisson 5:1a60144f7163 102 {
ericbisson 5:1a60144f7163 103 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 104 {
ericbisson 5:1a60144f7163 105 spi.write(0x79);
ericbisson 5:1a60144f7163 106 spi.write(0);
ericbisson 5:1a60144f7163 107 }
ericbisson 5:1a60144f7163 108 else
ericbisson 5:1a60144f7163 109 {
ericbisson 7:a57409bdf777 110 write_uart(0x79);
ericbisson 7:a57409bdf777 111 write_uart(0);
LouBe4 3:5dcf9b78f3ad 112 }
LouBe4 0:52fba6f1554b 113 }
ericbisson 5:1a60144f7163 114
ericbisson 5:1a60144f7163 115 // function used to write numbers to all four digits
ericbisson 5:1a60144f7163 116 void write_to_7segment(char d1, char d2, char d3, char d4)
ericbisson 5:1a60144f7163 117 {
ericbisson 5:1a60144f7163 118 ResetCursor();
ericbisson 5:1a60144f7163 119 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 120 {
ericbisson 5:1a60144f7163 121 spi.write(d1);
ericbisson 5:1a60144f7163 122 spi.write(d2);
ericbisson 5:1a60144f7163 123 spi.write(d3);
ericbisson 5:1a60144f7163 124 spi.write(d4);
ericbisson 5:1a60144f7163 125 }
ericbisson 5:1a60144f7163 126 else
ericbisson 5:1a60144f7163 127 {
ericbisson 7:a57409bdf777 128 write_uart(d1);
ericbisson 7:a57409bdf777 129 write_uart(d2);
ericbisson 7:a57409bdf777 130 write_uart(d3);
ericbisson 7:a57409bdf777 131 write_uart(d4);
ericbisson 5:1a60144f7163 132 }
ericbisson 5:1a60144f7163 133 }
ericbisson 5:1a60144f7163 134
ericbisson 5:1a60144f7163 135 void chip_select(bool bSet)
ericbisson 5:1a60144f7163 136 {
ericbisson 5:1a60144f7163 137 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 138 {
ericbisson 5:1a60144f7163 139 if (!bSet)
ericbisson 5:1a60144f7163 140 {
ericbisson 5:1a60144f7163 141 wait_us(25);
ericbisson 5:1a60144f7163 142 }
ericbisson 5:1a60144f7163 143 cs = bSet;
ericbisson 5:1a60144f7163 144 if (bSet)
ericbisson 5:1a60144f7163 145 {
ericbisson 5:1a60144f7163 146 wait_us(25);
ericbisson 5:1a60144f7163 147 }
ericbisson 5:1a60144f7163 148 }
ericbisson 5:1a60144f7163 149 }
ericbisson 5:1a60144f7163 150
ericbisson 5:1a60144f7163 151 // function used to calculate and return the new value of a moving average
ericbisson 5:1a60144f7163 152 int moyenne_mobile(int newData)
ericbisson 5:1a60144f7163 153 {
ericbisson 5:1a60144f7163 154 int sum = 0;
ericbisson 5:1a60144f7163 155 MovingAverage.buffer[MovingAverage.cursor] = newData;
ericbisson 5:1a60144f7163 156 MovingAverage.cursor++;
ericbisson 5:1a60144f7163 157 if (MovingAverage.cursor >= MOVING_AVG_SIZE)
ericbisson 5:1a60144f7163 158 {
ericbisson 5:1a60144f7163 159 MovingAverage.cursor = 0;
ericbisson 5:1a60144f7163 160 MovingAverage.bFilled = true;
ericbisson 5:1a60144f7163 161 }
ericbisson 5:1a60144f7163 162
ericbisson 5:1a60144f7163 163 if (MovingAverage.bFilled)
ericbisson 5:1a60144f7163 164 {
ericbisson 5:1a60144f7163 165 for (int i = 0; i < MOVING_AVG_SIZE; i++)
ericbisson 5:1a60144f7163 166 {
ericbisson 5:1a60144f7163 167 sum += MovingAverage.buffer[i];
ericbisson 5:1a60144f7163 168 }
ericbisson 5:1a60144f7163 169 sum = sum / MOVING_AVG_SIZE;
ericbisson 5:1a60144f7163 170 }
ericbisson 5:1a60144f7163 171 else
ericbisson 5:1a60144f7163 172 {
ericbisson 5:1a60144f7163 173 for (int i = 0; i < MovingAverage.cursor; i++)
ericbisson 5:1a60144f7163 174 {
ericbisson 5:1a60144f7163 175 sum += MovingAverage.buffer[i];
ericbisson 5:1a60144f7163 176 }
ericbisson 5:1a60144f7163 177 sum = sum / MovingAverage.cursor;
ericbisson 5:1a60144f7163 178 }
ericbisson 5:1a60144f7163 179
ericbisson 5:1a60144f7163 180 return sum;
ericbisson 5:1a60144f7163 181 }