Éric Bisson
/
Code_APP3_R
app3
Fork of Code_APP1 by
main.cpp@4:ffd7cef72270, 2017-01-16 (annotated)
- Committer:
- LouBe4
- Date:
- Mon Jan 16 19:19:27 2017 +0000
- Revision:
- 4:ffd7cef72270
- Parent:
- 3:5dcf9b78f3ad
- Child:
- 5:1a60144f7163
Affichage SPI marche;
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 | 4:ffd7cef72270 | 16 | pc.printf("%f\n", angle); |
LouBe4 | 3:5dcf9b78f3ad | 17 | int angleInt = (int)(angle*100); |
LouBe4 | 4:ffd7cef72270 | 18 | snprintf(bufferAngle, 5, "%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 | 4:ffd7cef72270 | 27 | pc.printf("%c", bufferAngle[0]); |
LouBe4 | 4:ffd7cef72270 | 28 | pc.printf("%c", bufferAngle[1]); |
LouBe4 | 4:ffd7cef72270 | 29 | pc.printf("%c", bufferAngle[2]); |
LouBe4 | 4:ffd7cef72270 | 30 | pc.printf("%c", bufferAngle[3]); |
LouBe4 | 4:ffd7cef72270 | 31 | pc.printf("\n"); |
LouBe4 | 4:ffd7cef72270 | 32 | |
LouBe4 | 1:3430643e8ed4 | 33 | } |
LouBe4 | 0:52fba6f1554b | 34 | |
LouBe4 | 0:52fba6f1554b | 35 | int main() { |
LouBe4 | 0:52fba6f1554b | 36 | spi.format(8,0); |
LouBe4 | 0:52fba6f1554b | 37 | spi.frequency(250000); |
LouBe4 | 3:5dcf9b78f3ad | 38 | while(1) |
LouBe4 | 3:5dcf9b78f3ad | 39 | { |
LouBe4 | 3:5dcf9b78f3ad | 40 | //Activer l'accéléromètre pour lecture 8 bits |
LouBe4 | 3:5dcf9b78f3ad | 41 | char activation[2] = {0x2A, 0x03}; |
LouBe4 | 3:5dcf9b78f3ad | 42 | char fullScale[2] = {0x0E, 0x00}; |
LouBe4 | 3:5dcf9b78f3ad | 43 | char resultat[5] = {0}; |
LouBe4 | 3:5dcf9b78f3ad | 44 | i2c.write(addrChip, activation, 2, true); |
LouBe4 | 3:5dcf9b78f3ad | 45 | i2c.write(addrChip, fullScale, 2, true); |
LouBe4 | 3:5dcf9b78f3ad | 46 | |
LouBe4 | 3:5dcf9b78f3ad | 47 | //Aller lire les valeurs d'accélération |
LouBe4 | 3:5dcf9b78f3ad | 48 | buffer[0] = 0x01; |
LouBe4 | 3:5dcf9b78f3ad | 49 | i2c.write(addrChip, buffer, 1, true); |
LouBe4 | 3:5dcf9b78f3ad | 50 | i2c.read(addrChip, buffer, 3); |
LouBe4 | 3:5dcf9b78f3ad | 51 | calculer_angle(resultat, buffer[2]); |
LouBe4 | 3:5dcf9b78f3ad | 52 | |
LouBe4 | 3:5dcf9b78f3ad | 53 | cs = 0; |
LouBe4 | 3:5dcf9b78f3ad | 54 | wait_us(25); |
LouBe4 | 3:5dcf9b78f3ad | 55 | spi.write(0x77); |
LouBe4 | 3:5dcf9b78f3ad | 56 | spi.write(0b00000010); |
LouBe4 | 3:5dcf9b78f3ad | 57 | wait_us(100); |
LouBe4 | 3:5dcf9b78f3ad | 58 | spi.write(resultat[0]); |
LouBe4 | 3:5dcf9b78f3ad | 59 | spi.write(resultat[1]); |
LouBe4 | 4:ffd7cef72270 | 60 | spi.write(resultat[2]); |
LouBe4 | 3:5dcf9b78f3ad | 61 | spi.write(resultat[3]); |
LouBe4 | 3:5dcf9b78f3ad | 62 | wait_us(25); |
LouBe4 | 3:5dcf9b78f3ad | 63 | cs = 1; |
LouBe4 | 3:5dcf9b78f3ad | 64 | wait(0.25); |
LouBe4 | 3:5dcf9b78f3ad | 65 | } |
LouBe4 | 0:52fba6f1554b | 66 | } |