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

8個花樣流水燈c程序

作者:佚名   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2011年08月21日   【字體:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
void delay(uint t)//延時函數(shù)
 {
  uint x,y;
  for(x=t;x>0;x--)
   for(y=110;y>0;y--);
 }
void main()//主函數(shù)
 {
  uchar temp,i;
  while(1)
  {
   temp=0x01; //8盞燈單個亮向左移
   for(i=0;i<8;i++)
     {
      P2=~temp;
      delay(50);
      temp<<=1;
     }
   temp=0x80;
   for(i=0;i<8;i++)//8盞燈單個亮反向右移
     {
      P2=~temp;
      delay(50);
      temp>>=1;
      }
   temp=0xfe;     
   for(i=0;i<8;i++)//8盞燈逐個向右亮
     {
      P2=temp;
      delay(50);
      temp<<=1;
     }
   temp=0xfe;  
   for(i=0;i<8;i++)//8盞燈逐個滅向右
     {
      P2=~temp;
      delay(50);
      temp<<=1;
     }
   temp=0x7f;  
   for(i=0;i<8;i++)//8盞燈逐個亮向左
     {
      P2=temp;
      delay(50);
      temp>>=1;
     }
   temp=0x7f;  
   for(i=0;i<8;i++)//8盞燈逐個滅向右
     {
      P2=~temp;
      delay(50);
      temp>>=1;
     }
    delay(100);
    P2=0x55;//隔一個亮
    delay(100);
    P2=0xaa;//隔一個亮
    delay(100);
    P2=0x00;//全亮
    delay(100);
    P2=0xff;//全滅
  }
 }
關(guān)閉窗口

相關(guān)文章