UART Using ARM7

Description

UART its name implies it is universal. It can establish communication between the microcontroller and another device such as Bluetooth, GSM, GPS, RF module, Personal computer.

  • UART port can be used to interface to an RS232 port of a personal computer, hence voltage level of UART must be converted to voltage level compatible to RS232.
  • To convert the UART voltage level to the RS232 voltage level, you may use max232.
  • Other devices with UART ports such as GSM module, GPS module, Bluetooth module, RF module does not require the UART converter.
  • This device can be interface to microcontroller directly as long as the voltage level for both ends are compatible with each other.
  • ARM7 LPC21xx has two UART, UART0, and UART1.
  • UART0/1 includes fractional baud rate generator, auto-bauding capabilities, and handshake flow-control fully implemented in hardware.
  • The baud rate is determined by the register values U0DLL and U0DLM.

Registers Used For UART

  • APBDIV : APB divider register.
  • PINSEL0: pin0select register.
  • U0LCR: UART0 Line Control Register.
  • U0LSR: UART0 Line Status Register.
  • U0DLL and U0DLM : UART0 Divisor Latch registers.
  • U0THR: UART0 Transmit Holding Register.
  • U0RBR: UART0 Receiver Buffer register.

APBDIV – APB Divider Register

  • The APB Divider register(8bit) contains two bits, allowing three divider values used to select the processor clock.
  • Select one half of the processor clock.

VPBDIV – VPB Divider Register

  • VPBDIV is an 8bit register it is used to select the processor clock, first two-bit is allocated so as per the datasheet 10=APB bus clock is one half of the processor clock.
  • Hence in programming.
    VPBDIV =0X02

PINSEL0 :Pin0select Register

  • PINSEL0=0X00000005
    5 = 01 | 01
    RXD|TXD
    Setting port0, 0th and 1st pin as receiver and transmitter.

U0LCR – UART0 Line Control Register

  • The U0LCR is an 8bit register that determines the format of the data character that is to be transmitted or received.

U0LCR – UART0 Line Control Register

  • Set U0LCR=0X83.

U0LSR – UART0 Line Status Register

  • The U0LSR is a read-only register that provides status information on the UART0 TX and RX blocks.
  • This register gives the status of UART whether the data transmission and data receive is completed or not.

UART0 Divisor Latch registers

  • U0DLL and U0DLM -UART0 Divisor Latch registers.
  • The UART0 Divisor Latch is part of the UART0 Baud Rate Generator and holds the value used to divide the clock in order to produce the baud rate clock.
  • The U0DLL and U0DLM registers together form a 16-bit divisor where U0DLL contains the lower 8 bits of the divisor and U0DLM contains the higher 8 bits of the divisor.

BaudRate Calculation

  • Baudrate=9600,pclk=30mhz
    9600= 30mhz/16*(256*UnDLM+UnDLL)
    16*(256*UnDLM+UnDLL)=30MHZ/9600 (30MHZ/9600)=3125
  • DLM RESET VALUE =0X00 hence
    16*UnDLL=3125
    UnDLL=3125/16= 195
    hex value for 195 is C3
    U0DLL=0XC3

U0THR – UART0 Transmit Holding Register

  • THR Writing to the UART0 Transmit Holding Register causes the data to be stored in the UART0 transmit FIFO.
  • The byte will be sent when it reaches the bottom of the FIFO and the transmitter is available.

U0RBR – UART0 Receiver Buffer register

  • The UART0 Receiver Buffer Register contains the oldest received byte in the UART0 Rx FIFO.

Code

Transmit

 #include <LPC21xx.H>
 int main()
 {
 VPBDIV = 0x02; //Divide Pclk by two
 PINSEL0= 0x00000005; // Enable Rx and Tx
 U0LCR= 0x83; // 8 bits, no Parity, 1 Stop bit
 U0DLL= 0xC3; // 9600 Baud Rate @ 30MHz VPB Clock
 U0DLM=0X00;
 U0LCR = 0x03; //DLAB=0
 while(1)
 {
 U0THR='A'; //letter ‘A’ transfer to serial communication
 while (!(U0LSR & 0x20));// wait for previous transmission to finish
 while(1);
 }
 } 

Receive

 #include <LPC21xx.H>
 int main()
 {
 unsigned char RX;
 VPBDIV = 0x02;//pclk by two
 PINSEL0= 0x00000005; // Enable Rx and Tx
 U0LCR= 0x83;// 8 bits, no Parity, 1 Stop bit
 U0DLL= 0xC3;// 9600 Baud Rate @ 30MHz VPB Clock
 U0DLM=0X00;
 U0LCR = 0x03; //DLAB=0
 while(1)
 {
 //wait for receive data ready bit is set
 while(!(U0LSR & 0X01));
 //store the received data in RX from RBR
 RX=U0RBR;
 }
 }