Thursday, April 30, 2015

89C51 Interfacing with 16x2 LCD in 4-bit mode


What it will Do?
It is simple program to display small message on LCD using only 6 IO lines of 89C51
For More Details Click Here (LCD Post)

Steps :
1. Define your connections of RS,E, D4,D5,D6,D7 (in 4-bit mode LCD higher bits are used)
2. Adjust delay if required
3. LCD pin 5 (R/W) must be connected to Ground
4. Connect 1K Ohm Resistor between pin 1 and 3 of LCD for contrast.


Code :


/* LCD Module Software                      */
/* 2nd Dec 2005                             */
/* Copyright 2005 Circuits4You.com          */ 
/* WWW - http://www.circuits4you.com        */
/* Email - info@circuits4you.com            */
 
/* LCD Pin-5(R/W) must be connected to ground*/

#include <at89x51.h>
#include <string.h>
#include <stdio.h> 
#define EN P0_3  //LCD Pin 6
#define RS P0_2  //LCD Pin 4

#define lcd_D0 P0_4  //LCD D4 Pin 11
#define lcd_D1 P0_5  //LCD D5 Pin 12
#define lcd_D2 P0_6  //LCD D6 Pin 13
#define lcd_D3 P0_7  //LCD D7 Pin 14

void delay(int);
void time1ms();
void lcdbyte(char);
void displaybyte(char);
void main() 
{ 
     char string[] = {"Testing 1,2,3 " "It' Works ! "}; 
     char init[]={0x03,0x03,0x03,0x02,0x28,0x01,0x0E,0x06,0x02};               // 4-bit interface

     int count; int len; 

      for (count = 0; count <= 8; count++) 
       { 
            RS=0;   //Command Mode 
            lcdbyte(init[count]); 
       } 

           len = strlen(string); 
     for (count = 0; count < len; count++) 
      { 
          RS=1;   //Data Mode
          lcdbyte(string[count]);
      } 
}


void delay(int n)      /* do nothing n*1ms */
{
    int i;
    for (i=0; i< n ; i++)
    time1ms();
}

void time1ms()    /* 1 ms delay with XTAL 11.0592MHz */
{
    int i;
    for (i = 0; i<50; i++);  // the value shown in this line, 50 was calibrated for 1ms
// you may change it!
}

void lcdbyte(char lcd_a)
{
       RS=1;       

       lcd_D0=(lcd_a & 0x01);
       lcd_D1=(lcd_a & 0x02);
       lcd_D2=(lcd_a & 0x04);
       lcd_D3=(lcd_a & 0x08);

          EN=1;        /* Set Enable                  */ 
          delay(20); 
          EN=0;       /* Reset Enable               */ 
          delay(20);

       lcd_D0=(lcd_a & 0x10);
       lcd_D1=(lcd_a & 0x20);
       lcd_D2=(lcd_a & 0x40);
       lcd_D3=(lcd_a & 0x80);

          EN=1;        /* Set Enable                  */ 
          delay(20); 
          EN=0;       /* Reset Enable               */ 
          delay(20);
}



No comments:

Post a Comment