当前位置:网站首页>按键控制流水灯(计时器)
按键控制流水灯(计时器)
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
边栏推荐
猜你喜欢

ROS file system and related commands

MySQL - low level settings

Kind of weird!Access the destination URL, the host can container but not

MySQL事务(transaction) (有这篇就足够了..)

UG NX二次开发(C#)-外部模式-导出dwg格式的文件

Metasploit (MSF) Basic Super Detailed Edition

HCIP 第十三天

Compact格式下MySQL的数据如何存储到磁盘

Introduction to mysql operation (4) ----- data sorting (ascending, descending, multi-field sorting)

HCIP 第六天
随机推荐
研发过程中的文档管理与工具
MySQL - low level settings
2022-08-01 第四小组 修身课 学习笔记(every day)
CollectionUtil: a collection of functional style tool
HCIP 第十一天
MySQL事务(transaction) (有这篇就足够了..)
Hack The Box - File Transfers Module详细讲解中文教程
Understand Chisel language. 31. Chisel advanced communication state machine (3) - Ready-Valid interface: definition, timing and implementation in Chisel
postgres 水平分表,自动创建分区,按时间分表
metabase访问adb mysql 如何控制会话时区??
小说里的编程 【连载之二十三】元宇宙里月亮弯弯
2022年防止网络攻击的15个网络安全实践,你学会了吗?
7.联合索引(最左前缀原则)
原型模式
mysql如何从某一行检索到最后
Visual Analysis of DeadLock
gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
Install Metasploitable2 on VMware
读入、输出优化
DeadLock的可视化分析