#include<reg51.h>
void cct_init(void);
void delay(int);
void lcdinit(void);
void wcmd(int);
void wdata(char);
void enter_roll(void);
char READ_SWITCHES(void);
char keypad(int);
//********************
// Define Pins
//********************
sbit RowA = P1^0; //RowA
sbit RowB = P1^1; //RowB
sbit RowC = P1^2; //RowC
sbit RowD = P1^3; //RowD
sbit C1 = P1^4; //Column1
sbit C2 = P1^5; //Column2
sbit C3 = P1^6; //Column3
sbit C4 = P1^7; //Column4
sbit en = P3^6; //E pin for LCD
sbit rs = P3^7; //RS pin for LCD
sbit te = P3^3; //transmission inable for H12E
char key;
int a,b,k=0;
int c; // check taht enter function called in which condition i.e for enter or change or status
char u[10];
void delay(int c)
{
int i,j;
for(i=0;i<c;i++)
for(j=0;j<50;j++);
}
void cct_init(void)
{
P1 = 0xf0; //used for generating outputs and taking inputs from Keypad
P2 = 0x00; //used as data port for LCD
en = 0; //used for E
rs = 0; //used for RS
}
void lcdinit()
{
wcmd(0x38);
delay(200);
wcmd(0x0c);
delay(200);
wcmd(0x01);
delay(200);
wcmd(0x06);
delay(200);
}
void wcmd(int b)
{
rs = 0;
P2 = b;
en = 1;
delay(50);
en = 0;
delay(50);
}
void wdata(char a)
{
rs = 1;
P2 = a;
en = 1;
delay(50);
en = 0;
delay(50);
}
void lcd(char *disp)
{
int k,i;
i=0;
k=0;
for(k=0;disp[k]!='\0';k++)
{
wdata(disp[k]);
if(i>=15)
{
wcmd(0x07);
}
i++;
}
}
void keypad(int k)
{
key = get_key(); // Get pressed key
wdata(key); // Echo the key pressed to LCD
u[k]=key;
}
void enter_roll()
{
b=0;
wcmd(0x01);
lcd("enter your rollnumber");
delay(100);
wcmd(0XC0);
do
{
keypad(b);
b++;
}while( key !='#' );
}
//////////////////////////////////
//******************************//
//////////////////////////////////
// Main program
int main(void)
{
cct_init();
lcdinit(); // Initilize LCD
lcd(" welcome");
while(1)
{
for(k=0;k<1;k++)
{
key = get_key(); // Get pressed key
wcmd(0X01);
}
enter_roll();
}
}
char READ_SWITCHES(void)
{
RowA = 0; RowB = 1; RowC = 1; RowD = 1; //Test Row A
if (C1 == 0) { delay(500); while (C1==0); return '1'; }
if (C2 == 0) { delay(500); while (C2==0); return '2'; }
if (C3 == 0) { delay(500); while (C3==0); return '3'; }
if (C4 == 0) { delay(500); while (C4==0); return '+'; }
RowA = 1; RowB = 0; RowC = 1; RowD = 1; //Test Row B
if (C1 == 0) { delay(500); while (C1==0); return '4'; }
if (C2 == 0) { delay(500); while (C2==0); return '5'; }
if (C3 == 0) { delay(500); while (C3==0); return '6'; }
if (C4 == 0) { delay(500); while (C4==0); return '-'; }
RowA = 1; RowB = 1; RowC = 0; RowD = 1; //Test Row C
if (C1 == 0) { delay(500); while (C1==0); return '7'; }
if (C2 == 0) { delay(500); while (C2==0); return '8'; }
if (C3 == 0) { delay(500); while (C3==0); return '9'; }
if (C4 == 0) { delay(500); while (C4==0); return '%'; }
RowA = 1; RowB = 1; RowC = 1; RowD = 0; //Test Row D
if (C1 == 0) { delay(500); while (C1==0); return '*'; }
if (C2 == 0) { delay(500); while (C2==0); return '0'; }
if (C3 == 0) { delay(500); while (C3==0); return '#'; }
if (C4 == 0) { delay(500); while (C4==0); return '/'; }
return 'n'; // Means no key has been pressed
}
char get_key(void) //get key from user
{
char key = 'n'; //assume no key pressed
while(key=='n') //wait untill a key is pressed
key = READ_SWITCHES(); //scan the keys again and again
return key; //when key pressed then return its value
}