Transmitting data from PC to LCD using UART of PIC16F628A

Our world relies upon Communication. In today’s world, we can make almost anything communicate with everything! How about communication between a PC and a microcontroller (PIC)?

Now, before making a PC and a PIC talk, if you are not sure about USART module and LCD, here are some good articles about them to just refresh:

1. PIC USART module – CT

2. A note on character LCD displays – CT

In layman terms, a USART module in a PIC is like our voice box. Without the USART module, the controller will be in a mute state.What actually we are gonna do here is, When you type in your terminal program (on your PC), the PIC receives it and displays it in a 16×2 LCD. Also, it echos back the typed character to the PC.

PIC16F628A has one USART module, which is configured in asynchronous (full duplex) mode. PORT B pins RB1 and RB2 acts as RX and TX pins respectively. The LCD is operated in 4-Bit mode, PC and PIC are bridged via a SiLabs CP210x based USB to UART module (You can replace this with a MAX232 TTL to RS232 and interface via the DB9 connector)

The PIC:

Controller used here is PIC16F628A. It has two ports, A and B. Since LCD is interfaced in 4-bit mode, only 6 pins of the MCU is used by the LCD (4 data pins + Register select pin + Enable pin). Port B is sufficient for the LCD and UART operation. RB1 and RB2 are configured as UART RX and TX respectively, RB0 and RB3 as Register select and enable for LCD respectively and RB4-RB7 as 4-bit data for LCD. Internal oscillator with no clock out is used at 4MHz (Refer the compiler)

The LCD:

A 16×2 LCD(16-pin) module based on Hitachi HD44780 controller is used. Pins 4(RS), 6(E), 11-14(DB4-DB7) are interfaced with the MCU, pin 5(Read/Write) is grounded since no read operation is performed. Connecting 15th and 16th pins are optional (These are Back-Light LED pins)

The USB to TTL bridge:

Connects the PIC UART with the USB port of the PC. Also, we can draw power(5V and 3.3V) from this module. Alternatively, a MAX232 RS232 to TTL level shifter IC can be used to bridge UART with PC serial port.

The Compiler:

The compiler used is MikroC pro for PIC from Mikroelectronika. I chose this compiler because it has large amounts of libraries. We can easily configure UART and LCD without pulling out your hair trying assembly. The IDE also looks clean and there is a clear help tutorial to get started on the mikroe website: Here is the screen shot of configuring and using the internal oscillator. You can access this under Project–>Edit Project

The Code:

// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

char i;
int row=1,col=1;

void main(){

Lcd_Init(); //Initialize LCD
UART1_Init(1200); //Initialize UART module

Lcd_Cmd(_LCD_CLEAR); //Clear Display
Lcd_Cmd(_LCD_UNDERLINE_ON); //Underline fashion cursor
Lcd_Out(1,2,"LCD/UART TEST");


if(UART1_Data_Ready()==1){ //If Data Ready,
i=UART1_Read(); //Read it and store in variable i
Lcd_Chr(row,col,i); //Print it in co−ordinates specified
if(i==27){ //If ESC is pressed, clear display
UART1_Write(i); //Echo back in UART
if(col==17 && row==1){ //On end of row, goto second row
if(row==2 && col==17){ //On end of display, clear display

Circuit Diagram:

Some Pictures of the working setup and the terminal program:

MikroC also houses a USART terminal program and it can be accessed through Tools–>USART Terminal. You can also use the windows hyperterminal.
Baud rate and COM port selection can be made in the USART terminal.

And the experimental Setup: