/*----------------------------------------------- 功能:流水燈對稱移動閃爍(雙閃爍) 作者:fei yu 日期:2010-6-14 ------------------------------------------------*/ #include<REG52.H> #define uint unsigned int void delay(uint); main() { uint comp1=0xfe; uint comp2=0x80; P1=0x7e; delay(30000); while(1) { P1=0xff; comp1<<=1; comp1|=0x01; comp2>>=1; P1&=comp1; P1^=comp2; delay(30000); if(P1==0xe7) { comp1<<=1; comp1|=0x01; comp2>>=1; } if(comp1==0x7f) { comp1=0xfe; comp2=0x80; } } } void delay(uint cnt) { while(cnt--); } /*----------------------------------------------------------------- 只循環(huán)一次,而沒有一直循環(huán)下去,出錯地方在: 通過添加一條測試語句: if(comp1==0x7f) { comp1=0xfe; comp2=0x80; P1=0x00; delay(30000); } 發(fā)現(xiàn)if語句沒有被執(zhí)行,自然繼續(xù)左右移動: 1111 1111&1111 1111^0000 0000==11111 1111 所以看起來是執(zhí)行了一次while中的代碼。 具體為什么不行,還不清楚…… 更正下列代碼后,能夠?qū)崿F(xiàn)功能。 if(P1==0x7e) { comp1=0xfe; comp2=0x80; } 或者: if(comp2==0x01) { comp1=0xfe; comp2=0x80; } --------------------------------------------------------------*/ ************************************************************************************************************************************** /*----------------------------------------------- 功能:流水燈(單向單閃爍) 作者:fei yu 日期:2010-6-14 ------------------------------------------------*/ #include<reg52.h> #define uint unsigned int void delay(uint); main() { //uint fre=0x03; //uint comp1=0xfe,comp2=0x80; P1=0xfe; while(1) { /*------------------------------------------------------------------ 模塊1:循環(huán)單向閃爍,只有一個燈亮滅 執(zhí)行3次,轉(zhuǎn)入下一種閃爍 --------------------------------------------------------------------*/ while(1) { delay(30000); P1<<=1; P1|=0x01; if(P1=0x7f) { delay(30000); P1=0xfe; } } } } void delay(uint cnt){while(cnt--);} /*----------------------------------------- 程序運行結(jié)果左右兩端跳動,原因是: if(P1=0x7f);中的等號也成了賦值號,更正為if(P1==0x7f); 特別注意,不要把判斷語句種的等號誤寫為賦值號。 -----------------------------------------*/ ************************************************************************************************************************************** /*----------------------------------------------- 功能:花樣燈(單向單閃爍+單向雙閃爍) 作者:fei yu 日期:2010-6-14 ------------------------------------------------*/ #include<reg52.h> #define uint unsigned int void delay(uint); main() { uint fre=0x04; uint comp1=0xfe,comp2=0x80; while(1) { /*------------------------------------------------------------------- 模塊1:循環(huán)單向閃爍,只有一個燈亮滅 執(zhí)行3次,轉(zhuǎn)入下一種閃爍 --------------------------------------------------------------------*/ P1=0xfe; while(1!=fre--) { delay(30000); P1<<=1; P1|=0x01; if(P1==0x7f) { delay(30000); P1=0xfe; } } /*------------------------------------------------------------------- 模塊2:循環(huán)單向閃爍,只有兩個燈亮或者滅 執(zhí)行3次,轉(zhuǎn)入下一種閃爍 --------------------------------------------------------------------*/ P1=0xfc; while(3!=fre++) { delay(30000); P1<<=2; P1|=0x03; if(P1==0x3f) { delay(30000); P1=0xfc; } } } } void delay(uint cnt) { while(cnt--); } /*---------------------------------------------------- 兩個模塊均沒有問題,但是放在一起,并沒有得到想要的結(jié)果, 第一個循環(huán)沒有進行完全,且兩個循環(huán)的循環(huán)的次數(shù)與要求的不符。 錯誤地方在于:模塊1和模塊2的循環(huán)控制的只是一次亮滅,更正為: #include<reg52.h> #define uint unsigned int void delay(uint); main() { uint fre=0x04; uint fre1,fre2; uint comp1=0xfe,comp2=0x80; while(1) { /*-------------------------------------------------------------------- 模塊1:循環(huán)單向閃爍,只有一個燈亮滅 執(zhí)行3次,轉(zhuǎn)入下一種閃爍 --------------------------------------------------------------------------*/ P1=0xfe; while(1!=fre--) { fre1=0x08; while(1!=fre1--) { delay(3000000); P1<<=1; P1|=0x01; if(P1==0x7f) { delay(3000000); P1=0xfe; } } } /*------------------------------------------------------------------- 模塊2:循環(huán)單向閃爍,只有兩個燈亮或者滅 執(zhí)行3次,轉(zhuǎn)入下一種閃爍 --------------------------------------------------------------------*/ P1=0xfc; while(3!=fre++) { fre2=0x04; while(1!=fre2--) { delay(3000000); P1<<=2; P1|=0x03; if(P1==0x3f) { delay(3000000); P1=0xfc; } } } } } void delay(uint cnt) { while(cnt--); } 注意控制語句中fre++和fre--;并且fre1和fre2的初始化不能出錯, 由于if()語句的出現(xiàn),fre1和fre2的取值要比預想的減一。 ----------------------------------------------------------*/