给你一个lcd1602和键盘的程序,你稍加修改就可以用了#include "AT89X51.h"
#include "lcd_1602.c"
#include "key.c"
main()
{
unsigned char key;
LCD_Initial();
GotoXY(0,0);
Print("wait you press!");
while(1)
{
key=keyscan1();
if(key!=255)
{
GotoXY(0,1);
LCD_Write(1,key/100+0x30);
LCD_Write(1,key%100/10+0x30);
LCD_Write(1,key%10+0x30);
}
}
}
//端口定义
#define DBPort P0 //LCD数据端口
sbit LcdRs = P2^0;
sbit LcdRw = P2^1;
sbit LcdEn = P2^2;
sbit Lcdbf = P0^7; //LCD忙标志 Busy Flagvoid delay(unsigned int t) //延时
{
while(t--);
}void LCD_Wait(void) //读忙状态
{
LcdRs=0;
LcdRw=1;
LcdEn=1;delay(1);LcdEn=0; //下降沿
while(Lcdbf)
{
LcdEn=0;delay(1);LcdEn=1; //仿真才需要此语句,实际硬件中不需要
}
} void LCD_Write(bit style, unsigned char input) //写数据1/命令0
{
LcdRs=style;
LcdRw=0;
DBPort=input;
LcdEn=1;delay(1);LcdEn=0;
LCD_Wait();
}void LCD_Initial(void) //初始化LCD
{
LCD_Write(0,0x38); //8位数据端口,2行显示,5*7点阵
delay(300);
LCD_Write(0,0x0c); //显示模式
LCD_Write(0,0x01); //清屏
LCD_Write(0,0x06); //输入模式
delay(300);
}void GotoXY(unsigned char x, unsigned char y) //移动光标到指定位置
{
if(y==0) LCD_Write(0,0x80|x);
if(y==1) LCD_Write(0,0xc0|x);
}void Print(unsigned char *str) //指定坐标输出字符串
{
while(*str) LCD_Write(1,*str++);
}
//P1接4*4键盘
unsigned char keyscan1() //行列法1
{
unsigned char row,rol;
P1=0x0f;
if(P1!=0x0f)
{
delay(10); //延时消抖
if (P1_0==0) row=0;
else if(P1_1==0) row=1;
else if(P1_2==0) row=2;
else if(P1_3==0) row=3;
else return(255); P1=0xf0;
if (P1_4==0) rol=0;
else if(P1_5==0) rol=1;
else if(P1_6==0) rol=2;
else if(P1_7==0) rol=3;
else return(255); while(P1!=0xf0); //等待释放
return(row*4+rol);
}
else return(255);
}