找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3050|回复: 18
收起左侧

<新手求助>单片机LED灯!

  [复制链接]
ID:219774 发表于 2017-7-14 22:20 | 显示全部楼层 |阅读模式
假设LED1~LED8   怎么从LED4  LED5向两边扩散依次亮?

      依次亮
回复

使用道具 举报

ID:219770 发表于 2017-7-14 22:55 来自手机 | 显示全部楼层
设个数组,用for循环4次
回复

使用道具 举报

ID:219796 发表于 2017-7-15 00:47 来自手机 | 显示全部楼层
a=0x10;b=0x01;for(i=0;i<4;i--){a<<=1;b>>=1;c=a|b;P0=c;delay();}
回复

使用道具 举报

ID:164602 发表于 2017-7-15 08:26 | 显示全部楼层
很简单啊,就是这样的:
#include<reg51.h>
#include<intrins.h>

#define LED8 P2

void Delay10ms(unsigned char c);

unsigned char code LLED[7]={0x81,0x42,0x24,0x18,0x24,0x42,0x81};

void main()
{
        unsigned char c,n;
        LED8=0xff;
        c=50;
        while(1)
        {
                for (n=0;n<7;n++)
                {
                        LED8=~LLED[n];
                        Delay10ms(c);
                }
        }       
}
void Delay10ms(unsigned char c)
{
    unsigned char a,b;
    for(;c>0;c--)
        for(b=38;b>0;b--)
            for(a=130;a>0;a--);
}

评分

参与人数 1黑币 +3 收起 理由
627431363 + 3 很给力!

查看全部评分

回复

使用道具 举报

ID:27916 发表于 2017-7-15 09:53 | 显示全部楼层
建个表,一次一次读出来显示
回复

使用道具 举报

ID:219825 发表于 2017-7-15 10:06 | 显示全部楼层
首先要看控制器的型号,51系列可以使用一组IO口进行中间向两侧的移位操作,其他型号要具体分析
回复

使用道具 举报

ID:219852 发表于 2017-7-15 11:42 | 显示全部楼层
unsigned char LED[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
回复

使用道具 举报

ID:219882 发表于 2017-7-15 14:37 | 显示全部楼层
把每种状态引脚值存数组里,循环调用数组
回复

使用道具 举报

ID:219774 发表于 2017-7-15 15:27 | 显示全部楼层
qingtian2090 发表于 2017-7-15 14:37
把每种状态引脚值存数组里,循环调用数组

用那个函数啊?
回复

使用道具 举报

ID:207702 发表于 2017-7-15 15:32 | 显示全部楼层
左移,右移  #include<intrins.h>
回复

使用道具 举报

ID:219918 发表于 2017-7-15 17:51 | 显示全部楼层
从LED4 LED依次左移右移
回复

使用道具 举报

ID:219925 发表于 2017-7-15 18:11 | 显示全部楼层
unsigned char i;  for(i=4;i<=8;i++) { LED(i)=亮;LED(8-i)=亮;delay(xxx);LED(i)=灭;LED(8-i)=灭;}
回复

使用道具 举报

ID:219925 发表于 2017-7-15 18:12 | 显示全部楼层
看我的就OK了,效率高,注意是伪代码,我看楼上很多的数据结构都学得不怎样
回复

使用道具 举报

ID:196174 发表于 2017-7-15 18:23 | 显示全部楼层
设个数组,用for循环4次
回复

使用道具 举报

ID:219950 发表于 2017-7-15 20:46 | 显示全部楼层
用一个case语句就能解决
回复

使用道具 举报

ID:220231 发表于 2017-7-17 11:48 | 显示全部楼层
通过两个灯两个灯的亮,里面加入延时函数就行
回复

使用道具 举报

ID:219774 发表于 2017-7-17 20:39 | 显示全部楼层
#include<reg51.h>
#include<intrins.h>

#define LED8 P0

void Delay20ms(unsigned char c);

unsigned char code LLED[7]={0x18,0x24,0x42,0x81,0x18,0x24,0x42};

void main()
{
        unsigned char c,n;
        LED8=0x01;
        c=200;
        while(1)
        {
                for (n=0;n<7;n++)
                {
                        LED8=LLED[n];
                        Delay20ms(c);
                }
        }        
}
void delay20ms(unsigned char c)   //误差 0us
{
    unsigned char a,b;
        for(;c>0;c--)
    for(b=215;b>0;b--)
        for(a=45;a>0;a--);
    _nop_();  //if Keil,require use intrins.h
    _nop_();  //if Keil,require use intrins.h
}
回复

使用道具 举报

ID:220355 发表于 2017-7-20 11:29 | 显示全部楼层
看你连接在那个io口上了,假设连接在P1口,让P1口的值依次为11100111,11011011,10111101,01111110就行了
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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