標(biāo)題:
單片機(jī)數(shù)字鐘計(jì)時(shí)倒計(jì)時(shí)準(zhǔn)點(diǎn)報(bào)時(shí)程序
[打印本頁(yè)]
作者:
493527108
時(shí)間:
2020-6-8 17:24
標(biāo)題:
單片機(jī)數(shù)字鐘計(jì)時(shí)倒計(jì)時(shí)準(zhǔn)點(diǎn)報(bào)時(shí)程序
1234.JPG
(201.99 KB, 下載次數(shù): 19)
下載附件
2020-6-8 17:23 上傳
#include "reg51.h"
typedef unsigned int u16;
typedef unsigned char u8;
//
sbit naodeng=P2^0;
sbit K1=P3^0;
sbit K2=P3^2;
sbit K3=P3^3;
sbit K4=P2^3;
sbit K5=P2^4;
sbit K6=P2^5;
sbit K7=P2^6;
sbit beep=P2^7;
//
void shijian();
void Timer0Init();
void Timer1Init();
void DigDisplay();
void alarm();
void tiao_nao();
void moshi();
void delay(u16 i);
//
bit nao;
bit ji;
u8 a=0;
u8 display[]={0,0,0,0,0,0};
u8 ssec,sec,min,hour,nao_hour=0,nao_min=0;ji_hour=0,ji_min=0;ji_sec=0;
//
void delay(u16 i)
{
while(i--);
}
//定時(shí)器中斷初始化
void Timer0Init()
{
TMOD|=0X01;
EX0=1;
IT0=1;
PX0=1;
EX1=1;
IT1=0;
TH0=0Xd8;
TL0=0Xf0;
ET0=1;
EA=1;
TR0=1;
}
void Timer1Init()
{
ET0=0;
TMOD|=0X10;
EX0=1;
IT0=1;
PX0=1;
EX1=1;
IT1=0;
TH0=0Xd8;
TL0=0Xf0;
ET1=1;
EA=1;
TR1=1;
}
//6位LED顯示
void DigDisplay()
{
u8 i,j,m,temp;
u8 led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
temp=0x01;
for(i=0;i<6;i++)
{
P1=0x00;
j=display[i];
P1=led[j];
P0=~temp;
temp<<=1;
for(m=0;m<100;m++);
}
}
//報(bào)警函數(shù)
void alarm()
{
if(nao_hour==hour&&nao_min==min&&sec>=0&&sec<20&&nao==1)
{
beep=1;
delay(5);
beep=0;
delay(5);
}
}
//整點(diǎn)
void bee()
{
if(min==0&&sec>=0&&sec<2)
{
beep=1;
delay(5);
beep=0;
delay(5);
}
}
//鬧鐘時(shí)間調(diào)節(jié)
void tiao_nao()
{
IT0=0;
EX1=0;
EX0=0;
delay(10);
while(K1)
{
if(K2==0)
{delay(10);if(K2==0)nao_hour++;while(!K2);}
if(nao_hour>=24)nao_hour=0;
if(K3==0)
{delay(10);if(K3==0)nao_min++;while(!K3); }
if(nao_min>=60)nao_min=0;
display[5]=0;
display[4]=0;
display[3]=nao_min%10;
display[2]=nao_min/10;
display[1]=nao_hour%10;
display[0]=nao_hour/10;
DigDisplay();
}
IT0=1;
EX1=1;
EX0=1;
ET0=1;
ET1=0;
nao=1;
}
//鬧鐘功能
void moshi()
{
if(K1==0)
{
delay(100);
if(K1==0)
{
a++;
if(a>=2)a=0;
while(!K1);
switch(a)
{
case(0):nao=~nao;break;
case(1):tiao_nao();break;
}
}
} while(!K1);
}
//時(shí)間顯示
void shijian()
{
display[5]=sec%10;
display[4]=sec/10;
display[3]=min%10;
display[2]=min/10;
display[1]=hour%10;
display[0]=hour/10;
DigDisplay();
}
//計(jì)時(shí)
void tiao_ji()
{
IT0=0;
EX1=0;
EX0=0;
delay(10);
while(K4)
{
if(K5==0)
{delay(10);if(K5==0)ji_hour++;while(!K5);}
if(ji_hour>=24)ji_hour=0;
if(K6==0)
{delay(10);if(K6==0)ji_min++;while(!K6); }
if(ji_min>=60)ji_min=0;
if(K7==0)
{delay(10);if(K7==0)ji_sec++;while(!K7); }
if(ji_sec>=60)ji_sec=0;
display[5]=ji_sec%10;
display[4]=ji_sec/10;
display[3]=ji_min%10;
display[2]=ji_hour/10;
display[1]=ji_hour%10;
display[0]=ji_hour/10;
DigDisplay();
}
IT1=1;
EX1=1;
EX0=1;
ET0=0;
ET1=1;
ji=1;
hour=ji_hour;
min=ji_min;
sec=ji_sec;
}
//計(jì)時(shí)功能
void jishi()
{
if(K4==0)
{
delay(100);
if(K4==0)
{
Timer1Init();
a++;
if(a>=3)a=0;
while(!K4);
switch(a)
{
case(0):ji=~ji;EA=1; ET0=1;ET1=0; break;
case(1):tiao_ji(); break;
case(2):; break;
}
}
} while(!K4);
}
//
void shiji()
{
display[5]=sec%10;
display[4]=sec/10;
display[3]=min%10;
display[2]=min/10;
display[1]=hour%10;
display[0]=hour/10;
DigDisplay();
}
//T0中斷
void Timer0() interrupt 1
{
TH0=0Xd8;
TL0=0Xf0;
ssec++;
if(ssec>=100)
{
ssec=0;
sec++;
if(sec>=60)
{
sec=0;
min++;
if(min>=60)
{
min=0;
hour++;
if(hour>=24)
{hour=0;
}
}
}
}
}
void Timer1() interrupt 3
{
TH0=0Xd8;
TL0=0Xf0;
ssec++;
if(ssec>=100)
{
ssec=0;
sec--;
if(sec<=0)
{
if(min>0)
{
min--;
sec=60;
}
else
{
if(hour>0)
{
hour--;
min=60;
sec=60;
}
else
{sec=0;
beep=1;
delay(5);
}
}
}
}
}
//外部中斷0
void int0() interrupt 0
{
delay(10);
hour++;
if(hour>=24)hour=0;
}
//外部中斷1
void int1() interrupt 2
{
min++;
if(min==60) min=0;
while(!K3);
}
//main函數(shù)
void main()
{
Timer0Init();
while(1)
{
if(nao==1)naodeng=0;
else naodeng=1;
shijian();
shiji();
moshi();
alarm();
bee();
jishi();
}
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1