標題: 51單片機流水燈多種方法實現(xiàn) [打印本頁]

作者: 彬bin    時間: 2019-4-28 22:27
標題: 51單片機流水燈多種方法實現(xiàn)


原代碼:


#include<reg52.h>
#include<intrins.h>//字符循環(huán)移動函數(shù)庫
void delays()//延遲
{        
        int x,y;
           for(x=0;x<110;++x)        
        for(y=0;y<1000;++y);
}
         
void delayms(unsigned int xms)//x ms延遲
{
          unsigned int c,h;
          for(c = 0; c<xms; ++c)
        for(h = 0; h<110; ++h);
}


void main()
{        
        //從左到右,再從右到左的流水燈
        unsigned char j;
        unsigned char k;
        unsigned char temp;
        int i=0;
        P2 = 0xff;
        delays();
        temp=0x7f;


        for(k=0;k<7;k++)
        {        
                P2=temp;
                 delays();
                 temp=_cror_(temp,1);        //字符循環(huán)右移實現(xiàn)右移        
        }


        for(j=0;j<8;j++)
        {
                P2=temp;
                 delays();
                 temp=_crol_(temp,1);        //字符循環(huán)左移實現(xiàn)左移        
        }
        
        /*另外一種思路,用移位運算符實現(xiàn)
        unsigned char a,i;
        a=0xfe;
        for(i=0;i<7;i++)
        {
                P2=a;
                delays();
                a=a<<1;//1移動,其他補零
                a=a|0x01;//左移一位后與0x01相或(只要滿足一個條件)        0000 0001
                // //或a=a|0x00;逐個點亮
        }
        a=0x7f;
        for(i=0;i<7;i++)
        {
                P2=a;
                delays();
                a=a>>1;//1移動,其他補零
                a=a|0x80;//右移一位后與0x80相或(只要滿足一個條件)        1000 0000
                // //或a=a|0x00;逐個點亮
        }*/


        /*用數(shù)組實現(xiàn)從兩側(cè)到中間,再從中間到兩側(cè)流水燈
        int e;
        int i=0;
        unsigned char d[]={0x7e,0xbd,0xdb,0xe7};//定義亮燈熄燈數(shù)組


        for(e=0;e<3;e++)                         //兩側(cè)到中間
        {        
                P2=d[e];
                delays();
        }


        for(e=3;e>=0;e--)                         //中間到兩側(cè)
        {
                P2=d[e];
                delays();
        }*/
}

新手上路,請多指教,謝謝!










歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1