当前位置:网站首页>STC定时器初值计算
STC定时器初值计算
2022-07-13 18:06:00 【LED_H】
一、计算方法
都以6MHz晶振,定时20ms为例:
1. 方法1:
公式:N=65536-(Fosc/FD/(Freq))
N: 定时器16位计数器
Fosc: 晶振频率,系统频率
FD: 分频 (Frequency division),通过分频可增大计数器的定时范围。
Freq : 频率 (Freq=1000000(微秒)/TIME(微秒),TIME:所定时间)
1、先算出计算所需要定时器的时间对应的频率Freq,如定时20ms,对应的Freq为(1000000/20000)=50Hz。
2、通过单片机晶振频率计算单片机定时器需要计数的值:(Fosc/12/Freq)=( 6000000Hz/12/50)=10000【这里使用12分频】,所以定时器计数10000次才能达到时间20ms。(这里是12T和传统的89C52一样,计数一次所以要除以12)
3、定时器16位,最多可计数65536次,溢出为中断,所以用(65536-10000)=55536为定时器里面的初值。
2、方法2:
这里STC设置定时器T0为12T,和传统的51单片机一样
1、6MHz说明单片机每秒振荡6000 000次,每1毫秒振荡6000次。
2、单片机12个振荡时钟定时器计一次,我们需要定时20ms,单片机每1ms计数总量为:6000/12=500,则说明单片机每1ms进行500次计数,我们需要定时20ms所以定时器总的计数为500*20=10000次。
3、我们已经得出定时器定时20ms需要的计数值,因为定时器为计数到65536才中断,所以需要赋初值为65536-10000=55536;
二、C语言使用
方法1:
#define FOSC 6000000L //系统频率
#define T0_TIM (65536-(Fosc/12/50))
union VarS16{
U16 mU16;
U8 mU8[2];
};
void main()
{
union VarS16 tmp;
tmp.U16 = TO_TIM;
TMOD = 0x00;
//高位在前
TL0 = tmp.mU8[1];
TH0 = tmp.mU8[0];
TR0 = 1; //T0开始计数
ET0 = 1; //使能T0中断
EA = 1; //开启总中断
while(1)
{
;
}
}
void tm0_isr() interrupt 1 using 1
{
/*写入中断需要执行的代码*/
}
方法2:
#define FREQ_KHZ 6000L //系统频率
union VarS16{
U16 mU16;
U8 mU8[2];
};
void main()
{
union VarS16 tmp;
tmp.mU16 = (FREQ_KHZ/12L) * 20; //
tmp.mU16 = (~tmp.mU16) + 1; //取反 +1 相当于 ( 2^16 - tmp.mU16 )
TMOD = 0x00;
//高位在前
TL0 = tmp.mU8[1];
TH0 = tmp.mU8[0];
TR0 = 1; //T0开始计数
ET0 = 1; //使能T0中断
EA = 1; //开启总中断
while(1)
{
;
}
}
void tm0_isr() interrupt 1 using 1
{
/*写入中断需要执行的代码*/
}
边栏推荐
- Dictionary tree
- How to solve the relationship between the two use cases?
- Alipay computer website payment
- 最长上升子序列 最长公共子序列 最大字段和 最长不重复子串
- ABAP Bapi copy the standard project template to achieve project initiation
- Flink on Yan ha mode construction problem
- Idea annotation template, such configuration is enough!
- Set up in Jenkins to show the summary of allure Report
- Scheduledthreadpoolexecutor source code and error explanation
- Why is it said that the testing post is a giant pit? The 10-year-old tester told you not to be fooled~
猜你喜欢

LVM与磁盘配额

Unittest one stop learning

RAID disk array

Byte test director stayed up for 10 days, and the test post interview script came out of the liver, giving you wings to your big factory dream~

Interface test and interface test automation

"Why do you want to resign, Tencent, which broke its head?"

自动备份MySQL。且保留7天案例

2021-11-13 attack and defense world test record 01misc

Linux下安装单机版redis

太香了, 终于明白为什么这么多人要转行软件测试了~
随机推荐
线程池和生产者消费者模型
“终于我从字节离职了...“一个年薪40W的测试工程师的自白..
文件管理-阿里云OSS学习(一)
Detailed explanation of sliding window
Number system conversion and subnet Division
How to quickly test the new product just taken over
都说软件测试工资高,那么软件测试如何才能月薪过10k呢..
Unittest one stop learning
Scheduledthreadpoolexecutor source code and error explanation
Why is it said that the testing post is a giant pit? The 10-year-old tester told you not to be fooled~
回溯
"MySQL database principle, design and application" after class exercises and answers compiled by dark horse programmer
1、 Disk data recovery experiment report
【LeetCode】1252. Number of odd value cells
2021/12/12 attack and defense world crypto question making record
As an interviewer for the test development post, how do I choose people?
作为测试开发岗的面试官,我都是怎么选人的?
It is said that the salary of software testing is high, so how can the monthly salary of software testing exceed 10K
Data storage and disaster recovery (2nd Edition) editor in chief Lu Xianzhi Wu Chunling comprehensive training answer
为什么都说测试岗位是巨坑?10年测试人告诉你千万别上当~