1、中断系统
中断源:引脚发生电平跳变、定时器时间到达、串口接收到通信数据
EXTI外部中断:EXTI0-EXTI4,EXTI9—5,EXTI15—10
TIM定时器中断、ADC 模数转换中断、USART、SPI、I2C、RTC实时时钟、窗口看门狗中断、PVD中断
中断优先级:区别多个中断源的轻重缓急
中断嵌套:中断的中断
中断程序:硬件自动调用函数
2、NVIC(内核外设,嵌套中断向量控制器):
用来管理中断、分配优先级的,分16个中断优先级,包括抢占优先级和响应优先级
0个抢占优先级 × 16个响应优先级
2个抢占优先级 × 8个响应优先级
4个抢占优先级× 4个响应优先级
8个抢占优先级 × 2个响应优先级
16个抢占优先级 × 0个响应优先级
3、AFIO:① 复用功能引脚重映射 ② 中断引脚选择
4、EXTI外部中断
监测指定GPIO口的电平变化:
16个GPIO口(相同Pin不能同时触发),PVD输出、RTC闹钟、USB唤醒、以太网唤醒
上升沿/下降沿/双边沿触发中断/软件触发
中断响应(触发CPU执行中断函数)+ 事件响应(通知其他外设工作)
5、红外对射式传感器触发LED流水灯实验
外设代码:
CountSensor.c
#include "stm32f10x.h"
uint16_t Count = 0;
void CountSensor_Init()
{
/* RCC开启GPIOB时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//PB14口,GPIOB是APB2总线的外设
/* RCC开启AFIO时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//AFIO是APB2总线的外设
/* EXTI 和 NVIC 不需要开启时钟 */
/* 配置GPIO */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* 配置AFIO */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);
/* 配置EXTI */
EXTI_InitTypeDef EXTI_InitStruct;
EXTI_InitStruct.EXTI_Line = EXTI_Line14;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStruct);
/* 配置NVIC */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStruct);
}
uint16_t CountSensor_GetCnt(void)
{
return Count;
}
void CountSensor_ClearCnt(void)
{
Count = 0;
}
void EXTI15_10_IRQHandler(void)
{
if (SET == EXTI_GetITStatus(EXTI_Line14))
{
Count = 1;
EXTI_ClearITPendingBit(EXTI_Line14);
}
}
CountSensor.h
#ifndef __CNT_SENSOR_H__
#define __CNT_SENSOR_H__
void CountSensor_Init(void);
uint16_t CountSensor_GetCnt(void);
void CountSensor_ClearCnt(void);
#endif
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "CountSensor.h"
int main(void)
{
OLED_Init();
CountSensor_Init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
while(1)
{
if(0 != CountSensor_GetCnt())
{
/* RCC开启GPIOA时钟 */
GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET);
Delay_ms(100);
GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET);
GPIO_WriteBit(GPIOA, GPIO_Pin_2, Bit_RESET);
Delay_ms(100);
GPIO_WriteBit(GPIOA, GPIO_Pin_2, Bit_SET);
GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
Delay_ms(100);
GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
CountSensor_ClearCnt();
}
}
}
标签:优先级,Pin,中断,NVIC,外部,GPIO,EXTI From: https://www.cnblogs.com/czy363/p/18681617