![]() |
發(fā)布時間: 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)精確定 ... |
本帖最后由 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); } //服務(wù)函數(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); } //服務(wù)函數(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; } |
中斷內(nèi)部有問題,自己推力一遍試試 |
在中斷的else敘述中多增加一個S=0xfe; |
你中斷裡頭的i數(shù)到7的時候你的變數(shù)S是0x7F,也就是0111 1111(二進制),當(dāng)你的i來到8的時候,你將你的i設(shè)為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;的敘述看看吧! |
參與人數(shù) 1 | 黑幣 +60 | 收起 理由 |
---|---|---|
![]() | + 60 | 回帖助人的獎勵! |
Powered by 單片機教程網(wǎng)