專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

單片機(jī)控制LED流水燈從中間向兩邊,從兩邊向中間

作者:佚名   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2008年09月20日   【字體:

  這個(gè)就是把 先奇數(shù)亮再偶數(shù)亮,循環(huán)三次;一個(gè)燈上下循環(huán)三次;兩個(gè)分別從兩邊往中間流動(dòng)三次;再從中間往兩邊流動(dòng)三次;不過這個(gè)程序?qū)崿F(xiàn)的 應(yīng)該是這樣的  先奇數(shù)亮再偶數(shù)亮,循環(huán)三次;一個(gè)燈上下循環(huán)三次;兩個(gè)分別從兩邊往中間流動(dòng);再從中間往兩邊流動(dòng);

#include< reg52.h>
#include< intrins.h>
#define uint unsigned int
#define uchar unsigned char
void delay();
void main()
{ uchar temp,temp1,temp2,a,b;
   P3=0xff; 
  while(1)
{   for(b=3;b>0;b--) 
 { temp=0xaa;
   P1=temp; 
   delay();
   temp=0x55; 
   P1=temp;
   delay();
  }
  for(a=3;a>0;a--) 
    { temp=0xfe;
     for(b=7;b>0;b--)
     {  P1=temp;
        temp=_crol_(temp,1);
        delay();
        P1=temp;
        delay();
     }
          } 
       temp1=0xfe; 
    temp2=0x7f;
   for(a=8;a>0;a--)
   {  
    temp=temp1&temp2;
    P1=temp;
    delay();
   temp1=_crol_(temp1,1);
   temp2=_cror_(temp2,1);
   }
 
}
}
void delay()
{ uint a,b;
 for(a=100;a>0;a--)
 for(b=600;b>0;b--);
}

   程序?qū)崿F(xiàn)的第2種方法:下面是http://www.torrancerestoration.com單片機(jī)12群里的朋友木信大俠提出的,實(shí)現(xiàn)單片機(jī)led流水燈從中間向兩邊,從兩邊向中間的效果,下面的程序就是實(shí)現(xiàn)思路,這個(gè)是直接調(diào)用,應(yīng)該明白吧,數(shù)組那其實(shí)也可以改一下,如采用一維數(shù)組,在多次調(diào)用;也可以采用二維數(shù)組。主要看個(gè)人喜好.
-----------------------------------------------------
#include< reg52.h>
unsigned char code TABLE[]={
0xFF,0xE7,0xC3,0x81,0x00,     //從中間向兩側(cè)點(diǎn)亮
0xFF,0xE7,0xC3,0x81,0x00, 
0xFF,0xE7,0xC3,0x81,0x00,
0xFF,0x7E,0x3C,0x18,0x00,     //從兩側(cè)向中間點(diǎn)亮
0xFF,0x7E,0x3C,0x18,0x00,
0xFF,0x7E,0x3C,0x18,0x00
}; 
unsigned char i; 

DELAY()
{
     unsigned int s;  
for(s=0;s<30000;s++); 
}

main()
{
     while(1)
{
if(TABLE[i]!=0x01) 
{
   P1=TABLE[i];     
   i++;
   DELAY();
  }

  else
  {
   i=0; 
  }
}
}

關(guān)閉窗口