APP 1 S5.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
vinbel93
Date:
Tue Jan 12 15:24:02 2016 +0000
Parent:
13:08ef55cd14c6
Commit message:
UART with mbed.h

Changed in this revision

APP.cpp Show annotated file Show diff for this revision Revisions of this file
APP.h Show annotated file Show diff for this revision Revisions of this file
UART.cpp Show diff for this revision Revisions of this file
UART.h Show diff for this revision Revisions of this file
--- a/APP.cpp	Tue Jan 12 15:17:21 2016 +0000
+++ b/APP.cpp	Tue Jan 12 15:24:02 2016 +0000
@@ -6,39 +6,37 @@
 #include <stdint.h>
 
 #include "mbed.h"
-#include "UART.h"
 #include "APP.h"
-#include "LPC17xx.h"
 
 
-static I2C i2c(p28, p27);
+static I2C    i2c(p28, p27);
+static Serial uart(p9, p10);
 
 void setup()
 {
-    SystemCoreClockUpdate();
-
     // Setup UART and I2C.
-    UARTInit(9600);
+    uart.format(8, Serial::None, 1);
+    uart.baud(9600);
     i2c.frequency(100000);
-
+ 
     // Clear display
-    UARTSend(0x76);
-
+    uart.putc(0x76);
+ 
     // Decimal point for display
-    UARTSend(0x77);
-    UARTSend(0x02);
+    uart.putc(0x77);
+    uart.putc(0x02);
  
     // Maximum brightness
-    UARTSend(0x7A);
-    UARTSend(0xFF);
+    uart.putc(0x7A);
+    uart.putc(0xFF);
 }
-
+ 
 void activateAccelerometer()
 {
     uint8_t data[2] = {CTRL_REG, 0x01};
     i2c.write(ACC_ADDRESS, (char*) data, 2);
 }
-
+ 
 uint8_t readStatus()
 {
     uint8_t result[1];
@@ -48,7 +46,7 @@
  
     return result[0];
 }    
-
+ 
 int16_t convertToAcceleration(uint8_t hi, uint8_t lo)
 {
     int16_t value = (lo >> 2) | (hi << 6);
@@ -59,7 +57,7 @@
  
     return value;
 }
-
+ 
 int16_t readAndComputeAngle()
 {
     uint8_t result[6];
@@ -87,21 +85,27 @@
 {
     uint8_t digits[4];
     sprintf((char*) digits, "%d", angle);
- 
+
     if (angle < 100)
     {
-        UARTSend('0');
-        UARTSend('0');
-        UARTSend(digits, 2);
+        uart.putc('0');
+        uart.putc('0');
+        uart.putc(digits[0]);
+        uart.putc(digits[1]);
     }
     else if (angle < 1000)
     {
-        UARTSend('0');
-        UARTSend(digits, 3);
+        uart.putc('0');
+        uart.putc(digits[0]);
+        uart.putc(digits[1]);
+        uart.putc(digits[2]);
     }
     else
     {
-        UARTSend(digits, 4);
+        uart.putc(digits[0]);
+        uart.putc(digits[1]);
+        uart.putc(digits[2]);
+        uart.putc(digits[3]);
     }
 }
  
@@ -119,11 +123,11 @@
         {
             // Get accelerations, compute angle
             uint16_t angle = readAndComputeAngle();
-
+ 
             // Update 7 segment display
             updateDisplay(angle);
         }
-
+ 
         wait(0.4);
     }
 }
--- a/APP.h	Tue Jan 12 15:17:21 2016 +0000
+++ b/APP.h	Tue Jan 12 15:24:02 2016 +0000
@@ -3,7 +3,7 @@
 // Jeremy Pare      - parj2713
 
 #pragma once
-
+ 
 #define ACC_ADDRESS              0x1D << 1
 #define ACC_X_REG                0x01
 #define CTRL_REG                 0x2A
--- a/UART.cpp	Tue Jan 12 15:17:21 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,61 +0,0 @@
-// UART.cpp
-// Vincent Belanger - belv1802
-// Jeremy Pare      - parj2713
-
-#include <stdint.h>
-
-#include "UART.h"
-#include "APP.h"
-#include "LPC17xx.h"
-
-
-void UARTInit(uint32_t baudrate)
-{
-    uint32_t Fdiv;
-    uint32_t pclkdiv, pclk;
-
-    // Select UART3
-    LPC_PINCON->PINSEL0 &= ~0x00000003;
-    LPC_PINCON->PINSEL0 |= 0x00000002;
-
-    LPC_SC->PCONP |= 1 << 25; // Activate UART3 in PCONP register.
-    pclkdiv = (LPC_SC->PCLKSEL1 >> 18) & 0x03; // Bits 18~19 are for UART3
-
-    switch (pclkdiv)
-    {
-        case 0x00:
-        default:
-            pclk = SystemCoreClock/4;
-            break;
-        case 0x01:
-            pclk = SystemCoreClock;
-            break;
-        case 0x02:
-            pclk = SystemCoreClock/2;
-            break;
-        case 0x03:
-            pclk = SystemCoreClock/8;
-            break;
-    }
-   
-    LPC_UART3->LCR = 0x83;         // 8 bits, no Parity, 1 Stop bit, DLAB = 1
-    Fdiv = (pclk / 16) / baudrate; // baud rate
-    LPC_UART3->DLM = Fdiv / 256;   // MSB
-    LPC_UART3->DLL = Fdiv % 256;   // LSB
-    LPC_UART3->LCR = 0x03;         // DLAB = 0
-}
-
-void UARTSend(uint8_t byte)
-{
-    LPC_UART3->THR = byte;
-}
-
-void UARTSend(uint8_t *buffer, uint32_t length)
-{
-    while (length != 0)
-    {
-        LPC_UART3->THR = *buffer;
-        buffer++;
-        length--;
-    }
-}
--- a/UART.h	Tue Jan 12 15:17:21 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-// UART.h
-// Vincent Belanger - belv1802
-// Jeremy Pare      - parj2713
-
-#pragma once
-
-void UARTInit(uint32_t baudrate);
-void UARTSend(uint8_t byte);
-void UARTSend(uint8_t *buffer, uint32_t length);