Spread the love

lcd

____________________________________________________________________________________________________

Introduction

I think there is no more need to introduce how to interface LCD with microcontroller because we earlier learnt it. So in this module we learn how to interface LCD with micrcontroller in 4-bit mode rather than 8-bit conventional mode.

Benefit of LCD interfacing in  4-bit

There are limited numbers of GPIO (general purpose input outputs) pins in micrcontrollers. When we design the large embedded system with single micrcontroller, there is need of efficient use of GPIOs. And as a good programmer, it is nice practice to use GPIO pins efficiently. So interfacing LCD with microcontroller by using 4-bit mode, we save 4 GPIO pins.

 

Concept of 4-bit mode

Till now whatever we discussed in the previous part of ths LCD module, we were dealing with 8-bit mode. Now we are going to learn how to use LCD in 4-bit mode.

                        In 4-bit mode, the data is sent in nibbles(1 nibble= 4 bit)  form, first we send the higher nibble and then the lower nibble with same RS, RW and EN pin fuctioning as we were doing in 8-bit mode. To enable the 4-bit mode of LCD, we need to follow special sequence of initialization that tells the LCD controller that user has selected 4-bit mode of operation.

 

Connection description:

Connections of LCD with microcontroller are shown in circuit diagram of next tab. In 4-bit mode, the Data lines must be connected with D4, D5, D6 and D7 pins of LCD module and RS, RW & EN pin functioning same as it was in 8-bit mode.

 Configuring LCD to 4-bit mode:

The LCD can be configured in 4-bit mode by sending appropriate command which is called “Function set” to it. The Function set is hexadecimal code for LCD’s control unit, which selects working modes of LCD. The “Function Set” is mentioned below:

Untitled

DL – Data Length, DL = 1 for 8-bit mode, and  DL = 0 for 4-bit mode

N  –   No. of Lines N = 1 for 2 Lines selection , and N = 0 for  1Lines selection

F  –   Fonts F = 1 for 5×10 dots, and F = 0 for 5×7 dots

                         According to Function Set, the value of 4–bit mode will be 0010 0000(0x20) because DL=0. The value of “Function Set” for the LCD configuration 2 line (N=1), 5X7 dots (F=0) and 4-bit (DL=0) mode will be 0010 1000(0x28).

                        When the power supply is given to LCD, it remains in 8-bit mode(default). Now, if 0x20 is sent, lower nibble will not be received by LCD because four data lines (D4-D7) are connected, So 0x02 is sent instead of 0x20.

Steps of Programming

 

Step1: Initializing LCD in 4-bit mode.

void lcd_ini( )
{
cmd(0X02);         // to initialize LCD in 4-bit mode.

cmd(0X28);         //to initialize LCD in 2 lines, 5X7 dots and 4bit mode

cmd(0x0E);         //display ON, cursor blinking

cmd(0X06);         //shift cursor to right on writing

cmd(0X01);         //clear display screen

cmd(0X80);         //force cursor to starting position of first line
}

Step2: Nibble sending.

 

For example, If lower 4 pins of any 8-bit port are connected to LCD’s data line. So, the lower nibble of a byte can be sent to LCD data lines by masking higher nibble. To send higher nibble, data byte is shifted right for four places. higher nibble replaces lower nibble by this shifting. Data is sent after masking the byte.

 

Example:    if you want to send 0x28, then

void dis_cmd(char cmd_value)  // if we want to send 0x28
 {  
      // at this position cmd_value=0x28
      char cmd_value1;  
      cmd_value1 = cmd_value & 0xF0;          //mask lower nibble  

// at this position cmd_value1=0x20
                           
      LCD_cmd(cmd_value1);               // only higher nibble is send to LCD  
      cmd_value1 = ((cmd_value<<4) & 0xF0);     // left shift 4-bit to cmd_value& anding with 0x40
// at this position cmd_value1=0x80   
                                      
      LCD_cmd(cmd_value1);               // only higher nibble is send to LCD 

//  at this position 0x28 received by LCD
 }

void LCD_cmd(unsigned char cmd)
{
      ctrl=cmd;        // load PA4-PA7 pins
      ctrl&=~(1<<rs);  // clear rs  
      ctrl&=~(1<<rw);  // clear rw
      ctrl|=(1<<en);   // set en
      _delay_ms(1);    // halt for some time 
      ctrl&=~(1<<en);  // clear en
          _delay_ms(40);  // halt for some time 
          return;
}

Step 3: Exactly the same way we send the data to LCD and function of RS, RW and EN is same as we were doing.

1. Circuit diagram of 4 bit mode LCD interfacing with AVR (ATmega16)

4 bit lcd interfacing with avr (ATmega16)

1. Program of  4 bit mode LCD interfacing with AVR (ATmega16)

/******************************************************
www.firmcodes.com
DEVELOPED BY:- FIRMWARE DEVELOPER
WHAT PROGRAM DO:- 4 bit mode LCD interfacing with AVR (ATmega16)
******************************************************/

#include <avr/io.h>
#include <util/delay.h>
              
#define ctrl PORTD
#define en 2                         // enable signal
#define rw 1                       // read/write signal
#define rs 0                     // register select signal

void LCD_cmd(unsigned char cmd);
void lcd_ini(void);
void LCD_write(unsigned char data);
void dis_cmd(char);  
void dis_data(char); 
void LCD_write_string(unsigned char *str);
 
void main()
{
DDRD=0xFF;                                  // setting the port c                                                                       // setting for port D
                            
lcd_ini();                                 // initialization of LCD
_delay_ms(30);                        // delay of 50 mili seconds
LCD_write_string("FIRMCODES.COM");         // function to print string on LCD
while(1);
}

void lcd_ini(void)
{
dis_cmd(0x02); // to initialize LCD in 4-bit mode.
_delay_ms(1);
dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
_delay_ms(1);
dis_cmd(0x01);                                 // clear LCD
_delay_ms(1);
dis_cmd(0x0E);                        // cursor ON
_delay_ms(1);
dis_cmd(0x80);                     // —8 go to first line and –0 is for 0th position
_delay_ms(1);
return;
}

 void dis_cmd(char cmd_value)  
 {  
      char cmd_value1;  
      cmd_value1 = cmd_value & 0xF0;          //mask lower nibble  
                          //because PA4-PA7 pins are used.   
      LCD_cmd(cmd_value1);               // send to LCD  
      cmd_value1 = ((cmd_value<<4) & 0xF0);     //shift 4-bit and   
                                    //mask  
      LCD_cmd(cmd_value1);               // send to LCD  
 }                                
 void dis_data(char data_value)  
 {  
      char data_value1;  
      data_value1=data_value & 0xF0;  
      LCD_write(data_value1);  
      data_value1=((data_value<<4) & 0xF0);  
      LCD_write(data_value1);  
 }  
                                                                                                     
void LCD_cmd(unsigned char cmd)
{
      ctrl=cmd;
      ctrl&=~(1<<rs);  
      ctrl&=~(1<<rw);  
      ctrl|=(1<<en);  
      _delay_ms(1);  
      ctrl&=~(1<<en);
          _delay_ms(40);  
          return;
}

void LCD_write(unsigned char data)
{
   ctrl= data;
   ctrl|=(1<<rs);  
   ctrl&=~(1<<rw);     
   ctrl|=(1<<en);  
   _delay_ms(1);  
   ctrl&=~(1<<en); 
   _delay_ms(40);
return ;
}

void LCD_write_string(unsigned char *str)             //store address value of the string in pointer *str
{
int i=0;
while(str[i]!='\0')                               // loop will go on till the NULL character in the string
{
dis_data(str[i]);                            // sending data on LCD byte by byte
i++;
}
return;
}

PROTEUS File for SIMULATION(Password Of RAR file is :-firmcodes.com)

download (1)

coming soon………….