Pages

Write a C program to display the code of the key pressed in 8051

#include <REG51xD2.H>

#include "lcd.h"
unsigned char getkey();
void delay(unsigned int);
main()
{
unsigned char key;
InitLcd(); /* Initialise LCD */
WriteString("Key Pressed="); /* Display msg on LCD */
while(1)
{
GotoXY(12,0); /* Set Cursor Position */
key = getkey(); /* Call Getkey method */
}
}
unsigned char getkey()
{
unsigned char i,j,k,indx,t;
P0=0x0ff;
P1=0x0ff;
P2 = 0x00; /* P2 as Output port */
indx = 0x00; /* Index for storing the first value of
scanline */
for(i=0x00E;i>=0x00B;i<<=1) /* for 4 scanlines */
{
P2 = i; /* write data to scanline */
t = P0; /* Read readlines connected to P0*/
t = ~t;
if(t>0) /* If key press is true */
{
delay(6000); /* Delay for bouncing */
for(j=0;j<=7;j++) /* Check for 8 lines */
{
t >>=1;
if(t==0) /* if get pressed key*/
{
k = indx+j; /* Display that by converting to Ascii */
t = k>>4;
t +=0x30;
WriteChar(t); /* Write upper nibble */
t = k & 0x0f;
if(t > 9)
t+=0x37;
else
t+=0x30;
WriteChar(t); /* write lower nibble */
return(indx+j); /* Return index of the key pressed */
}
}
}
indx += 8; /* If no key pressed increment index */
}
}
void delay(unsigned int x) /* Delay routine */
{
for(;x>0;x--);
}

0 comments:

Post a Comment