Wednesday, April 2, 2014

INTERFACING OF LCD IN 4-BIT MODE USING MICROCONTROLLER


INTERFACING OF LCD IN 4-BIT MODE USING MICRO-CONTROLLER

In this method of Interfacing LCD to Microcontroller using 4-bit mode is to reduce the usage of pin.

Mostly the Circuit having multiple function uses this method.

In this method PORT0 of UC is connected to LCD as Follows:

P0.0 => rs;    Register select Pin
P0.1 => en;  Enable Pin
P0.4 => D4;  data line
P0.5 => D5;         “
P0.6 => D6;         “
P0.7 => D7;         “

In method we use same port for control pin as well as data line. So each data is send by breaking it into two 4bit part.

To initialize LCD in this method a command used is 0x28

Which means 16 x 2 LCD in 4-bit mode.

Before doing that we should send a sequence of 0x30, 0x30, and 0x20.

To send this we use only cmd_hf function which send only 3, 3, and 2 as sequence to LCD to enable in 4-Bit mode.

To send each data we should first clear the higher order bit i.e. from P0.4 – P0.7

And masking the input is properly shown in video.

  • We should mask Higher bit and rest all bit are clear.
  •  We send by latching.
  • We should now mask lower bit and send to UC.




This is how a 4-Bit mode LCD works.
Thank you for reading.
Plz Do share and Subscribe.

Source Code: click for code
For Videos on this Example: Click on the link








3 comments:

  1. char vState;
    short i=0, j, s;
    bit pbState, input;

    // LCD module connections
    sbit LCD_RS at RC0_bit ;
    sbit LCD_EN at RC1_bit ;
    sbit LCD_D4 at RC2_bit ;
    sbit LCD_D5 at RC5_bit ;
    sbit LCD_D6 at RC6_bit ;
    sbit LCD_D7 at RC7_bit ;

    sbit LCD_RS_Direction at TRISC0_bit ;
    sbit LCD_EN_Direction at TRISC1_bit ;
    sbit LCD_D4_Direction at TRISC2_bit ;
    sbit LCD_D5_Direction at TRISC5_bit ;
    sbit LCD_D6_Direction at TRISC6_bit ;
    sbit LCD_D7_Direction at TRISC7_bit ;
    // End LCD module connections

    voideeprom_rw(short pMode)
    {
    if(pMode == 0)
    {
    ` eeprom_write(j,vState);
    delay_ms(20);
    }
    if(pMode == 1)
    {
    `vState=eeprom_read(j);
    delay_ms(20);
    }
    }
    void motor(int pType) //driving the motor clockwise or counter-clockwise
    { j=0;
    while(j!=67)
    {
    // if TRUE Clockwise else Counter-Clockwise
    PORTB = (pType == 1) ?0b00110011 : 0b10011001;
    Delay_ms(50);
    PORTB = (pType == 1) ?0b01100110 : 0b11001100;
    Delay_ms(50);
    PORTB = (pType == 1) ?0b11001100 : 0b01100110;
    Delay_ms(50);
    PORTB = (pType == 1) ?0b10011001 : 0b00110011;
    Delay_ms(50);
    j++;
    }
    if (pType == 1)
    {
    Lcd_Out(1,2,"Tank is Close");
    vState = "Tank is Close";
    Delay_ms(50);
    }
    else
    {
    Lcd_Out(1,2,"Tank is Open");
    vState = "Tank is Open";
    Delay_ms(50);
    }
    }

    voidpush_button()
    {
    if (Button(&PORTA, 1, 1, 1)) // Detect logical one
    {
    pbState = 1; // Update flag
    i = 1;
    }
    else (pbState&& Button(&PORTA, 1, 1, 0)) // Detect one-to-zero transition
    {
    pbState = 0; // Update flag
    }
    }

    void buzzer()
    {
    if (input==1)
    PORTC3_bit = 0;
    else PORTC3_bit = 0;
    }

    voidinit_all()
    {
    CMCON = 0x07; // To turn off comparators
    ADCON1 = 0x06; // To turn off analog to digital converters
    TRISB = 0x00; //output
    PORTB = 0x00;
    TRISC = 0x00;
    PORTC = 0x00;
    TRISA1_bit = 1; //set A1 as input pin
    TRISA2_bit = 1;

    Lcd_Init();
    motor(1);
    delay_ms(20);
    }

    void main()
    {
    init_all();
    while(s==1)
    {
    input = PORTA2_bit;
    push_button();
    if (i==1)
    {
    if (pbState==1) //is the push button pressed?
    {
    motor(0);
    }
    else
    {
    motor(1);
    }
    }
    else
    {
    if (input==1)
    {
    motor(1);
    buzzer();
    if (pbState == 1)
    {
    input = 0;
    buzzer();
    }
    }
    }
    }
    }

    the errors are:
    0 1 mikroCPIC1618.exe -MSF -DBG -pP16F877A -DL -O11111114 -fo20 -N"C:\Users\acer\Desktop\LPG safety device\Papers\programs\MyProject199.mcppi" -SP"C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for PIC\defs\" -SP"C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for PIC\Uses\P16\" -SP"C:\Users\acer\Desktop\LPG safety device\Papers\programs\" "MyProject199.c" "__Lib_Math.mcl" "__Lib_MathDouble.mcl" "__Lib_System.mcl" "__Lib_Delays.mcl" "__Lib_Button.mcl" "__Lib_GlcdFonts.mcl" "__Lib_Glcd.mcl" "__Lib_LcdConsts.mcl" "__Lib_Lcd.mcl"
    0 1139 Available RAM: 352 [bytes], Available ROM: 8192 [bytes]
    0 122 Compilation Started MyProject199.c
    2 393 's' Identifier redefined MyProject199.c
    6 402 ; expected, but 'at' found MyProject199.c
    6 371 Specifier needed MyProject199.c
    6 396 Invalid declarator expected'(' or identifier MyProject199.c
    7 402 ; expected, but 'at' found MyProject199.c
    7 371 Specifier needed MyProject199.c
    7 396 Invalid declarator expected'(' or identifier MyProject199.c
    7 393 '' Identifier redefined MyProject199.c
    8 402 ; expected, but 'at' found MyProject199.c
    8 371 Specifier needed MyProject199.c
    8 396 Invalid declarator expected'(' or identifier MyProject199.c
    8 393 '' Identifier redefined MyProject199.c
    8 312 Internal error '' MyProject199.c
    0 102 Finished (with errors): 06 Apr 2015, 23:08:26 MyProject199.mcppi

    ReplyDelete
  2. I am having a problem in doingy project on sms based home automation..
    Can u help me..??
    Me me at iwasalive010@gmail.com

    ReplyDelete