![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
pre commentaire
Fork of Code_APP3_R by
main.cpp@3:5dcf9b78f3ad, 2017-01-16 (annotated)
- Committer:
- LouBe4
- Date:
- Mon Jan 16 19:09:24 2017 +0000
- Revision:
- 3:5dcf9b78f3ad
- Parent:
- 2:9aacd567c70b
- Child:
- 4:ffd7cef72270
truc;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LouBe4 | 0:52fba6f1554b | 1 | #include "mbed.h" |
LouBe4 | 0:52fba6f1554b | 2 | |
LouBe4 | 3:5dcf9b78f3ad | 3 | #define PI 3.1416 |
LouBe4 | 3:5dcf9b78f3ad | 4 | |
LouBe4 | 0:52fba6f1554b | 5 | Serial pc(USBTX, USBRX); |
LouBe4 | 0:52fba6f1554b | 6 | SPI spi(p11, p12, p13); |
LouBe4 | 0:52fba6f1554b | 7 | DigitalOut cs(p14); |
LouBe4 | 0:52fba6f1554b | 8 | I2C i2c(p28, p27); |
LouBe4 | 0:52fba6f1554b | 9 | PwmOut led1(LED1); |
LouBe4 | 0:52fba6f1554b | 10 | int addrChip = 0x3A; |
LouBe4 | 1:3430643e8ed4 | 11 | char buffer[3]; |
LouBe4 | 1:3430643e8ed4 | 12 | |
LouBe4 | 3:5dcf9b78f3ad | 13 | void calculer_angle(char bufferAngle[], int accZ) |
LouBe4 | 1:3430643e8ed4 | 14 | { |
LouBe4 | 3:5dcf9b78f3ad | 15 | float angle = abs(cos(static_cast<float>(accZ*90/64)*PI/180)*90); |
LouBe4 | 3:5dcf9b78f3ad | 16 | pc.printf("%c\n", angle); |
LouBe4 | 3:5dcf9b78f3ad | 17 | int angleInt = (int)(angle*100); |
LouBe4 | 3:5dcf9b78f3ad | 18 | snprintf(bufferAngle, sizeof bufferAngle, "%d", angleInt); |
LouBe4 | 3:5dcf9b78f3ad | 19 | |
LouBe4 | 3:5dcf9b78f3ad | 20 | if(angle < 10) |
LouBe4 | 3:5dcf9b78f3ad | 21 | { |
LouBe4 | 3:5dcf9b78f3ad | 22 | bufferAngle[3] = bufferAngle[2]; |
LouBe4 | 3:5dcf9b78f3ad | 23 | bufferAngle[2] = bufferAngle[1]; |
LouBe4 | 3:5dcf9b78f3ad | 24 | bufferAngle[1] = bufferAngle[0]; |
LouBe4 | 3:5dcf9b78f3ad | 25 | bufferAngle[0] = '0'; |
LouBe4 | 3:5dcf9b78f3ad | 26 | } |
LouBe4 | 1:3430643e8ed4 | 27 | } |
LouBe4 | 0:52fba6f1554b | 28 | |
LouBe4 | 0:52fba6f1554b | 29 | int main() { |
LouBe4 | 0:52fba6f1554b | 30 | spi.format(8,0); |
LouBe4 | 0:52fba6f1554b | 31 | spi.frequency(250000); |
LouBe4 | 3:5dcf9b78f3ad | 32 | while(1) |
LouBe4 | 3:5dcf9b78f3ad | 33 | { |
LouBe4 | 3:5dcf9b78f3ad | 34 | //Activer l'accéléromètre pour lecture 8 bits |
LouBe4 | 3:5dcf9b78f3ad | 35 | char activation[2] = {0x2A, 0x03}; |
LouBe4 | 3:5dcf9b78f3ad | 36 | char fullScale[2] = {0x0E, 0x00}; |
LouBe4 | 3:5dcf9b78f3ad | 37 | char resultat[5] = {0}; |
LouBe4 | 3:5dcf9b78f3ad | 38 | i2c.write(addrChip, activation, 2, true); |
LouBe4 | 3:5dcf9b78f3ad | 39 | i2c.write(addrChip, fullScale, 2, true); |
LouBe4 | 3:5dcf9b78f3ad | 40 | |
LouBe4 | 3:5dcf9b78f3ad | 41 | //Aller lire les valeurs d'accélération |
LouBe4 | 3:5dcf9b78f3ad | 42 | buffer[0] = 0x01; |
LouBe4 | 3:5dcf9b78f3ad | 43 | i2c.write(addrChip, buffer, 1, true); |
LouBe4 | 3:5dcf9b78f3ad | 44 | i2c.read(addrChip, buffer, 3); |
LouBe4 | 3:5dcf9b78f3ad | 45 | calculer_angle(resultat, buffer[2]); |
LouBe4 | 3:5dcf9b78f3ad | 46 | |
LouBe4 | 3:5dcf9b78f3ad | 47 | cs = 0; |
LouBe4 | 3:5dcf9b78f3ad | 48 | wait_us(25); |
LouBe4 | 3:5dcf9b78f3ad | 49 | spi.write(0x77); |
LouBe4 | 3:5dcf9b78f3ad | 50 | spi.write(0b00000010); |
LouBe4 | 3:5dcf9b78f3ad | 51 | spi.write(0x7A); |
LouBe4 | 3:5dcf9b78f3ad | 52 | spi.write(0xFF); |
LouBe4 | 3:5dcf9b78f3ad | 53 | wait_us(100); |
LouBe4 | 3:5dcf9b78f3ad | 54 | spi.write(resultat[0]); |
LouBe4 | 3:5dcf9b78f3ad | 55 | spi.write(resultat[1]); |
LouBe4 | 3:5dcf9b78f3ad | 56 | spi.write(resultat[3]); |
LouBe4 | 3:5dcf9b78f3ad | 57 | spi.write(resultat[4]); |
LouBe4 | 3:5dcf9b78f3ad | 58 | wait_us(25); |
LouBe4 | 3:5dcf9b78f3ad | 59 | cs = 1; |
LouBe4 | 3:5dcf9b78f3ad | 60 | wait(0.25); |
LouBe4 | 3:5dcf9b78f3ad | 61 | } |
LouBe4 | 0:52fba6f1554b | 62 | } |