LPC-2148 UART

Hi

I am new to embedded development

I am trying to write this simple program that reads from UART1 and send to UART0. I wrote the following code but it does not work

I am using a Olimex LPC-P2148 board and the Peripherals Clock Frequency is set to the Processor Clock Frequency

I do not know what I am missing

Thank you in advance

//////////////////////////////////////////////////////////////////////

#define FREQ 12000000

void InitializeUART0(unsigned int baudrate)

{

unsigned int temp;

unsigned int divisor = FREQ / (16 * baudrate);

U0LCR = 0x83; // DLAB = 1, 8 bit, 1 stop bit, no parity

U0DLL = divisor & 0xFF; // LSB

U0DLM = (divisor >> 8 ) & 0xFF; // MSB

U0LCR = 0x03; //DLAB = 0, 8 bit, 1 stop bit, no parity

temp = PINSEL0;

PINSEL0 = (temp | 0x00000005); // Enable RxD0 and TxD0

}

void WriteToUART0(unsigned char c)

{

while ((U0LSR & 0x20) == 0); // wait for completion of previous data

U0THR = c;

}

unsigned char ReadFromUART0(void)

{

while ((U0LSR & 0x01) == 0); // wait for data

return U0RBR;

}

void InitializeUART1(unsigned int baudrate)

{

unsigned int temp;

unsigned int divisor = FREQ / (16 * baudrate);

U1LCR = 0x83; // DLAB = 1, 8 bit, 1 stop bit, no parity

U1DLL = divisor & 0xFF; // LSB

U1DLM = (divisor >> 8 ) & 0xFF; // MSB

U1LCR = 0x03; //DLAB = 0, 8 bit, 1 stop bit, no parity

temp = PINSEL0;

PINSEL0 = (temp | 0x00050000); // Enable RxD1 and TxD1

}

void WriteToUART1(unsigned char c)

{

while ((U1LSR & 0x20) == 0); // wait for completion of previous data

U1THR = c;

}

unsigned char ReadFromUART1(void)

{

while ((U1LSR & 0x01) == 0); // wait for data

return U1RBR;

}

int main(void)

{

unsigned char c;

InitializeUART0(9600);

InitializeUART1(9600);

while(1)

{

c = ReadFromUART1();

WriteToUART0(c);

}

return 0;

}

//////////////////////////////////////////////////////////////////////

IIRC, the equation for baud rate calculation is incorrect. On p. 99, you will find the equation for baud rate for USART0 (equation 2). Instead of “16U0DLM", it should be "256U0DLM”. Try that out and see if it makes it work for you because it had me messed up for a while. If not, post back and I’ll take a closer look.

The formula on page 99 of the datasheet, dictates that, U0DLM should be filled with the high byte of the prescaler, and U0DLL with the low byte of the prescaler, assuming you can live with the error indicated.

Baud = PCLK/(16*(256*U0DLM + U0DLL))

==> 256U0DLM + U0DLL = PCLK/(16Baud)

therefore if Prescaler = PCLK/(16*Baud)

then U0DLM should be (Prescaler >> 8 ) && 0xFF

U0DLL should be Prescaler && 0xFF

by the way the UART0 setup code works for sending carachters, it does not work for recieving characters

Thank you

here is what i use.

void UART0Initialize(unsigned long baudrate)

{

/* Configure UART */

unsigned int divisor = liblpc2000_get_pclk(liblpc2000_get_cclk(OSCILLATOR_CLOCK_FREQUENCY)) / (16 * baudrate);

U0LCR = 0x83; /* 8 bit, 1 stop bit, no parity, enable DLAB */

U0DLL = divisor & 0xFF;

U0DLM = (divisor >> 8) & 0xFF;

U0LCR &= ~0x80; /* Disable DLAB */

PINSEL0 = PINSEL0 & (~0xF)| 0x5;

U0FCR = 1;

/* Setup UART RX interrupt */

ctl_set_isr(6, 0, CTL_ISR_TRIGGER_FIXED, uart0ISR, 0);

ctl_unmask_isr(6);

U0IER = 1;

}

void UART1Initialize(unsigned long baudrate)

{

/* Configure UART */

unsigned int divisor = liblpc2000_get_pclk(liblpc2000_get_cclk(OSCILLATOR_CLOCK_FREQUENCY)) / (16 * baudrate);

U1LCR = 0x83; /* 8 bit, 1 stop bit, no parity, enable DLAB */

U1DLL = divisor & 0xFF;

U1DLM = (divisor >> 8) & 0xFF;

U1LCR &= ~0x80; /* Disable DLAB */

PINSEL0 = PINSEL0 & ~(0xFFFF << 16) | (0x5555 << 16);

U1FCR = 1;

/* Setup UART RX interrupt */

ctl_set_isr(7, 0, CTL_ISR_TRIGGER_FIXED, uart1ISR, 0);

ctl_unmask_isr(7);

U1IER = 1;

}

void UARTWriteChar(unsigned char ch)

{

while ((U0LSR & 0x20) == 0);

U0THR = ch;

}

unsigned char UARTReadChar(void)

{

while ((U0LSR & 0x01) == 0);

return U0RBR;

}

void UART1WriteChar(unsigned char ch)

{

while ((U1LSR & 0x20) == 0);

U1THR = ch;

}

unsigned char UART1ReadChar(void)

{

while ((U1LSR & 0x01) == 0);

return U1RBR;

}

I used your code without enabling interrupts, but no it does not work

is enabling interrupts required to receive characters on the UART?

Thanks