LCD interfacing 8051 Microcontrollers 4-bit

August 16th, 2009 by admin No comments »

Interfacing Standard Alphanumeric LCD using 4 bit mode in 8051 MCU

flag

In this article i will explain how to interface a standard 16×2 or 20×4 alphanumeric LCD to 8051 microcontrollers using 4 bit data mode. “LCD.h” file is available for free download from here.

LCD is the most common display device used by the designer for testing purpose and for displaying real time or stored data. It is very easy to drive a LCD module using a microcontroller.

Table below shows the pin details of LCD (16 pins)

lcd pins electroons.com

Circuit Diagram shows Connection of LCD to any PORT of Microcontroller.

8051lcd

While using LCD in 4 bit data mode it saves 4 bits of our total GPIO lines, that’s why it is most commonly used. MSB of any data or commnad is sent first over 4 bits and then 4 LSB sent by shifting the data byte 4 times left.

Below is the code which is compiled usiong Keil uVision3 Compiler.

/****************************************************************
File name : LCD.H for driving LCD in 4bit mode
Auther: Anupam Dubey (dubey.anupam@gmail.com)
*****************************************************************/

#define LCDPORT P2
#define RS P2_0
#define RW P2_1
#define E  P2_2
bit  status=0
#define lcd_delay 60
void delay(unsigned int j)
{
unsigned int i,k;
for(i=0;i<j;i++)
{
for(k=0;k<100;k++);
}
}
void _lcd_init_write(unsigned char a)
{
RS = 0;
RW = 0;
LCDPORT=a;
E=1;
delay(lcd_delay);
E=0;
}
void lcd_com(unsigned char a){
unsigned char temp;
if(status){
status=0;
 goto a;
 }
RS=0;
a:
RW=0;
temp=a;
temp&=0xF0;
LCDPORT&=0x0F;
LCDPORT|=temp;
E=1;
delay(lcd_delay);
E=0;
temp=a<<4;
temp&=0xF0;
LCDPORT&=0x0F;
LCDPORT|=temp;
E=1;
delay(lcd_delay);
E=0;
}
void lcd_data(unsigned char a){
status=1;
RS=1;
lcd_com(a);
}
void lcd_init(void){
delay(lcd_delay);
_lcd_init_write(0x30);
delay(lcd_delay);
_lcd_init_write(0x30);
delay(lcd_delay);
_lcd_init_write(0x30);
delay(lcd_delay);
_lcd_init_write(0x20);
delay(lcd_delay);
lcd_com(0x28);
delay(lcd_delay);
lcd_com(4);
delay(lcd_delay);
lcd_com(0x85);
delay(lcd_delay);
lcd_com(6);
delay(lcd_delay);
lcd_com(1);
delay(lcd_delay);
}
void lcd_puts(char *aaa)
{
unsigned int i=0;
for(;aaa[i]!=0;i++)lcd_data(aaa[i]);
}

#define LCDPORT P2
#define RS P2_0
#define RW P2_1
#define E P2_2
bit status=0;
#define lcd_delay 60
void delay(unsigned int j)
{
unsigned int i,k;
for(i=0;i<j;i++)
{
for(k=0;k<100;k++);
}
}
void _lcd_init_write(unsigned char a)
{
RS = 0;
RW = 0;
LCDPORT=a;
E=1;
delay(lcd_delay);
E=0;
}
void lcd_com(unsigned char a){
unsigned char temp;
if(status){
status=0;
goto a;
}
RS=0;
a:
RW=0;
temp=a;
temp&=0xF0;
LCDPORT&=0x0F;
LCDPORT|=temp;
E=1;
delay(lcd_delay);
E=0;
temp=a<<4;
temp&=0xF0;
LCDPORT&=0x0F;
LCDPORT|=temp;
E=1;
delay(lcd_delay);
E=0;
}
void lcd_data(unsigned char a){
status=1;
RS=1;
lcd_com(a);
}
void lcd_init(void){
delay(lcd_delay);
_lcd_init_write(0x30);
delay(lcd_delay);
_lcd_init_write(0x30);
delay(lcd_delay);
_lcd_init_write(0x30);
delay(lcd_delay);
_lcd_init_write(0x20);
delay(lcd_delay);
lcd_com(0x28);
delay(lcd_delay);
lcd_com(4);
delay(lcd_delay);
lcd_com(0x85);
delay(lcd_delay);
lcd_com(6);
delay(lcd_delay);
lcd_com(1);
delay(lcd_delay);
}
void lcd_puts(char *aaa)
{
unsigned int i=0;
for(;aaa[i]!=0;i++)lcd_data(aaa[i]);
}

Download lcd.h Here

Hello World Program 8051

August 11th, 2009 by admin No comments »

LED blinking 8051 Family

Now let us do Hello World program for 8051 family of micro controllers. We know that in embedded world , Hello World program is LED blinking.

This program is for blinking LEDs connected to some port pin in a 8051 pin compatible package.

For doing experiment with 8051 family u need a ‘C’ cross compiler that can generate machine code for 8051 architecture. Two most popular compilers are Keil uVision & SDCC (Small device C compiler). SDCC is freeware while evaluation version of Keil uVision is free with simulation code size of limited 2K.

Schematic for which we are going to write our program

8051 LED blinkingLEDs are connected in sinking mode i.e. a logic zero at corresponding pin will lite up that LED and a logic 1 will make LED lit off. It is due fact that 8051 family controllers cant provide that much source current that can drive a LED.

As shown in circuit LEDs are connected to lower nibble of PORT 1.

Here is the program…

#include<at89x52.h>  // standard header file for SFR/Macro definitions of AT89S52
void delay(void)   // delay function to generate time delay between two events
{
unsigned int i,j;
for(i=0;i<120;i++)
{
for(j=0;j<1300;j++);
}
}
void main(void)
{
P1=0x0F;  // initially all LEDs are Off
while(1==1)  // infinite loop to do a particular task infinitely
{
P1=0x00;  // LEDs On
delay();
P1=0x0F;    // LEDs Off       0x0Fh=0b00001111
delay();
}
}