本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
#include<reg51.h>
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit beep=P3^0;
unsigned char time1;
unsigned char t=0;
unsigned char second=0;
unsigned char minute=0;
unsigned char hour=12;
unsigned char time[]={0xc0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xbf};
void display();
void keyscan();
void delay(unsigned char k);
void ln();
void delay (unsigned char k)
{
unsigned char j;
while((k--)!=0)
{
for(j=0;j<125;j++)
{;}
}
}
void keyscan (void)
{
if(key1==0)
{
delay(10);
if(key1==0)
{
while(!key1);
time1++;
if(time1==1)
{
TR0=0;
}
if(time1==2)
{
TR0=0;
}
if(time1==3)
{
TR0=0;
}
if(time1==4)
{
time1=0;
TR0=1;
}
}
}
if(time1!=0)
{
if(key2==0)
{
delay(10);
if(key2==0);
{
while(!key2);
if(time1==1)
{second++;
if(second==60)
{
second=0;
}
}
if(time1==2)
{
minute++;
if(minute==60)
{
minute=0;
}
}
if(time1==3)
{
hour++;
if(hour==24)
{
hour=0;
}
}
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
while(!key3);
if(time1==1)
{
second--;
if(second==-1)
{
second=59;
}
}
if(time1==2)
{
minute--;
if(minute==-1)
{
minute=59;
}
}
if(time1==3)
{
hour--;
if(hour==-1)
{
hour=23;
}
}
}
}
}
}
main()
{
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
ET1=1;
ET0=1;
EA=1;
TR0=1;
while(1)
{
ln();
keyscan();
display();
}
}
void timer0() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
t++;
if(t==20)
{t=0;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
}
}
void display()
{
P2=0x01;
P0=time[second%10];
delay(4);
P2=0x02;
P0=time[second/10];
delay(4);
P2=0x08;
P0=time[minute%10];
delay(4);
P2=0x10;
P0=time[minute/10];
delay(4);
P2=0x40;
P0=time[hour%10];
delay(4);
P2=0x80;
P0=time[hour/10];
delay(4);
P2=0X24;
P0=time[10];
delay(4);
}
void ln()
{ unsigned char a;
beep=0;
if((minute==33)&(second<=20))
{
for(a=0;a<12;a++)
{ beep=~beep;
delay(1);
}
}
}
想弄下,调时对应位闪烁,于是我又加了些程序,不过貌似没反应,
在开头我定义了个sbit flag;又加了个中断void timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
t1++;
if(t1==8)
{
t1=0;
flag=~flag;
}
当然主函数中添了个TR1=0;我想先加个对应秒闪烁试试,于是我定义了个 sbit miaos=P2^0;(对应的秒个位),接着我把在按键扫描模块keyscan中if(time1==1)
{TR0=0;}改为if(time1==1) {TR0=0;TR1=1;miaos=flag;} 此时按我理解,按键按过1次后中断0打开时间停止;中断1打开,对应的秒个位应该闪烁的啊!but没反应。。。。。。求分析!
下面我qq截了张图;
[attachment=21903] |