当前位置:网站首页>Timer interrupt experiment
Timer interrupt experiment
2022-07-27 02:17:00 【Ruomu·】
Catalog
Two 、 Configuration of timer interrupt
1. Configuration process of timer interrupt :
3、 ... and .TIM Introduction to library functions
Four , Using variables across files
5、 ... and . A problem about timer interrupt experiment
One 、 What is? TIM
1.TIM Definition
TIM(Timer) Timer : The timer can count the input clock , And trigger the interrupt when the count value reaches the set value
TIM The timer not only has the basic timing interrupt function , It also includes the selection of internal and external clock sources , Input capture , Output comparison , Encoder interface , Master slave trigger mode and other functions .
2. Timer classification :
| type | Number | Bus | function |
| Advanced timer | TIM1,TIM8 | APB2 | It has all functions of general timer , And additionally, it has a repetition counter , Dead zone generation , Complementary output , Brake input and other functions |
| Universal timer | TIM2,TIM3,TIM4,TIM5 | APB1 | Have all functions of basic timer , In addition, it has internal and external clock source selection , Input capture , Output comparison , Encoder interface , Master slave trigger mode and other functions |
| Basic timer | TIM6,TIM7 | APB1 | Have timed interrupts , The main mode triggers DAC The function of |
Two 、 Configuration of timer interrupt
1. Configuration process of timer interrupt :
First step , Turn on RCC The clock . Turn on the clock here , The reference clock of the timer and the working clock of the whole peripheral will be turned on at the same time
The second step , Select the clock source of the time base unit , For timed interrupts , We choose the internal clock source
The third step , Configure the time base unit , Including prescaler , Automatic reassembler , Counting mode, etc
Step four , Configure output interrupt control , Allow update interrupt output to NVIC
Step five , To configure NVIC, stay NVIC Open the channel interrupted by the timer , And assign a priority
Step six , Operation control , After the configuration of the whole module is completed , Enable the counter , Otherwise, the counter will not run
2. According to the routine
Timer.c
#include "stm32f10x.h" // Device header
//extern uint16_t Num;
void Timer_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);// First step
TIM_InternalClockConfig(TIM2); // The second step , You can choose an internal clock without writing , Because after the timer is powered on, the default is the internal clock
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 10000-1; // cycle , Namely ARR The value of the automatic reloader
TIM_TimeBaseInitStructure.TIM_Prescaler = 7200-1;//PSC Prescaler value , Prescaler and counter have 1 Number deviation
// At this time, the prescaler is 72M Conduct 7200 Frequency division of the system , What you get is 10K The counting frequency of , stay 10K At different frequencies , meter 10000 Number , That is to say 1s
// Realize the calculation of timing time
TIM_TimeBaseInitStructure.TIM_RepetitionCounter =0;// Repeat counter value , This advanced timer is only used , Let's give it directly to 0
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
// Come here , The time base unit is configured , Next, enable the update interrupt
TIM_ClearFlag(TIM2,TIM_FLAG_Update);// It solves the problem of interruption immediately after power on
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);// To interrupt
// This enables the update interrupt to NVIC Access to
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// Priority groups
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;// preemption
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;// Response priority
NVIC_Init(&NVIC_InitStructure);
// At this time, the interrupt channel is opened
TIM_Cmd(TIM2,ENABLE);
// So the timer can work
}
main.c
#include "stm32f10x.h" // Device header
#include "OLED.h"
#include "Timer.h"
uint16_t Num;
int main(void)
{
OLED_Init();
Timer_Init();
OLED_ShowString(1, 1, "Num:");
while(1)
{
OLED_ShowNum(1,5,Num,5);
OLED_ShowNum(2,5,TIM_GetCounter(TIM2),5);//CNT Change of counter value ( from 0 To auto reload value , That's what we set up 10000-1)
}
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET)// Check the interrupt flag bit
{
Num++;
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);// Clear interrupt flag bit
}
}
3、 ... and .TIM Introduction to library functions

TIM_TimeBaseInit(); Initialize the time base unit
TIM_TimeBaseStructInit(), This function can assign structural variables to default values ;
TIM_Cmd(), This is used to enable the counter . Corresponding operation control ;
TIM_ITConfig(), This is used to enable interrupt the output signal ; Corresponding to interrupt output control ;
TIM_InternalClockConfig(). Select the internal clock
TIM_ITRxExternalClockConfig(), First choice ITRx Clocks for other timers ;
TIM_TIxExternalClockConfig(), choice TIx Capture channel clock ;
TIM_ETRClockMode1Config(). choice ETR Through the external clock mode 1 Input clock ;
TIM_ETRClockMode2Config(), choice ETR Through the external clock mode 2 Input clock ;
TIM_ETRConfig(), Used separately to configure ETR Pin prescaler , Polarity , These filter parameters ;
TIM_PrescalerConfig(), Used to write the pre frequency division value separately
TIM_CounterModeConfig(), Used to change the counting mode of the counter ;
TIM_ARRPreloadConfig(), Automatic reinstaller pre installed function configuration
TIM_SetCounter(), Write a value to the counter ;
TIM_SetAutoreload(), Write a value for the automatic reloader ;
TIM_GetCounter(); Get the value of the current counter , If you want to see where the current counter is , You can call this function , The return value is the value of the current counter ;
TIM_GetPrescaler(); Get the current prescaler value ; If you want to see the prescaled value , Just call this function ;
FlagStatus TIM_GetFlagStatus(TIM_TypeDef* TIMx, uint16_t TIM_FLAG);
void TIM_ClearFlag(TIM_TypeDef* TIMx, uint16_t TIM_FLAG);
ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, uint16_t TIM_IT);
void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT);
These four functions are used to obtain flag bits and clear flag bits
Four , Using variables across files
If you want to use variables across files , There are two ways :
( In the main In a statement , Take interrupt function as an example )
The first one is : You can use variables on the file , use extern Declare the variables to be used ;
Extern Declare variables , Just tell the compiler , I have now Num This variable , He defined in other documents , As for where , You go by yourself Look for it.
Then the compiler will diligently look for , When the compiler finds it , Will be able to extern The declared variable is used as a reference to the declared file variable .
The second kind : Copy the interrupt function directly , Put it in main Back ;
5、 ... and . A problem about timer interrupt experiment
Reset it ,Num It's from 1 Start counting , This indicates that the interrupt function enters once immediately after initialization
, The problem is TimeBaseInit function

Prescaler has a buffer register , We write values only when updating events , Will really work , So here, in order to make the value work immediately , At the end , An update event is generated manually , such , The value of prescaler is valid , But at the same time , His side effect is , Update events and update interrupts occur at the same time , The update interrupt will set the update interrupt flag , Once we finish initializing , The update interrupt will immediately enter , This is just when the power is on , Immediately enter the reason for the interruption
Solution : stay TimeBaseInit Behind , Open the front of the interrupt , Call it manually TIM_ClearFlag., Manually clear the update interrupt flag , Can solve .
边栏推荐
- Ospf基础配置应用( 综合实验: 干涉选举 缺省路由 区域汇总 认证--接口认证)
- Codeforces Round #796 (Div. 2), problem: (1688C) Manipulating History
- Brief introduction of VLAN principle and specific experimental configuration
- 【mysql】mysql启动关闭命令以及一些报错解决问题
- Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay
- [FPGA tutorial case 28] one of DDS direct digital frequency synthesizers based on FPGA -- principle introduction
- Dynamic routing rip protocol experiment
- OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
- 6.28大华笔试
- 机械硬盘选购指南——从选购经历谈起
猜你喜欢

OSPF在MGRE环境下的实验

【volatile原理】volatile原理

2022zui新抖音24小时循环值守直播监控(一)直播间开播监控

初识C语言(1)
![[详解C语言]一文带你玩转选择(分支)结构](/img/ca/7ee9f62a2478785c97684c7a0cc749.png)
[详解C语言]一文带你玩转选择(分支)结构

Experiment of OSPF in mGRE environment

C语言——数据类型、基本数据类型的取值范围

Solution: various error reports and pit stepping and pit avoidance records encountered in the alchemist cultivation plan pytoch+deeplearning (II)
![[explain C language in detail] takes you to play with the choice (Branch) structure](/img/ca/7ee9f62a2478785c97684c7a0cc749.png)
[explain C language in detail] takes you to play with the choice (Branch) structure
![C language implementation of the small game [sanziqi] Notes detailed logic clear, come and have a look!!](/img/b9/ade9a808a3f6d24cd9825dc9b010c1.png)
C language implementation of the small game [sanziqi] Notes detailed logic clear, come and have a look!!
随机推荐
[explain C language in detail] takes you to play with loop structure (for_while_do while)
FID index reproduction step on the pit to avoid the pit text generation image FID quantitative experiment whole process reproduction (FR é Chet inception distance) quantitative evaluation experiment s
C language implementation of the small game [sanziqi] Notes detailed logic clear, come and have a look!!
2022年最新文本生成图像研究 开源工作速览(Papers with code)
TCP的三次握手与四次挥手(简述)
C语言实现小游戏【三子棋】注释详细 逻辑清晰 快来看看吧!!
静态路由基础配置(IP地址的规划、静态路由的配置),实现全网可达。
7.16 多益网络笔试
【数据库课程设计】SQLServer数据库课程设计(学生宿舍管理),课设报告+源码+数据库关系图
OGeek Meetup第一期,携手CubeFS火热来袭
JS 99 multiplication table
JS logical operator
静态路由综合实验
STM32 HAL库串口(UART/USART)调试经验(一)——串口通信基础知识+HAL库代码理解
HCIA Basics (1)
MySQL课程1.简单命令行--简单记录 欢迎补充纠错
6.29 众安暑期测开实习一面
TCP's three handshakes and four waves (brief introduction)
Codeforces Round #810 (Div. 2), problem: (B) Party
2022最新直播监控24小时监控(三)直播间弹幕解析