From January of 2021 to the middle of March 2021, I worked on a new program that included a keypad. When keys on the keypad were pressed, numbers/letters/symbols would appear on the screen. I used the terminal Tera Term to display the numbers/letters/symbols and used MIDE-51 to write our code. I used C language to code the program. The keypad I used was a Grayhill 96BB2-006-R keypad.
The layout plan for every portion of code is shown below.
#include <8052.h>
#include <stdio.h>
void InitSerialPort(void)
{
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xf3; /*TH1=0xf3; reload value for 2400 baud @ 12MHz*/
PCON=0x80; /* Smod1=1 related to 4800, 12MMHz*/
TR1 = 1; /* TR1: timer 1 start to run */
TI = 1; /* TI: set TI to send first char of UART */
}
int getchar()
{ while (!RI); /* assumes UART is initialized */
RI = 0;
return SBUF;
}
int putchar (int c)
{ while (!TI); /* assumes UART is initialized */
TI = 0;
SBUF = c;
return c;
}
void gets(char *str)
{ while(1)
{ *str=(char)getchar();
if ((*str=='\n')||(*str=='\r')) break;
putchar(*str);
str++;
}
}
int kbhit(void)
{
P1=0xf0;
if (P1==0xf0)
{
return 0;
}
else {
return 1 ;
}
}
char waitkey(void)
{ char i;
while (kbhit()==0) {};
delay20ms();
while (kbhit()==0) {};
for (i=0;i<3;i++){
}
return 0;
}
void delay20ms(void)
{ int i;
for (i=0;i<200;i++){
}
}
char keypad(void)
{char k;
here: while(kbhit()==0); //if no key is pressed, wait.
delay20ms();
if (kbhit()==0) goto here;
scankey();
k=scankey();
if (k==1) goto here;
while(kbhit()==1);
return k;
}
char scankey(void)
{ P1=0xfe;
if (P1==0xee) return '1';
if (P1==0xde) return '2';
if (P1==0xbe) return '3';
if (P1==0x7e) return 'A';
P1=0xfd;
if (P1==0xed) return '4';
if (P1==0xdd) return '5';
if (P1==0xbd) return '6';
if (P1==0x7d) return 'B';
P1=0xfb;
if (P1==0xeb) return '7';
if (P1==0xdb) return '8';
if (P1==0xbb) return '9';
if (P1==0x7b) return 'C';
P1=0xf7;
if (P1==0xe7) return '*';
if (P1==0xd7) return '0';
if (P1==0xb7) return '#';
if (P1==0x77) return 'D';
return 1;
}
void main(void)
{char k;
InitSerialPort();
while (1)
{
k=keypad();
printf("%c",k);
}
}
From this project, I learned how to de-bounce the program. I also learned how to create and call a function as well as how to get a function to return a value whose type matches the function type (in C language). I understood how the scankey works in a code such as how the key value returns from scankey to main().