Thursday, April 30, 2015

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


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

Steps :
1. Define your connections of RS,E, D0 to D7.
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                   */ 
/* WWW - http://www.circuits4you.com             */
/* Email - info@circuits4you.com                 */
 
/* Pin-5 must be connected to ground             */

#include <at89x51.h>
#include <string.h>
#include <stdio.h> 
#define EN P0_0   //LCD Pin 6
#define RS P0_1   //LCD Pin 4
#define lcd_data P2  //LCD Data Lines port
void delay(int);
void time1ms();
void main() 
{ 
     char string[] = {"Testing 1,2,3 " "It' Works ! "}; 
     char init[10]; 
     int count; int len; 
     init[0] = 0x0F; /* Init Display */ 
     init[1] = 0x01; /* Clear Display */ 
     init[2] = 0x38; /* Dual Line / 8 Bits */ 

//Send Commands
     RS=1;             /*Set Register Select */ 
     for (count = 0; count <= 2; count++) 
       { 
            lcd_data=init[count]; 
            EN=1;      /* Set Enable                   */
            delay(20); /* Larger Delay for INIT */ 
            EN=0;      /* Reset Enable               */ 
            delay(20); /* Larger Delay for INIT */ 
       } 

//Send Display Data
        RS=0;           /* Reset Register Select */ 
        len = strlen(string);            
     for (count = 0; count < len; count++) 
      { 
          lcd_data=string[count];
          EN=1;        /* Set Enable                  */ 
          delay(2); 
          EN=0;       /* Reset Enable               */ 
          delay(2);
      } 
}

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!
}


No comments:

Post a Comment