Éric Bisson
/
Code_APP3_R
app3
Fork of Code_APP1 by
main.cpp
- Committer:
- LouBe4
- Date:
- 2017-01-16
- Revision:
- 3:5dcf9b78f3ad
- Parent:
- 2:9aacd567c70b
- Child:
- 4:ffd7cef72270
File content as of revision 3:5dcf9b78f3ad:
#include "mbed.h" #define PI 3.1416 Serial pc(USBTX, USBRX); SPI spi(p11, p12, p13); DigitalOut cs(p14); I2C i2c(p28, p27); PwmOut led1(LED1); int addrChip = 0x3A; char buffer[3]; void calculer_angle(char bufferAngle[], int accZ) { 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() { spi.format(8,0); spi.frequency(250000); 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); } }