|
#include<reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit addr0 = P1^0;
sbit addr1 = P1^1;
sbit addr2 = P1^2;
sbit beep=P1^5;
u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//顯示0~F的值
u8 LedBuff[3]= {
//數(shù)碼管的顯示緩沖區(qū),初值0x00確保啟動(dòng)時(shí)都不亮
0x00,0x00,0x00
};
u8 flag1s = 0;
u16 cnt = 0;
u8 i = 0;
void delay(u16 j)
{
while(j--);
}
void Timer0Init(void);
void main()
{
u8 sec = 24; //記錄經(jīng)過的秒數(shù)
Timer0Init();
while(1)
{
if (flag1s == 1) //判斷 1 秒定時(shí)標(biāo)志
{
flag1s = 0; //1 秒定時(shí)標(biāo)志清零
sec--; //秒計(jì)數(shù)自加 1
//以下代碼將 sec 按十進(jìn)制位從低到高依次提取并轉(zhuǎn)為數(shù)碼管顯示字符
LedBuff[2] = smgduan[sec%10];
LedBuff[1] = smgduan[sec/10%10];
LedBuff[0] = smgduan[sec/100%10];
if(sec == 0)
{
sec = 24;
beep=~beep;
delay(10);
}
}
}
}
void Timer0Init() //1毫秒@11.0592MHz
{
TMOD &= 0xF0; //設(shè)置定時(shí)器模式
TMOD |= 0x01; //設(shè)置定時(shí)器模式
TL0 = 0x66; //設(shè)置定時(shí)初值
TH0 = 0xFC; //設(shè)置定時(shí)初值
TF0 = 0; //清除TF0標(biāo)志
TR0 = 1; //定時(shí)器0開始計(jì)時(shí)
EA = 1;
ET0 = 1;
}
void InterruptTimer0() interrupt 1
{
static u8 i = 0;
TH0 = 0xfc;
TL0 = 0x67;
cnt++;
if(cnt == 1000)
{
cnt = 0;
flag1s = 1;
}
P0 = 0x00;
P1 = (0xf8 & P1) | i;
P0 = LedBuff[i];
if(i < 3)
{
i++;
}
else
i = 0;
}
|
|