標題:
單片機流水燈錯誤出在哪里?個位大神,請幫我看一下
[打印本頁]
作者:
dadahai
時間:
2017-6-29 18:57
標題:
單片機流水燈錯誤出在哪里?個位大神,請幫我看一下
問題描述:流水燈只依次循環(huán)點亮一遍。
我想讓它一直在循環(huán)點亮。
題目:如圖8-3所示,P1口外接8個LED燈,要求編程實現(xiàn)循環(huán)流水燈功能,時間間隔0.5s,單片機外接晶振12MHz。
(所用實現(xiàn)方法:用定時器T0實現(xiàn)精確定時0.5s,將0.5s分解為100個5000us,用定時器T0的方式0實現(xiàn)。)
00.png
(23.96 KB, 下載次數(shù): 53)
下載附件
2017-6-29 18:51 上傳
#include < reg51.h>
unsigned char count=0,i=0;
unsigned char s=0xfe;
//主函數(shù)
main ( )
{
P1=0xfe;
TMOD=0x00;
TH0=0x63;
TL0=0x18;
ET0=1;
EA=1;
TR0=1;
while(1);
}
//服務函數(shù)
void Timer0_int( ) interrupt 1
{
count++;
if (count==100)
{
count=0;
i++;
if(i<8)
{
s=s<<1;
s=s^0x01;
P1=s;
}
else
i=0;
}
TH0=0x63;
TL0=0x18;
}
作者:
Tomcute
時間:
2017-6-29 23:58
你中斷裡頭的i數(shù)到7的時候你的變數(shù)S是0x7F,也就是0111 1111(二進制),當你的i來到8的時候,你將你的i設為0,但是你的s是停留在0111 1111,所以下一次進中斷時,你的i會加1,你的s會先變成1111 1110,然後執(zhí)行s=s^0x01,之後就變成了1111 1111,再下一次進中斷還是先變成1111 1110, 再執(zhí)行行s=s^0x01,之後就變成了1111 1111,到這裡你就看的懂了吧!在else中除了將i歸0後,再補一個s=0xfe;的敘述看看吧!
作者:
Tomcute
時間:
2017-6-30 00:01
在中斷的else敘述中多增加一個S=0xfe;
作者:
凌辰
時間:
2017-6-30 15:27
中斷內(nèi)部有問題,自己推力一遍試試
作者:
wulin
時間:
2017-6-30 18:40
本帖最后由 wulin 于 2017-6-30 21:10 編輯
兩個方法可以解決你的問題
方法1.
#include < reg51.h>
#include <intrins.h>
unsigned char count=0,i=0;
unsigned char s=0xfe;
//主函數(shù)
main ( )
{
P1=0xfe;
TMOD=0x00;
TH0=0x63;
TL0=0x18;
ET0=1;
EA=1;
TR0=1;
while(1);
}
//服務函數(shù)
void Timer0_int( ) interrupt 1
{
count++;
if (count>=100)
{
count=0;
P1=_crol_(P1,1);//P1循環(huán)左移一位
// P1=_cror_(P1,1);//P1循環(huán)右移一位
/* i++;
if(i<8)
{
s=s<<1;
s=s^0x01;
P1=s;
}
else
i=0;*/
}
TH0=0x63;
TL0=0x18;
}
方法2;#include < reg51.h>
#include <intrins.h>
unsigned char count=0,i=0;
unsigned char s=0xfe;
//主函數(shù)
main ( )
{
P1=0xfe;
TMOD=0x00;
TH0=0x63;
TL0=0x18;
ET0=1;
EA=1;
TR0=1;
while(1);
}
//服務函數(shù)
void Timer0_int( ) interrupt 1
{
count++;
if (count>=100)
{
count=0;
i++;
s=s<<1;
s=s^0x01;
P1=s;
if(i>=8)
{
i=0;
P1=s=0xfe;
}
}
TH0=0x63;
TL0=0x18;
}
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1