51单片机实现电子时钟代码
生活随笔
收集整理的這篇文章主要介紹了
51单片机实现电子时钟代码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#include<reg52.h>
sbit key1=P3^1;
sbit key2=P3^0;
sbit key3=P3^2;
sbit key4=P3^3;
sbit a=P2^2; //位選聲明
sbit b=P2^3;
sbit c=P2^4;
void delay(int);
void zhongduan();
void display(int,int,int,int,int,int);
void keyscan(); //獨(dú)立按鍵
char num[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
char num2[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//帶點(diǎn)數(shù)據(jù)
int shu1,shu2,shu3,shu4;//
int ge,shi,fen1,fen2,hour1,hour2;
int main(){
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
//TH1=(65536-45872)/256;
//TL1=(65536-45872)%256;
EA=1;
ET0=1;
//ET1=1;
TR0=1;
//TR1=1;
while(1) { //顯示
display(ge,shi,fen1,fen2,hour1,hour2);
keyscan();
}
return 0;
}
void delay(int x){//timer
int i,j;
for(i=x;x>0;x--)
for(j=110;j>0;j--);
}
void zhongduan()interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
shu2++;
if(shu2==20){ //1s
shu2=0;
shu1++;
if(shu1==60){
shu3++;
shu1=0;
}
shi=shu1/10;
ge=shu1%10;
if(shu3==60){
shu4++;
shu3=0;
}
fen1=shu3%10;
fen2=shu3/10;
if(shu4==24){
shu4=0;
}
hour1=shu4%10;
hour2=shu4/10;
}//第一個if
}//函數(shù)void display(int ge,int shi,int fen1,int fen2,int hour1,int hour2){
P0=num[ge]; //段選 秒個位
a=0; //位選
b=0;
c=0;
delay(1);P0=num[shi]; //段選 秒十位
a=1; //位選
b=0;
c=0;
delay(1);P0=num2[fen1]; //段選 分個位
a=0; //位選
b=1;c=0;
delay(1);P0=num[fen2]; //段選 分十位
a=1; //位選
b=1;
c=0;
delay(1);P0=num2[hour1]; //段選 時個位
a=0; //位選
b=0;
c=1;
delay(1);P0=num[hour2]; //段選 時十位
a=1; //位選
b=0;
c=1;
delay(1);
}//fd=16*16^1+13*16^0=256+13=269=>11111101
void keyscan(){
if(key1==0){
delay(5);
if(key1==0){
shu4++;
if(shu4==24)
shu4=0;
while(!key1);//等待按鍵釋放}
}
if(key2==0)
{
delay(5);
if(key2==0)
{
shu3++;
if(shu4>=24)
shu3=0;
while(!key2);//
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
shu1+=10;
if(shu1>=60)
shu1=0;
while(!key3);
}
}if(key4==0)
{
delay(5);
if(key4==0)
{
while(!key4)
TR0=~TR0;
}
}//return 0;
}
總結(jié)
以上是生活随笔為你收集整理的51单片机实现电子时钟代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 科学绘图经典--Grapher
- 下一篇: CTF之MISC套路