LED闪烁回顾
LED闪烁本质就是把LED的输入端不断更改高低电平即可。
记得延时函数!
LED流水灯是什么?
LED流水灯就是使8个发光二极管逐个点亮,显而易见,需要LED闪烁为基础、
8个LED输出规律
有8个LED,则有8个引脚(I/O口),要同时控制8个引脚,有两种方法可行。
通过编写LED的二进制位数转换为十六进制得到的表。
原理如下:
原理图
LED LED LED LED LED LED LED LED
1 1 1 1 1 1 1 0 0xfe
1 1 1 1 1 1 0 1 0xfd
1 1 1 1 1 0 1 1 (以此类推)
1 1 1 1 0 1 1 1
1 1 1 0 1 1 1 1
1 1 0 1 1 1 1 1
1 0 1 1 1 1 1 1
0 1 1 1 1 1 1 1
初始状态:
LED_PORT=1111 1110
i=0:LED_PORT=1111 1101
i=1:LED_PORT=1111 1011
i=2:LED_PORT=1111 0111
i=3:LED_PORT=1110 1111
i=4:LED_PORT=1101 1111
i=5:LED_PORT=1011 1111
i=6:LED_PORT=0111 1111
i=7:LED_PORT=1111 1110
之后定义数码管数组,输入相应的十六进制数字,再编写循环函数,延时–数组+1–延时。
软件设计
#include <reg52.h>
#include "intrins.h"//引入移位操作符头文件
//LED_PORT = ~(0X01<<I);将1右移i位,然后按位取反再赋回去
//_cro?_(a,b) a为移动的值,b为移动的位数,若移位溢出,则用0补齐
typedef unsigned char u8;
typedef unsigned int u16;
void delay(u16 i)
{
while(i--);
}
//方法1
#define led p2 //宏定义p2端口,统一作为led的接口
void main()
{
int i = 0;
led = 0xfe;//表示为1111 1110
delay(50000);
while(1)
{
for(i = 0;i<7;i++)
{
led=_crol_(led,1);
delay(50000);
}
for(i = 0;i<7;i++)
{
led=_cror_(led,1);
delay(50000);
}
}
}
//方法2
//LED1~LED8对应端口为P2^0~P2^7
//本来表示为1111 1110,按位取反~为0000 0001
#define LED_PORT P2//宏定义P2端口
void main()
{
u8 i = 0;
LED_PORT=~0x01;//0xfe按位取反得到
delay(50000);
while(1)
{
for(i = 0;i<7;i++)
{
LED_PORT=_crol_(LED_PORT,1);//将亮灯的“0”左移
delay(50000);
}
for(i = 0;i<7;i++)
{
LED_PORT=_cror_(LED_PORT,1);//将亮灯的“0”右移
delay(50000);
}
}
}