#include <reg51.h>
#include <stdio.h>
#define LCDport P1 /* Data pins connected to port P1 */
sbit RS = P3^5; /* RS pin connected to pin 0 of port P2 */
sbit RW = P3^6; /* RW pin connected to pin 1 of port P2 */
sbit EN = P3^7; /* EN pin connected to pin 2 of port P2 */
#define KEYport P0 /* Key pad connected to port P0*/
#define C1 P0^0 /* Col 0 pin connected to pin 0 of port P0*/
#define C2 P0^1 /* Col 1 pin connected to pin 1 of port P0 */
#define C3 P0^2 /* Col 2 pin connected to pin 2 of port P0 */
#define C4 P0^3 /* Col 3 pin connected to pin 3 of port P0 */
void delayUs(unsigned int wait);
void delayMs(unsigned int wait);
void LCD_Command(unsigned char cmd);
void putchar(char tx_data);
/* functions for delay */
void delayUs(unsigned int wait)
{
wait >>= 3;
while(wait--);
}
void delayMs(unsigned int wait)
{
while(wait--)
delayUs(1000);
}
/* Function to send command to LCD */
void LCD_Command(unsigned char cmd)
{
LCDport = cmd;
RS = 0;
RW = 0;
EN = 1;
delayMs(5);
EN = 0;
}
/*Function to send data to LCD */
void LCD_Data(unsigned char Data)
{
LCDport = Data;
RS = 1;
RW = 0;
EN = 1;
delayMs(5);
EN = 0;
}
/* Function to prepare the LCD */
void LCD_init(void)
{
delayMs(20);
LCD_Command(0x33); // Init 8 it
delayMs(15);
LCD_Command(0x38); // Function set
delayMs(15);
LCD_Command(0x0C); // display on
delayMs(10);
LCD_Command(0x06); // direction
delayMs(5);
LCD_Command(0x01); // home...
delayMs(5);
}
/* Function to position cursor on the LCD */
void LCD_goto(int x, int y)
{
unsigned char addr = 0x80; // default line
if (y ==2 ) addr = 0xC0; // 2nd line
if (y ==3 ) addr = 0x90; // 3rd line
if (y ==4 ) addr = 0xD0; // 4th line
addr+=x; // Column required
LCD_Command(addr);
}
/* Function to print on the LCD */
void LCD_print(int x, int y, char* str)
{
LCD_goto(x,y);
while(*str!=0) // this is a ponter This means...
LCD_Data(*str++); // While the character pointed to isn't 0 print to LCD then increment..
}
/* Function to pset up Uart */
void UART_Init()
{
TMOD = 0x20; /* Timer 1, 8-bit auto reload mode */
TH1 = 0xFD; /* Load value for 9600 baud rate */
SCON = 0x50; /* Mode 1, reception enable */
TR1 = 1; /* Start timer 1 */
}
/* Function to send a character */
void putchar(char tx_data)
{
SBUF = tx_data; /* Load char in SBUF register */
while (TI == 0); /* Wait until stop bit transmit */
TI = 0; /* Clear TI flag */
}
/* Function to fetch a character */
char getchar(void)
{
while(!RI); // Wait here until SBUF clear
RI = 0;
return SBUF;
}
/* Function to send a line of characters untill a null*/
char keypad(void)
{
unsigned char keymask = 0xEF;
char key = 0, row;
char act_key[] = {0,1,4,7,11,2,5,8,10,3,6,9,12} ;
for(row=0;row < 4; row++)
{
KEYport = keymask;
if(!C1) key = 1;
if(!C2) key = 2;
if(!C3) key = 3;
if(!C4) key = 4;
if(key)
{
key += (row*4);
return act_key[key];
}
keymask <<= 1;
keymask ++;
}
return key;
}
void main(void)
{
char key; // receive variable
char buff[20];
int pos = 0; // where we are on screen
UART_Init(); /* UART initialize function */
LCD_init(); // set up LCD
puts("Press some keys\n\r"); /* Transmit 'test' */
while(1)
{
key = keypad();
if (key)
{
sprintf(buff,"Key Pressed %d ", key) ;
LCD_print(0,0,buff);
}
}
}