STM32_中断
中断控制器
所有的中断都需要搭配中断控制器
所有的引脚使用都要配置时钟一样
//配置嵌套向量中断控制器(NVIC)的优先级分组。它决定了系统中断优先级的分配方式,将优先级划分为 抢占优先级 和 子优先级。
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断优先级分组,即优先级分级个数
//定义一个中断优先级初始化的结构体
NVIC_InitTypeDef NVIC_InitStrue;
typedef struct
{
uint8_t NVIC_IRQChannel; /*!< Specifies the IRQ channel to be enabled or disabled.*/
//Preemption Priority(抢占优先级):表示中断能否抢占正在执行的其他中断。优先级值越低,优先级越高(例如:0 是最高优先级)。
//当多个中断发生时,抢占优先级高的中断可以打断抢占优先级低的中断。
//Subpriority(子优先级):如果多个中断具有相同的抢占优先级,子优先级用于决定它们的服务顺序。通常子优先级不影响抢占,而是仅用于调度。
uint8_t NVIC_IRQChannelPreemptionPriority; /*!< Specifies the pre-emption priority for the IRQ channel specified in NVIC_IRQChannel.*/
uint8_t NVIC_IRQChannelSubPriority; /*!< Specifies the subpriority level for the IRQ channel specified in NVIC_IRQChannel. */
FunctionalState NVIC_IRQChannelCmd; /*!< Specifies whether the IRQ channel defined in NVIC_IRQChannel will be enabled or disabled. */
} NVIC_InitTypeDef;
//根据赋值初始化
NVIC_Init(&NVIC_InitStrue);
外部中断
EXTI_InitTypeDef EXTI_InitStructure;
typedef struct
{
uint32_t EXTI_Line; /*!< Specifies the EXTI lines to be enabled or disabled.*/
EXTIMode_TypeDef EXTI_Mode; /*!< Specifies the mode for the EXTI lines. */
EXTITrigger_TypeDef EXTI_Trigger; /*!< Specifies the trigger signal active edge for the EXTI lines.*/
FunctionalState EXTI_LineCmd; /*!< Specifies the new state of the selected EXTI lines.*/
}EXTI_InitTypeDef;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
//中断函数,函数名确定
void EXTI9_5_IRQHandler(void) //对应起来的NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //属于外部中断5-9
EXTI_ClearITPendingBit(EXTI_Line5); //清除EXTI5线路中断标志
定时中断
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue;
typedef struct
{
//定时器模块接收的时钟频率通常很高(例如 72 MHz),如果直接用于计数或输出控制,可能不适合实际应用场景。预分频器将时钟频率除以一个整数值,从而降低计数频率,使定时器更灵活。
//计数频率
uint16_t TIM_Prescaler; /*!< Specifies the prescaler value used to divide the TIM clock.*/
uint16_t TIM_CounterMode; /*!< Specifies the counter mode.*/
uint16_t TIM_Period; /*!< Specifies the period value to be loaded into the active Auto-Reload Register at the next update event. */
uint16_t TIM_ClockDivision; /*!< Specifies the clock division.*/
uint8_t TIM_RepetitionCounter; /*!< Specifies the repetition counter value. Each time the RCR downcounter reaches zero, an update event is generated and counting restarts from the RCR value (N).*/
} TIM_TimeBaseInitTypeDef;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStrue); //根据TIM_TimeBaseInitStrue的参数初始化定时器TIM2
//TIM_IT:指定要配置的中断源。常见中断类型(标准库定义):TIM_IT_Update:更新中断(计数器溢出时触发)。TIM_IT_CC1:捕获/比较 1 中断(如 PWM 信号检测):。TIM_IT_Trigger:触发中断(当触发事件发生时)。
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能TIM2中断,中断模式为更新中断:TIM_IT_Update
TIM_Cmd(TIM2, ENABLE); //使能定时器TIM2
//中断服务函数 名称指定
void TIM2_IRQHandler(){}
TIM_GetITStatus(TIM2, TIM_IT_Update)==1
//在中断服务函数中,需要调用 TIM_ClearITPendingBit 清除中断标志位,否则中断可能持续触发。
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
串口中断
USART_InitTypeDef USART_InitStrue;
typedef struct
{
uint32_t USART_BaudRate; /*!< This member configures the USART communication baud rate. The baud rate is computed using the following formula:
- IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate)))
- FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */
uint16_t USART_WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.*/
uint16_t USART_StopBits; /*!< Specifies the number of stop bits transmitted.*/
uint16_t USART_Parity; /*!< Specifies the parity mode. */
uint16_t USART_Mode; /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.*/
uint16_t USART_HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled or disabled.*/
} USART_InitTypeDef;
USART_Init(USART1,&USART_InitStrue);//根据上面设置USART_InitStrue参数初始化串口1
USART_Cmd(USART1,ENABLE); //使能串口1
//指定要配置的 USART 中断类型。
//常见选项:USART_IT_TXE:发送数据寄存器空中断。USART_IT_RXNE:接收数据寄存器非空中断。USART_IT_TC:传输完成中断。USART_IT_PE:奇偶校验错误中断。
//中断服务函数(ISR, Interrupt Service Routine)不是完全自己定义的,而是基于固定的命名规则实现的。
//这些规则由芯片的启动代码(startup 文件)或底层库定义。你需要根据具体的中断事件编写函数逻辑,但函数的名称和声明必须与启动代码或库的要求一致。例如:void USART1_IRQHandler(void)
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //使能接收中断void USART1_IRQHandler(void)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!