You are on page 1of 4

LCD interfacing

#include <REGX52.H>
#define LCD_data P2
#define LCD_rs P1_0
#define LCD_rw P1_1
#define LCD_en P1_2

void LCD_busy(void);
void LCD_poweron(void);
void LCD_command(unsigned char var);
void LCD_senddata(unsigned char var);
void LCD_sendstring(unsigned char *var);
void LCD_init(void);
void main(void)
{
unsigned char msg[] ="Learning";
LCD_poweron(); // 15ms delay
LCD_init();
LCD_command (0x80); // Set CGRAM adress,data is Avaible from
uC
LCD_sendstring(msg);
while(1);
}
void LCD_busy()
{
unsigned char i,j;
for(i=0;i<50;i++)
for(j=0;j<255;j++);
}
void LCD_poweron()
{
unsigned int i;
for (i=0;i<22500; i++);
}
void LCD_command(unsigned char var)
{
LCD_data = var; //Function set: 2 Line, 8-bit, 5x8 dots
LCD_rs = 0; //Selected command register
LCD_rw = 0; //We are writing in instruction register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy(); //Wait for LCD to process the command
}
void LCD_sendstring(unsigned char *var)
{
while(*var) //till string ends
LCD_senddata(*var++); //send characters one by one
}
void LCD_senddata(unsigned char var)
{
P2 = var; //Function set: 2 Line, 8-bit, 5x7 dots
LCD_rs = 1; //Selected data register
LCD_rw = 0; //We are writing
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy(); //Wait for LCD to process the command
}
void LCD_init(void)
{
LCD_data = 0x38; //Function set: 2 Line, 8-bit, 5x8 dots
LCD_rs = 0; //Selected command register
LCD_rw = 0; //We are writing in data register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy(); //Wait for LCD to process the command
LCD_data = 0x0F; //Display on, Curson blinking command
LCD_rs = 0; //Selected command register
LCD_rw = 0; //We are writing in data register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy(); //Wait for LCD to process the command
LCD_data = 0x01; //Clear LCD
LCD_rs = 0; //Selected command register
LCD_rw = 0; //We are writing in data register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy(); //Wait for LCD to process the command
LCD_data = 0x06; //Entry mode, auto increment with no shift
LCD_rs = 0; //Selected command register
LCD_rw = 0; //We are writing in data register
LCD_en = 1; //Enable H->L
LCD_en = 0; //Enable H->L
LCD_busy();
}
================================================================================
==========
7segment for keil
keyport equ P2 ;Keypad port connected here
col1 equ P2.0 ;Column 1
col2 equ P2.1 ;Column 2
col3 equ P2.2 ;Column 3
col4 equ P2.3 ;Column 4
keyval equ 30H ;To store key number
pressed bit 0H ;Flag
key_init:
mov keyport,#0FH ;Make rows as o/p and col as i/p
ret
get_key:
mov keyval,#0 ;reset the number
mov keyport,#7FH ;make Row1 low
acall read_col ;read columns
jb pressed, done ;check if flag is set
mov keyval,#4 ;if not then read next row
mov keyport,#0BFH ;make Row2 low
acall read_col ;read columns
jb pressed, done ;check if flag is set
mov keyval,#8 ;if not then read next row
mov keyport,#0DFH ;make row3 low
acall read_col ;read columns
jb pressed, done ;check if flag is set
mov keyval,#12 ;if not read row4
mov keyport,#0EFH ;make row4 low
acall read_col ;read columns
done:
ret
read_col: ;read columns routine
clr pressed ;reset the flag
jb col1, nextcol ;check if first key is pressed
jnb col1,$ ;if yes then wait for key release
setb pressed ;set the flag
ret
nextcol: ;read col2
jb col2, nextcol1 ;check if second key is pressed
jnb col2,$ ;if yes then wait for key release
inc keyval ;its key number 2
setb pressed ;set the flag
ret
nextcol1: ;read col3
jb col3, nextcol2 ;check if third key is pressed
jnb col3,$ ;if yes then wait for key release
inc keyval ;its key 3
inc keyval
setb pressed ;set the flag
ret
nextcol2: ;read column 4
jb col4, exit ;check if fourth key pressed
jnb col4,$ ;if yes then wait for key release
inc keyval ;its key 4
inc keyval
inc keyval
setb pressed ;set the flag
ret
exit: ;if no key is pressed
clr pressed ;clr the flag
clr keyval ;reset the number
ret
end
unsigned char get_key(){
unsigned char i,k,key=0;
k=1;
for(i=0;i<4;i++){ //loop for 4 rows
keyport &=~(0x80>>i); //to make rows low 1 by 1
if(!col1){ //check if key1 is pressed
key = k+0; //set key number
while(!col1); //wait for release
return key; //return key number
}
if(!col2){ //check if key2 is pressed
key = k+1; //set key number
while(!col2); //wait for release
return key; //return key number
}
if(!col3){ //check if key3 is pressed
key = k+2; //set key number
while(!col3); //wait for release
return key; //return key number
}
if(!col4){ //check if key4 is pressed
key = k+3; //set key number
while(!col4); //wait for release
return key; //return key number
}
k+=4; //next row key number
keyport |= 0x80>>i; //make the row high again
}
return FALSE; //return false if no key pressed
}

You might also like