当前位置:网站首页>单片机之流水灯
单片机之流水灯
2022-07-30 05:46:00 【缘聚654】
之前已经实现了led的点亮以及闪烁,今天完成流水灯的实现。

可以看到单片机的led模块有8个led,从D1到D8,而实现led流水灯就是让这8个小灯依次亮灭并且循环这一过程。
首先我们要知道控制一个灯亮的方式,灯有8个,所以使用二进制中的0或1来控制每个灯的亮灭。
例如首先要第一个灯亮,则表示为1111 1110,0是亮,1是灭,从左往右表示为D8到D1,转换为十六进制就是0xFE;由此可以得到实现流水灯的主要代码,同时为了避免代码执行过快而观察不到流水灯,所以代码之间还需要加入延时的代码。
完整代码如下
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 129;
k = 119;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2=0xFE;
Delay500ms();
P2=0xFD;
Delay500ms();
P2=0xFB;
Delay500ms();
P2=0xF7;
Delay500ms();
P2=0xEF;
Delay500ms();
P2=0xDF;
Delay500ms();
P2=0xBF;
Delay500ms();
P2=0x7F;
Delay500ms();
}
}打开keil新建一个工程,并输入代码

接着打开stc,打开流水灯对应的文件,点编译同时进行冷启动

这样就实现了单片机流水灯的操作
具体的实际操作参考b站江科大自化协51单片机入门教程P6。
WeChat_20220619220448
边栏推荐
- [Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]
- 你不知道的JS语法篇笔记
- 独立按键控制led进阶(1)
- 爬2.12.6的Antd上传图片坑
- Machine Learning, Deep Learning Based on MATLAB 2021b
- Insertion Sort in Classic Sort
- Kunlun State Screen Production (Serialization 2)---Basic Chapter (setting and display, serial transmission)
- Acwing刷题第一节
- Deep Interpretation of void (*signal(int , void(*)(int)))(int) in "C Traps and Defects"
- 如何判断 PCB 板是否变形?
猜你喜欢

2021 soft exam intermediate pass

Duplicate keys detected:‘/da…‘

Acwing刷题第一节

QT serial 2: LORA test platform based on QT and STM32H750 (1)

this的指向问题

pdf和word等文档添加水印

js高级学习笔记(详细)

vscode 设置 sublime 的主题
Three working modes of CPU: real mode, protected mode, long mode
![[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]](/img/f8/3eabe8833caec0be25aefb72f9c86e.png)
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]
随机推荐
"R Language + Remote Sensing" Comprehensive Evaluation Method of Water Environment
Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"
IO进程线程->目录IO->day3
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]
【markdown常用用法】
超详细的PCB高可靠辨别方法
干货 | 什么是FOC?一文带你看BLDC电机驱动芯片及解决方案
jvm之逃逸分析
ipconfig命令指南
Acwing Brush Questions Section 1
使用Dva项目作Antd的Demo
自定义类加载器
华秋第八届硬创赛与安创加速器达成战略合作,助力硬科技项目成长
[Jiangsu University of Science and Technology Automation Association stm32F103c8t6] Notes [Initial 32 MCU and TIM timing interrupt initialization parameter configuration]
CPU的三种工作模式:实模式、保护模式、长模式
关于 PCB 多层板制程能力不得不说的那些事儿
conda常用命令总结(持续更新)
暂时存着阿里云
pdf和word等文档添加水印