LPC2478 UART0

Dear All,

For the past few days I have been trying to send a string of data to the PC but I get a garbage value…

The following is the code…


#include “LPC24xx.h” /* LPC23xx/24xx definitions */

#define EA_BOARD_LPC24XX 0

#define LINE_FEED 0x0A

#define CARRIAGE_RET 0x0D

/* System configuration: Fosc, Fcclk, Fcco, Fpclk must be defined */

/* PLL input Crystal frequence range 4KHz~20MHz. */

#define Fosc 12000000

/* System frequence,should be less than 80MHz. */

#define Fcclk 48000000

#define Fcco 288000000

/* APB clock frequence , must be 1/2/4 multiples of ( Fcclk/4 ). */

/* If USB is enabled, the minimum APB must be greater than 16Mhz */

#define Fpclk (Fcclk / 2)

void ptchr(char c)

{

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

U0THR = c;

}

void UARTInit(unsigned long PortNum,unsigned long baudrate)

{

unsigned long Fdiv;

if ( PortNum == 0 )

{

PINSEL0 = 0x00000050; /* RxD0 and TxD0 */

U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */

Fdiv = ( Fpclk / 16 ) / baudrate; /*baud rate */

U0DLM = Fdiv / 256;

U0DLL = Fdiv % 256;

U0LCR = 0x03; /* DLAB = 0 */

U0FCR = 0x07; /* Enable and reset TX and RX FIFO. */

}

else if (PortNum == 1)

{

#if EA_BOARD_LPC24XX

PINSEL7 |= 0x0000000F; /* P3.16 TXD1, P3.17 RXD1 */

#else /* Default is Keil MCB2300 board */

PINSEL0 |= 0x40000000; /* Enable TxD1 P0.15 */

PINSEL1 |= 0x00000001; /* Enable RxD1 P0.16 */

#endif

U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */

Fdiv = ( Fpclk / 16 ) / baudrate ; /*baud rate */

U1DLM = Fdiv / 256;

U1DLL = Fdiv % 256;

U1LCR = 0x03; /* DLAB = 0 */

U1FCR = 0x07; /* Enable and reset TX and RX FIFO. */

}

}

int main (void)

{

int i;

char c[16]=“Philips LPC”;

UARTInit(0,9600); /* baud rate setting */

/* Keep Transmitting until Null character(‘\0’) is reached */

for(i=0;i<16;i++)

{

ptchr(c*);*

  • }*
  • U0THR=LINE_FEED;*
  • U0THR=CARRIAGE_RET;*
    _ /* Wait till U0THR and U0TSR are both empty */_
  • while(!(U0LSR & 0x20)){}*
    }
    ---------------------------------------------------------------------------
    It would be great if someone can help me sort this problem…

I can immediately see a couple of problems:

A)

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

should be

while(!(U0LSR & 0x20));

In C && is the logical ‘and’ operator; & is the bit-mask ‘and’ operator which is what you need here.

B)

/* Keep Transmitting until Null character(‘\0’) is reached */

for(i=0;i<16;i++)

The code is not doing what the comment says it should be doing. It is always going to transmit 16 characters including any garbage that is after the null character as well.

char c[16]=“Philips LPC”;

only populates the array c with 12 characters.

From what I see here I recommend that you spend some time brushing up on your understanding of the C language before doing much more coding. If you do not you are going to be spending an awful lot of time trying to identify obscure problems at the testing / debugging stage. In addition to studying a reference like Kernighan & Ritchie’s ‘C Programming Language’ book, an article well worth reading is “C Traps and Pitfalls”:

http://www.literateprogramming.com/ctraps.pdf

The introduction says it all:

“The C language and its typical implementations are designed to be used easily by experts. The language is terse and expressive. There are few restrictions to keep the user from blundering. The user who has blundered is often rewarded by an effect that is not obviously related to the cause”.

When you do restart coding make sure that you read what you have written at least once, and that you understand what you have written, before attempting to execute it

Hi!!! Thank you Chris for your suggestions

I am a bit rusty with C programming since I am programming after almost 6 months…

Even after doing the necessary changes, it is still showing garbage value.

I tried using the sample code for UART provided by NXP. It is an interrupt driven code.

With this code, nothing is displayed on the com port of the PC.

SushilR:
I tried using the sample code for UART provided by NXP. It is an interrupt driven code.

With this code, nothing is displayed on the com port of the PC.

You might need to isolate the problem first. Are you sure that the software (terminal emulator?) at the PC end is configured and working correctly? Test it by communicating with a development board and compiled software that you know does work.

Hi!!! Thank you Chris for your suggestions

The problem was not with the code but with the startup file.

Once I used the startup file provided in NXP Sample Examples and not the one generated by Keil when device LPC2478, the problem got solved.

i am new to this, I have a lpc2478 stk, I could work together to send data by R232 "UART0