51单片机第一篇章-流水灯

294 阅读1分钟

流水灯LED-从P2-0到p2-8 (手动代码和库函数API使用)

核心:延时函数和for循环

代码

从P0-P8-P0~~~

unsigned char i; while(1) { for(i=0;i<7;i++) { P2=~(0X01<<i); Delay(1000); } }

从P8-P0-P8~~~

unsigned char i; while(1) { for(i=0;i<7;i++) { P2=~(0X80>>i); Delay(1000); } }

当左移和右移函数以文档流形式置于while(1)内,可实现左右来回流水灯。

库函数实现

必须引入头文件

#include "intrins.h"

微信截图_20221108170852.png 引入之后可看到 微信图片_20221108170433.jpg 这里运用的就是2位16进制的移位 _cror_右移 _crol_左移

库函数实现流水灯

`

LED_PORT=~0X80;

while(1) {
    
     for(i=0;i<8;i++)
     {
     LED_PORT=_cror_(LED_PORT,1);
      Delay(1000);}
}`