当前位置:网站首页>按键控制流水灯(计时器)
按键控制流水灯(计时器)
2022-08-02 07:39:00 【缘聚654】
今天我们来了解并学会使用单片机的内部定时器
定时器最基本的功能就是计时,本次也是基于这个功能来实现流水灯,当然不同型号的单片机其内部计时器也是有些许差异的,基于本人的单片机型号的定时器配置如下
下面是定时器内部结构框图,从中可以很直观的看出定时器的工作原理
再使用定时器时我们首先要通过编程来对定时器的模式来进行配置,在STC中也为我们提供了十分方便的配置定时器模式的方法
要注意的是在设置参数时一定要选择与单片机类型对应的,否则将会出现错误。
学会使用定时器后进行流水灯的编程就十分容易了,在模块化编程中加入定时器模块并对定时器进行配置,然后再主函数中加入定时器中断函数。
#include <REGX52.H>
#include "TIMER0.h"
#include "Key.h"
#include <INTRINS.H>
unsigned char KeyNum,LEDMode;
void main()
{
P2=0xFE;
Timer0_Init();
while(1)
{
KeyNum=Key();
if(KeyNum)
{
if(KeyNum==1)
{
LEDMode++;
if(LEDMode>=2)
LEDMode=0;
}
}
}
}
unsigned int T0Count;
void Timer0_Poutine() interrupt 1
{
TL0=0x18;
TH0=0xFC;
T0Count++;
if(T0Count>=500)
{
T0Count=0;
if(LEDMode==0)
P2=_crol_(P2,1);
if(LEDMode==1)
P2=_cror_(P2,1);
}
}
定时器
#include <REGX52.H>
void Timer0_Init()
{
TMOD=TMOD&0xF0;
TMOD=TMOD|0x01;
TF0=0;
TR0=1;
TH0=0xFC;
TL0=0x18;
ET0=1;
EA=1;
PT0=0;
}
运行结果
f3dc52bab67a4b8bc4e19f791f1224
边栏推荐
- playwright 爬虫使用
- MySQL - index explanation
- 研发创新编码器霍尔板,引领企业高质量发展
- flutter在导航栏处实现对两个列表的点击事件
- mysql去除重复数据
- Understand Chisel language. 31. Chisel advanced communication state machine (3) - Ready-Valid interface: definition, timing and implementation in Chisel
- From cloud computing to function computing
- 7.联合索引(最左前缀原则)
- Comprehensive experiment of MPLS and BGP
- MySQL-执行流程+缓存+存储引擎
猜你喜欢
3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
Inverter insulation detection detection function and software implementation
WebForm DropDownList分别绑定年月
Understand Chisel language. 31. Chisel advanced communication state machine (3) - Ready-Valid interface: definition, timing and implementation in Chisel
多表的查询
研发创新编码器霍尔板,引领企业高质量发展
Probability Theory and Mathematical Statistics
HCIP 第八天
Fatal error compiling: 无效的目标发行版: 11
Understand the Chisel language. 30. Chisel advanced communication state machine (2) - FSMD: Take Popcount as an example
随机推荐
Understand the Chisel language. 30. Chisel advanced communication state machine (2) - FSMD: Take Popcount as an example
原型模式
替换ptmalloc,使用tcmalloc和jemalloc
WebForm DropDownList bind year and month respectively
MySQL-Execution Process + Cache + Storage Engine
mysql去除重复数据
DeadLock的可视化分析
hdu1752 copy
读入、输出优化
(2022 Niu Ke Duo School 5) B-Watches (two points)
Elasticserch 自定义字段,用户会频繁的创建和删除字段,怎么设计mapping?
59: Chapter 5: Develop admin management services: 12: MongoDB usage scenarios; (non-core data, non-core data with a relatively large amount of data, small private files such as face photos;)
Hack The Box - File Transfers Module详细讲解中文教程
Introduction to Totem Pole and Push-Pull Circuits
MySQL - Index Optimization and Query Optimization
uniapp 禁止默认返回事件
HCIP第七天
【Network】IP, subnet mask
LeetCode brush questions (7)
HCIP 第十天