#include "LPC2148.h"
// VIC controller configuration
#define TXD0_P00_SEL (1<<0)
#define RXD0_P01_SEL (1<<2)
//PCLK frequency 60 MHz
#define PCLK 60000000
//Set bitrate
#define UART_BAUD(baud) (unsigned short)(PCLK/(baud*16.0)+0.5)
#define U0LCR_8Bit_Data 3
#define U0LCR_1Bit_Stop 0
#define U0LCR_No_Parity 0
#define U0FCR_14Char_Fifo (3<<6)
#define U0FCR_FIFO_Enable 0x1
#define U0LSR_THRE 0x20
#define U0LSR_RDR 0x1
#define U0LCR_Divisor_Latch_Access_Bit 0x80U
//Initializing UART0
static void Uart0Init(unsigned short BaudRate)
{
//Chose RX and TX
PINSEL0 |= TXD0_P00_SEL | RXD0_P01_SEL;
//Set transfer speed
U0LCR=U0LCR_Divisor_Latch_Access_Bit;
U0DLL=(unsigned char)BaudRate;
U0DLM=(unsigned char)(BaudRate>>8);
//Set 8,n,1
U0LCR=U0LCR_8Bit_Data | U0LCR_1Bit_Stop | U0LCR_No_Parity;
//Enable fifo
U0FCR=U0FCR_FIFO_Enable | U0FCR_14Char_Fifo;
//Disable interrupts and zeros interrupts flags
U0IER=0;
U0IIR=0;
//When character is received LSR set to 0
U0LSR=0;
//IO0DIR = 0x00000100;
}
static void Uart0Puts (const char *str)
{
//Send next character
while (*str)
{
//wait until get permission to save into send buffer
while(!(U0LSR&U0LSR_THRE));
//sent char and go to the next char
U0THR=*str++;
}
}
static void Uart0Gets (char *str)
{
char c;
do
{
//wait for char
while(!(U0LSR&U0LSR_RDR));
//receive char
c=U0RBR;
IOCLR0=0x100;
wait();
IOSET0=0x100;
//save char
*str++=c;
//wait for transmitter
while(!(U0LSR&U0LSR_THRE));
//send back char
U0THR=c;
}
while (c!='r');
*(str-1)=0;
}
void wait(void)
{
int i;
for (i=0;i<100000;i++);
}
char buf[1024];
int main (void)
{
Uart0Init(UART_BAUD(9600));
IODIR0 = 0x00000100;
PINSEL0|=0x100;
IOSET0=0x100;
Uart0Puts("A");
wait();
Uart0Puts("T");
wait();
Uart0Puts("H");
wait();
Uart0Puts("\r");
wait();
IOCLR0=0x100;
while(1)
{
//receive line
Uart0Gets(buf);
}
return(0);
}