Forums

debugging UART1

Started by Andy Zammy February 3, 2009
hi all,

i'm trying to get my uart peripheral to work (lpc2368 on MCB2300), and i'm struggling with it. first off, i'm using uart1 - the reason i'm using 1 is because i'm not 100% sure how i can reprogram with uart0 once i start using it in an application. just so i know, how would i use uart0 in an application and still use it to reprogram? just cycle power? anyway, not the most important part so:

i've got it to the point where i manage to see rubbish on the tera term (so i send something but its not coherant) and see rubbish on the dev board lcd (so i recieve something but its not coherant). what i see on the terminal program depends on the speed (NOT baud rate) i'm shooting stuff at it. eg, i try to stream the ascii 'a' and end up with the char '2'. i try to stream the ascii 'A' and end up with the char '2'. when i say stream, i mean i have a for loop constantly sending 'a' with no delay, the only delay is the wait for the send buffer to empty. however, when i add significant delay (second or so inbetween sends), the char that the terminal reads becomes a block.

i've currently tried with two cpu speeds: first try was 72MHz - that was a complete failure: it did shoot out ascii but much too slowly to see on a terminal program (i was at 20ms/div on the scope). i read that some 2368 might not be functional in the range 48-72MHz, so i scrapped that, and switched to 4MHz irc. in the end (in both attempts) i used this calculator to get my variables for the uart baudrate:
http://prototalk.net/forums/showthread.php?t

and here is the code: (there are 2 initialise uart functions in there, just in case someone can spot what i was doing wrong for the 72MHz try - i'm mainly interested in getting 4Mhz to work first though)

int main(void)
{
init_leds();
lcd_init();
// enable_pll(); used this in my 72MHz attempt
initialise_uart1_1(); // my 4mHz attempt
// initialise_uart1(); used this in my 72MHz attempt

for(;;)
{
sendchar(0x61);
}
}

void initialise_uart1_1(void)
{
PINSEL0 |= 0x40000000; /* Enable TxD1 */
PINSEL1 |= 0x00001555; /* Enable RxD1, CTS1, DCD1, DSR1, DTR1, RI, RTS */
CCLKCFG = 0x00;
IRCTRIM = 0x01; /* 4MHz IRC osc ??? */
PCLKSEL0 |= 0x100; /* Select UART1 clock to be equal to PCLK */

/* Sets DLAB = 1 so it is possible to access the */
U1LCR |= 0x83; /* dividers DLLSB and DLMSB to help set wanted baud rate + 8bits 1stop no parity */

U1DLL = 0x17; /* Set DLL == 23 */
U1DLM = 0x00; /* Set DLM == 0 */

U1LCR ^= 0x80; /* Sets DLAB = 0 to restrict access to DLLSB and DLMSB */

U1FDR |= 0xf2; /* make DIVADDVAL = 2 and MULVAL = 15 */

U1FCR = 0x07; /* enable FIFO and reset Rx and Tx buffers */
}

int sendchar(int ch) /* Write character to Serial Port */
{
while (!(U1LSR & 0x20)); /* Wait untill Transmit Holding Register is empty */
return (U1THR = ch); /* Place value 'ch' into Transmit Holding Register */
}

int getkey(void) /* Read character from Serial Port */
{
while (!(U1LSR & 0x01));
return (U1RBR);
}

void initialise_uart1(void)
{
PINSEL0 |= 0x40000000; /* Enable TxD1 */
PINSEL1 |= 0x00001555; /* Enable RxD1, CTS1, DCD1, DSR1, DTR1, RI, RTS */

/* This function implements the forumula:
baudrate = PCLK/16*(256*U1DLM+U1DLL)*(1+DivAddVal/MulVal)
by manipulating the registers containing the variables in the formula
*/

PCLKSEL0 |= 0x100; /* Select UART1 clock to be equal to PCLK */

/* Sets DLAB = 1 so it is possible to access the */
U1LCR |= 0x83; /* dividers DLLSB and DLMSB to help set wanted baud rate + 8bits 1stop no parity */

U1DLL = 0x01; /* Set DLL == 1 */
U1DLM = 0x77; /* Set DLM == 119 */

U1LCR ^= 0x80; /* Sets DLAB = 0 to restrict access to DLLSB and DLMSB */

U1FDR |= 0x21; /* make DIVADDVAL = 1 and MULVAL = 2 */

U1FCR = 0x07; /* enable FIFO and reset Rx and Tx buffers */
}

any help would be much appreaciated.
i hope i've not forgotten anything, if so just yell and i'll put it up.

zammy


An Engineer's Guide to the LPC2100 Series