当前位置:网站首页>[Jiangsu University of Science and Technology Automation Association stm32F103c8t6] Notes [Initial 32 MCU and TIM timing interrupt initialization parameter configuration]
[Jiangsu University of Science and Technology Automation Association stm32F103c8t6] Notes [Initial 32 MCU and TIM timing interrupt initialization parameter configuration]
2022-07-30 07:14:00 【Clockwisee】
接着上一篇笔记写
六、TIM定时中断
TIM基本结构---------------------------------编写原理 如图中所述



- 72M对7200进行分频,得到的就是10K的计数频率 ,在10kfrequency count down10000个数就是1s
1、RCC开启时钟
This basically every code is the first step,不用多想,在这里打开时钟后,The base clock of the timer and the working clock of the entire peripheral will be turned on at the same time
2、选择时基单元的时钟源
对于定时中断,We choose the internal clock source
3、配置时基单元
Including the prescaler here、自动重装器、计数模式等等,These parameters can be configured with a structure
4、配置输出中断控制,允许更新中断输出到NVIC
5、配置NVIC
在NVIC中打开定时器中断的通道,并分配一个优先级
6、运行控制
——————————————————————————————————————整个模块配置完成后,我们还需要使能一下计数器,要不然计数器是不会运行的,当定时器使能后,计数器就会开始计数了,当计数器更新时,触发中断,Finally, we write a timer interrupt function,In this way, the interrupt function will be automatically executed every once in a while,好,These are the general ideas of how we initialize the timer
The following is the code routine of the Jiangke University Automation Association:
#include "stm32f10x.h" // Device header
//extern uint16_t Num; //引用其他文件(主函数)的Num变量
void Timer_Init(void)
{
//开启时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//TIM2是APB1总线上的外设
//选择时基单元的时钟
TIM_InternalClockConfig(TIM2); //tim.h--->选择内部时钟,可以不写,因为定时器上电后默认选择内部时钟
//初始化时基单元 类似于GPIO的初始化
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; //配置时基单元
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //指定时钟分频 ,1分频 影响不大,Jump to definition can also be changed to 2、4分频
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数(And down、Three kinds of centers count it)
TIM_TimeBaseInitStructure.TIM_Period = 10000 - 1; //周期 ARR自动重装器的值//有1的误差//形成CNT计数 重装值
TIM_TimeBaseInitStructure.TIM_Prescaler = 7200 - 1; //PSC预分频器的值 10k的计数频率 计10000的数//有1的误差//形成CNT计数 倍数变化
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器的值 //高级定时器才有 Generic timer here 不用
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);//这里选择的是TIM2,注意下面的NVIC通道
TIM_ClearFlag(TIM2, TIM_FLAG_Update);//TIM_TimeBaseInitThere is an explanation,Interrupts are initialized at the same time,At this time, you need to manually clear the update interrupt flag bit,防止reset直接到1
//使能更新中断,Interrupt output control
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能中断 update 更新中断
//NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //优先级分组
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM2在NVIC里的通道
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
//启动定时器,运行控制
TIM_Cmd(TIM2, ENABLE); //启动定时器,This will allow the timer to start working,An interrupt is triggered when an update occurs
}
//——————————————————————————————————————————————————————————————————————————————————————————At this point, the timer initialization code is completed
//Next, write the interrupt function
/* void TIM2_IRQHandler(void) //startup_stm32f10x_md.s------->定时器2 的中断函数 { if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)//See update interrupt flag bit { //Num++; TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//清零标志位 } } */
The external clock is used as a counter
外部中断GPIO的配置,pull-up,Floating is not good
*********************************************************************
#include "stm32f10x.h" // Device header
//PA0 TIM2的ETR引脚
void Timer_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//External interrupt to be configuredGPIO
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0x00);//通过ETRpin's external clock mode2配置
//TIM_ExtTRGPSC_OFF No frequency division is required //TIM_ExtTRGPolarity_NonInverted极性,Active high or rising edge //0x00No filter needed
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 10 - 1; //从0计到9 //CNT计数器的值
TIM_TimeBaseInitStructure.TIM_Prescaler = 1 - 1;//No frequency division is required //If there is a prescaler Just block it a few times and add it once //被分频
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE);
}
uint16_t Timer_GetCounter(void)
{
return TIM_GetCounter(TIM2);
}
/* void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); } } */
七、跨越不同.c文件的变量
Use the same variable across files,An error will be reported if it is not handled properly
解决方法
1、For example, the variable inside the interrupt,If you want to use it in the main function,in the file that uses the variable(中断文件)的上面,用externDeclare the variables to be used
比如:extern uint16_t Num;
2、Copy and paste the interrupt function below the main function,Then comment out the interrupt function in the interrupt,put it where it is used,这样更方便
边栏推荐
- 基于OpenCV的双目重建
- 1.03 original Acegi security mechanism
- 原创 Acegi 1.03 安全机制
- Target detection, object classification and semantic segmentation of UAV remote sensing images based on PyTorch deep learning
- jvm之方法区
- Self-augmented Unpaired Image Dehazing via Density and Depth Decomposition program running record
- 对于国内数据交换平台的分析
- QT连载2:基于QT和STM32H750的LORA试验平台(1)
- Receive emails from gmail with pop3
- ipconfig命令指南
猜你喜欢
CPU的三种工作模式:实模式、保护模式、长模式

【速成MSP430f149】电赛期间学习MSP430f149笔记
Three working modes of CPU: real mode, protected mode, long mode

无人机生态环境监测、图像处理与GIS数据分析

查找Proj4js地图投影参数

【江科大自化协stm32F103c8t6】笔记之【入门32单片机及利用TIM输出比较配置PWM】

电子工程师怎么才能规范设计标准、提高设计效率?

QT连载2:基于QT和STM32H750的LORA试验平台(1)

高交会重要活动之一|2020中国硬件创新大赛全国总决赛

2021-09-16 集成学习上--task1机器学习数学基础
随机推荐
边境的悍匪—机器学习实战:第十五章 使用CNN和RNN处理序列
OpenCV中(rows,cols)与图像(x,y)
Receive emails from gmail with pop3
边境的悍匪—机器学习实战:第三章 分类
CLUE模型构建方法、模型验证及土地利用变化情景预测
关于 PCB 多层板制程能力不得不说的那些事儿
2021-09-16 集成学习上--task1机器学习数学基础
昆仑通态屏幕制作(连载4)---基础篇(图形设定与显示,按钮灯)
如何开发出成功的硬件产品,一个产品由概念的产生到产品的落地量产又需要经历哪些流程呢?
新导则下 防洪评价报告编制方法及洪水建模(HEC-RAS)
干货:线上下单应知应会,快来了解下
XMLBean的基础运用
【Qingdao Station】High-level application of SWAT model and modeling of areas without data, uncertainty analysis and climate change, improvement of land use surface pollution impact model and case analy
ipconfig Command Guide
虚拟机栈帧结构
R-GIS: 如何用R语言实现GIS地理空间分析及模型预测
一文盘点五款 BLDC 风机参考方案,建议先马
R语言 生态环境领域应用
openssl1.1.1ARM双编译
边境的悍匪—机器学习实战:第九章 无监督学习任务