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:
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: www.mikroe.com. 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
Delay_ms(100);
UART1_Init(1200); //Initialize UART module
Delay_ms(200);
Lcd_Cmd(_LCD_CLEAR); //Clear Display
Lcd_Cmd(_LCD_UNDERLINE_ON); //Underline fashion cursor
Lcd_Out(1,2,"LCD/UART TEST");
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
//Lcd_Cmd(_LCD_CLEAR);
while(1){
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
Lcd_Cmd(_LCD_CLEAR);
col=1,row=1;
}
UART1_Write(i); //Echo back in UART
col=col+1;
}
if(col==17 && row==1){ //On end of row, goto second row
row=2;
col=1;
}
if(row==2 && col==17){ //On end of display, clear display
Lcd_Cmd(_LCD_CLEAR);
col=1;
row=1;
}
}
}
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.
You may also like:






I would like to know where you can buy the USB-serial adapter (which is shown in second image), and if once connected to the computer after you create a virtual COM port, you can use any serial programmer and if it goes well for any project where it is requested the serial (not just the usual pin 2, 3 and 5).
It uses an integrated FTDI FT232 or another model?
Thank you.
@Vito,
Hi, the USB to TTL bridge used here is based on a converter IC from Silicon Labs (CP2102) and not from FTDI. You can purchase it from here:
http://probots.co.in/index.php?main_page=product_info&cPath=67_88&products_id=411
Thank you for having responded to me.
Unfortunately, the adapter used in this guide, you can not order it in Italy (for the moment, is sold only in India).
Alternatively, I found a similar adapter on ebay, do you think this adapter (www.ebay.it/itm/FT232RL-USB2-0-to-TTL-Level-5V-3-3V-Serial-port-Module-dupont-line-/170881426651?pt=LH_DefaultDomain_0&hash=item27c953a4db) is a valid and functional alternative to the one used in this guide?
Hi Vito,
Yes, go for it. It WILL work
Thanks for reply.
I just bought the USB-Serial ( http://www.ebay.it/itm/170881426651?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649 ), we hope that within a month of waiting, and that arrivals works well with any programmer, schematic, and serial device.
Hello and thank you.
Hello, arrived this morning.
The adapter in question (170881426651), very small, about 4.5 x3cm
I know how it works, when I can.
Thank you all.
PIC micro controller has good Advantages .It has small length instructions and most of them are single cycle executable .PIC has High Execution speed. Its really very good Post.