当前位置:网站首页>[Arduino learning notes] Series 1 - timer configuration
[Arduino learning notes] Series 1 - timer configuration
2021-02-23 18:01:25 【simpower】
Catalog
Two 、 The basic concept of timer
Timer speed (HZ) = Arduino Clock speed (16MHz) / Prescaler coefficients
3、 ... and 、 Timer configuration code
Arduino In the process of learning, we usually use library function operation . But there are no examples of timers . therefore , Here is a brief description of the configuration process of timing interrupt . Reference material :http://www.instructables.com/id/Arduino-Timer-Interrupts/.
One 、Arduino About timer
Arduino UNO There are three timers , Namely timer0,timer1 and timer2. Each timer has a counter , Incrementing at each clock cycle of the timer . Triggered when the counter reaches the value specified in the compare match register CTC Timer interrupt . Once the timer counter reaches this value , It will clear on the next timer of the timer clock ( Reset to zero ), Then it will continue to count again to compare matches . By selecting the match value and setting the timer to increase the speed of the counter , You can control how often the timer interrupts .
Next, I will introduce the configuration relationship of each register of the timer .
Two 、 The basic concept of timer
1、 Prescaled coefficients and comparison matcher
Arduino Clock with 16MHz function . A scale value of the counter indicates 1 / 16,000,000 second (~63ns), Run away 1s You need to count 16,000,000.
1、Timer0 and timer2 yes 8 Bit timer , You can store the maximum counter value 255.
2、Timer1 It's a 16 Bit timer , You can store the maximum counter value 65535.
Once the counter reaches its maximum , It will go back to zero ( This is called overflow ). therefore , We need to divide the clock frequency , The prescaler . The increment speed of the timing counter is controlled by the prescaler . The counting speed of prescaler and timer is as follows :
Timer speed (HZ) = Arduino Clock speed (16MHz) / Prescaler coefficients
therefore ,1 The prescaler will be 16MHz Up counter ,8 The prescaler will be in 2MHz Increasing ,64 Preassigned frequency counter = 250kHz, And so on .
The prescaled coefficients of the three timers are shown in table :
I'll explain in the next step CS12,CS11 and CS10 The meaning of .
Now you can calculate with the following steps Interrupt frequency . The following formula :
Interrupt frequency (Hz)=(Arduino Clock speed 16MHz)/( Preassigned frequency counter *( Compare matching registers + 1)
Rearrange the equations above , Give the interrupt frequency you want , You can solve the comparison register value :
Compare matching registers = [16,000,000Hz /( Preassigned frequency counter * Required interrupt frequency )] - 1
remember , When you use a timer 0 and 2 when , This number has to be less than 256, about timer1 Less than 65536.
So if you want to interrupt every second ( The frequency is 1Hz): Compare matching registers = [16,000,000 /( Preassigned frequency counter * 1)] -1
The prescaler is 1024, You get : Compare matching registers = [16,000,000 /(1024 * 1)] -1 = 15,624, because 256 <15,624 <65,536, You have to use timer1 To implement this interrupt .
3、 ... and 、 Timer configuration code
int toggle0,toggle1,toggle2=0;
void setup(){
cli();// Turn off global interrupt
// Set the timer 0 by 10kHz(100us)
TCCR0A = 0;// Will the whole TCCR0A Register set to 0
TCCR0B = 0;// Will the whole TCCR0B Register set to 0
TCNT0 = 0;// Initialize the counter value to 0
// Set the counter to 10kHZ, namely 100us
OCR0A = 24;// Compare matching registers = [16,000,000Hz /( Preassigned frequency counter * Required interrupt frequency )] - 1
// Compare matching registers =24, Interrupt interval =100us I.e. interrupt frequency 10khz
TCCR0A |= (1 << WGM01);// open CTC Pattern
TCCR0B |= (1 << CS01) | (1 << CS00); // Set up CS01 Position as 1(8 Multiple prescaler )
TIMSK0 |= (1 << OCIE0A);// Enable timer compare interrupt
// Set the timer 1 by 1kHz
TCCR1A = 0;// Will the whole TCCR1A Register set to 0
TCCR1B = 0;// Will the whole TCCR1B Register set to 0
TCNT1 = 0;// Initialize the counter value to 0
// Set the counter to 10kHZ, namely 1ms
OCR1A = 15624;//199;// = (16*10^6)/(1000*8) - 1 (must be <65536)
TCCR1B |= (1 << WGM12);// open CTC Pattern
TCCR1B |= (1 << CS12) | (1 << CS10);// Set up CS11 Position as 1(8 Multiple prescaler )
TIMSK1 |= (1 << OCIE1A);
// Set the timer 2 by 8kHz
TCCR2A = 0;// set entire TCCR2A register to 0
TCCR2B = 0;// same for TCCR2B
TCNT2 = 0;//initialize counter value to 0
// set compare match register for 8khz increments
OCR2A = 249;// = (16*10^6) / (8000*8) - 1 (must be <256)
// turn on CTC mode
TCCR2A |= (1 << WGM21);// open CTC Pattern
// Set CS21 bit for 8 prescaler
TCCR2B |= (1 << CS21);
// enable timer compare interrupt
TIMSK2 |= (1 << OCIE2A);
sei();// Turn on global interrupt
pinMode(13,OUTPUT);
}
// interrupt 0 Service functions
ISR(TIMER0_COMPA_vect){
// The generation frequency is 2kHz / 2 = 1kHz Pulse wave of ( The full wave switches to two cycles , Then switch to low )
if(toggle0){
digitalWrite(8,HIGH);
toggle0 = 0;
}
else{
digitalWrite(8,LOW);
toggle0 = 1;
}
}
ISR(TIMER1_COMPA_vect){// timer1 interrupt 1Hz Switch pins 13(LED)
// The generation frequency is 1Hz / 2 = 0.5kHz Pulse wave of ( The full wave switches to two cycles , Then switch to low )
if(toggle1){
digitalWrite(13,HIGH);
toggle1 = 0;
}
else{
digitalWrite(13,LOW);
toggle1 = 1;
}
delay(2000);
}
ISR(TIMER2_COMPA_vect){// timer1 interrupt 8kHz Switch pins 9
// The generation frequency is 8kHz / 2 = 4kHz Pulse wave of ( The full wave switches to two cycles , Then switch to low )
if(toggle2){
digitalWrite(9,HIGH);
toggle2 = 0;
}
else{
digitalWrite(9,LOW);
toggle2 = 1;
}
}
void loop(){
}
版权声明
本文为[simpower]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/02/20210223175952154B.html
边栏推荐
- Two solutions and solutions of garbled code on Microsoft edge page of win10 Home Edition gpedit.msc Solutions to the problem that commands cannot be used
- PAT_甲级_1110 Complete Binary Tree
- PAT_ Grade A_ 1110 Complete Binary Tree
- 实际工作中到底如何开展性能测试????
- How to carry out performance test in actual work????
- UNI-APP 记录
- Uni-app record
- PostgreSQL
- PostgreSQL
- 【STM32F407】第5章 RL-USB移植(MDK AC6)
猜你喜欢
-
单机最快MQ—Disruptor
-
PAT_甲级_1111 Online Map
-
[stm32f407] Chapter 5 rl-usb porting (MDK AC6)
-
Single fastest MQ - disruptor
-
PAT_ Grade A_ 1111 Online Map
-
如何避免微服务设计中的耦合问题
-
How to avoid coupling problem in microservice design
-
51信用卡股价年初至今上浮5倍,引入银行背景高管担任行政总裁
-
51 the share price of credit card has risen five times since the beginning of the year, and senior executives with bank background have been introduced as the chief executive
-
prometheus监控之进程监控(process-exporter)
随机推荐
- 华为轮值董事长胡厚崑:技术创新的同时要避免社会发展的分化
- 疫情推动“宅经济”,企业防御DDoS更加不能松懈
- 二分图最小点覆盖构造方案+König定理证明
- Anno&Viper -分布式锁服务端怎么实现
- 解决Win7 X64由于百联控件造成的蓝屏问题 (PassGuard_X64.sys)
- Process exporter of Prometheus monitoring
- 浅谈 Vite 2.0 原理,依赖预编译,插件机制是如何兼容 Rollup 的?
- Hu houkun, Huawei's rotating Chairman: avoid the differentiation of social development while making technological innovation
- The epidemic situation promotes "residential economy", and enterprises' defense against DDoS cannot be relaxed
- Construction scheme of minimum point cover of bipartite graph + proof of K ü nig theorem
- npm install 版本号^的坑
- Activity显示界面背后的故事:一文让你理清View的那些复杂关系
- Android面试官:Window连环十二问你顶得住吗?(快扶我起来,我还能问)
- 开发一个小程序,最好先做好课前工作
- SQL Server中DELETE和TRUNCATE的区别
- Simar 的 参考书
- 【招聘】分布式存储架构师 40K-80K*14薪
- How to implement anno & Viper - distributed lock server
- Solve the blue screen problem of win7 x64 caused by Bailian control (PassGuard)_ X64.sys)
- Talk about the vite 2.0 principle, dependence precompile, how is plug-in mechanism compatible with rollup?
- 哔哩哔哩视频爬取源码分享
- NPM install version number ^
- The story behind the activity display interface: let you clarify the complex relationships of view
- Android Interviewer: can you stand up to the 12 questions of windows? (help me up, I can still ask)
- Develop a small program, it is best to do a good job before class
- The difference between delete and truncate in SQL Server
- SIMAR's reference book
- [recruitment] 40k-80k * 14 salary for distributed storage architect
- Bili Bili video crawling source code sharing
- 面试这么久,第一次投诉,就这样没了……
- 二分图最小点覆盖构造方案+König定理证明
- Anno&Viper -分布式锁服务端怎么实现
- Interview so long, the first complaint, so gone
- Construction scheme of minimum point cover of bipartite graph + proof of K ü nig theorem
- How to implement anno & Viper - distributed lock server
- 混合云组网与管理(Wireguard+OpenVPN+LDAP)
- 如何将福禄克DSX2-5000、8000 CH恢复出厂设置
- Hybrid cloud networking and management (wireguard + OpenVPN + LDAP)
- How to restore the factory settings of fluke dsx2-5000 and 8000 Ch
- 线段树&数链剖分