一個(gè)鬧鐘的單片機(jī)程序
代碼如下:
//File: Clock.c
#include REGX52.H>
const unsigned char LED7Code[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned int hour,minute,second,cHour,cMinute;
bit isClick = 0;
unsigned char modify =0x00;//是否處于時(shí)間編輯狀態(tài)
bit isClock =0;//是否開啟鬧鐘
unsigned char timercount;
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
timercount++;
if(timercount==20)
{
timercount=0;
second++;
if(second==60)
{
minute++;
second=0;
}
if(minute==60)
{
hour++;
minute=0;
}
if(hour==24)
{
hour=0;
}
}
}
//延遲
void Delay(unsigned int time)
{
unsigned char a,b;
for(a=0;atime;a++)
for(b=0;b120;b++);
}
//用于顯示時(shí)間
void ShowTime(void)
{
unsigned int a,b, tempH,tempM;
unsigned char showChar;
if(modify==0x00 || modify ==0x01 || modify ==0x02)
{
tempH=hour;
tempM=minute;
}
else
{
tempH=cHour;
tempM=cMinute;
}
if(modify==0x00 || modify ==0x01 || modify ==0x03)
{
//顯示第4個(gè)數(shù)碼管
P0=0xFF;
P2=0xFF;
a=tempH/10;
showChar=LED7Code[a];
P0=showChar;
P2_3=0x00;
Delay(1);//延遲很短
//顯示第3個(gè)數(shù)碼管
P0=0xFF;
P2=0xFF;
b=tempH%10;
showChar=LED7Code[b];
P2_2=0x00;
P0=showChar;
Delay(1);//延遲很短
}
if(modify==0x00 || modify ==0x02 || modify ==0x04)
{
//顯示第2個(gè)數(shù)碼管
P0=0xFF;
P2=0xFF;
a=tempM/10;
showChar=LED7Code[a];
P0=showChar;
P2_1=0x00;
Delay(1);//延遲很短
//顯示第1個(gè)數(shù)碼管
P0=0xFF;
P2=0xFF;
b=tempM%10;
showChar=LED7Code[b];
P2_0=0x00;
P0=showChar;
Delay(1);//延遲很短
}
}
//蜂鳴器響
void Beep()
{
unsigned char i ;
for (i=0 ;i100 ;i++)
{
Delay(100) ;
P3_7=!P3_7 ; //Beep取反
}
P3_7=1 ; //關(guān)閉蜂鳴器
Delay(100);
}
void main(void)
{
unsigned int i;
hour=12;
minute=30;
second=0;
cHour=7;
cMinute=0;
TMOD=0x01;
ET0=0x01;//定時(shí)器T0可以中斷
EA=0x01;//表示允許所有的中斷
TR0=0x01;//開始T0計(jì)時(shí)
while(1)
{
if(P1_4==0x00)//按下了K1鍵,增加小時(shí)或分鐘
{
if(!isClick)
{
switch(modify)
{
case 0x01://調(diào)整小時(shí)
hour++;
if(hour==24)
hour=0;
break;
case 0x02://調(diào)整分鐘
minute++;
if(minute==60)
minute=0;
break;
case 0x03://調(diào)整鬧鐘的小時(shí)
cHour++;
if(cHour==24)
cHour=0;
break;
case 0x04://調(diào)整鬧鐘的分鐘
cMinute++;
if(cMinute==60)
cMinute=0;
break;
default:
break;
}
isClick=1;
}
}
else if(P1_5==0x00)//按下了K2鍵,減少小時(shí)或分鐘
{
if(!isClick)
{
switch(modify)
{
case 0x01://調(diào)整時(shí)間的小時(shí)
if(hour==0)
hour=23;
else
hour--;
break;
case 0x02://調(diào)整時(shí)間的分鐘
if(minute==0)
minute=59;
else
minute--;
break;
case 0x03://調(diào)整鬧鐘的小時(shí)
if(cHour==0)
cHour=23;
else
cHour--;
break;
case 0x04://調(diào)整鬧鐘的分鐘
if(cMinute==0)
cMinute=59;
else
cMinute--;
break;
default:
break;
}
isClick=1;
}
}
else if(P1_6==0x00)//按下了K3鍵,更改模式,第一次修改小時(shí),第二次修改分鐘,第三次恢復(fù)
{
if(!isClick)
{
switch(modify)
{
case 0x00://不是修改狀態(tài),調(diào)整小時(shí)
modify=0x01;
break;
case 0x01://調(diào)整分鐘
modify=0x02;
break;
case 0x02://調(diào)整鬧鐘的小時(shí)
modify=0x03;
break;
case 0x03://調(diào)整鬧鐘的分鐘
modify=0x04;
break;
case 0x04://不修改
modify=0x00;
break;
default:
break;
}
isClick=1;
}
}
else if(P1_7==0x00)//按下了K4鍵,開啟或關(guān)閉鬧鐘
{
if(!isClick)
{
isClock=~isClock;
isClick=1;
}
}
else
isClick=0;
//鬧鐘是否響了
if(isClock)
{
if(hour==cHour minute == cMinute)
{
Beep();//響
}
}
ShowTime();
}
}
評論