Transmitting data from PC to LCD using UART of PIC16F628A

Anish July 17, 2012 12 Comments

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: 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.

And the experimental Setup:

 

Comments
  • Prathiksha
    April 30, 2015

    Sir I am doing a project to interface pic16f628a with 16*2 LCD display and RF module 434Mhz for wireless remote to control a robot equipped with camera.Below is the code in mikroC pro .I am not getting any output at output pins.Please help with this code.
    sbit LCD_RS at RA4_bit;
    sbit LCD_EN at RA7_bit;
    sbit LCD_D4 at RA0_bit;
    sbit LCD_D5 at RA1_bit;
    sbit LCD_D6 at RA2_bit;
    sbit LCD_D7 at RA3_bit;
    sbit LCD_RS_Direction at TRISA4_bit;
    sbit LCD_EN_Direction at TRISA7_bit;
    sbit LCD_D4_Direction at TRISA0_bit;
    sbit LCD_D5_Direction at TRISA1_bit;
    sbit LCD_D6_Direction at TRISA2_bit;
    sbit LCD_D7_Direction at TRISA3_bit;
    // End LCD module connection
    void main()
    { int sw;
    CMCON |= 7; // Disable Comparators
    INTCON=0;//ALL INTERRUPT DISABLED

    TRISA=0x00; //porta as output
    TRISB=0xfb; //portb rb2 as output,remaining input
    PORTB=0x00; //logic value of portb
    Lcd_Init();// Initialize LCD
    Delay_ms(100);//stabilize function
    Lcd_Out(1,1,”HI”);
    Delay_ms(1000);
    TXSTA=0x20;// transmit 8bit data in asynchronous low speed mode.
    UART1_Init(4800); //initialize uart
    Delay_ms(100);
    Lcd_Cmd(_LCD_CLEAR ); //clear lcd
    Delay_ms(1000);
    Lcd_Cmd(_LCD_BLINK_CURSOR_ON); //cursor off
    Delay_ms(1000);
    do
    {
    if(PORTB !=0x00) //if any switch is pressed
    {
    Delay_ms(100);//switch debounce
    if(PORTB!=0x00)
    {sw=PORTB;
    switch(sw)
    {
    case 0x01:Lcd_Out(1,1,”FORWARD”); //first switch for forward motion display on lcd
    break;
    case 0x02:Lcd_Out(1,1,”BACKWARD”);
    break;
    case 0x80:Lcd_Out(1,1,”LEFT”);
    break;
    case 0x08:Lcd_Out(1,1,”RIGHT”);
    break;
    case 0x10:Lcd_Out(1,1,”CAM LEFT”);
    break;
    case 0x20:Lcd_Out(1,1,”CAM RIGHT”);
    break;
    default:break;
    }
    UART1_Write(sw); //transmit sw at uart pin
    Delay_ms(1000);
    }
    }
    Lcd_Cmd(_LCD_CLEAR);
    }
    while(1);
    }

  • moj
    February 22, 2015

    i want to display text in c# textbox “CLEARED” to lcd via pic18f452. i am using mikroC. For serial transmission i am using a cp2120 usb-ttl module. i am simulating with proteus. Can you please correct this code i have written and explain why it is behaving…

    code for “send” button in c# is :
    private void button1_Click(object sender, EventArgs e)
    {
    string text = txt_decision.Text;
    serialPorr1.WriteLine(text);
    }

    code for PIC in mikroC is :
    //LCD INITIALISATIONS//
    char text;
    void main(){
    uart1_Init(9600);
    delay_ms(100);
    Lcd_Init();
    delay_ms(100);
    Lcd_Cmd(_lcd_clear);
    while(1){
    if (UART1_Data_Ready() == 1){
    UART1_Read_Text(text, “D”, 6);
    Lcd_Out(1,3,text);
    delay_ms(2000);
    }
    }
    }

    When i click SEND the Lcd displays EEARE then i click it again it shows RLEAR, if i click again it goes back to show EEARE….What seems to be the problem in my codes ???

  • December 25, 2013

    I got a slight problem with usb to ttl converter. The PIC doesn’t properly receive the text I send using the PC. This only happens when I use the module on my laptop. when i connect it to a desktop, everything works fine. I’m really confused now. Please help me to figure this out.

    • Anish
      December 25, 2013

      Dilupa,
      If the converter is working if plugged on to your desktop and not in laptop, probably, its a driver problem. Download and install the latest driver for the module. If you use a linux based system, it may also have permission issues. Try to run the terminal as a root user.

  • June 14, 2013

    thank you sir for this interresting project i was looking to find something about how to communicate a led matrix with microcontroller to display message and change it when i want with my computer can you help on that sir??
    all regards

  • Vito
    July 26, 2012

    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.

    • Anish
      July 26, 2012

      @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

      • Vito
        July 27, 2012

        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?

        • Anish
          July 28, 2012

          Hi Vito,
          Yes, go for it. It WILL work 🙂

          • Vito
            August 2, 2012

            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. 🙂

          • Vito
            September 7, 2012

            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. 🙂

  • July 18, 2012

    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.

Leave a Reply to Electronics Projects Cancel reply

Your email address will not be published. Required fields are marked *