找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1620|回复: 0
打印 上一主题 下一主题
收起左侧

51单片机流水灯多种方法实现

[复制链接]
跳转到指定楼层
楼主
ID:524018 发表于 2019-4-28 22:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


原代码:


#include<reg52.h>
#include<intrins.h>//字符循环移动函数库
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);        //字符循环右移实现右移        
        }


        for(j=0;j<8;j++)
        {
                P2=temp;
                 delays();
                 temp=_crol_(temp,1);        //字符循环左移实现左移        
        }
        
        /*另外一种思路,用移位运算符实现
        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;逐个点亮
        }*/


        /*用数组实现从两侧到中间,再从中间到两侧流水灯
        int e;
        int i=0;
        unsigned char d[]={0x7e,0xbd,0xdb,0xe7};//定义亮灯熄灯数组


        for(e=0;e<3;e++)                         //两侧到中间
        {        
                P2=d[e];
                delays();
        }


        for(e=3;e>=0;e--)                         //中间到两侧
        {
                P2=d[e];
                delays();
        }*/
}

新手上路,请多指教,谢谢!





评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表