微控梦想

搜索:
您的位置: 首页 > 单片机 > » 正文

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

时间:   来源:嵌入网   作者:佚名   阅读:

/*-----------------------------------------------
功能:流水灯对称移动闪烁(双闪烁)

------------------------------------------------*/

#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--);    
}


/*-----------------------------------------------------------------

只循环一次,而没有一直循环下去,出错地方在:

通过添加一条测试语句:

                                                 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;
                                                 }

--------------------------------------------------------------*/


------------ ---------- ----------- ---------- --------- -------- ------- ------ ----- ---- --- -- -  -  -