Éric Bisson
/
Code_APP3_R
app3
Fork of Code_APP1 by
Diff: main.cpp
- Revision:
- 3:5dcf9b78f3ad
- Parent:
- 2:9aacd567c70b
- Child:
- 4:ffd7cef72270
diff -r 9aacd567c70b -r 5dcf9b78f3ad main.cpp --- a/main.cpp Sun Jan 15 19:40:31 2017 +0000 +++ b/main.cpp Mon Jan 16 19:09:24 2017 +0000 @@ -1,5 +1,7 @@ #include "mbed.h" +#define PI 3.1416 + Serial pc(USBTX, USBRX); SPI spi(p11, p12, p13); DigitalOut cs(p14); @@ -8,35 +10,53 @@ int addrChip = 0x3A; char buffer[3]; -int calculer_angle(char accX, char accY, char accZ) +void calculer_angle(char bufferAngle[], int accZ) { - pc.printf(&accX); - pc.printf(&accY); - pc.printf(&accZ); - return 0; + float angle = abs(cos(static_cast<float>(accZ*90/64)*PI/180)*90); + pc.printf("%c\n", angle); + int angleInt = (int)(angle*100); + snprintf(bufferAngle, sizeof bufferAngle, "%d", angleInt); + + if(angle < 10) + { + bufferAngle[3] = bufferAngle[2]; + bufferAngle[2] = bufferAngle[1]; + bufferAngle[1] = bufferAngle[0]; + bufferAngle[0] = '0'; + } } int main() { - - //Activer l'accéléromètre pour lecture 8 bits - char activation[2] = {0x2A, 0x03}; - i2c.write(addrChip, activation, 2, true); - - //Aller lire les valeurs d'accélération - buffer[0] = 0x01; - i2c.write(addrChip, buffer, 1, true); - i2c.read(addrChip, buffer, 3); - int angle = calculer_angle(buffer[0], buffer[1], buffer[2]); - //pc.printf("%02x\t", buffer[0]); - //pc.printf("%02x\t", buffer[1]); - //pc.printf("%02x\t", buffer[2]); - //pc.printf("\n"); - - - //----Base SPI---- - cs = 0; spi.format(8,0); spi.frequency(250000); - wait(0.1); - cs = 1; + while(1) + { + //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); + + //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]); + + cs = 0; + wait_us(25); + spi.write(0x77); + spi.write(0b00000010); + spi.write(0x7A); + spi.write(0xFF); + wait_us(100); + spi.write(resultat[0]); + spi.write(resultat[1]); + spi.write(resultat[3]); + spi.write(resultat[4]); + wait_us(25); + cs = 1; + wait(0.25); + } }