当前位置:网站首页>单片机之流水灯
单片机之流水灯
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
边栏推荐
- VSCode hides the left activity bar
- Explore the efficiency of make_shared
- C语言,库函数中qsort的用法,及解释
- C language, usage of qsort in library function, and explanation
- 动态规划入门 JS
- Kunlun On-state Screen Production (serial 1)---Contact
- [Punctuality Atom] Learning and use of IIC (unfinished...)
- HSPF model application
- 编程测试6.21
- 【正点原子】IIC的学习与使用(未完...)
猜你喜欢
IEEE在指定期刊下搜索相关论文
干货 | 什么是FOC?一文带你看BLDC电机驱动芯片及解决方案
CPU的三种工作模式:实模式、保护模式、长模式
xxx is not in the sudoers file.This incident will be reported错误
The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]
高交会重要活动之一|2020中国硬件创新大赛全国总决赛
FPGA parsing B code----serial 2
Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
vscode 设置 sublime 的主题
随机推荐
2020-09-03解决pip install安装非常慢[Errno 101] 网络不可达问题
[Quick MSP430f149] Notes on learning MSP430f149 during the game
C语言,库函数中qsort的用法,及解释
华秋第八届硬创大赛携手NVIDIA初创加速计划,赋能企业发展
HSPF model application
Comparison of advantages and disadvantages of VsCode and Sublime editors
关于报错vscode
动态规划进阶 JS
关于 PCB 多层板制程能力不得不说的那些事儿
【江科大自化协stm32F103c8t6】笔记之【入门32单片机及EXTI外部中断初始化参数配置】
TCP为什么要三次握手,握手过程中丢包会怎么样?
表格比手机屏幕宽时不压缩,可左右滚动,格子内容不换行
This beta version of Typora is expired, please download and install a newer; workaround
QT serialization 1: readyRead() function, the solution to incomplete data subcontracting
[Jiangsu University of Science and Technology Automation Association stm32F103c8t6] Notes [Initial 32 MCU and TIM timing interrupt initialization parameter configuration]
ssh script space character conversion
Insertion Sort in Classic Sort
独立按键控制led进阶(1)
三种内核结构---宏内核、微内核、混合内核
ipconfig Command Guide