Lpc11c24 - uart

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Lpc11c24 - uart

1,759 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by teslabox on Wed Apr 04 01:43:21 MST 2012
Hello everyone,

I try to run the UART communication on the LPC11C24 and there are some problems - nothing is sent. I use XTAL 12 MHz and my FCCO frequency is 192 MHz, at the output PLL is 12 MHz and UARTPCLK frequency is 12 MHz (UARTCLK Divider = 1). Fractional Divider (1+D/M) is set for 115200 kbps UART speed.
Below is my C code:
void UARTInit (void)
{
    LPC_SYSCON->SYSAHBCLKCTRL |= (1<12);
    LPC_SYSCON->UARTCLKDIV = 1;

    LPC_IOCON->PIO1_6 &=~(1<<4)|(1<<0);        /* UART as the I/O config 
    LPC_IOCON->PIO1_6 |= (1<<4)|(1<<0);     /* UART as the RXD */
    LPC_IOCON->PIO1_7 &=~(1<<4)|(1<<0);        /* UART as the I/O config 
    LPC_IOCON->PIO1_7 |= (1<<4)|(1<<0);     /* UART as the TXD */
    LPC_UART->LCR |= ((1<<0)|(1<<1));     // bit 0-1: Word Length Select             = 11
    LPC_UART->LCR &=~(1<<2);             // bit 2:   Stop Bit Select = 0
    LPC_UART->LCR &=~(1<<3);             // bit 3:   Parity Enable = 0
    LPC_UART->LCR &=~((1<<4)|(1<<5));     // bit 4-5: does not matter (Parity Select     = 00)
    LPC_UART->LCR &=~(1<<6);            // bit 6:   Break Control = 0
    LPC_UART->LCR |= (1<<7);            // acces to the DLM % DLL; bit 7: Divisor Latch Access Bit (DLAB) = 1

    LPC_UART->DLM = 0;            // DLM = 0
    LPC_UART->DLL = 3;            // DLL = 3 (because DLM=0)
    LPC_UART->FDR = 0xCE;         // D=14, M=12 => D/M=1,1667 => 1+(D/M)=2,1667
    LPC_UART->LCR &=~(1<<7);    // no access to the DLL & DLM; bit 7: Divisor Latch Access Bit (DLAB) = 0
    LPC_UART->FCR = 0;            // no FIFO for UART   

}

void UART_SendByte (uint8_t Byte)
{
    while ((LPC_UART->LSR & 0x20) == 0); 
    LPC_UART->THR = Byte;                       
}
0 Kudos
Reply
4 Replies

1,440 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by gbm on Wed Apr 04 12:44:43 MST 2012
1. Make sure that IOCON block is enabled.
2. Replace ORs and ANDs with simple assignments, so that you know the real values being written to registers.
3. Start with any UART speed not requiring fractional divider.
4. Note that statements used to set UART divisors for a given baud speed are usually incorrect - use the one below.
void UART0_Init(int baudrate)
{
    unsigned int Fdiv = (SystemCoreClock / 16 + baudrate / 2) / baudrate ;    // Set baud rate

    LPC_UART0->LCR = 0x83;        // 8 bits, no Parity, 1 Stop bit, DLAB=1
    LPC_UART0->DLM = Fdiv / 256;                            
    LPC_UART0->DLL = Fdiv % 256;
    LPC_UART0->LCR = 0x03;        // 8 bits, no Parity, 1 Stop bit DLAB = 0
    LPC_UART0->FCR = 0x07;        // Enable and reset TX and RX FIFO
}

5. According to LPC11xx docs, UART pins should be set to UART functions in IOCON BEFORE the clock is enabled for UART block in SYSAHBCLKCTRL

6. Don't forget to buy me a beer.
0 Kudos
Reply

1,440 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by RodColeman on Wed Apr 04 06:15:12 MST 2012
Please go to folder CONFIG in your project. Then find driver_config.h


#define CONFIG_ENABLE_DRIVER_UART    1



You also have to enable the drivers.

Go to folder drivers, select uart.c. right-click > properties > C/C++ build & make sure "exclude resource from build" is NOT selected,for all builds.

Then do same for uart.h.

finally, add

#include uart.h



in your source code [eg main.c]

Now, you can use the driver's UARTsend() function to send some data.

if still does not work, use debug to tell us what is the value of SYSAHBCLKCTRL register, and the baud rate divider registers, and the IOCON registers for TXD & RXD.

Do this with a breakpoint set just before the UARTsend().
0 Kudos
Reply

1,440 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by teslabox on Wed Apr 04 05:44:03 MST 2012
I read the example for proper settings of theese values in the datasheet on the page 209 [I](13.5.15.1.2 Example 2: UART_PCLK = 12 MHz, BR = 115200)[/I]. That's the same setting as mine so I changed theese values in my C code and nothing again - on the oscilloscope is a straight line on 3,24 V (hi level) at TxD line during I send bytes.

void UART_Init (void)
{
    LPC_SYSCON->SYSAHBCLKCTRL |= (1<12);    // set the clock and power for UART
    LPC_SYSCON->UARTCLKDIV = 1;                // set UART clock divider value = 1

    LPC_IOCON->PIO1_6 &=~(1<<4)|(1<<0);        // UART as the I/O config
    LPC_IOCON->PIO1_6 |= (1<<4)|(1<<0);        // UART as the RXD */
    LPC_IOCON->PIO1_7 &=~(1<<4)|(1<<0);        // UART as the I/O config
    LPC_IOCON->PIO1_7 |= (1<<4)|(1<<0);     // UART as the TXD */

    LPC_UART->LCR |= ((1<<0)|(1<<1));         // bit 0-1: Word Length Select    = 11
    LPC_UART->LCR &=~(1<<2);                 // bit 2:   Stop Bit Select     = 0
    LPC_UART->LCR &=~(1<<3);                 // bit 3:   Parity Enable         = 0
    LPC_UART->LCR &=~((1<<4)|(1<<5));         // bit 4-5: does not matter (Parity Select     = 00)
    LPC_UART->LCR &=~(1<<6);                // bit 6:   Break Control         = 0
    LPC_UART->LCR |= (1<<7);                // access to the DLM % DLL; bit 7: Divisor Latch Access Bit (DLAB) = 1

    LPC_UART->DLM = 0;                        // DLM = 0
    LPC_UART->DLL = 4;                        // DLL = 3 (because DLM=0)
    LPC_UART->FDR = 0x85;                     // D=14, M=12 => D/M=1,1667 => 1+(D/M)=2,1667

    LPC_UART->LCR &=~(1<<7);                // no access to the DLL & DLM; bit 7: Divisor Latch Access Bit (DLAB) = 0
    LPC_UART->FCR = 0x07;                    // no FIFO for UART
}


From the other hand:
How to join the uart.c and uart.h files to the project in LPCXpresso4 for compile  it?
I downloaded the example project dedicated for LPC11C24 called "can_onchip" and there are all drivers for all peripherials but like "unactive".
0 Kudos
Reply

1,440 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by RodColeman on Wed Apr 04 04:38:04 MST 2012
Hi, Yes the UART does not work if you use the standard NXP driver and a 12MHz clock, because fractional baud rate are not handled correctly by the uart.c driver.

If you don't like to use the NXP driver, just copy the UARTInit() function from it, and modify.

This is how I got mine working on a LPC1114:


UARTInit(115200);  // start UART using NXP driver uart.h / uart.c
uartReKon();   //fix the baudrate using fractional divider 6,5: required when using 12M IRC


Here's the code to set up fractional baud rate for 115200 from 12M clock:


void uartReKon  (void) {
LPC_UART->LCR = 0x83; //DLAB=1 to get access to baud dividers
LPC_UART->DLM = 0;
LPC_UART->DLL = 4;
LPC_UART->FDR = 0x85;     // 12MHz, 115200: DIVADDVAL =5, MULVAL =8
LPC_UART->LCR = 0x03;// DLAB = 0
LPC_UART->FCR = 0x07;/* Enable and reset TX and RX FIFO. */;
}


hope that works for you.
0 Kudos
Reply