app3

Dependencies:   mbed

Fork of Code_APP1 by Louis Beaulieu

main.cpp

Committer:
LouBe4
Date:
2017-01-16
Revision:
4:ffd7cef72270
Parent:
3:5dcf9b78f3ad
Child:
5:1a60144f7163

File content as of revision 4:ffd7cef72270:

#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("%f\n", angle);
    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';
    }    
    pc.printf("%c", bufferAngle[0]);
    pc.printf("%c", bufferAngle[1]);
    pc.printf("%c", bufferAngle[2]);
    pc.printf("%c", bufferAngle[3]);
    pc.printf("\n");
    
}

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);
        wait_us(100);
        spi.write(resultat[0]);
        spi.write(resultat[1]);
        spi.write(resultat[2]);
        spi.write(resultat[3]);
        wait_us(25);
        cs = 1;
        wait(0.25);
    }
}