Éric Bisson
/
Code_APP3_R
app3
Fork of Code_APP1 by
main.cpp
- Committer:
- ericbisson
- Date:
- 2017-01-17
- Revision:
- 8:6f2b7f9b0d0d
- Parent:
- 7:a57409bdf777
- Child:
- 9:dc81d8ee4a97
File content as of revision 8:6f2b7f9b0d0d:
#include "mbed.h" #include "header.h" Serial pc(USBTX, USBRX); SPI spi(p11, p12, p13); DigitalOut cs(p14); I2C i2c(p28, p27); PwmOut led1(LED1); void calculer_angle(char bufferAngle[], int accZ) { float angle = abs(cos(static_cast<float>(moyenne_mobile(accZ)*90/64)*PI/180)*90); int angleInt = (int)(angle*100); // reset buffer bufferAngle[0] = '0'; bufferAngle[1] = '0'; bufferAngle[2] = '0'; bufferAngle[3] = '0'; signed char pos = 3; while (angleInt > 1 && pos >= 0) { bufferAngle[pos] = angleInt % 10; angleInt = angleInt / 10; pos--; } } void uart_init() { pc.printf("uart before\n"); *PCONP |= 0x02000000; // Turn ON PCUART3 *PCLKSEL1 |= 0x00040000; // Set PCLK_UART3 to 01 (CLK / 1) *PINSEL0 &= ~0x00000003; // Turn Off TxD3 P0.0 *PINSEL0 |= 0x00000002; // Enable TxD3 P0.0 *U3FCR |= 0x00000007; // Reset Tx, Reset Rx, Enable FIFO *U3LCR |= 0x00000083; // Enable DLAB, 8-bit char length *U3DLM |= 0x00000000; // DLMSB ; Baud rate *U3DLL |= 0x00000060; // DLLSB ; Baud rate *U3LCR &= ~0x00000080; // Turn off DLAB pc.printf("uart init()\n"); } int main() { int addrChip = 0x3A; char buffer[3] = {0,0,0}; SetClockAndMode(250000, 0); change_dots(0x02); //Activer l'accéléromètre pour lecture 8 bits char activation[2] = {0x2A, 0x03}; char fullScale[2] = {0x0E, 0x00}; char resultat[4] = {'0','0','0','0'}; i2c.write(addrChip, activation, 2, true); i2c.write(addrChip, fullScale, 2, true); while(1) { //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]); chip_select(false); write_to_7segment(resultat[0],resultat[1],resultat[2],resultat[3]); chip_select(true); wait(0.1); } } void SetClockAndMode(int Speed, char Mode) { if (IS_USING_SPI) { spi.format(8,Mode); spi.frequency(Speed); } else { uart_init(); } } void write_uart(unsigned char value) { pc.printf("%d - ", reverse(value)); *U3THR = reverse(value); // Data to send ; LSB first *U3TER |= 0x80; // Enable TXEn //*U3TER &= ~0x80; // Disable TXEn } // inverse bit order unsigned char reverse(unsigned char b) { b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; b = (b & 0xCC) >> 2 | (b & 0x33) << 2; b = (b & 0xAA) >> 1 | (b & 0x55) << 1; return b; } // function to change displayed dots void change_dots(char dot) { if (IS_USING_SPI) { spi.write(0x77); spi.write(dot); } else { write_uart(0x77); write_uart(dot); } } void ResetCursor() { if (IS_USING_SPI) { spi.write(0x79); spi.write(0); } else { write_uart(0x79); write_uart(0); } } // function used to write numbers to all four digits void write_to_7segment(char d1, char d2, char d3, char d4) { ResetCursor(); if (IS_USING_SPI) { spi.write(d1); spi.write(d2); spi.write(d3); spi.write(d4); } else { write_uart(d1); write_uart(d2); write_uart(d3); write_uart(d4); pc.printf("\n"); } } void chip_select(bool bSet) { if (IS_USING_SPI) { if (!bSet) { wait_us(25); } cs = bSet; if (bSet) { wait_us(25); } } } // function used to calculate and return the new value of a moving average int moyenne_mobile(int newData) { int sum = 0; MovingAverage.buffer[MovingAverage.cursor] = newData; MovingAverage.cursor++; if (MovingAverage.cursor >= MOVING_AVG_SIZE) { MovingAverage.cursor = 0; MovingAverage.bFilled = true; } if (MovingAverage.bFilled) { for (int i = 0; i < MOVING_AVG_SIZE; i++) { sum += MovingAverage.buffer[i]; } sum = sum / MOVING_AVG_SIZE; } else { for (int i = 0; i < MovingAverage.cursor; i++) { sum += MovingAverage.buffer[i]; } sum = sum / MovingAverage.cursor; } return sum; }