Monday, August 29, 2022

ARM7 LPC 2148 Embedded C program to interface PC (UART)

 



Circuit Diagram




UART – Universal Asynchronous Receiver Transmitter (UART0 and UART1)

UART0 has lines TXD, RXD and Gnd lines for interfacing simple serial port.

UART1 has lines apart from the above, control lines for interfacing Modem also. Now, we are going to see only the registers available in UART0

UART0 is used to send the byte of data serially and receive the byte of the data serially.

At the time of receiving, it receives the bits seri ally and assembles as parallel 8 bit data and it places in the receiver buffer register U0RBR. It is the content of the top of the FIFO buffer registers. At the time of transmitting, the user is supposed to place the 8 bit data in Transmit Hold Register Namely U0THR.

In serial communication, the serial data is sent in the same rate at which the receiving system also receives. This is called baud rate (bits per second). For example, 9600 baud means 9600 bits per second. The internal clock is divided by 16 bit no. to arrive at the required baud rate.

The Most Significant 8 bits of the divider value are stored is called U0DLM. The Least Significant 8 bits of

the divider value are stored in the register called U0DLL. At the time of writing in these registers only, DLAB bit in the U0LCR register (Line Control Register) is to be made 1. Whenever, the U0THR register is empty, the next byte of data can be sent to send it out serially. Similarly, whenever, a byte of data is received, it is placed in U0RBR. As soon as it happen, interrupt may be raised to inform the user. But interrupts are to be enabled before the use. 

There is an interrupt enable register namely U0IER. By writing proper bits in the register, the above events will raise the interrupt. There is another register U0IIR, which is used to find what are all the interrupts pending. The register FIFO control register U0FCR is used to reset TX FIFO (Transmit First In First Out register set) and RX FIFO (Receive First In First Out) and to enable them.

The register U0LCR, line control register is used to write serial protocol parameters namely, the word length of the data, no. of stop bits, parity enable, parity select, Break control. The 7th bit in this register namely DLAB is used to enter the 16 bit divisor data. Line Status register U0LSR is the status register to know whether data has been received, data can be sent for transmission, to find out errors. There is another register U0TER which is used to enable the serial transmitter


PROGRAMLOGIC:

* Configure UART of ARM7 in 9600 baud rate.

* Program loop is created to send given string serially through transmitter pin of UART.

* Hyper terminal of PC is configured in 9600 baud rate in COM port 1

* Data transmitted through UART is received by receiver pin of COM port1 of PC.

* Received string is displayed in hyper terminal


PROGRAM CODE

#include <LPC214x.H>
#define PCLK 30000000 // PCLK for configuration baudrate
void UART0_Init(unsigned int baudrate);
void UART0_PutC(char c);
void UART0_PutS(char *p);
unsigned int getchar (void);
int main(void)
{
VPBDIV = 0x02; //Divide Pclk by two
UART0_Init(9600);
while(1)
{
UART0_PutS("\f*** TRENDY CODING ***\n\n\r");
}
}
unsigned int getchar (void) /* Read character from Serial Port */
{
while (!(U0LSR & 0x01));
return (U0RBR);
}
void UART0_Init(unsigned int baudrate)
{
unsigned short BAUD_REG;
BAUD_REG = PCLK/(16*baudrate); // Calculate for U0DL value
PINSEL0|= 0x00000005; // Enable rx,tx
U0LCR = 0x00000083; // 8 bit data,1 stop bit,no parity bit
U0DLL = BAUD_REG & 0xFF; // U0DL for low by te
U0DLM = (BAUD_REG>>8); // U0DL for high byte
U0LCR = 0x00000003; // DLAB =0
}
void UART0_PutC(char c)
{
while(!(U0LSR & 0x20)); // Wait until UART0 ready to send character
U0THR = c; // Send character
}
void UART0_PutS(char *p)
{
while(*p) // Point to character
{
UART0_PutC(*p++); // Send character then point to next character
}
}

No comments:

Post a Comment

GPS sensor interface with ESP8266 using Blynk IoT cloud

   Circuit diagram: Source Code: #include <TinyGPS++.h> #include <SoftwareSerial.h> #define BLYNK_PRINT Serial #include <ESP8...