?? +
字號:
/*-----------------------------------------------
名稱:99秒倒計時器
內容:通過定時器倒計時
------------------------------------------------*/
#include<reg52.h> //包含頭文件,一般情況不需要改動,頭文件包含特殊功能寄存器的定義
code unsigned char tab[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//共陰數碼管 0-9
unsigned char Dis_Shiwei;//定義十位
unsigned char Dis_Gewei; //定義個位
/******************************************************************/
/* 延時函數 */
/******************************************************************/
void delay(unsigned int cnt)
{
while(--cnt);
}
/******************************************************************/
/* 主函數 */
/******************************************************************/
main()
{
TMOD |=0x01;//定時器設置 10ms in 12M crystal,工作在模式1,16位定時
TH0=0xd8;
TL0=0xf0;
IE= 0x82; //打開中斷
TR0=1; //打開定時開關
while(1)
{
P0=Dis_Shiwei;//顯示十位
P2=0;
delay(300); //短暫延時
P0=Dis_Gewei; //顯示個位
P2=1;
delay(300);
}
}
/******************************************************************/
/* 定時器中斷函數 */
/******************************************************************/
void tim(void) interrupt 1 using 1
{
static unsigned char second=99,count; //初值99
TH0=0xd8; //重新賦值
TL0=0xf0;
count++;
if (count==100)
{
count=0;
second--;//秒減1
if(second==0)
{ //這里添加定時到0的代碼 ,可以是燈電路,繼電器吸合等,或者執行一個程序
second=99; //減到0是重新賦值99
}
Dis_Shiwei=tab[second/10];//十位顯示值處理
Dis_Gewei=tab[second%10]; //個位顯示處理
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -