当前位置:网站首页>STC timer initial value calculation
STC timer initial value calculation
2022-07-16 07:55:00 【LED_ H】
One 、 computing method
Are subject to 6MHz Crystal oscillator , timing 20ms For example :
1. Method 1:
The formula :N=65536-(Fosc/FD/(Freq))
N: Timer 16 Bit counter
Fosc: Crystal oscillator frequency , System frequency
FD: frequency division (Frequency division), The timing range of the counter can be increased by frequency division .
Freq : frequency (Freq=1000000( Microsecond )/TIME( Microsecond ),TIME: Set time )
1、 First calculate the frequency corresponding to the time of the timer required for calculation Freq, Such as timing 20ms, Corresponding Freq by (1000000/20000)=50Hz.
2、 Calculate the value that the SCM timer needs to count through the crystal vibration frequency of the SCM :(Fosc/12/Freq)=( 6000000Hz/12/50)=10000【 Use here 12 frequency division 】, So the timer counts 10000 Times to reach the time 20ms.( Here is 12T And traditional 89C52 equally , Count once, so divide by 12)
3、 Timer 16 position , Up to... Can be counted 65536 Time , Overflow is interrupt , So use (65536-10000)=55536 For the initial value in the timer .
2、 Method 2:
here STC Set the timer T0 by 12T, And traditional 51 It's like a single chip computer
1、6MHz It shows that the single chip microcomputer oscillates every second 6000 000 Time , Every time 1 Millisecond oscillation 6000 Time .
2、 Single chip microcomputer 12 An oscillating clock timer counts once , We need timing 20ms, SCM every 1ms The total count is :6000/12=500, It means that every single chip computer 1ms Conduct 500 Times count , We need timing 20ms So the total count of the timer is 500*20=10000 Time .
3、 We have got the timer timing 20ms Required count value , Because the timer counts to 65536 Just interrupt , So we need to assign the initial value to 65536-10000=55536;
Two 、C Language use
Method 1:
#define FOSC 6000000L // System frequency
#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;
// High in the former
TL0 = tmp.mU8[1];
TH0 = tmp.mU8[0];
TR0 = 1; //T0 Start counting
ET0 = 1; // Can make T0 interrupt
EA = 1; // Open total interrupt
while(1)
{
;
}
}
void tm0_isr() interrupt 1 using 1
{
/* Write the code to be executed by the interrupt */
}
Method 2:
#define FREQ_KHZ 6000L // System frequency
union VarS16{
U16 mU16;
U8 mU8[2];
};
void main()
{
union VarS16 tmp;
tmp.mU16 = (FREQ_KHZ/12L) * 20; //
tmp.mU16 = (~tmp.mU16) + 1; // Take the opposite +1 amount to ( 2^16 - tmp.mU16 )
TMOD = 0x00;
// High in the former
TL0 = tmp.mU8[1];
TH0 = tmp.mU8[0];
TR0 = 1; //T0 Start counting
ET0 = 1; // Can make T0 interrupt
EA = 1; // Open total interrupt
while(1)
{
;
}
}
void tm0_isr() interrupt 1 using 1
{
/* Write the code to be executed by the interrupt */
}
边栏推荐
猜你喜欢

还在用策略模式解决 if-else?Map+函数式接口方法才是YYDS

CCF 201909-1 称检测点查询

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

Are you still using the strategy mode to solve if else? Map+ functional interface method is yyds

Linux上安装Redis

A simple JVM tuning. Write it in your resume

POI框架学习-导入导出案例

IDEA 注释模板,这样配置才够逼格!

RAID disk array

Redis can only cache? Too out!
随机推荐
Day 5 of leetcode question brushing
HCIA复习
ABAP BAPI 复制标准项目模板实现项目立项
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~
Five years' experience: the monthly salary is 3000 to 30000, and the change of Test Engineers
Day 18 of leetcode
Network layer protocol
nodejs+express设置和获取cookie,session
C# .Net Core and Net5 Skills record
V891-Z3735F重做系统,驱动修复汇总
mysql基础相关(重要)
Ugly number
Idea annotation template, such configuration is enough!
CCF 202012-2 期末预测之最佳阈值
Day 12 of leetcode + day 1 of DL
Attack and defense World Web
Data storage and disaster recovery (2nd Edition) editor in chief Lu Xianzhi Wu Chunling comprehensive training answer
Day 8 of leetcode question brushing
01 knapsack filling form implementation
Linux下安装单机版redis