当前位置:文档之家› 基于51单片机的各种花样的流水灯c51程序

基于51单片机的各种花样的流水灯c51程序

基于51单片机的各种花样的流水灯c51程序/*-----------------------------------------------功能:流水灯对称移动闪烁(双闪烁)------------------------------------------------*/#include<REG52.H>#define uint unsigned intvoid 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--);}/*----------------------------------------------------------------- 只循环一次,而没有一直循环下去,出错地方在:通过添加一条测试语句:if(comp1==0x7f){comp1=0xfe; comp2=0x80; P1=0x00; delay(30000);}发现if语句没有被执行,自然继续左右移动:1111 1111&1111 1111^0000 0000==11111 1111所以看起来是执行了一次while中的代码。

具体为什么不行,还不清楚……更正下列代码后,能够实现功能。

if(P1==0x7e){comp1=0xfe;comp2=0x80;}或者:if(comp2==0x01){comp1=0xfe;comp2=0x80;}--------------------------------------------------------------*/*********************************************/*-----------------------------------------------功能:流水灯(单向单闪烁)------------------------------------------------*/#include<reg52.h>#define uint unsigned intvoid delay(uint);main(){//uint fre=0x03;//uint comp1=0xfe,comp2=0x80;P1=0xfe;while(1){/*------------------------------------------------------------------模块1:循环单向闪烁,只有一个灯亮灭执行3次,转入下一种闪烁--------------------------------------------------------------------*/ while(1){delay(30000);P1<<=1;P1|=0x01;if(P1=0x7f){delay(30000);P1=0xfe;}}}}void delay(uint cnt){while(cnt--);}/*-----------------------------------------程序运行结果左右两端跳动,原因是:if(P1=0x7f);中的等号也成了赋值号,更正为if(P1==0x7f);特别注意,不要把判断语句种的等号误写为赋值号。

-----------------------------------------*/**************************************/*-----------------------------------------------功能:花样灯(单向单闪烁+单向双闪烁)------------------------------------------------*/#include<reg52.h>#define uint unsigned intvoid delay(uint);main(){uint fre=0x04;uint comp1=0xfe,comp2=0x80;while(1){/*-------------------------------------------------------------------模块1:循环单向闪烁,只有一个灯亮灭执行3次,转入下一种闪烁--------------------------------------------------------------------*/ P1=0xfe;while(1!=fre--){delay(30000);P1<<=1;P1|=0x01;if(P1==0x7f){delay(30000);P1=0xfe;}}/*-------------------------------------------------------------------模块2:循环单向闪烁,只有两个灯亮或者灭执行3次,转入下一种闪烁--------------------------------------------------------------------*/ P1=0xfc;while(3!=fre++){delay(30000);P1<<=2;P1|=0x03;if(P1==0x3f){delay(30000);P1=0xfc;}}}}void delay(uint cnt){while(cnt--);}/*----------------------------------------------------两个模块均没有问题,但是放在一起,并没有得到想要的结果,第一个循环没有进行完全,且两个循环的循环的次数与要求的不符。

错误地方在于:模块1和模块2的循环控制的只是一次亮灭,更正为:#include<reg52.h>#define uint unsigned intvoid delay(uint);main(){uint fre=0x04;uint fre1,fre2;uint comp1=0xfe,comp2=0x80;while(1){/*--------------------------------------------------------------------模块1:循环单向闪烁,只有一个灯亮灭执行3次,转入下一种闪烁--------------------------------------------------------------------------*/P1=0xfe;while(1!=fre--){fre1=0x08;while(1!=fre1--){delay(3000000);P1<<=1;P1|=0x01;if(P1==0x7f){delay(3000000);P1=0xfe;}}}/*-------------------------------------------------------------------模块2:循环单向闪烁,只有两个灯亮或者灭执行3次,转入下一种闪烁--------------------------------------------------------------------*/ 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()语句的出现,fre1和fre2的取值要比预想的减一。

----------------------------------------------------------*/。

相关主题