-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuart.c
67 lines (46 loc) · 1.59 KB
/
uart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<lpc214x.h>
#include<stdio.h>
#include<string.h>
// https://www.youtube.com/watch?v=Cb21y910CKE&ab_channel=AndreasSpiess
// as shown ^, just tuning into the correct com port with the correct baud rate in PuTTY should be enough to receive the uart messages on the pc
void delay_ms(int ms){
int i, j;
for(j=0;j<ms;j++){
for(i=0;i<12000;i++);
}
}
void UART_INIT(void){
PINSEL0 = 0x00000005; /* 1:0 = 01 -> P0.0 function set as TX, 3:2 = 01 -> P0.1 function set as RX, OR'ing is being done to make sure the function of other pins is undisturbed */
U0LCR = 0x83; /* 1:0 = 11 -> 8 bit mode, 7 = 1 -> DLM and DLL can be edited, Data tx and rx OFF */
U0DLM = 0x00; /* divisor = Pclk/(16*baudrate), for baudrate = 9600, pclk = 60Mhz divisor = 391 => usb = 0x01 */
U0DLL = 0x60; /* divisor = Pclk/(16*baudrate), for baudrate = 9600, pclk = 60Mhz divisor = 391 => lsb = 0x87 */
U0LCR = 0x03; /* 1:0 = 11 -> 8 bit mode, 7 = 0 -> DLM and DLL can NOT be edited, Data tx and rx ON */
}
void UART_TXCHAR(char data){
U0RBR = data;
while((U0LSR & 0x20)==0);
}
char UART_RX(void){
char data;
while((U0LSR & 0x01)==0);
data = U0THR;
return data;
}
void UART_TXSTRING(char* data){
int i = 0;
while(data[i] != 0){
UART_TXCHAR(data[i]);
i++;
}
}
int main(void){
int i = 0;
UART_INIT();
for(i=0; i<20; i++){
UART_TXSTRING("Hello UART, LPC2148 to PC");
UART_TXCHAR(0x0a); // Just the unicode for '\n' -> New Line
UART_TXCHAR(0x0d); // Just the unicode for '\t' -> Carriage return
delay_ms(2000);
}
return 0;
}