51單片機(jī)通過(guò)計(jì)時(shí)器實(shí)現(xiàn)倒計(jì)時(shí)
實(shí)驗(yàn)環(huán)境
軟件: Keil5+Proteus7
元件: AT89C51 * 1,7SEG-MPX2-CA * 1
仿真圖
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-564862.html
代碼實(shí)現(xiàn)
//名稱(chēng): 定時(shí)器實(shí)現(xiàn)數(shù)碼管倒計(jì)時(shí)
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
#define SEG P3
sbit SCON1 = P2^0;
sbit SCON2 = P2^1;
uchar num=15,ge,shi;
uchar count=0;
uchar flag;
//定義表格存放共陽(yáng)極LED的點(diǎn)陣碼
unsigned char code ledTab[]={
0xC0, // 1100 0000 數(shù)字0
0xF9, // 1111 1001 數(shù)字1
0xA4, // 1010 0100 數(shù)字2
0xB0, // 1011 0000 數(shù)字3
0x99, // 1001 1001 數(shù)字4
0x92, // 1001 0010 數(shù)字5
0x82, // 1000 0010 數(shù)字6
0xF8, // 1111 1000 數(shù)字7
0x80, // 1000 0000 數(shù)字8
0x90 // 1001 0000 數(shù)字9
};
void T0INTinit() //定時(shí)器T0初始化1秒
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void T0INT() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
flag++;
if(flag==20)//當(dāng)定時(shí)到1秒時(shí)執(zhí)行花括號(hào)里面的語(yǔ)句
{
flag=0;
num--;
if (num == 0)
num = 15;
}
}
//主程序
void main(){
T0INTinit();
while(1){ //死循環(huán)
SEG=0xff; //消影,不然只顯示一位
shi = num / 10;
ge = num % 10;
switch(count){
//通過(guò)余暉效應(yīng),雖然是不同時(shí)間,但人眼感覺(jué)是一起亮的
case 0: SCON1=0;SCON2=1;SEG=ledTab[ge];break;
case 1: SCON1=1;SCON2=0;SEG=ledTab[shi];break;
}
count++;
if(count>1) count=0;
}
}
成果圖
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-564862.html
到了這里,關(guān)于51單片機(jī)通過(guò)計(jì)時(shí)器實(shí)現(xiàn)倒計(jì)時(shí)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!