sbit beep = P2^3; //蜂鸣器定义位
sbit dula = P2^6; //段选定义位
sbit wela = P2^7; //位选定义位
// sbit INT_1 = P3^3;
sbit s3 = P3^4; //设置时钟选中位
sbit s4 = P3^5; //设置闹钟选中位
sbit s2 = P3^6; //下调设置位
// sbit INT_0 = P3^2;
sbit s5 = P3^7; //上调设置位
uchar move,temp1,temp2,temp3,temp4,temp5,temp6,k,j;
uint hour,min,sec,count,tim; //当前时间
uint alarm_hour,alarm_min,alarm_sec; //闹钟时间
// int fla;
uchar code d_code[]= //段选表
{
0xed,0x81,0xf4,0xb5,
0x99,0x3d,0x7d,0x8d,
0xfd,0xbd
};
uchar code w_code[] = //位选表
{
0xfe,0xfd,
0xfb,0xf7,
0xef,0xdf
};
/* void delay_1s() //插入的汇编实现准确的1s定时
{
#pragma ASM
DEL: MOV R7,#10
DEL1:MOV R6,#200
DEL2:MOV R5,#248
DJNZ R5,$
DJNZ R6,DEL2
DJNZ R7,DEL1
RET
#pragma ENDASM
}*/
void delay(uint z) //可变延时代码
{
uint i,j;
for(i=z;i > 0;i--)
for(j=25;j>0;j--);
}
void delay10ms() //延时程序,消除按键抖动干扰
{
uchar i,j;
for(i=20;i>0;i--)
for(j=248;j>0;j--);
}
/* void flash()
{
P0 = 0x00;
wela = 1;
wela = 0;
P0 = 0x00;
dula = 1;
dula = 0;
delay(17);
}
*/
void init();
void clock_set();
void alarm_clock();
void show_led();
void main(void)
{
init();
while(1)
{
temp1 = d_code[hour/10];
temp2 = d_code[hour%10]|0x02;
temp3 = d_code[min/10];
temp4 = d_code[min%10]|0x02;
temp5 = d_code[sec/10];
temp6 = d_code[sec%10];
show_led();
if(s3==0) //判断是否时钟设置按钮按下
{
delay10ms();
if(s3==0)
{
while(s3==0)
show_led(); //防抖动后执行时钟设置代码
clock_set();
}
}
if(s4 == 0) //判断是否闹铃设置按钮按下
{
delay10ms();
if(s4==0)
{
while(s4==0)
show_led();
alarm_clock();
}
}
if(alarm_hour==hour&&alarm_min == min&&sec-alarm_sec<8)
{
beep=0;
delay10ms();
beep=1;
j=_crol_(k,1);
k=j;
P1=j;
}
if(sec-alarm_sec>=8)
{
P1 = 0x0ff;
}
}
}
void init(void)
{
hour = 12;
min = 0;
sec = 0;
tim = 0;
k=0x0fe;
// mode = 0;
move = 0;
// fla = 0;
// flag = 1;
TMOD = 0x01; //工作在方式1
TH0 = 0x4c; //计时每次50ms,总共计时20次正好是1s了 // 通过矫正后的值
TL0 = 0x0fb;
ET0 = 1;
TR0 = 1;
EA = 1;
//while(10){
P0 = w_code[0];
wela = 1;
wela = 0;
P0 = d_code[hour/10];
dula = 1;
dula = 0;
// delay(10);
P0 = w_code[1];
wela = 1;
wela = 0;
P0 = d_code[hour%10];
dula = 1;
dula = 0;
// delay(10);
P0 = w_code[2];
wela = 1;
wela = 0;
P0 = d_code[min/10];
dula = 1;
dula = 0;
// delay(10);
P0 = w_code[3];
wela = 1;
wela = 0;
P0 = d_code[min%10];
dula = 1;
dula = 0;
// delay(10);
P0 = w_code[4];
wela = 1;
wela = 0;
P0 = d_code[sec/10];
dula = 1;
dula = 0;
// delay(10);
P0 = w_code[5];
wela = 1;
wela = 0;
P0 = d_code[sec%10];
dula = 1;
dula = 0;
// }
}
static time0() interrupt 1 using 1 //一秒中断代码
{
TH0 = 0x4c; //计时每次50ms,总共计时20次正好是1s了
TL0 = 0x0fb;
tim++;
if(tim==20) //1s中断
{
tim = 0;
sec++;
if(sec==60)
{
sec = 0;
min++;
if(min==60)
{
min = 0;
hour++;
if(hour==24)
{
hour = 0;
}
}
}
}
}
void clock_set()
{
EA = 0;
while(1)
{
/* if(s3==0) //mode选择开中断
{
delay10ms();
if(s3==0)
{
EA = 1;break;
}
}*/
if(s4==0)
{
delay10ms();
if(s4==0)
{
while(s4==0)
show_led();
move++;
if(move == 3) {move = 0;EA = 1;break;}
}
}
if(s5==0&&move==0)
{
delay10ms();
if(s5==0)
{
while(s5==0)
show_led();
sec++;
if(sec==60)
{
sec = 0;
}
}
}
if(s2==0&&move==0)
{
delay10ms();
if(s2==0)
{
while(s2==0)
show_led();
sec--;
if(sec==-1)
{
sec = 59;
}
}
}
if(s5==0&&move==1)
{
delay10ms();
if(s5==0)
{
while(s5==0)
show_led();
min++;
if(min==60)
{
min = 0;
}
}
}
if(s2==0&&move==1)
{
delay10ms();
if(s2==0)
{
while(s2==0)
show_led();
min--;
if(min==-1)
{
min = 59;
}
}
}
if(s5==0&&move==2)
{
delay10ms();
if(s5==0)
{
while(s5==0)
show_led();
hour++;
if(hour==24)
{
hour = 0;
}
}
}
if(s2==0&&move==2)
{
delay10ms();
if(s2==0)
{
while(s2==0)
show_led();
hour--;
if(hour==-1)
{
hour = 23;
}
}
}
{
temp1 = d_code[hour/10];
temp2 = d_code[hour%10];
temp3 = d_code[min/10];
temp4 = d_code[min%10];
temp5 = d_code[sec/10];
temp6 = d_code[sec%10];
if(move == 2)
{
// temp1 = temp1|0x02;
temp2 = temp2|0x02;
}
if(move == 1)
{
// temp3 = temp3|0x02;
temp4 = temp4|0x02;
}
if(move == 0)
{
// temp5 = temp5|0x02;
temp6 = temp6|0x02;
}
show_led();
}
/* if(s3==0)
{
delay10ms();
if(s3==0)
{
while(s3==0);
EA = 1;
break;
}
}*/
}
}
void alarm_clock()
{
alarm_hour = hour;
alarm_min = min;
alarm_sec = sec;
while(1)
{
/* if(s3==0) //mode选择开中断
{
delay10ms();
if(s3==0)
{
EA = 1;break;
}
}*/
if(s3==0)
{
delay10ms();
if(s3==0)
{
while(s3==0)
show_led();
move++;
if(move == 3) {move = 0;break;} //闹钟设置完成后回到主程序中
}
}
if(s5==0&&move==0) //秒钟设置
{
delay10ms();
if(s5==0)
{
while(s5==0)
show_led();
alarm_sec++;
if(alarm_sec==60)
{
alarm_sec = 0;
}
}
}
if(s2==0&&move==0)
{
delay10ms();
if(s2==0)
{
while(s2==0)
show_led();
alarm_sec--;
if(alarm_sec==-1)
{
alarm_sec = 59;
}
}
}
if(s5==0&&move==1) //分钟设置
{
delay10ms();
if(s5==0)
{
while(s5==0)
show_led();
alarm_min++;
if(alarm_min==60)
{
alarm_min = 0;
}
}
}
if(s2==0&&move==1)
{
delay10ms();
if(s2==0)
{
while(s2==0)
show_led();
alarm_min--;
if(alarm_min==-1)
{
alarm_min = 59;
}
}
}
if(s5==0&&move==2) //时钟设置
{
delay10ms();
if(s5==0)
{
while(s5==0)
show_led();
alarm_hour++;
if(alarm_hour==24)
{
alarm_hour = 0;
}
}
}
if(s2==0&&move==2)
{
delay10ms();
if(s2==0)
{
while(s2==0)
show_led();
alarm_hour--;
if(alarm_hour==-1)
{
alarm_hour = 23;
}
}
}
temp1 = d_code[alarm_hour/10];
temp2 = d_code[alarm_hour%10];
temp3 = d_code[alarm_min/10];
temp4 = d_code[alarm_min%10];
temp5 = d_code[alarm_sec/10];
temp6 = d_code[alarm_sec%10];
if(move == 2)
{
// temp1 = temp1|0x02;
temp2 = temp2|0x02;
}
if(move == 1)
{
// temp3 = temp3|0x02;
temp4 = temp4|0x02;
}
if(move == 0)
{
// temp5 = temp5|0x02;
temp6 = temp6|0x02;
}
show_led();
/* if(s4==0)
{
delay10ms();
if(s4==0)
{
while(s4==0);
break;
}
}*/
}
}
void show_led()
{
P0 = w_code[0];
wela = 1;
wela = 0;
P0 = temp1;
dula = 1;
dula = 0;
delay(17);
P0 = w_code[1];
wela = 1;
wela = 0;
P0 = temp2;
dula = 1;
dula = 0;
delay(17);
P0 = w_code[2];
wela = 1;
wela = 0;
P0 = temp3;
dula = 1;
dula = 0;
delay(17);
P0 = w_code[3];
wela = 1;
wela = 0;
P0 = temp4;
dula = 1;
dula = 0;
delay(17);
P0 = w_code[4];
wela = 1;
wela = 0;
P0 = temp5;
dula = 1;
dula = 0;
delay(17);
P0 = w_code[5];
wela = 1;
wela = 0;
P0 = temp6;
dula = 1;
dula = 0;
delay(17);
}