app3

Dependencies:   mbed

Fork of Code_APP1 by Louis Beaulieu

Revision:
8:6f2b7f9b0d0d
Parent:
7:a57409bdf777
Child:
9:dc81d8ee4a97
--- a/main.cpp	Mon Jan 16 22:23:21 2017 +0000
+++ b/main.cpp	Tue Jan 17 02:09:22 2017 +0000
@@ -11,30 +11,42 @@
 {
     float angle = abs(cos(static_cast<float>(moyenne_mobile(accZ)*90/64)*PI/180)*90);
     int angleInt = (int)(angle*100);
-    snprintf(bufferAngle, 5, "%d", angleInt);
     
-    if(angle < 10)
+    // reset buffer
+    bufferAngle[0] = '0';
+    bufferAngle[1] = '0';
+    bufferAngle[2] = '0';
+    bufferAngle[3] = '0';
+    
+    signed char pos = 3;
+    while (angleInt > 1 && pos >= 0)
     {
-        bufferAngle[3] = bufferAngle[2];
-        bufferAngle[2] = bufferAngle[1];
-        bufferAngle[1] = bufferAngle[0];
-        bufferAngle[0] = '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};
-    //*clk_ptr |= PCLK_UART0;
-    pc.printf("main() clk_ptr");
-    //*baudRate_ptr |= DLAB_ACTIVATE;
-    pc.printf("main() DLAB_ACTIVATE");
-    //*baudRate_ptr |= DLMSB;
-    pc.printf("main() DLMSB");
-    //*baudRate_ptr |= DLLSB;
-    pc.printf("main() DLLSB");
-    //*enableFIFO_ptr |= FIFO_ACTIVATE;
-    pc.printf("main() FIFO_ACTIVATE");
     
     SetClockAndMode(250000, 0);
     change_dots(0x02);
@@ -42,7 +54,7 @@
     //Activer l'accéléromètre pour lecture 8 bits
     char activation[2] = {0x2A, 0x03};
     char fullScale[2] = {0x0E, 0x00};
-    char resultat[5] = {0};
+    char resultat[4] = {'0','0','0','0'};
     i2c.write(addrChip, activation, 2, true);
     i2c.write(addrChip, fullScale, 2, true);
     
@@ -52,6 +64,7 @@
         buffer[0] = 0x01;
         i2c.write(addrChip, buffer, 1, true);
         i2c.read(addrChip, buffer, 3);
+
         calculer_angle(resultat, buffer[2]);
         
         chip_select(false);
@@ -70,17 +83,24 @@
     }
     else
     {
-        // TODO
+        uart_init();
     }
 }
 
-void write_uart(char value)
+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) 
 {
-    /*
-    *U0THR |= value;
-    *pinSelect_ptr |= PIN9_TX;
-    *enableDataFlow_ptr |= TXE0;
-    */
+   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
@@ -129,6 +149,7 @@
         write_uart(d2);
         write_uart(d3);
         write_uart(d4);
+        pc.printf("\n");
     }
 }