APP 1 S5.

Dependencies:   mbed

Revision:
13:08ef55cd14c6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UART.cpp	Tue Jan 12 15:17:21 2016 +0000
@@ -0,0 +1,61 @@
+// 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--;
+    }
+}