EmbeddedRelated.com
Forums

LPC2103 - having trouble getting UART working

Started by ah_sandiego June 18, 2007
Below is the code I am using. It is based on code I had working on
the LPC2148. I don't know what I am missing.

Has anyone got this working on the LPC2103??

Thanks
AH.
#define PLOCK 0x400

void InitializeClocksAndMemory(void)
{
// LPC2103 has clock at FOSC.7456MHz (crystal)

// P=2 gives an FCCO = 14.7456MHz * M * 2 * P = 235.9296MHz
PLLCFG=0x23; // MSEL=3 and PSEL=1
FeedPLL(); // Thus M=4 and P=2

// This sets CCLK = FOSC*M = 14.7456MHz*4 = 58.9824MHz
// FCCO = CCLK * 2 * P = 58.982MHz * 2 * 2 = 235.9296MHz

// Enabling the PLL to lock to the requested frequency
PLLCON=0x1;
FeedPLL();

// PLOCK bit (of PLLSTAT) will equal 1 when PLL is locked
// to requested frequency
// Wait for the PLL to lock to this frequency set above
while((PLLSTAT&PLOCK)==0);

// Sets PLLC. Connect the PLL as the clock source
// Now CCLK should be running at 58.982MHz
// and thus ARM is running at 294.912MHz
PLLCON=0x3;
FeedPLL();
// Enabling MAM and setting number of clocks used for
// Flash memory fetch (4 cclks in this case)
MAMCR=0x2; // Memory accelerator functions fully enabled
MAMTIM=0x4; // fetch cycles are 4 clocks in duration

// Sets the Peripheral Clock (PCLK)
// to *HALF* the processor clock CCLK
APBDIV = 0x02; // PCLK = 29.491MHz
} // void InitializeClocksAndMemory(void)

// Setting up the UART requires assigning the GPIOs
// and configuring the BAUD rate

void Initialize_UART(void)
{
long PinSel0 = PINSEL0; // Read current pin config for PORT0

// To enable the UART0,
// TX is at P0.0 so PINSEL0[1:0] = 0b01 = 0x1
// RX is at P0.1 so PINSEL0[3:2] = 0b01 = 0x4
PinSel0 |= 0x1; // enable P0.0 as TX
PinSel0 |= 0x4; // enable P0.1 as RX

PINSEL0 = PinSel0; // assign new pin functions
// UART0 has a 16 byte RX/TX FIFO
// U0RBR = Recieve Buffer Register on UART0
// U0THR = Transmit Holding Register on UART0

// U0LCR = Line Control Register
// Want to setup 8 bit characters, i.e. ASCII,
// 1 stop bit, no parity
// Divisor Latch Access Bit DLAB (bit 7) must be set too
U0LCR=0x83;

// U0FCR = UART0 FIFO Control Register - Enable and reset
// both RX/TX FIFOs by putting 0b111
U0FCR=0x7; // See page 90 of user manual

// Want a BAUD rate of 9600
// With PCLK = 29.491MHz, U0DLL0
// MULVAL, DIVADDVAL=1

U0DLL=0xB4; // LSB = 180
U0DLM=0x0; // MSB
U0FDR=0xF1; // Fractional Divide Register
// MULVAL | DIVADDVAL = 15 | 1

// Clear DLAB - now the divisor registers can't be modified
// and access to the TX and RX buffers is available
U0LCR=0x3;

} // end of Initialize_UART

An Engineer's Guide to the LPC2100 Series

This code is fine. I was overwriting the PINSEL0 values in main and
that was why it was not working.

--- In l..., "ah_sandiego"
wrote:
>
> Below is the code I am using. It is based on code I had working on
> the LPC2148. I don't know what I am missing.
>
> Has anyone got this working on the LPC2103??
>
> Thanks
> AH.
> #define PLOCK 0x400
>
> void InitializeClocksAndMemory(void)
> {
> // LPC2103 has clock at FOSC.7456MHz (crystal)
>
> // P=2 gives an FCCO = 14.7456MHz * M * 2 * P = 235.9296MHz
> PLLCFG=0x23; // MSEL=3 and PSEL=1
> FeedPLL(); // Thus M=4 and P=2
>
> // This sets CCLK = FOSC*M = 14.7456MHz*4 = 58.9824MHz
> // FCCO = CCLK * 2 * P = 58.982MHz * 2 * 2 = 235.9296MHz
>
> // Enabling the PLL to lock to the requested frequency
> PLLCON=0x1;
> FeedPLL();
>
> // PLOCK bit (of PLLSTAT) will equal 1 when PLL is locked
> // to requested frequency
> // Wait for the PLL to lock to this frequency set above
> while((PLLSTAT&PLOCK)==0);
>
> // Sets PLLC. Connect the PLL as the clock source
> // Now CCLK should be running at 58.982MHz
> // and thus ARM is running at 294.912MHz
> PLLCON=0x3;
> FeedPLL();
> // Enabling MAM and setting number of clocks used for
> // Flash memory fetch (4 cclks in this case)
> MAMCR=0x2; // Memory accelerator functions fully enabled
> MAMTIM=0x4; // fetch cycles are 4 clocks in duration
>
> // Sets the Peripheral Clock (PCLK)
> // to *HALF* the processor clock CCLK
> APBDIV = 0x02; // PCLK = 29.491MHz
> } // void InitializeClocksAndMemory(void)
>
> // Setting up the UART requires assigning the GPIOs
> // and configuring the BAUD rate
>
> void Initialize_UART(void)
> {
> long PinSel0 = PINSEL0; // Read current pin config for PORT0
>
> // To enable the UART0,
> // TX is at P0.0 so PINSEL0[1:0] = 0b01 = 0x1
> // RX is at P0.1 so PINSEL0[3:2] = 0b01 = 0x4
> PinSel0 |= 0x1; // enable P0.0 as TX
> PinSel0 |= 0x4; // enable P0.1 as RX
>
> PINSEL0 = PinSel0; // assign new pin functions
> // UART0 has a 16 byte RX/TX FIFO
> // U0RBR = Recieve Buffer Register on UART0
> // U0THR = Transmit Holding Register on UART0
>
> // U0LCR = Line Control Register
> // Want to setup 8 bit characters, i.e. ASCII,
> // 1 stop bit, no parity
> // Divisor Latch Access Bit DLAB (bit 7) must be set too
> U0LCR=0x83;
>
>
> // U0FCR = UART0 FIFO Control Register - Enable and reset
> // both RX/TX FIFOs by putting 0b111
> U0FCR=0x7; // See page 90 of user manual
>
> // Want a BAUD rate of 9600
> // With PCLK = 29.491MHz, U0DLL0
> // MULVAL, DIVADDVAL=1
>
> U0DLL=0xB4; // LSB = 180
> U0DLM=0x0; // MSB
> U0FDR=0xF1; // Fractional Divide Register
> // MULVAL | DIVADDVAL = 15 | 1
>
> // Clear DLAB - now the divisor registers can't be modified
> // and access to the TX and RX buffers is available
> U0LCR=0x3;
>
> } // end of Initialize_UART
>
Hi
I am currently working on lpc2148 with 12MHz crystal on my board.I am
facing problem in serial communication. I have the same problem of
you, something missing....? Can you send me the LPC2148 code that you
had working?

Thanks

--- In l..., "ah_sandiego" wrote:
>
> Below is the code I am using. It is based on code I had working on
> the LPC2148. I don't know what I am missing.
>
> Has anyone got this working on the LPC2103??
>
> Thanks
> AH.
> #define PLOCK 0x400
>
> void InitializeClocksAndMemory(void)
> {
> // LPC2103 has clock at FOSC.7456MHz (crystal)
>
> // P=2 gives an FCCO = 14.7456MHz * M * 2 * P = 235.9296MHz
> PLLCFG=0x23; // MSEL=3 and PSEL=1
> FeedPLL(); // Thus M=4 and P=2
>
> // This sets CCLK = FOSC*M = 14.7456MHz*4 = 58.9824MHz
> // FCCO = CCLK * 2 * P = 58.982MHz * 2 * 2 = 235.9296MHz
>
> // Enabling the PLL to lock to the requested frequency
> PLLCON=0x1;
> FeedPLL();
>
> // PLOCK bit (of PLLSTAT) will equal 1 when PLL is locked
> // to requested frequency
> // Wait for the PLL to lock to this frequency set above
> while((PLLSTAT&PLOCK)==0);
>
> // Sets PLLC. Connect the PLL as the clock source
> // Now CCLK should be running at 58.982MHz
> // and thus ARM is running at 294.912MHz
> PLLCON=0x3;
> FeedPLL();
> // Enabling MAM and setting number of clocks used for
> // Flash memory fetch (4 cclks in this case)
> MAMCR=0x2; // Memory accelerator functions fully enabled
> MAMTIM=0x4; // fetch cycles are 4 clocks in duration
>
> // Sets the Peripheral Clock (PCLK)
> // to *HALF* the processor clock CCLK
> APBDIV = 0x02; // PCLK = 29.491MHz
> } // void InitializeClocksAndMemory(void)
>
> // Setting up the UART requires assigning the GPIOs
> // and configuring the BAUD rate
>
> void Initialize_UART(void)
> {
> long PinSel0 = PINSEL0; // Read current pin config for PORT0
>
> // To enable the UART0,
> // TX is at P0.0 so PINSEL0[1:0] = 0b01 = 0x1
> // RX is at P0.1 so PINSEL0[3:2] = 0b01 = 0x4
> PinSel0 |= 0x1; // enable P0.0 as TX
> PinSel0 |= 0x4; // enable P0.1 as RX
>
> PINSEL0 = PinSel0; // assign new pin functions
> // UART0 has a 16 byte RX/TX FIFO
> // U0RBR = Recieve Buffer Register on UART0
> // U0THR = Transmit Holding Register on UART0
>
> // U0LCR = Line Control Register
> // Want to setup 8 bit characters, i.e. ASCII,
> // 1 stop bit, no parity
> // Divisor Latch Access Bit DLAB (bit 7) must be set too
> U0LCR=0x83;
>
>
> // U0FCR = UART0 FIFO Control Register - Enable and reset
> // both RX/TX FIFOs by putting 0b111
> U0FCR=0x7; // See page 90 of user manual
>
> // Want a BAUD rate of 9600
> // With PCLK = 29.491MHz, U0DLL0
> // MULVAL, DIVADDVAL=1
>
> U0DLL=0xB4; // LSB = 180
> U0DLM=0x0; // MSB
> U0FDR=0xF1; // Fractional Divide Register
> // MULVAL | DIVADDVAL = 15 | 1
>
> // Clear DLAB - now the divisor registers can't be modified
> // and access to the TX and RX buffers is available
> U0LCR=0x3;
>
> } // end of Initialize_UART
>
--- In l..., "kanaryes" wrote:
>
> Hi
> I am currently working on lpc2148 with 12MHz crystal on my board.I am
> facing problem in serial communication. I have the same problem of
> you, something missing....? Can you send me the LPC2148 code that you
> had working?
>
> Thanks

I posted 'lpc2148-uart.zip' in the Files folder. This archive has
code for the PLL and UART of an LPC2148. I will delete the file in a
couple of days.

Richard