当前位置:网站首页>STM32 interrupt plum blossom two degrees (one)
STM32 interrupt plum blossom two degrees (one)
2022-07-19 16:50:00 【Clouds blow eyebrows】
Catalog
One . Composition and understanding of interruption
1.4 Interrupt diagram : 1.5 Interrupt nesting diagram :
2.2.2EXTI Corresponding to the channel NVIC Interrupt handling
3、 ... and .EXTI Interrupt configuration encoder and infrared sensor code thinking and analysis
Four .TIM Interrupt timer and PWM Wave output
4.1 Timer related understanding
4.1.2TIM Counting mode of the counter :
4.2 The whole configuration process
One . Composition and understanding of interruption
1.1 interrupt :
While the main program is running , An interrupt source appears ( Specific interrupt trigger conditions ), bring CPU First deal with the interrupt program , Then continue to follow the main program .
1.2 Interrupt priority :
When multiple interrupt sources apply for interrupts at the same time , Execute one by one according to the priority .
1.3 Break nesting
When an interrupt program is running , There are new interrupt sources with higher priority to apply for interrupts , Thus pause the current interrupt function , Execute high priority interrupt handling functions . After processing, return to .
1.4 Interrupt diagram : 1.5 Interrupt nesting diagram :
1.5 The type of interrupt
EXTI External interrupt 、TIM、ADC、USART、SPI、IIC Wait for multiple peripherals . Is to use NVIC Unified management interruption , Each interrupt has 16 Programmable priority . Group priorities , Further set preemption priority and response priority .
1.6NVIC Priority groups
NVIC As interrupt management , Its interrupt priority is that of the priority register 4 Bit decided , this 4 Bits can be segmented , Divided into high n Bit preemption priority and low 4-n Bit response priority . The following figure shows the bit priority grouping method :
Response priority :
When executing an interrupt function , Those with high response priority will not stop the interrupt function from executing its own interrupt function , Instead, wait until the currently executed interrupt function is completed , The next one executes its own interrupt function . That is to cut in line .
preemption :
Even if there are interrupt function execution , The function will also be suspended , Execute interrupt function with high preemption priority . That is, interrupt nesting .
Two .EXTI External interrupt
EXTI Is designated GPIO Interruption caused by the change of port level .
2.1 Trigger mode :
Rising edge / Falling edge / On both sides / Software triggers . You can imagine , One IO The change of port level is not instantaneous , There will be a downward level change line from high potential to low potential , This is the falling edge . Others can be analogized .
Software trigger is the trigger set in the program code .
EXTI The number of channels is 16 individual , But after deployment, it will come with 4 individual " Rub the net " The passage of ,PVD,RTC,USB, Ethernet .
Be careful :EXTI Support all GPIO mouth , But the same IO Slogans cannot be interrupted at the same time , such as PB0 and PA0 You cannot apply for configuration interruption at the same time .
Here's another word :GPIOA,B,C,D There will be 16 individual IO mouth , namely 0-15, however EXTI At most 16 Channels , So the same IO Slogans cannot be configured at the same time .
2.2EXTI The basic structure
GPIO Port selection ------>AFIO Interrupt pin selection ------>EXTI Edge detection and control ------->NVIC management ------->CPU perform .
2.2.1AFIO
AFIO Data selector , speak GPIO Of IO The slogan is connected to EXTI, meanwhile EXTI Of 16 Channels 0-15, The connection corresponds to AFIO Selected pin 0-15. such as AFIO choice GPIO_B1 that EXTI The channel on the corresponding configuration is the channel 1.
2.2.2EXTI Corresponding to the channel NVIC Interrupt handling
In order to save resources ,EXTI Of 0-15 Corresponding to the channel NVIC There will be no 15 individual , about STM32C8T6 Come on ,EXTI5-9 and EXTI 10-15 It's a passage .
namely 5-9 and 10-15 Will trigger the same interrupt function .
3、 ... and .EXTI Interrupt configuration encoder and infrared sensor code thinking and analysis
#include "stm32f10x.h" // Device header
int16_t Encoder_Count;
void Encoder_Init(void)
{//CODE Five steps
//1. Configure clock RCC
// 2. To configure GPIO data
// 3. To configure AFIO
// 4. To configure EXTI
// 5. To configure NVIC
//1.
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//2.
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//3. AFIO The code of is shown as EXTI
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3);
//4.
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line2 | EXTI_Line3;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStructure);
//5.
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&NVIC_InitStructure);
}
int16_t Encoder_Get(void)
{
int16_t Temp;
Temp = Encoder_Count;
Encoder_Count = 0;
return Temp;
}
void EXTI2_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line2) == SET)
{
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 0)
{
Encoder_Count --;
}
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
void EXTI3_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line3) == SET)
{
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == 0)
{
Encoder_Count ++;
}
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
Four .TIM Interrupt timer and PWM Wave output
4.1 Timer related understanding
The timer can make technical changes to the input clock , And when the count value reaches ARR( Reload value ) Trigger interrupt . according to STM32 Different reference clocks of different models , The maximum timing is also different .
Time base unit :PSC( Preassigned frequency counter ),CNT( Counter ),ARR( Reload value ) form . These are all 16 Bit register .
Be careful : Actual frequency division coefficient =PSC+1, such as 1 Frequency division so PSC=0.
4.1.1 Timer type
Commonly used is the general timer , If you don't know which bus to mount , You can enter a bus to view the header file description .
4.1.2TIM Counting mode of the counter :
Count up / Count down / Center alignment . Center alignment is 0-ARR-0 Trigger twice . There are three options for general timer and advanced timer . The basic timer can only be up .
Add : Timer clock source selection can be internal and external , The basic timer can only be internal .
4.2 The whole configuration process
#include "stm32f10x.h" // Device header
void Timer_Init(void)
{ // Turn on the clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// Select internal clock source
TIM_InternalClockConfig(TIM2);
// Configure the time base unit
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 10000 - 1;
TIM_TimeBaseInitStructure.TIM_Prescaler = 7200 - 1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
// To configure NVIC grouping
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//NVIC Channel selection and priority configuration
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);
// Can make
TIM_Cmd(TIM2, ENABLE);
}
4.2PWM Output comparison OC
stay TIM Configure on the basis of CCR that will do .
#include "stm32f10x.h" // Device header
void PWM_Init(void)
{ // Configure clock source
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_InternalClockConfig(TIM2);// Select internal clock source
// Configure the time base unit
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; //ARR
TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; //PSC
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
// To configure CCR
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0; //CCR
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
// Can make TIM2
TIM_Cmd(TIM2, ENABLE);
}
边栏推荐
- 【mysql篇-进阶篇】存储引擎
- 在go中使用yaml
- Condition judgment function of MySQL function summary
- 【C语言】文件操作详解
- Hold the seat, wait a minute
- 最强大脑-C#开发傅立叶残影题目
- 7.17模拟赛总结
- Abstract static method
- Vs2019 MFC dynamically creates edit control, csliderctrl class member function create application creates slider control control [mfc dynamically creates control 4]
- 边际效用递减规律,真用起来,还挺多坑
猜你喜欢
JS 之 操作 Array 数组
Alibaba cloud (OSS) file upload and deletion
[MySQL - Advanced] storage engine
The item reported an error "beaninitializationexception: com.xxxxx.xx.dao.data.dao can't get a sessionfactory"“
项目报错“BeanInitializationException: com.xxxxx.xx.dao.data.Dao can‘t get a sessionFactory“
Cloud service ecs/rds: build ECS management Linux, build cloud database management, and create RDS MySQL;
在go中使用yaml
[C language] detailed explanation of file operation
UDP 通信流程
Teach you how to play starrocks hand in hand. Here comes the super detailed tutorial!
随机推荐
UVA10341 Solve It
CANoe:. What is a vmodule file
Interview question 01.04 Palindrome arrangement auxiliary array method
mysql函数汇总之系统信息函数
5.在项目中集成elenment-plus
JS 之 操作 Function 函数
UDP communication process
torch dist分布式数据汇总
关于在使用ssh命令远程执行命令中有转义字符的问题
Codeworks 5 questions per day (average 1500) - day 18
IDEA快捷键大全 + 动图演示
The fourth day of MATLAB learning (decision statement)
Suffix expression (day 65)
阿里云(OSS)文件上传和删除
System information function of MySQL function summary
The development idea plug-in failed to pass the audit due to compatibility problems
占位,稍等补
[stack queue]
电脑打开热点后无法连接到网络
Hold the seat, wait a minute