新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 3种方法实现流水灯

3种方法实现流水灯

作者: 时间:2016-12-01 来源:网络 收藏
(P0一般接10K的上拉电阻,此图复位电路没给出)

当然,有些比较有心的同学会做些有花样的,如这种“心”形的流水灯
下面试代码:

本文引用地址:https://www.eepw.com.cn/article/201612/324330.htm

(1)用总线的方法实现流水灯

#include

#define uchar unsigned char

#define uint unsigned int

uchartable[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

void delay(uint xms) //延时约1ms

{

uintx,y;

for(x=110;x>0;x--)

for(y=xms;y>0;y--);

}

void main()

{

uchar i;

while(1)

{

for(i=0;i<8;i++)

{

P0=table[i];

delay(500);

}

}

}

(2)移位实现流水灯

#include

#define uchar unsigned char

#define uint unsigned int

void delay(uint xms) //延时约1ms

{

uintx,y;

for(x=110;x>0;x--)

for(y=xms;y>0;y--);

}

void main()

{

uchar a,i;

while(1)

{

a=0xfe;

for(i=0;i<8;i++)

{

P0=a;

a=a<<1;

a=a|0x01;

delay(500);

}

}

}

(3)用库函数实现流水灯

#include

#include//此头文件包含移位函数

#define uchar unsigned char

#define uint unsigned int

void delay(uint xms) //延时约1ms

{

uint x,y;

for(x=110;x>0;x--)

for(y=xms;y>0;y--);

}

void main()

{

uint a;

a=0xfe;

while(1)

{

P2=a;

a=_crol_(a,1);

delay(500);

}

}



关键词: 流水灯上拉电

评论


技术专区

关闭