Quantcast
Channel: Gadgetronicx
Viewing all articles
Browse latest Browse all 215

How to program UART in ARM Microcontrollers

$
0
0
Serial-communication-UART-tutorial-ARM-microcontrollers
Serial communication(UART) serves an important purpose of communicating with the external devices just using two pins (RX and TX). This tutorial focuses on Programming Serial communication in ARM7 Microcontrollers. I assume that you are familiar with the working principle behind Serial communication in Microcontrollers, so let's not waste time with the introductions.

UART IN ARM MICROCONTOLLERS:

ARM controllers consists of two in built UART's namely UART0 & UART1. Both the UART's are similar in nature except modem interface which is present in UART1. In this tutorial we focus on programming UART0 and you can learn UART1 by yourself.

REGISTERS USED:

Here is the list of registers used to Program UART0

U0RBR:
UART0 Receiver Buffer register is used to store the received data from external devices via RXD0 pin.

U0THR:
UART0 Transmit Buffer Register used to hold the data which is about to transmit to external devices from the Microcontroller.

U0LCR:
UART0 Line Control register is used to assign the format such as word length, parity, stop bits etc of the data  that is to be transmitted or received. DLAB bit of this register plays a significant role, it must be 1 to access Baud rate generation registers (U0DLM & U0DLL) whereas it should be 0 to access TX and RX registers (U0RBR & U0THR).

U0LSR:
UART0 Line Status Register is a read only register which is used to understand the status such as Transmission complete, Receive complete etc of the UART0.

U0DLM & U0DLL:
These two registers are used to generate the required Baud rate for the serial communication to take place. These Two register UART0 Divisor Latch MSB (U0DLM) & UART0 Divisor Latch LSB (U0DLL) together forms a 16 bit register which holds a divisor value to generate baud rate from VPB clock.

Kindly refer the manual for brief description of these registers. get the manual from the below link

BAUD RATE GENERATION IN UART0:

As i said above the two registers U0DLM & U0DLL holds the 16 bit value for baud rate generation and we have to determine this value and load in these registers to generate required baud rate. The value in these registers will divide the VPB clock to generate the required baud rate clock for the UART and  it must be 16x of the desired baud rate.

Here we are about to use standard "9600" as Baud rate. Since it must be 16x of desired rate for calculation
    Required Baud Rate = 16 x Desired Baud Rate
    Required Baud Rate = 16 x 9600
    Required Baud Rate = 153600

We are using external crystal frequency of 20MHz, and by default Peripheral Clock is 1/4 th of the processor clock fed in to the controller.
VPB Clock or Peripheral Clock = 20MHz/4 = 5MHz

To obtain the value of the U0DLM & U0DLL
 Value = 5MHz / Desired Baud Rate
 Value = 5000000/153600 = 32.5
 Value =33
33 Equivalent Hex value is 0x21. So the values of U0DLM & U0DLL are

U0DLM= 0x00
U0DLL= 0x21

STEPS TO PROGRAM UART:

  1. Assign the format of the data to be transmitted or received using U0LCR register.
  2. Make the DLAB bit in the U0LCR register to access the Baud rate registers U0DLM & U0DLM to set the desired baud rate.
  3. After setting the baud rate make the DLAB bit low to access the U0THR and U0RBR to send and receive data .
  4. Then preform either transmit or receive operation using your microcontroller.
  5. You can determine the status of the UART0 using the U0LSR register.

CODE:

The below code was built using Keil uVision 4. This code was built in way to send three characters "SND" initially and then display the received characters in a LCD.

  1. #include<lpc21xx.h>
  2. void delay(void);
  3. void receive(void);
  4. void transmit(void);
  5. char c;
  6. void lcd(unsigned char a,int b);
  7. int main(void)
  8. {
  9.    PINSEL0|=(1<<0)|(1<<2);  //Selecting pin functions
  10.    PINSEL2=0;
  11.    IODIR1=0x03ff0000;           //Setting Directions for LCD
  12.    lcd(0x38,0);
  13.    lcd(0x0f,0);
  14.    U0LCR=(1<<7);                //Making DLAB bit as '1'
  15.    U0DLL=0x21;                    //Baud Rate Value
  16.    U0LCR=0x03;                    //DLAB as 0 and word length selection
  17.    delay();
  18.    transmit();  
  19.    while(1)
  20.      {
  21.        receive();                      //Infinite receive loop
  22.      }
  23. }

  24. void delay(void)                   //Software Delay
  25.  {
  26.    unsigned int i;
  27.    for(i=0;i<=3000;i++);
  28.  }

  29. void lcd(unsigned char a,int b)   //LCD subroutine
  30.  {
  31.   IOSET1=a<<16;
  32.   IOSET1=b<<24;
  33.   IOSET1=1<<25;
  34.   delay();
  35.   IOCLR1=1<<25;
  36.   IOCLR1=b<<24;
  37.   IOCLR1=a<<16;
  38.  }

  39. void receive(void)               //Receive subroutine
  40.  {
  41.   while(!(U0LSR&(1<<0)));    //Status check
  42.   c=U0RBR;
  43.   lcd(c,1);
  44.  }

  45. void transmit(void)          //Transmit subroutine for "SND"
  46. {
  47. U0THR='S';                      
  48. while(!(U0LSR&(1<<5)));  //Status check
  49. U0THR='N';
  50. while(!(U0LSR&(1<<5)));
  51. U0THR='D';
  52. while(!(U0LSR&(1<<5)));
  53. }


Viewing all articles
Browse latest Browse all 215

Trending Articles