单片机STC89C52学习——07 LED闪烁
汇总:00 单片机STC89C52学习
参考教程:普中科技
1 编程预备知识
- 预处理命令
#define
:便于修改
例如:#define A P0
- 循环左移、循环右移函数
_crol_(a,b);
:循环左移函数,a为左移的值,b为左移位数,包含在instrins.h
库函数中
_cror_(a,b);
:循环右移函数
2 程序:LED流水灯——8个LED流水闪烁
#include "reg52.h"
#include "intrins.h"// 因为用到循环左移右移函数
typedef unsigned char u8;// Keil中占1个字节
typedef unsigned int u16;// Keil中占2个字节
#define led P2
void delay (u16 i)
{
while (i --);
}
void main()
{
u8 i = 0;// 0~255
led = 0xfe;// 1111 1110,D1亮
delay (50000);// 约450ms
while (1)
{
for (i = 0; i < 7; i ++)// 注意是7次
{
led = _crol_(led,1);// 左移,1111 1110 -> ... -> 0111 1111
delay (50000);// 约450ms
}
for (i = 0; i < 7; i ++)// 注意是7次
{
led = _cror_(led,1);// 右移,0111 1111 -> ... -> 1111 1110
delay (50000);// 约450ms
}
}
}
效果:LED从D1亮到D8再返回D1,如此循环
网友评论