Thursday, May 17, 2012

LCD interfacing with Microcontrollers tutorial - 4-bit Mode



Introduction
Till now whatever we discussed in the previous part of ths LCD tutorial, we were dealing with 8-bit mode. Now we are going to learn how to use LCD in 4-bit mode. There are many reasons why sometime we prefer to use LCD in 4-bit mode instead of 8-bit. One basic reason is lesser number of pins are needed to interface LCD.

In 4-bit mode the data is sent in nibbles, first we send the higher nibble and then the lower nibble. 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. We call this special sequence as resetting the LCD. Following is the reset sequence of LCD.

  1. Wait for abour 20mS
  2. Send the first init value (0x30)
  3. Wait for about 10mS
  4. Send second init value (0x30)
  5. Wait for about 1mS
  6. Send third init value (0x30)
  7. Wait for 1mS
  8. Select bus width (0x30 - for 8-bit and 0x20 for 4-bit)
  9. Wait for 1mS

The busy flag will only be valid after the above reset sequence. Usually we do not use busy flag in 4-bit mode as we have to write code for reading two nibbles from the LCD. Instead we simply put a certain ammount of delay usually 300 to 600uS. This delay might vary depending on the LCD you are using, as you might have a different crystal frequency on which LCD controller is running. So it actually depends on the LCD module you are using. So if you feel any problem running the LCD, simply try to increase the delay. This usually works. For me about 400uS works perfect.

 Sending data/command in 4-bit Mode
 We will now look into the common steps to send data/command to LCD when working in 4-bit mode. As i already explained in 4-bit mode data is sent nibble by nibble, first we send higher nibble and then lower nibble. This means in both command and data sending function we need to saperate the higher 4-bits and lower 4-bits.


The common steps are:

  1. Mask lower 4-bits
  2. Send to the LCD port
  3. Send enable signal
  4. Mask higher 4-bits
  5. Send to LCD port
  6. Send enable signal

We are done with the theory part now, In the next section we will take a look at the programming microcontroller to control LCD in 4-bit mode.

4-bit Initialization
Initialization of LCD is completed only after the reset sequence and basic initialization commands. We have already discussed about the reset sequence of the lcd in the previous section. So lets look at the programming now...

//The pins used are same as explained earlier
#define lcd_port    P3

//LCD Registers addresses
#define LCD_EN      0x80
#define LCD_RS      0x20

void lcd_reset()
{
        lcd_port = 0xFF;
        delayms(20);
        lcd_port = 0x03+LCD_EN;
        lcd_port = 0x03;
        delayms(10);
        lcd_port = 0x03+LCD_EN;
        lcd_port = 0x03;
        delayms(1);
        lcd_port = 0x03+LCD_EN;
        lcd_port = 0x03;
        delayms(1);
        lcd_port = 0x02+LCD_EN;
        lcd_port = 0x02;
        delayms(1);
}

void lcd_init ()
{
        lcd_reset();         // Call LCD reset
        lcd_cmd(0x28);       // 4-bit mode - 2 line - 5x7 font.
        lcd_cmd(0x0C);       // Display no cursor - no blink.
        lcd_cmd(0x06);       // Automatic Increment - No Display shift.
        lcd_cmd(0x80);       // Address DDRAM with 0 offset 80h.
 }


Sending Dommand/Data to LCD in 4-bit mode 

void lcd_cmd (char cmd)
{
        lcd_port = ((cmd >> 4) & 0x0F)|LCD_EN;
        lcd_port = ((cmd >> 4) & 0x0F);

        lcd_port = (cmd & 0x0F)|LCD_EN;
        lcd_port = (cmd & 0x0F);

        delayus(200);
        delayus(200);
}

void lcd_data (unsigned char dat)
{
        lcd_port = (((dat >> 4) & 0x0F)|LCD_EN|LCD_RS);
        lcd_port = (((dat >> 4) & 0x0F)|LCD_RS);
     
        lcd_port = ((dat & 0x0F)|LCD_EN|LCD_RS);
        lcd_port = ((dat & 0x0F)|LCD_RS);

        delayus(200);
        delayus(200);
}
 
Download the example project which uses16x2 LCD and 4x3 keypad.


you may also want to visit the keypad tutorial.. click me

No comments:

Post a Comment

© All about engineering, AllRightsReserved.