Éric Bisson
/
Code_APP3_R
app3
Fork of Code_APP1 by
main.cpp
- Committer:
- ericbisson
- Date:
- 2017-01-16
- Revision:
- 5:1a60144f7163
- Parent:
- 4:ffd7cef72270
- Child:
- 6:2ec2a74c807f
File content as of revision 5:1a60144f7163:
#include "mbed.h" #include "header.h" #define PI 3.1416 Serial pc(USBTX, USBRX); SPI spi(p11, p12, p13); DigitalOut cs(p14); I2C i2c(p28, p27); PwmOut led1(LED1); void calculer_angle(char bufferAngle[], int accZ) { float angle = abs(cos(static_cast<float>(moyenne_mobile(accZ)*90/64)*PI/180)*90); int angleInt = (int)(angle*100); snprintf(bufferAngle, 5, "%d", angleInt); if(angle < 10) { bufferAngle[3] = bufferAngle[2]; bufferAngle[2] = bufferAngle[1]; bufferAngle[1] = bufferAngle[0]; bufferAngle[0] = '0'; } } int main() { int addrChip = 0x3A; char buffer[3] = {0,0,0}; SetClockAndMode(250000, 0); change_dots(0x02); //Activer l'accéléromètre pour lecture 8 bits char activation[2] = {0x2A, 0x03}; char fullScale[2] = {0x0E, 0x00}; char resultat[5] = {0}; i2c.write(addrChip, activation, 2, true); i2c.write(addrChip, fullScale, 2, true); while(1) { //Aller lire les valeurs d'accélération buffer[0] = 0x01; i2c.write(addrChip, buffer, 1, true); i2c.read(addrChip, buffer, 3); calculer_angle(resultat, buffer[2]); chip_select(false); write_to_7segment(resultat[0],resultat[1],resultat[2],resultat[3]); chip_select(true); wait(0.1); } } void SetClockAndMode(int Speed, char Mode) { if (IS_USING_SPI) { spi.format(8,Mode); spi.frequency(Speed); } else { // TODO } } // function to change displayed dots void change_dots(char dot) { if (IS_USING_SPI) { spi.write(0x77); spi.write(dot); } else { // TODO } } void ResetCursor() { if (IS_USING_SPI) { spi.write(0x79); spi.write(0); } else { // TODO } } // function used to write numbers to all four digits void write_to_7segment(char d1, char d2, char d3, char d4) { ResetCursor(); if (IS_USING_SPI) { spi.write(d1); spi.write(d2); spi.write(d3); spi.write(d4); } else { // TODO } } void chip_select(bool bSet) { if (IS_USING_SPI) { if (!bSet) { wait_us(25); } cs = bSet; if (bSet) { wait_us(25); } } } // function used to calculate and return the new value of a moving average int moyenne_mobile(int newData) { int sum = 0; MovingAverage.buffer[MovingAverage.cursor] = newData; MovingAverage.cursor++; if (MovingAverage.cursor >= MOVING_AVG_SIZE) { MovingAverage.cursor = 0; MovingAverage.bFilled = true; } if (MovingAverage.bFilled) { for (int i = 0; i < MOVING_AVG_SIZE; i++) { sum += MovingAverage.buffer[i]; } sum = sum / MOVING_AVG_SIZE; } else { for (int i = 0; i < MovingAverage.cursor; i++) { sum += MovingAverage.buffer[i]; } sum = sum / MovingAverage.cursor; } return sum; }