中的第壹個和第三個功能有的,至於第2個,程序裏是壹直倒計時,結束了
就再重置30,繼續倒計時。
如果妳需要鍵盤按鍵的話,只需要按鍵將中斷關了或者開了,就可以。
#include <reg52.h>
code unsigned char tab[]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//***陽數碼管 0-9
unsigned char Dis_Shiwei;//定義十位
unsigned char Dis_Gewei; //定義個位
void delay(unsigned int cnt)
{
while(--cnt);
}
main()
{
TMOD |=0x01;//定時器設置 10ms in 12M crystal
TH0=0xd8; // 初值55536,計數10000次,每次1us,總計10ms
TL0=0xf0;
IE= 0x82; //打開中斷
TR0=1; //啟動計數器TR0
while(1)
{
P0=Dis_Shiwei;//顯示十位
P2=0xFD; // P2=1111 1101
delay(300);//短暫延時
P0=Dis_Gewei; //顯示個位
P2=0xFE; // P2=1111 1110
delay(300);
}
}
/********************************/
/* 定時中斷 */
/********************************/
void tim(void) interrupt 1 using 1 //前1為定時中斷TR0
{
static unsigned char second=30,count; //初值30
TH0=0xd8;//重新賦值
TL0=0xf0;
count++;
if (count==100)
{
count=0;
second--;//秒減1
if(second==0)
{ //這裏添加定時到0的代碼 ,可以是燈電路,繼電器吸合等,或者執行壹個程序
second=30; //減到0是重新賦值99
}
Dis_Shiwei=tab[second/10];//十位顯示值處理
Dis_Gewei=tab[second%10]; //個位顯示處理
}
}