C51單片機(jī)運(yùn)用定時(shí)器T0中斷實(shí)現(xiàn)電子時(shí)鐘
上面是電路原理圖
下面是程序源代碼:
/*****************************************************************************
功能描述:運(yùn)用定時(shí)器T0實(shí)現(xiàn)電子時(shí)鐘
**************************************************************************/
#include"reg52.h"
#include"intrins.h"
#define uint unsigned int
#define uchar unsigned char
sbit p32=P3^2;
sbit p33=P3^3;
uchar num;
uint number,number0,number1;
uchar code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
void tiaoz();
void delay()
{
uint b;
for(b=300;b>0;b--);
}
void time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num++;
//tiaoz();
}
//以下屏蔽內(nèi)容為 利用兩個(gè)外部中斷來調(diào)整“時(shí),分”
/*void tiaof() interrupt 0 //分針調(diào)整
{
if(p32==0)
{
delay();
if(p32==0)
{
number=0;
number0++;
while(!p32);
if(number0>=60)
number0=0;
}
}
}
void tiaos() interrupt 2 //時(shí)針調(diào)整
{
if(p33==0)
{
delay();
if(p33==0)
{
number=0;
number1++;
while(!p33);
if(number1>=24)
number1=0;
}
}
}
*/
//運(yùn)用獨(dú)立按鍵來調(diào)整“時(shí),分”
void tiaoz0()
{
if(p32==0)
{
delay();
if(p32==0)
{
number=0;
number0++;
while(!p32);
if(number0>=60)
number0=0;
}
}
if(p33==0)
{
delay();
if(p33==0)
{
number=0;
number1++;
while(!p33);
if(number1>=24)
number1=0;
}
}
}
評論