單片機STC89C52學(xué)習(xí)——07 LED閃爍
匯總:00 單片機STC89C52學(xué)習(xí)
參考教程:普中科技
1 編程預(yù)備知識
- 預(yù)處理命令
#define
:便于修改
例如:#define A P0
- 循環(huán)左移、循環(huán)右移函數(shù)
_crol_(a,b);
:循環(huán)左移函數(shù),a為左移的值,b為左移位數(shù),包含在instrins.h
庫函數(shù)中
_cror_(a,b);
:循環(huán)右移函數(shù)
2 程序:LED流水燈——8個LED流水閃爍
#include "reg52.h"
#include "intrins.h"http:// 因為用到循環(huán)左移右移函數(shù)
typedef unsigned char u8;// Keil中占1個字節(jié)
typedef unsigned int u16;// Keil中占2個字節(jié)
#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疼约,如此循環(huán)