#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
//用液晶屏显示参数
uchar code zifx0[]="0123456789abcdef";
uchar code zifu0[]="Now Number:0000";
uchar code zifu1[]="Set Number:0000";
sbit RS=P3^1; //写信号
sbit RWW=P3^3; //读信号
sbit E=P3^4; //使能信号
#define e1 E=1
#define rd1 RWW=1
#define rs1 RS=1
#define e0 E=0
#define rd0 RWW=0
#define rs0 RS=0
#define PP P1
//延时函数ms
void _delay_ms(uint t)
{
uint i,j;
for(i=0;i<t;i++)
for(j=0;j<120;j++);
}
//延时函数us
void _delay_us(uchar t)
{
while(t>0)t--;
}
//写1602控制字
void lcd1602_writecrtl(uchar dat)
{
rd0;//读信号置0
rs0;//写信号置0
_delay_us(5);
PP=dat;
e1;//使能信号置1
_delay_us(5);
e0;//使能信号置0
}
//写1602数据
void lcd1602_writenumber(uchar dat)
{
rd0;//读信号置0
rs1;//写信号置1
_delay_us(5);
PP=dat;
e1;//使能信号置1
_delay_us(5);
e0;//使能信号置0
}
//1602初始化
void lcd1602_init()
{
lcd1602_writecrtl(0x38); //显示模式
lcd1602_writecrtl(0x06); //显示光标移动位置
lcd1602_writecrtl(0x0c); //显示开及光标设置
lcd1602_writecrtl(0x01); //显示清屏
}
//显示地址
void lcd1602_adr(uchar dat)
{
lcd1602_writecrtl(0x80 | dat);
}
//行显示-
void LCD1602_string(uchar hang,uchar lie,uchar const *p)
{
uchar a;
if(hang == 1) a = 0x00;
if(hang == 2) a = 0x40;
a = a + lie - 1;
lcd1602_adr(a);
while(1)
{
if(*p == '\0') break;
lcd1602_writenumber(*p);
p++;
}
}
uint keyn1=16;//按键值
uint keyn=16;//缓冲
#define PK P2
//按键扫描
void key_z()
{
PK=0X0F;
if(!(PK==0X0F))
{
if(!(PK==0X0F))
{
keyn1=0;
keyn=0;
PK=0X0F;
if(PK==0X0E) keyn1=0;
if(PK==0X0D) keyn1=1;
if(PK==0X0B) keyn1=2;
if(PK==0X07) keyn1=3;
PK=0XF0;
if(PK==0XE0) keyn=0+keyn1;
if(PK==0XD0) keyn=4+keyn1;
if(PK==0XB0) keyn=8+keyn1;
if(PK==0X70) keyn=12+keyn1;
switch(keyn)
{
case 0: keyn1=1; break;
case 4: keyn1=2; break;
case 8: keyn1=3; break;
case 12:keyn1=10; break;
case 1: keyn1=4; break;
case 5: keyn1=5; break;
case 9: keyn1=6; break;
case 13:keyn1=11; break;
case 2: keyn1=7; break;
case 6: keyn1=8; break;
case 10:keyn1=9;break;
case 14:keyn1=12;break;
case 3: keyn1=14;break;//*
case 7: keyn1=0;break;
case 11:keyn1=15;break;//#
case 15:keyn1=13;break;
}
}
else keyn1=16;
PK=0X0F;while((!(PK==0X0F)));
}
}//LED
sbit led1=P0^0;
sbit beer=P3^5;
sbit out=P3^6;
sbit k1=P3^7;
sbit P32=P3^2;
//变量
uint now=0;
uint set=10;
uint bzbu=0;//设置
uint bz=0;//模式
uint qidong=0;//运行
//显示控制
void dis()
{
//now
lcd1602_adr(0x0B);
lcd1602_writenumber(0x30+now%10000/1000);
lcd1602_writenumber(0x30+now%1000/100);
lcd1602_writenumber(0x30+now%100/10);
lcd1602_writenumber(0x30+now%10);
//set
lcd1602_adr(0x4B);
lcd1602_writenumber(0x30+set%10000/1000);
lcd1602_writenumber(0x30+set%1000/100);
lcd1602_writenumber(0x30+set%100/10);
lcd1602_writenumber(0x30+set%10);
if(bz==0)lcd1602_writenumber(' ');
if(bz==1)lcd1602_writenumber('<');
//启动
if(bz==0)
{
if(k1==0){qidong=(qidong+1)%2;if(qidong==1){led1=0;;out=0;}else {led1=1;out=1;}while(k1==0);}
}
//启动
if(qidong==1)
{
if(now>=set){qidong=0;led1=1;out=1;beer=0;_delay_ms(500); }
}
//启动
if(qidong==0)
{
beer=1;
}
}
//按键功能函数
void key()
{
//扫描
key_z();
if(keyn1<16)
{
if((keyn1<10)&&(bz==1))
{
if(bzbu==0){set=keyn1;}
if(bzbu==1){set=set*10+keyn1;}
if(bzbu==2){set=set*10+keyn1;}
if(bzbu==3){set=set*10+keyn1;}
bzbu=(bzbu+1)%4;
}
if((keyn1==0x0a)&&(bz==0)){now=0;}//复位
if((keyn1==0x0b)&&(bz==1)){set=0;}//复位输入
if((keyn1==0x0c)&&(qidong==0)){bz=1;}//设置
if(keyn1==0x0d){bz=0;}//退出
keyn1=16;
}
}
//-----------------------
void main()
{
_delay_us(10);
_delay_ms(10);
//中断允许
IT0=1;
EX0=1;
EA=1;
//初始化
lcd1602_init();
LCD1602_string(1,1,zifu0);
LCD1602_string(1,1,zifu0);
LCD1602_string(2,1,zifu1);
while(1)
{
dis(); //显示
key();
}
}
//INT0中断函数
void EX_INT0() interrupt 0
{
if(qidong==1)
{
_delay_ms(3);if(P32==0)now=now+1;
}
}
|