app3

Dependencies:   EthernetInterface WebSocketClient mbed-rtos mbed

Fork of Code_APP1 by Louis Beaulieu

Committer:
ericbisson
Date:
Tue Jan 17 02:09:22 2017 +0000
Revision:
8:6f2b7f9b0d0d
Parent:
7:a57409bdf777
Child:
9:400cfcf4b06e
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LouBe4 0:52fba6f1554b 1 #include "mbed.h"
ericbisson 5:1a60144f7163 2 #include "header.h"
LouBe4 0:52fba6f1554b 3
LouBe4 0:52fba6f1554b 4 Serial pc(USBTX, USBRX);
LouBe4 0:52fba6f1554b 5 SPI spi(p11, p12, p13);
LouBe4 0:52fba6f1554b 6 DigitalOut cs(p14);
LouBe4 0:52fba6f1554b 7 I2C i2c(p28, p27);
LouBe4 0:52fba6f1554b 8 PwmOut led1(LED1);
LouBe4 1:3430643e8ed4 9
LouBe4 3:5dcf9b78f3ad 10 void calculer_angle(char bufferAngle[], int accZ)
LouBe4 1:3430643e8ed4 11 {
ericbisson 5:1a60144f7163 12 float angle = abs(cos(static_cast<float>(moyenne_mobile(accZ)*90/64)*PI/180)*90);
LouBe4 3:5dcf9b78f3ad 13 int angleInt = (int)(angle*100);
LouBe4 3:5dcf9b78f3ad 14
ericbisson 8:6f2b7f9b0d0d 15 // reset buffer
ericbisson 8:6f2b7f9b0d0d 16 bufferAngle[0] = '0';
ericbisson 8:6f2b7f9b0d0d 17 bufferAngle[1] = '0';
ericbisson 8:6f2b7f9b0d0d 18 bufferAngle[2] = '0';
ericbisson 8:6f2b7f9b0d0d 19 bufferAngle[3] = '0';
ericbisson 8:6f2b7f9b0d0d 20
ericbisson 8:6f2b7f9b0d0d 21 signed char pos = 3;
ericbisson 8:6f2b7f9b0d0d 22 while (angleInt > 1 && pos >= 0)
LouBe4 3:5dcf9b78f3ad 23 {
ericbisson 8:6f2b7f9b0d0d 24 bufferAngle[pos] = angleInt % 10;
ericbisson 8:6f2b7f9b0d0d 25 angleInt = angleInt / 10;
ericbisson 8:6f2b7f9b0d0d 26 pos--;
ericbisson 5:1a60144f7163 27 }
LouBe4 1:3430643e8ed4 28 }
LouBe4 0:52fba6f1554b 29
ericbisson 8:6f2b7f9b0d0d 30 void uart_init()
ericbisson 8:6f2b7f9b0d0d 31 {
ericbisson 8:6f2b7f9b0d0d 32 pc.printf("uart before\n");
ericbisson 8:6f2b7f9b0d0d 33
ericbisson 8:6f2b7f9b0d0d 34 *PCONP |= 0x02000000; // Turn ON PCUART3
ericbisson 8:6f2b7f9b0d0d 35 *PCLKSEL1 |= 0x00040000; // Set PCLK_UART3 to 01 (CLK / 1)
ericbisson 8:6f2b7f9b0d0d 36 *PINSEL0 &= ~0x00000003; // Turn Off TxD3 P0.0
ericbisson 8:6f2b7f9b0d0d 37 *PINSEL0 |= 0x00000002; // Enable TxD3 P0.0
ericbisson 8:6f2b7f9b0d0d 38 *U3FCR |= 0x00000007; // Reset Tx, Reset Rx, Enable FIFO
ericbisson 8:6f2b7f9b0d0d 39 *U3LCR |= 0x00000083; // Enable DLAB, 8-bit char length
ericbisson 8:6f2b7f9b0d0d 40 *U3DLM |= 0x00000000; // DLMSB ; Baud rate
ericbisson 8:6f2b7f9b0d0d 41 *U3DLL |= 0x00000060; // DLLSB ; Baud rate
ericbisson 8:6f2b7f9b0d0d 42 *U3LCR &= ~0x00000080; // Turn off DLAB
ericbisson 8:6f2b7f9b0d0d 43
ericbisson 8:6f2b7f9b0d0d 44 pc.printf("uart init()\n");
ericbisson 8:6f2b7f9b0d0d 45 }
ericbisson 8:6f2b7f9b0d0d 46
LouBe4 0:52fba6f1554b 47 int main() {
ericbisson 5:1a60144f7163 48 int addrChip = 0x3A;
ericbisson 5:1a60144f7163 49 char buffer[3] = {0,0,0};
ericbisson 6:2ec2a74c807f 50
ericbisson 5:1a60144f7163 51 SetClockAndMode(250000, 0);
ericbisson 5:1a60144f7163 52 change_dots(0x02);
ericbisson 5:1a60144f7163 53
ericbisson 5:1a60144f7163 54 //Activer l'accéléromètre pour lecture 8 bits
ericbisson 5:1a60144f7163 55 char activation[2] = {0x2A, 0x03};
ericbisson 5:1a60144f7163 56 char fullScale[2] = {0x0E, 0x00};
ericbisson 8:6f2b7f9b0d0d 57 char resultat[4] = {'0','0','0','0'};
ericbisson 5:1a60144f7163 58 i2c.write(addrChip, activation, 2, true);
ericbisson 5:1a60144f7163 59 i2c.write(addrChip, fullScale, 2, true);
ericbisson 5:1a60144f7163 60
LouBe4 3:5dcf9b78f3ad 61 while(1)
ericbisson 5:1a60144f7163 62 {
LouBe4 3:5dcf9b78f3ad 63 //Aller lire les valeurs d'accélération
LouBe4 3:5dcf9b78f3ad 64 buffer[0] = 0x01;
LouBe4 3:5dcf9b78f3ad 65 i2c.write(addrChip, buffer, 1, true);
LouBe4 3:5dcf9b78f3ad 66 i2c.read(addrChip, buffer, 3);
ericbisson 8:6f2b7f9b0d0d 67
LouBe4 3:5dcf9b78f3ad 68 calculer_angle(resultat, buffer[2]);
LouBe4 3:5dcf9b78f3ad 69
ericbisson 5:1a60144f7163 70 chip_select(false);
ericbisson 5:1a60144f7163 71 write_to_7segment(resultat[0],resultat[1],resultat[2],resultat[3]);
ericbisson 5:1a60144f7163 72 chip_select(true);
ericbisson 5:1a60144f7163 73 wait(0.1);
ericbisson 5:1a60144f7163 74 }
ericbisson 5:1a60144f7163 75 }
ericbisson 5:1a60144f7163 76
ericbisson 5:1a60144f7163 77 void SetClockAndMode(int Speed, char Mode)
ericbisson 5:1a60144f7163 78 {
ericbisson 5:1a60144f7163 79 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 80 {
ericbisson 5:1a60144f7163 81 spi.format(8,Mode);
ericbisson 5:1a60144f7163 82 spi.frequency(Speed);
ericbisson 5:1a60144f7163 83 }
ericbisson 5:1a60144f7163 84 else
ericbisson 5:1a60144f7163 85 {
ericbisson 8:6f2b7f9b0d0d 86 uart_init();
ericbisson 5:1a60144f7163 87 }
ericbisson 5:1a60144f7163 88 }
ericbisson 5:1a60144f7163 89
ericbisson 8:6f2b7f9b0d0d 90 void write_uart(unsigned char value)
ericbisson 8:6f2b7f9b0d0d 91 { pc.printf("%d - ", reverse(value));
ericbisson 8:6f2b7f9b0d0d 92 *U3THR = reverse(value); // Data to send ; LSB first
ericbisson 8:6f2b7f9b0d0d 93 *U3TER |= 0x80; // Enable TXEn
ericbisson 8:6f2b7f9b0d0d 94 //*U3TER &= ~0x80; // Disable TXEn
ericbisson 8:6f2b7f9b0d0d 95 }
ericbisson 8:6f2b7f9b0d0d 96
ericbisson 8:6f2b7f9b0d0d 97 // inverse bit order
ericbisson 8:6f2b7f9b0d0d 98 unsigned char reverse(unsigned char b)
ericbisson 7:a57409bdf777 99 {
ericbisson 8:6f2b7f9b0d0d 100 b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
ericbisson 8:6f2b7f9b0d0d 101 b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
ericbisson 8:6f2b7f9b0d0d 102 b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
ericbisson 8:6f2b7f9b0d0d 103 return b;
ericbisson 7:a57409bdf777 104 }
ericbisson 7:a57409bdf777 105
ericbisson 5:1a60144f7163 106 // function to change displayed dots
ericbisson 5:1a60144f7163 107 void change_dots(char dot)
ericbisson 5:1a60144f7163 108 {
ericbisson 5:1a60144f7163 109 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 110 {
LouBe4 3:5dcf9b78f3ad 111 spi.write(0x77);
ericbisson 5:1a60144f7163 112 spi.write(dot);
ericbisson 5:1a60144f7163 113 }
ericbisson 5:1a60144f7163 114 else
ericbisson 5:1a60144f7163 115 {
ericbisson 7:a57409bdf777 116 write_uart(0x77);
ericbisson 7:a57409bdf777 117 write_uart(dot);
ericbisson 5:1a60144f7163 118 }
ericbisson 5:1a60144f7163 119 }
ericbisson 5:1a60144f7163 120
ericbisson 5:1a60144f7163 121 void ResetCursor()
ericbisson 5:1a60144f7163 122 {
ericbisson 5:1a60144f7163 123 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 124 {
ericbisson 5:1a60144f7163 125 spi.write(0x79);
ericbisson 5:1a60144f7163 126 spi.write(0);
ericbisson 5:1a60144f7163 127 }
ericbisson 5:1a60144f7163 128 else
ericbisson 5:1a60144f7163 129 {
ericbisson 7:a57409bdf777 130 write_uart(0x79);
ericbisson 7:a57409bdf777 131 write_uart(0);
LouBe4 3:5dcf9b78f3ad 132 }
LouBe4 0:52fba6f1554b 133 }
ericbisson 5:1a60144f7163 134
ericbisson 5:1a60144f7163 135 // function used to write numbers to all four digits
ericbisson 5:1a60144f7163 136 void write_to_7segment(char d1, char d2, char d3, char d4)
ericbisson 5:1a60144f7163 137 {
ericbisson 5:1a60144f7163 138 ResetCursor();
ericbisson 5:1a60144f7163 139 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 140 {
ericbisson 5:1a60144f7163 141 spi.write(d1);
ericbisson 5:1a60144f7163 142 spi.write(d2);
ericbisson 5:1a60144f7163 143 spi.write(d3);
ericbisson 5:1a60144f7163 144 spi.write(d4);
ericbisson 5:1a60144f7163 145 }
ericbisson 5:1a60144f7163 146 else
ericbisson 5:1a60144f7163 147 {
ericbisson 7:a57409bdf777 148 write_uart(d1);
ericbisson 7:a57409bdf777 149 write_uart(d2);
ericbisson 7:a57409bdf777 150 write_uart(d3);
ericbisson 7:a57409bdf777 151 write_uart(d4);
ericbisson 8:6f2b7f9b0d0d 152 pc.printf("\n");
ericbisson 5:1a60144f7163 153 }
ericbisson 5:1a60144f7163 154 }
ericbisson 5:1a60144f7163 155
ericbisson 5:1a60144f7163 156 void chip_select(bool bSet)
ericbisson 5:1a60144f7163 157 {
ericbisson 5:1a60144f7163 158 if (IS_USING_SPI)
ericbisson 5:1a60144f7163 159 {
ericbisson 5:1a60144f7163 160 if (!bSet)
ericbisson 5:1a60144f7163 161 {
ericbisson 5:1a60144f7163 162 wait_us(25);
ericbisson 5:1a60144f7163 163 }
ericbisson 5:1a60144f7163 164 cs = bSet;
ericbisson 5:1a60144f7163 165 if (bSet)
ericbisson 5:1a60144f7163 166 {
ericbisson 5:1a60144f7163 167 wait_us(25);
ericbisson 5:1a60144f7163 168 }
ericbisson 5:1a60144f7163 169 }
ericbisson 5:1a60144f7163 170 }
ericbisson 5:1a60144f7163 171
ericbisson 5:1a60144f7163 172 // function used to calculate and return the new value of a moving average
ericbisson 5:1a60144f7163 173 int moyenne_mobile(int newData)
ericbisson 5:1a60144f7163 174 {
ericbisson 5:1a60144f7163 175 int sum = 0;
ericbisson 5:1a60144f7163 176 MovingAverage.buffer[MovingAverage.cursor] = newData;
ericbisson 5:1a60144f7163 177 MovingAverage.cursor++;
ericbisson 5:1a60144f7163 178 if (MovingAverage.cursor >= MOVING_AVG_SIZE)
ericbisson 5:1a60144f7163 179 {
ericbisson 5:1a60144f7163 180 MovingAverage.cursor = 0;
ericbisson 5:1a60144f7163 181 MovingAverage.bFilled = true;
ericbisson 5:1a60144f7163 182 }
ericbisson 5:1a60144f7163 183
ericbisson 5:1a60144f7163 184 if (MovingAverage.bFilled)
ericbisson 5:1a60144f7163 185 {
ericbisson 5:1a60144f7163 186 for (int i = 0; i < MOVING_AVG_SIZE; i++)
ericbisson 5:1a60144f7163 187 {
ericbisson 5:1a60144f7163 188 sum += MovingAverage.buffer[i];
ericbisson 5:1a60144f7163 189 }
ericbisson 5:1a60144f7163 190 sum = sum / MOVING_AVG_SIZE;
ericbisson 5:1a60144f7163 191 }
ericbisson 5:1a60144f7163 192 else
ericbisson 5:1a60144f7163 193 {
ericbisson 5:1a60144f7163 194 for (int i = 0; i < MovingAverage.cursor; i++)
ericbisson 5:1a60144f7163 195 {
ericbisson 5:1a60144f7163 196 sum += MovingAverage.buffer[i];
ericbisson 5:1a60144f7163 197 }
ericbisson 5:1a60144f7163 198 sum = sum / MovingAverage.cursor;
ericbisson 5:1a60144f7163 199 }
ericbisson 5:1a60144f7163 200
ericbisson 5:1a60144f7163 201 return sum;
ericbisson 5:1a60144f7163 202 }