刚学的单片机,下面的程序是在学习中断和定时器时写的,已调试通过, 多功能电子钟有如下功能: 1,可以用来做电子钟 2,可以用做报警器(按P3.2时,用做报警) 3,可以用做闹钟(按P1.6时,用做闹钟) 4,可以显示星期 程度当中还有好多的不妥之处,还请高手门帮忙指正. 程序如下: (由于注解太多,所以没有能写出来,请见谅!!),制作成功后的图片如附件. 内容如下:
#include <AT89X51.H> #define uchar unsigned char #define uint unsigned int uchar code dispcode[]={0x81,0x9f,0xa4,0x8c,0x9a,0xc8,0xc0,0x9d,0x80,0x88}; uchar code DATA[]={0xfc,0x92,0x98,0xac,0x89,0x01,0x5c,0x80,0x88}; uchar dispbitcode[]={0xfe,0xfd,0xfb,0xf7}; uchar dispbuf[8]={0,0,0,0}; uchar dispbitcnt; uchar second,minite,hour,day; uchar clockhour,clockminite; uint tcnt; uchar mstcnt; uchar t; uint a; //上面的是定义各个变量和数码管缓存 void yanshi(uint t) { for(;t>0;t--); }//延时子程序
void beep() { while(hour<clockhour){ for(a=0;a<1000;a++) {yanshi(5); P1_4=~P1_4;} for(a=0;a<1000;a++) {yanshi(5); P1_4=~P1_4;} for(a=0;a<5000;a++) {yanshi(6); P1_4=~P1_4;} if(P3_6==0){break;}} while(minite<clockminite){ for(a=0;a<1000;a++) {yanshi(5); P1_4=~P1_4;} for(a=0;a<1000;a++) {yanshi(5); P1_4=~P1_4;} for(a=0;a<5000;a++) {yanshi(6); P1_4=~P1_4;} if(P3_6==0){break;}}}//报警子程序(P1.4接的是BEEP,P3.6为O时跳出)
void baojing() { if(P1_7==0){ yanshi(500); if(P1_7==0){ while(1){ if(P1_7==1){ while(1){ beep(); if(P3_6==0) {break;}}} if(P3_6==0){break;}}}}}//报警执行子程序(用P1.7做信号检测脚,同样当P3.6为0时跳出)
void clock() { if(P3_3==0){yanshi(500); if(P3_3==0){clockminite++; if(clockminite==60) {clockminite=0;} dispbuf[3]=clockminite%10; dispbuf[2]=clockminite/10; while(P3_3==0);}} if(P3_6==0){yanshi(500); if(P3_6==0){clockhour++; if(clockhour==24){clockhour=0;} dispbuf[1]=clockhour%10; dispbuf[0]=clockhour/10; while(P3_6==0);}}}//设定闹钟子程序
void key() { if(P3_6==0){yanshi(500); if(P3_6==0){second++; if(second==60){second=0;} while(P3_6==0);}} if(P3_5==0){yanshi(1000); if(P3_5==0){minite++; if(minite==60){minite=0;} dispbuf[3]=minite%10; dispbuf[2]=minite/10; while(P3_5==0);}} if(P3_7==0){yanshi(500); if(P3_7==0){hour++; if(hour==24){hour=0;} dispbuf[1]=hour%10; dispbuf[0]=hour/10; while(P3_7==0);}} if(P3_4==0){yanshi(500); if(P3_4==0){day++; if(day==7){day=0; } P2=DATA[day]; while(P3_4==0);}} }//设定时间子程序
void laozhong() { if(hour==clockhour){ if(minite==clockminite){ for(a=0;a<1000;a++) {yanshi(5); P1_4=~P1_4;} for(a=0;a<1000;a++) {yanshi(5); P1_4=~P1_4;} for(a=0;a<5000;a++) {yanshi(6); P1_4=~P1_4;}}}}//闹钟子程序
void display() { dispbuf[3]=minite%10; dispbuf[2]=minite/10; dispbuf[1]=hour%10; dispbuf[0]=hour/10; P2=DATA[day]; }//显示子程序
void displayclock() { dispbuf[7]=clockminite%10; dispbuf[6]=clockminite/10; dispbuf[5]=clockhour%10; dispbuf[4]=clockhour/10; }//闹钟显示子程序
void time() { tcnt++; if(tcnt==4000){tcnt=0; second++; if(second==60){second=0; minite++; if(minite==60){minite=0; hour++; if(hour==24){hour=0; day++; if(day==7){day=0;}}}} display();}}//时间子程序
void t0(void) interrupt 1 using 0 { mstcnt++; if(mstcnt==4){mstcnt=0; P0=dispcode[dispbuf[dispbitcnt]]; P1=dispbitcode[dispbitcnt]; P2=DATA[day]; dispbitcnt++;} if(dispbitcnt==4){dispbitcnt=0;} time();displayclock();}//中断
void main(void) { TMOD=0x02; TH0=0x06; TL0=0x06; TR0=1; ET0=1; EA=1; while(1) { key();clock(); if(P1_6==0) {while(1){laozhong(); if(P3_6==0){break;}}} if(P3_2==0){yanshi(10000); if(P3_2==0){while(1) {baojing(); if(P3_6==0) {break;} }}}}} //S1:启动报警器(P3.2) //S2: clock minite 加(P3.3) //S3: 星期加(P3.4) //S4:time minite 加(P3.5) //S5: clock hour 加/退出报警器(P3.6) //S6:time hour 加(P3.7) //P1-7为信号检测脚
[此贴子已经被作者于2009-6-9 9:40:09编辑过]
|