#include
#include
#define uchar unsigned char
#define uint unsigned int
#define DD P0
sbit Rs=P0^0;
sbit Rw=P0^1;
sbit E=P0^2;
uchar key_led;
unsigned char code disp_code[]={"0123456789abcdef"};
unsigned char code key_code[]={
0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,
0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77 };//注意这里你自己看下你电路是怎么连得,对应改一下表单里的值
/*******************************************************************/
/*
*/
/* 延时子程序
*/
/*
*/
/*******************************************************************/
void delay(int ms)
{
int i;
while(ms--)
{
for(i = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
void delay_1ms(unsigned char i) //最小延时1ms
{ unsigned char j;
while(i--)
for(j=0;j<125; j++);
}
void delay_10ns(unsigned char i) //最小延时10ns
{
unsigned char j;
while(i--)
for(j=0;j<10; j++);
}
void write_com(unsigned char com,bit p) //写指令
{if(p)
delay_10ns(5);
E=0;
Rs=0;
Rw=0;
DD=com;
delay_10ns(50); //>40ns
E=1;
delay_1ms(2); //>150ns
E=0;
delay_10ns(4); //>25+10ns
}
void write_date(unsigned char DATA) //写数据
{
delay_10ns(50);
E=0;
Rs=1;
Rw=0;
DD=DATA;
delay_10ns(50);
E=1;
delay_10ns(50);
E=0;
delay_10ns(4);
}
void addr_x_y(unsigned char x,bit y) //写坐标,定位置
{
unsigned char temp=0x80;
if(y)
{
temp|=0x40;
}
temp|=x;
write_com(temp,0);
}
void desplay_char(unsigned char x,bit y,unsigned char p)
//在指定位置显示一个字符。
{
addr_x_y(x,y);
write_date(p);
}
void lcd_init(void)
{
delay_1ms(15);
write_com(0x38,0);
delay_1ms(5);
write_com(0x38,0);
delay_1ms(5);
write_com(0x38,0);
delay_1ms(5);
write_com(0x38,1);
write_com(0x08,1);
write_com(0x01,1);
write_com(0x06,1);
write_com(0x0c,1);
}
/**********************************************************
键盘扫描子函数
**********************************************************/
uchar keyscan()
{
uchar scan1,scan2,keycode,j;
P1=0xf0;
scan1=P1;
if((scan1&0xf0)!=0xf0) //判键是否按下
{
delay(30); //延时30ms
scan1=P1;
if((scan1&0xf0)!=0xf0) //二次判键是否按下
{
P1=0x0f;
scan2=P1;
keycode=scan1|scan2; //组合成键编码
for(j=0;j<=15;j++)
{
if(keycode== key_code[j]) //查表得键值
{
delay(30);
key_led=j;
}
}
}
}
else return(16) ;
}
/**********************************************************
判键是否按下子函数
**********************************************************/
void keydown()
{
P1=0xf0;
if((P1&0xf0)!=0xf0)
{
keyscan();
}
}
/*******************************************************************/
/*
*/
/* 主程序
*/
/*
*/
/*******************************************************************/
void main(void)
{
lcd_init(); // 初始化LCD
key_led=16;
while(1)
{
keydown();
desplay_char(0,0,disp_code[key_led]);
}
}