![]() |
發布時間: 2017-6-29 18:57
正文摘要:問題描述:流水燈只依次循環點亮一遍。我想讓它一直在循環點亮。 題目:如圖8-3所示,P1口外接8個LED燈,要求編程實現循環流水燈功能,時間間隔0.5s,單片機外接晶振12MHz。 (所用實現方法:用定時器T0實現精確定 ... |
本帖最后由 wulin 于 2017-6-30 21:10 編輯 兩個方法可以解決你的問題 方法1. #include < reg51.h> #include <intrins.h> unsigned char count=0,i=0; unsigned char s=0xfe; //主函數 main ( ) { P1=0xfe; TMOD=0x00; TH0=0x63; TL0=0x18; ET0=1; EA=1; TR0=1; while(1); } //服務函數 void Timer0_int( ) interrupt 1 { count++; if (count>=100) { count=0; P1=_crol_(P1,1);//P1循環左移一位 // P1=_cror_(P1,1);//P1循環右移一位 /* 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; //主函數 main ( ) { P1=0xfe; TMOD=0x00; TH0=0x63; TL0=0x18; ET0=1; EA=1; TR0=1; while(1); } //服務函數 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; } |
中斷內部有問題,自己推力一遍試試 |
在中斷的else敘述中多增加一個S=0xfe; |