Thursday, 23 July 2015

UART Library for ARM LPC2148


UART Library

UART hardware module is available with a number of ARM. UART Library provides comfortable work with the Asynchronous (full duplex) mode.
 
The ARM7 LPC2148 Primer board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports in system programming (ISP) which is done through serial port.

NXP’s ARM7 (LPC2148), ARM Primer Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 32-bit Microcontrollers.

UART

UART (Universal Asynchronous Receiver Transmitter) are one of the basic interfaces which provide a cost effective simple and reliable communication between one controller to another controller or between a controller and PC.

RS-232 Level Converter

Usually all the digital ICs work on TTL or CMOS voltage levels which cannot be used to communicate over RS-232 protocol. So a voltage or level converter is needed which can convert TTL to RS232 and RS232 to TTL voltage levels. The most commonly used RS-232 level converter is MAX232.

This IC includes charge pump which can generate RS232 voltage levels (-10V and +10V) from 5V power supply. It also includes two receiver and two transmitters and is capable of full-duplex UART/USART communication.

  • RS-232 communication enables point-to-point data transfer. It is commonly used in data acquisition applications, for the transfer of data between the microcontroller and a PC.
  • The voltage levels of a microcontroller and PC are not directly compatible with those of RS-232, a level transition buffer such as MAX232 be used.

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_Data_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_Write(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_Write(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_Write(text,’?’);    /* Receives character until ‘?’ via USART */

Example
#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"   //UART 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

Download UART Header File                                                  Download Sourcecode for KEIL

This are all UART Functions in UART.h

No comments:

Post a Comment