标题: 51单片机流水灯+花样灯程序 [打印本页]

作者: 思念、苦无药    时间: 2018-6-21 17:15
标题: 51单片机流水灯+花样灯程序
#include <REGX51.H>                      //头文件,定义单片机型号
#define uchar unsigned char              //伪指令用uchar代替unsigned char
#define uint unsigned int              //伪指令用uint代替unsigned int
uchar code a[6]={0xfa,0xf5,0xeb,0xd7,0xaf,0x5f}; //定义编译数组
uchar code b[6]={0x5f,0xaf,0xd7,0xeb,0xf5,0xfa}; //定义编译数组
void delay(uint x)                  //延时函数
{
   uint y;
   for(;x>0;x--)
      for(y=100;y>0;y--);
}
void main(void)     //函数的开始,(void)表示有返回值
{
    uint i;
        uchar temp;
        while(1)        //while循环(1)循环条件
        {
           temp=0xfe;
           for(i=0;i<8;i++)
           {
           P0=temp;
           delay(300);
           temp=(temp<<=1)|0x01;                   //temp值左移1位,从而实现流水灯效果,0x01表示在最右面补1
        }
        temp=0x7f;
        for(i=0;i<8;i++)
    {
           P0=temp;
           delay(300);
           temp=(temp>>=1)|0x80;                   //temp值右移1位,从而实现流水灯效果,0x80表示在最左面补1
        }
        for(i=0;i<6;i++)
        {
           P0=a[i];                           //遍历数组a,将数组a中的每个元素赋给P0
           delay(400);
        }
            for(i=0;i<6;i++)
                {
                   P0=b[i];        //遍历数组b,将数组b中的每个元素赋给P0
                   delay(400);
                   }
        }
}


作者: jiamingyang    时间: 2018-6-21 18:47
很简单,写的太复杂了




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1