Microcontroller › 8051 › program for 4 bit lcd interface with 89s52 › hi akshay, here below
May 20, 2016 at 11:42 am
#13931
Guest
hi akshay,
here below you can find 4-bit lcd C prgm code.
/*– — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
LCD clear initialization:
===================
This is the sub-routine to initialise the lcd to 4bit. this has more advantage
than the 8-bit lcd. One of the advantage is using less pins compared to 8bit lcd.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — */
void lcd_init() //LCD_INITIAL SUB-ROUTINE HEADER
{
cmd(0x01,0); // Lcd clear instruction
cmd(0x02,0);
cmd(0x30,0); // 8 bit initialisation
cmd(0x32,0); // auxillary instruction for 4 bit lcd
cmd(0x20,0); // 4 bit initialisation
cmd(0x28,0); // 4 bit, 5×7 dot matrix
cmd(0x0c,0); // display ON, cursor OFF
cmd(0x80,0); // First row, first position
}
/*– — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
4-bit lcd program sub routine:
=========================
This is the sub-routine to send data to the lcd. By seperating the 8bit data into
4-4bits and send to lcd using 4-lcd data lines.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — */
void cmd(char p,int q) // LCD COMMAND SUB-ROUTINE HEADER
{
char r; // Declaring the character variable
rs=q; // selecting whether the hex is command or data
lcd=lcd&0xf0; // clearing the lcd's 4bit
r=p>>4; // source data is rotated 4 times
r=r&0x0f; // getting upper nibble seperately
lcd=lcd|r; // moving the upper nibble to lcd
e=1; // enabling lcd
delay_us(20); // delay sub-routine is called for 7u.sec
e=0; // disabling lcd
lcd=lcd&0xf0; // clearing the lcd's 4bit
r=p; // moving source data to 'r'
p=p&0x0f; // getting lower nibble seperately
lcd=lcd|p; // moving the lower nibble to lcd
e=1; // enabling lcd
delay_us(20); // delay sub-routine is called for 7u.sec
e=0; // disabling lcd
}
/*– — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Sub-routine for to display the string in lcd :
===================================
This is the sub-routine which simplifies the string to display in the lcd and
make the user to prgm easier.this sub-routine includes displaying string, starting
address and time spend to display each character.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — */
void strg_cmd(char p[],char add,int u) // STRING FUNCTION SUB-ROUTINE HEADER
{
int i,j; // Declaring the int variables
j=strlen(p); // measuring the string length
cmd(add,0); // passing the starting address to lcd
for(i=0;i<j;i++) // for loop continues until the last character
{
cmd(p,1); // cmd sub-routine is called
delay_ms(u); // delay is called for each char to display
}
}