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.
Wait for abour 20mS
Send the first init value (0x30)
Wait for about 10mS
Send second init value (0x30)
Wait for about 1mS
Send third init value (0x30)
Wait for 1mS
Select bus width (0x30 - for 8-bit and 0x20 for 4-bit)
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.
LCD connections in 4-bit Mode
Above is the connection diagram of LCD in 4-bit mode, where we only need 6 pins to interface an LCD. D4-D7 are the data pins connection and Enable and Register select are for LCD control pins. We are not using Read/Write (RW) Pin of the LCD, as we are only writing on the LCD so we have made it grounded permanently. If you want to use it.. then you may connect it on your controller but that will only increase another pin and does not make any big difference. Potentiometer RV1 is used to control the LCD contrast. The unwanted data pins of LCD i.e. D0-D3 are connected to ground.
LCD connections 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:
Mask lower 4-bits
Send to the LCD port
Send enable signal
Mask higher 4-bits
Send to LCD port
Send enable signal
In the previous tutorials we were dealing with a common program from this tutorial we are going to introduce the concept of library. writing a library make the job of programmer easy as well as program looks neat and tidy. and easy to understand.
Library is written using two files header file(.h extension) and c file(.c extension file)
header file includes the function declaration whereas the c file includes the functions the concepts will be more clear by looking in program
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.
Wait for abour 20mS
Send the first init value (0x30)
Wait for about 10mS
Send second init value (0x30)
Wait for about 1mS
Send third init value (0x30)
Wait for 1mS
Select bus width (0x30 - for 8-bit and 0x20 for 4-bit)
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.
LCD connections in 4-bit Mode
Above is the connection diagram of LCD in 4-bit mode, where we only need 6 pins to interface an LCD. D4-D7 are the data pins connection and Enable and Register select are for LCD control pins. We are not using Read/Write (RW) Pin of the LCD, as we are only writing on the LCD so we have made it grounded permanently. If you want to use it.. then you may connect it on your controller but that will only increase another pin and does not make any big difference. Potentiometer RV1 is used to control the LCD contrast. The unwanted data pins of LCD i.e. D0-D3 are connected to ground.
LCD connections 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:
Mask lower 4-bits
Send to the LCD port
Send enable signal
Mask higher 4-bits
Send to LCD port
Send enable signal
In the previous tutorials we were dealing with a common program from this tutorial we are going to introduce the concept of library. writing a library make the job of programmer easy as well as program looks neat and tidy. and easy to understand.
Library is written using two files header file(.h extension) and c file(.c extension file)
header file includes the function declaration whereas the c file includes the functions the concepts will be more clear by looking in program
Circuit Diagram
CODE
LCD HEADER H- File
#ifndef __lcd_H__
#define __lcd_H__
void delay_ms(unsigned char ms);
void lcd_en();
void lcd_cmd(unsigned char command);
void lcd_putchar(unsigned char ascii);
void lcd_putstring(unsigned char *lcd_string);
void lcd_init();
#endif
LCD Library C file
#include <lcd.h>
#include <reg51.h>
sbit rs = P2^4;
sbit en = P2^5;
#define LCD_clear() lcd_cmd(0x1) /* Clear display LCD */
#define LCD_origin() lcd_cmd(0x2) /* Set to origin LCD */
#define LCD_row1() lcd_cmd(0x80) /* Begin at Line 1 */
#define LCD_row2() lcd_cmd(0xC0) /* Begin at Line 2 */
void delay_ms(unsigned char ms)
{
unsigned char n;
unsigned int i;
for (n=0; n<ms; n++)
{
for (i=0; i<1275; i++); /* For 1 ms */
}
}
void lcd_en()
{
en = 0; /* Clear bit P2.4 */
delay_ms(1);
en = 1; /* Set bit P2.4 */
}
void lcd_cmd(unsigned char command)
{
rs = 0; /* Clear bit P2.5 */
P2 = (P2 & 0xF0)|((command>>4) & 0x0F);
lcd_en();
P2 = (P2 & 0xF0)|(command & 0x0F);
lcd_en();
delay_ms(1);
}
void lcd_putchar(unsigned char ascii)
{
rs = 1; /* Set bit P2.5 */
P2 = (P2 & 0xF0)|((ascii>>4) & 0x0F);
lcd_en();
P2 = (P2 & 0xF0)|(ascii & 0x0F);
lcd_en();
delay_ms(1);
}
void lcd_putstring(unsigned char *lcd_string)
{
while (*lcd_string)
{
lcd_putchar(*lcd_string++);
}
}
void lcd_init()
{
en = 1; /* Set bit P2.4 */
rs = 0; /* Clear bit P2.5 */
lcd_cmd(0x33);
lcd_cmd(0x32);
lcd_cmd(0x28);
lcd_cmd(0x0C);
lcd_cmd(0x06);
lcd_cmd(0x01); /* Clear */
delay_ms(256);
}
Main Code
#include <reg51.h>
#include <lcd.h>
void main(void)
{
lcd_init();
while(1)
{
lcd_cmd(0x80);
lcd_putstring(" ProjectBandya ");
lcd_cmd(0xc0);
lcd_putstring("LCD 4 Bit");
}
}
Output
Thankyou Stay Connected:)
No comments:
Post a Comment