#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P3^5; //這里沒(méi)有讀寫定義是因?yàn)橹苯咏拥亓,因(yàn)槠綍r(shí)都用來(lái)寫程序;
sbit en=P3^4; //只要控制rs和en端就可以
sbit dula=P2^6;
sbit wela=P2^7;
uchar temp;int n;
int flag,nn;
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=10;b>0;b--);
}
void delay1(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=100;b>0;b--);
}
void write_com(uchar com)
{
P0=com; //命令寫入P0口,待會(huì)使能端可以讀走
rs=0; //寫命令
en=0;
delay(10);
en=1;
delay(10);
en=0;
}
void write_date(uchar date)
{
P0=date;
rs=1; //寫數(shù)據(jù)
en=0;
delay(10);
en=1;
delay(10);
en=0;
}
uint keyscan()
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{case 0xee:n=1;break;
case 0xde:n=2;break;
case 0xbe:n=3;break;
case 0x7e:n=4;break;
}
while(temp!=0xf0)
{temp=P3;
temp=temp&0xf0;
}
}
}
return n;
}
void init()
{
dula=0; //這里主要是防止數(shù)碼管亮導(dǎo)致功率增大影響LCD
wela=0;
write_com(0x38); //顯示模式設(shè)置
delay(20);
write_com(0x0f); //顯示光標(biāo)同時(shí)閃爍
delay(20);
write_com(0x06); //正序顯示,07是整屏左移
delay(20);
write_com(0x01); //清零顯示
delay(20);
write_com(0x80);
delay(20);
}
void main()
{
init();
while(keyscan())
{write_date(0x30+keyscan());
while(1);
}
}
|