今天剛剛完成了一個新的單片機實驗,犯了一個小小的錯誤,導致不能夠倒計時,弄了幾個小時, 結果發現是變量的取值范圍出錯了
任何一個實驗都應該自己動手去實踐,沒有實踐是不知道自己少了什么的。只有實踐才能夠獲得更多的調試經驗!
程序雖小,但只要實際去做了,就能獲得不少的經驗! 這就是做復雜程序的基礎!
共享自己寫的程序,畢竟是自己勞動了幾個小時的結果!與大家分享!
程序太小了,就沒有多少注釋了
/******************************************************
項目名稱:99秒倒計時
時間:2015-7-9
目的:使用51單片機的定時器1實現99秒倒計時
單片機平臺:KST51單片機開發平臺-金沙灘單片機
注意:unsigned char 的取值范圍為0-255
unsigned char 的取值范圍為0-65535
要結合單片機的硬件來看程序
**********************************************************/
#include<reg52.h>
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
unsigned char sec = 99;
unsigned char code LedChar[] ={
0xC0,0xF9,0xA4,0xB0,0x99, 0x92,0x82,0xF8,
0x80,0x90,0X88,0x83,0XC6,0xA1,0x86,0x8E
};
unsigned char LedBuff[6] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
void LedScan() //´Ëº¯ÊýʵÏÖLedµÄ¶¯Ì¬É¨Ãè
{
static unsigned char i = 0;
P0 = 0xFF;
P1 = (P1 & 0xF8)| i;
P0 = LedBuff[i];
if(i < 2)
i++;
else
i = 0;
}
void main()
{
ENLED = 0;
ADDR3 = 1;
//¶¨Ê±Æ÷¼Ä´æÆ÷³õʼ»¯
TMOD = 0X01;
TH0 = 0xFC; //1 ms¶¨Ê±
TL0 = 0x67;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
LedBuff[0] = LedChar[sec % 10];
LedBuff[1] = LedChar[sec/10 %10];
}
}
//ÖжϷþÎñº¯Êý
void InterruptTimer0() interrupt 1
{
static unsigned int cnt = 0;
//Range of "unsigned char" is 0 to 255
//Range of "unsigned int" is 0 to 65535
TH0 = 0xFC;
TL0 = 0x67;
cnt++;
LedScan();
if(cnt >= 1000)
{
cnt = 0;
sec--;
if(sec < 0)
{
sec = 99;
}
}
}
|