缘由用51单片机普中开发板实现数字时钟-嵌入式-CSDN问答
#include "reg52.h"
//定义按键
sbit key0=P3^0;
sbit key1=P3^1;
sbit key2=P3^2;
sbit key3=P3^3;
//定义数码管位驱运位
sbit L1=P2^2;
sbit L2=P2^3;
sbit L3=P2^4;
sbit beep=P2^5;
unsigned char code ShuMaGuan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F
,0x6F,0x00,0X80,64};//0~9隐.小数点-号
unsigned char Js=0, Miao=0, Fen=0, Shi=12;//中断计时 秒 分 时
void ShuMaXianShi(unsigned char s,unsigned char w,unsigned char d)
{unsigned char YanShi=0;if(w==2){L1=0;L2=L3=1;}//011else if(w==3){L2=0;L1=L3=1;}//101else if(w==4){L1=L2=0;L3=1;}//001else if(w==5){L1=L2=1;L3=0;}//110else if(w==6){L1=L3=0;L2=1;}//010else if(w==7){L1=1;L2=L3=0;}//100else if(w==8){L1=L2=L3=0;}//000else if(w==1){L1=L2=L3=1;}//111else {L1=L2=L3=0;}P0=ShuMaGuan[s]|ShuMaGuan[d];//d显示小数点11,10关闭while(++YanShi);P0=ShuMaGuan[10];L1=L2=L3=0;
}
void JiShi(/*时间计算*/)
{if(Js>=20){Js=0;if(++Miao>=60){Miao=0;++Fen;}if(Fen>=60){Fen=0;++Shi;}if(Shi>=24)Shi=0;}
}
void XianShi()
{ShuMaXianShi(Shi/10,1,10);ShuMaXianShi(Shi%10,2,10);ShuMaXianShi(12,3,10);ShuMaXianShi(Fen/10,4,10);ShuMaXianShi(Fen%10,5,10);ShuMaXianShi(12,6,10);ShuMaXianShi(Miao/10,7,10);ShuMaXianShi(Miao%10,8,10);
}
void ZhongDuanSheZhi()
{TH0+=0X4c;/*定时器赋初值,定时50ms触发中断,自动补偿方式*/TL0+=0X00; TMOD=0X01;TR0=1; ET0=1; //开启定时器EA=1; //全局中断开关
}
void ZhongDuan() interrupt 1
{++Js;TH0+=0X4c;TL0+=0X00;
}
void main()
{unsigned char H=0,L=0,Ys=0;ZhongDuanSheZhi();while(1){if(key0==0&&++Ys==0){Shi=(Shi?--Shi:23);}if(key1==0&&++Ys==0){Shi=(Shi>23?1:++Shi);}if(key2==0&&++Ys==0){Fen=(Fen?--Fen:59);}if(key3==0&&++Ys==0){Fen=(Fen>59?1:++Fen);}if(Fen==0&&Miao<9||Fen==30&&Miao<5)beep=~beep;XianShi();JiShi();}
}
#include "reg52.h"
//定义按键
sbit key0=P3^0;
sbit key1=P3^1;
sbit key2=P3^2;
sbit key3=P3^3;
//定义数码管位驱运位
sbit L1=P2^2;
sbit L2=P2^3;
sbit L3=P2^4;
sbit beep=P2^5;
unsigned char code ShuMaGuan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0X80,64};//0~9隐.小数点-号
unsigned char Js=0, Miao=0, Fen=0, Shi=12;//中断计时 秒 分 时
bit smk=0,bek=0;
void ShuMaXianShi(unsigned char s,unsigned char w,unsigned char d)
{unsigned char YanShi=0;if(smk){P0=L1=L2=L3=0;if(w==2){L1=0;L2=L3=1;}//011else if(w==3){L2=0;L1=L3=1;}//101else if(w==4){L1=L2=0;L3=1;}//001else if(w==5){L1=L2=1;L3=0;}//110else if(w==6){L1=L3=0;L2=1;}//010else if(w==7){L1=1;L2=L3=0;}//100else if(w==8){L1=L2=L3=0;}//000else if(w==1){L1=L2=L3=1;}//111else {L1=L2=L3=0;}P0=ShuMaGuan[s]|ShuMaGuan[d];//d显示小数点11,10关闭if(++YanShi>7)YanShi=smk=0;}
}
void XianShi()
{ShuMaXianShi(Shi/10,1,10);ShuMaXianShi(Shi%10,2,10);ShuMaXianShi(12,3,10);ShuMaXianShi(Fen/10,4,10);ShuMaXianShi(Fen%10,5,10);ShuMaXianShi(12,6,10);ShuMaXianShi(Miao/10,7,10);ShuMaXianShi(Miao%10,8,10);
}
void JiShi(/*时间计算*/)
{//if(Js%20==0)smk=1;if(Js==20){Js=0;if(++Miao==60){Miao=0;++Fen;}if(Fen==60){Fen=0;++Shi;}if(Shi==24)Shi=0;smk=1;bek=!bek;}XianShi();
}
void ZhongDuanSheZhi()
{TH0+=0X4c;/*定时器赋初值,定时50ms触发中断,自动补偿方式*/TL0+=0X00; TMOD=0X01;TR0=1; ET0=1; //开启定时器EA=1; //全局中断开关
}
void ZhongDuan() interrupt 1
{++Js;TH0+=0X4c;TL0+=0X00;
}
void main()
{unsigned char Ys=0;ZhongDuanSheZhi();while(1){if(key0==0&&++Ys==0){Shi=(Shi?--Shi:23);while(key0==0)JiShi();}if(key1==0&&++Ys==0){Shi=(Shi>23?0:++Shi);while(key1==0)JiShi();}if(key2==0&&++Ys==0){Fen=(Fen?--Fen:59);while(key2==0)JiShi();}if(key3==0&&++Ys==0){Fen=(Fen>59?0:++Fen);while(key3==0)JiShi();}if(bek&&(Fen==0&&Miao<9||Fen==30&&Miao<5))beep=~beep;else beep=0;JiShi();}
}
之上代码蜂鸣器周期约20MS之下蜂鸣器周期约0.5MS。