I am trying to write program for matrix keypad. I am working with 8051 and keil
I think I can't write whole program so I have started with function's and I hope you will help me to complete program
Do you think all the functions are correct? Should I write main function?
I think I can't write whole program so I have started with function's and I hope you will help me to complete program
C:
#include<reg51.h>
#define port P3 /* Data pins connected to port P1 */
sbit RS = P2^0; /* RS pin connected to pin 0 of port P2 */
sbit RW = P2^1; /* RW pin connected to pin 1 of port P2 */
sbit EN = P2^3; /* EN pin connected to pin 2 of port P2 */
sbit R1 = P1^0; /*Row 1 */
sbit R2 = P1^1; /*Row 2 */
sbit R3 = P1^2; /*Row 3 */
sbit R4 = P1^3; /*Row 4 */
sbit C1 = P1^4; /*Column 1 */
sbit C2 = P1^5; /*Column 2 */
sbit C3 = P1^6; /*Column 3 */
sbit C4 = P1^7; /*Column 4 */
void DelayUs(unsigned int wait);
void DelayMs(unsigned int wait);
void LCD_Command(unsigned char cmd);
void LCD_Data(unsigned char Data);
void LCD_init();
/* 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 instruction to LCD */
void LCD_Command(unsigned char cmd)
{
port = cmd;
RS=0;
RW=0;
EN=1;
DelayMs(5);
EN=0;
}
/*Function to send display dato LCD */
void LCD_Data(unsigned char Data)
{
port = Data;
RS=1;
RW=0;
EN=1;
DelayMs(5);
EN=0;
}
/* Function to prepare the LCD */
void LCD_init()
{
LCD_Command(0x38);
DelayMs(15);
LCD_Command(0x0f);
DelayMs(15);
LCD_Command(0x01);
DelayMs(15);
LCD_Command(0x81);
DelayMs(15);
}
char keypad(void)
{
unsigned char keymask = 0xEF;
char key = 0, row;
for(row=0;row < 3; row++)
{
P2 = keymask;
if(!C1) key = 1;
if(!C2) key = 2;
if(!C3) key = 3;
if(!C4) key = 4;
if(key)
{
key += (row*4);
return key;
}
keymask <<= 1;
keymask ++;
}
return key;
}
void LCD_goto( char x, char y)
{
int addr = 0x80;
if(y==2) addr+=0x40;
addr += x;
LCD_Command(addr);
}
Do you think all the functions are correct? Should I write main function?