Embedded System


Library for Embedded System

ARM UART Library

UART Library

UART hardware module is available with a number of ARM. UART Library provides comfortable work with the Asynchronous (full duplex) mode. LPC2148 has two serial ports. Here two serial port function do with UARTx . ‘x’ indicates 0 and 1.

You can easily communicate with other devices via RS232 protocol (for example with PC, see the figure at the end of the topic – RS232 HW connection). You need ARM MCU with hardware integrated UART, for example LPC2138. Then, simply use the functions listed below. Library Routines

  • UART0_Ini t / UART1_Init
  • UART0_Ready / UART1_Ready
  • UART0_Read / UART1_Read
  • UART0_Write / UART1_Write
  • UART0_Text / UART1_Text
  • UART0_Read_Size / UART1_Read_Size
  • UART0_Read_CMD / UART1_Read_CMD

UART_Init

Prototype void UART_Init(const unsigned int baud_rate); Returns Nothing. Description Initializes hardware UART module with the desired baud rate. Refer to the device data sheet for baud rates allowed for specific Fosc. If you specify the unsupported baud rate, compiler will report an error. Requires You need ARM MCU with hardware UART. Usart_Init needs to be called before using other functions from UART Library.

Example

This will initialize hardware UART and establish the communication at 2400 bps: UART0_Init(2400);

UART_Ready

Prototype unsigned short UART_Ready(void); Returns Function returns 1 if data is ready or 0 if there is no data. Description Use the function to test if data in receive buffer is ready for reading. Requires UART HW module must be initialized and communication established before using this function. See UART_Init.

Example

If data is ready, read it: char receive; ... if (UART0_Ready()) receive = UART0_Read;

UART_Read

Prototype unsigned short UART_Read(void); Returns Returns the received byte. If byte is not received, returns Last Received Byte. Description Function receives a byte via UART. Use the function UART_Ready to test if data is ready first. Requires UART HW module must be initialized and communication established before using this function. See UART_Init.

Example

If data is ready, read it: char receive; ... if (UART0_Ready()) receive = UART0_Read();

UART_Write

Prototype void UART_Write(unsigned short data); Returns Nothing. Description Function transmits a byte (data) via UART. Requires UART HW module must be initialized and communication established before using this function. See UART_Init.

Example

char chunk = 0x1E; UART0_Write(chunk); /* send chunk via USART */

UART_Text

Prototype void UART_Text(unsigned char *data); Returns Nothing. Description Function transmits a string via UART. If null detected terminate the sending. Requires UART HW module must be initialized and communication established before using this function. See UART_Init. Example

char *text = “Hello Universe”; UART0_Text(text); /* send string via USART */

UART_Read_Size

Prototype char* UART_Read_Size (unsigned char *destination, unsigned short size ); Returns Input destination address. Description Function read string by size via UART. Automatically add null to end location Requires UART HW module must be initialized and communication established before using this function. See UART_Init. Example

char text[20]; UART0_Read_SIze(text,5); /* Receives 5charcter via USART */

UART_Read_CMD

Prototype char* UART_Read_CMD (unsigned char *destination, unsigned short CMD ); Returns Input destination address. Description Function read string by byte (char) command via UART. Automatically add null to end location Requires UART HW module must be initialized and communication established before using this function. See UART_Init. Example

char text[20]; UART0_Read_CMD(text,’?’); /* Receives character until ‘?’ via USART */

Example

include the UART.h for ARM

#define CCLK	60//System Frequency should mentioned for LCD header
#define PCLK 	30//Peripheral Frequency should mentioned for UART header 
#include <lpc214x.h>
#include <stdio.h> 
#include "UART.h"	  //LCD Header Files
char *name = "Hitech Solutions";
void delay(unsigned int n) ;
void Enable_PLL()
{
PLL0CFG &=~(0x7F);	//For clear 0t06
PLL0CFG |=0x04;		//External OSC 12MHZ so 60/12 =5-1=4
PLL0CFG |=0x01<<5;	//Multipler and divider setup
PLL0CON=0x01;	//Enable PLL
PLL0FEED=0xAA;	//Feed sequence
PLL0FEED=0x55;
while(!(PLL0STAT & 0x0400)); //is locked?
PLL0CON=0x03;	//Connect PLL after PLL is locked
PLL0FEED=0xAA;	//Feed sequence
PLL0FEED=0x55;
VPBDIV=0x02;	//peripheral bus runs 2 times slower
}
int main()
{  
char text[20];
Enable_PLL();
 UART0_Init(9600); //Init UART0 for 9600 baud 
 delay(100);
 UART1_Init(9600);	//Init UART1 for 9600 baud
 delay(100);
 UART0_Write('0');	 //Write character to UART0
 UART1_Write('1');	   //Write character to UART1
 UART0_Text(name);	   //Write string to UART0
 UART0_Write(0x0d);	   //Write line feed
 UART0_Write('\n');
 UART0_Text(">>");	   //Write String
 UART0_Read_Size(text,5);	//Read String by size
 UART1_Text(text);		 //	Write readed string to UART1 
 UART0_Text(">>");		//Write String
 UART0_Read_CMD(text,0x0d); //Read String by Command
 UART1_Text(text);		 //	Write readed string to UART1 
 do{
 if(UART0_Ready())
	UART1_Write(UART0_Read());
   }while(1);
 return(0);
}
void delay(unsigned int n) 
{   
 unsigned int i,j;   
 for(i=0;i<n;i++)    
   for(j=0;j<12000;j++);
}

HW Connection

/media/uploads/anandgembedd/lpc21xx_uart.bmp

__/media/uploads/anandgembedd/uart.h__ /media/uploads/anandgembedd/main.c


All wikipages