|
#include <reg52.h>
#define uchar unsigned char
sbit p11=P1^1; //連的是繼電器。。
code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar shiwei;
uchar gewei;
void delay(unsigned int cnt)
{
while(--cnt);
}
void main()
{
TMOD|=0x01; /*定時(shí)器0 16位定時(shí)器 X=65535-10000(10毫秒)=55535=D8F0(十六進(jìn)制)定時(shí)10ms
*/
TH0=0xd8;
TL0=0xf0;
IE=0x82; //這里是中斷優(yōu)先級(jí)控制EA=1(開總中斷),ET0=1(定時(shí)器0允許中斷),這里用定時(shí)器0來(lái)定時(shí)
TR0=1; //開定時(shí)器0
while(1)
{
P0=shiwei; //99的十位
P2=0; //99的個(gè)位,
delay(300); //動(dòng)態(tài)掃描數(shù)碼管延時(shí)
P0=gewei;
P2=1;
delay(300);
}
}
void tim(void) interrupt 1 using 1 //定時(shí)器0中斷
{
static uchar second=99,count; //99只是一個(gè)數(shù),可以任意改,因?yàn)檫@里只學(xué)習(xí)怎樣實(shí)現(xiàn)倒計(jì)時(shí)
TH0=0xd8; //定時(shí)10毫秒
TL0=0xf0;
count++;
if(count==100) //10毫秒定時(shí),10*100=1000(毫秒)=1秒
{
count=0;
second--;
if(second==0)
{
p11=0; //這里讓繼電器動(dòng)作,當(dāng)然動(dòng)作之后,要復(fù)位才能等下次倒定時(shí)再動(dòng)作。
second=99; //回到99再循環(huán)來(lái),當(dāng)然,可以做其他的控制,
}
shiwei=tab[second/10]; //數(shù)碼管10位
gewei=tab[second%10]; //數(shù)碼管個(gè)位
} |
|