把按培哗陪键检测,和执行按键功能,分开。
参考如下程序:
void main(){
mm = 0, kk = 0
while(1) {
kk = keyscan() //返回0、1、2、3
if(kk == 0) display(shi, fen, miao) //没有按就正常走时
if(kk == 1) {mm++ if(mm == 4) 芦薯 mm = 0} //改变项目
if(kk == 2) {
switch(mm) {
case 1 : if(miao < 59) miao++ break
case 2 : if(fen < 59) fen++ break
case 3 : if(shi < 23) shi++ break
}
}
if(kk == 3) 配蠢{
switch(mm) {
case 1 : //相应项目-- break
case 2 : //相应项目-- break
case 3 : //相应项目-- break
}
}
}
}
#include"reg52.h"#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^7
sbit dula=P2^6
sbit s1=P3^4//模式的选择
sbit s2=P3^5//调高
sbit s3=P3^6//调低
sbit s4=P3^7//选择是否开启闹铃
sbit beep=P2^3
uchar tt,num,t1,settime,bi
uchar flag //至位
int fen,miao,fen1=0,miao1=10
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71}
uchar dis[]={0x40,0x40,0x40,0x40,0x40,0x40}
void timedisplay()
void display()
void keydisplay()
void setdisplay()
void display1()
void delay(uchar z)
{
uchar i,j
for(i=125i>0i--)
for(j=zj>0j--)
}
void warm()
{
if(bi==1)
{
beep=0
TR1=1
}
}
void init()
{
TMOD=0x11
TH0=(65536-50000)/256
TL0=(65536-50000)%6
TH1=(65536-50000)/指敏256
TL1=(65536-50000)%6
EA=1
ET0=1
ET1=1
TR0=1
}
void keyscan()
{
if(s4==0)
{
delay(5)
{
if (s4==0)
{
while(!s4)
bi++
if(bi==2)
{
bi=0
}
}
}
}
if(s1==0)
{
delay(5)
if(s1==0)
{
while(!s1)
num++
if(num==3)
{
num=0
}
}
}
if(num!=0)
{
if(s2==0)
{
delay(5)
if(s2==0)
{
if(num==1)
{
miao1++
if(miao1==60)
miao1=0
}
if(num==2)
{
fen1++
if(fen1==60)
fen1=0
}
while(!s2)
{
keydisplay()
}
}
}
if(s3==0)
{
delay(5)
if(s3==0)
{
if(num==1)
{
miao1--
if(miao1==-1)
miao1=59
}
if(num==2)
{
fen1--
if(fen1==-1)
fen1=59
}
while(!s3)
{
keydisplay()
}
}
}
}
}
void setdisplay() //设嫌坦置模式下的显示
{
dis[0]=0x40
dis[1]=0x40
dis[2]=table[0]
dis[3]=table[num]
dis[4]=0x40
dis[5]=0x40
display()
}
void keydisplay() //通过按键加减时的显示
{
if((flag==1)&&(num==2))
{
dis[0]=table[fen1/10]&0x00
dis[1]=table[fen1]&0x00
}
else
{
dis[0]=table[fen1/芹逗桐10]
dis[1]=table[fen1]
}
dis[2]=0x40
if((flag==1)&&(num==1))
{
dis[3]=table[miao1/10]&0x00
dis[4]=table[miao1]&0x00
}
else
{
dis[3]=table[miao1/10]
dis[4]=table[miao1]
}
dis[5]=0x40
display()
}
void display() //基本的动态显示
{
dula=1//the one
P0=dis[0]
dula=0
wela=1
P0=0xfe
wela=0
delay(1)
dula=1//the two
P0=dis[1]
dula=0
wela=1
P0=0xfd
wela=0
delay(1)
dula=1//the three
P0=dis[2]
dula=0
wela=1
P0=0xfb
wela=0
delay(1)
dula=1//the four
P0=dis[3]
dula=0
wela=1
P0=0xf7
wela=0
delay(1)
dula=1 //the five
P0=dis[4]
dula=0
wela=1
P0=0xef
wela=0
delay(1)
dula=1 //the six
P0=dis[5]
dula=0
wela=1
P0=0xdf
wela=0
delay(1)
}
void timedisplay()
{
dis[0]=table[fen/10]
dis[1]=table[fen]
dis[2]=0x40
dis[3]=table[miao/10]
dis[4]=table[miao]
dis[5]=0x40
display()
}
void main()
{
init()
while(1)
{
keyscan()
if(num!=0)
{
keydisplay()
}
if(num==0)
{
timedisplay()
}
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256
TL0=(65536-50000)%6
tt++
if(tt==20)
{
tt=0
miao++
if(miao==60)
{
miao=0
fen++
if(fen==60)
{
fen=0
}
}
}
if(!num&&fen1==fen&&miao1==miao)
{
warm()
}
settime++
if(settime==10)
{
flag=1
}
if(settime==20)
{
settime=0
flag=0
}
}
void timer1() interrupt 3
{
TH1=(65536-50000)/256
TL1=(65536-50000)%6
t1++
if(t1==60)
{
t1=0
beep=1
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)