Guides‎ > ‎

UART serial communication

posted Dec 10, 2010, 7:50 PM by Danny Ng   [ updated Feb 8, 2011, 6:44 PM ]
The gadget spec URL could not be found
One of the most basic communication between a PC and microcontroller system is through the Serial Port. Being easy to configure for communication made it a good choice for much of the projects. By setting the correct baud rate on the PIC and PC, Data can be transmitted in between 2 of the device. The Rx and Tx Pins of the EUASRT module are used for the communication. Line driver/receiver must be used to translate the different voltage level used in between the PIC and PC. In this demonstration a MAX 232 is used.  A simple circuit using pic18f2455 based on the schematic shown in the figure is used to test out the communication.
The first step for using UART is by setting the baud rate in the micro controller. It is down by loading the SPBRGH:SPBRG register with the correct value for the desired baud rate. Baud Rate Generator (BRG) will generate clock required to transmit and receive the data. The table below (obtain from 2455 datasheet page 247) show the formula for getting the boaud rate based on the SPBRGH:SPBRG value n. In the example a reload value of 16 is used. The baud generated with the config SYNC = 0, BRG16 = 1, BRGH = 1 will obtain baud rate of 117.647kbps, which is near the 115.2kpbs of the example.
The serial UART in the PIC must be enable by setting the TRIS register of the TX and RX pin to 1, SPEN (serial port enable) of RCSTA to 1, CREN (recieve enable) of RCSTA to 1 and  TXEN (transmit enable) of TXSTA to 1. With the baud rate set, transmitting data to can be done by loading the TXREG signify by 1 on the TXIF when the transmit buffer is empty. When data is received RCIF will be set and data can be read from the RCREG. Based on this some simple function are created for the transmit and receive data from the PC. UARTSendByte is use to send a byte while UARTReadByte is used to get 1 single byte. UARTSendLine will send all the data in an array of char.

void UARTSendByte(char data){
while(PIR1bits.TXIF == 0);
TXREG = data;

char UARTReadByte(void){
while(PIR1bits.RCIF == 0);
return RCREG;

void UARTSendLine(char *data){

Based on this a simple project (under attachment) is created to display character sent from pc to the PIC on the LCD. is sent to the LCD for display.

Danny Ng,
Dec 10, 2010, 8:42 PM