#Microcontrollers 8051 8-bit LCD Code in Embedded C


// Data Bus = P2

// Command Lines: rs=P1.0, rw=P1.1, en=P1.2 

#include <reg51.h>

#define databus P2

sbit rs=P1^0;

sbit rw=P1^1;

sbit en= P1^2;

void delay(int);

void lcdinit(void);

void lcdcmd(unsigned char);

void lcddata(unsigned char);

void lcdstring(char*);


void main()

{

char *text1 ={"Hello"};

char *text2 ={"PCCoE Pune"};


lcdinit();

lcdcmd(0x82); //cursor position 1st line 5th column

lcdstring(text1);

lcdcmd(0xC2); //cursor position 2nd line 5th column

lcdstring(text2);

while(1);

}


void delay(int ms)

{

int i,j;

for(i=0;i<ms;i++)

for(j=0;j<100;j++);

}


void lcdinit(void)

{

  lcdcmd(0x38); //16x2 mode, 5x7 character matrix

delay(10);

lcdcmd(0x0C);

delay(10);

lcdcmd(0x06);

delay(10);

lcdcmd(0x01); // clear LCD

delay(10);

lcdcmd(0x80); //Move cursor to starting position of 1st line

delay(10);

}



void lcdcmd(unsigned char ch)

{

databus = ch;

rs=0;

rw=0;

en=1;

delay(10);

en=0;

}


void lcddata(unsigned char ch)

{

  databus = ch;

rs=1;

rw=0;

en=1;

delay(10);

en=0;

}


void lcdstring(char *str)

{

int i;

for(i=0;str[i]!=0;i++)  

lcddata(str[i]);  //"H,e,l,l,o"

}

Comments