EXTI(Extern Interrupt)外部中断
EXTI可以监测指定GPIO口的电平信号,当其指定的GPIO口产生电平变化时,EXTI将立即向NVIC发出中断申请,经过NVIC裁决后即可中断CPU主程序,使CPU执行EXTI对应的中断程序。
支持的触发方式:上升沿/下降沿/双边沿/软件触发
支持的GPIO口:所有的GPIO口,但相同的Pin不能同时触发中断(例如PA0,PB0不能同时使用)
通道数:16个GPIO_Pin(GPIO_Pin_0~GPIO_Pin_15),外加PVD输出、RTC闹钟、USB唤醒、以太网唤醒
触发响应方式:中断响应/事件响应
1.对射式红外传感器计次
红外传感器:GPIOB_Pin_14
开启GPIO、AFIO时钟(APB2总线)
——GPIO配置:(结构体)模式、Pin、Speed
——AFIO配置:GPIO_EXTILineConfig(GPIO_PortSourceGPIOx, GPIO_PinSourcex)
配置AFIO的数据选择器,从而选择想要的中断引脚
——EXTI配置:(结构体)EXTI_InitTypeDef EXTI_InitStruct;EXTI_Init(&EXTI_InitStruct)
EXTI_Line:
EXTI_Line0 、EXTI_Line1 、 EXTI_Line2 、 EXTI_Line3 、 EXTI_Line4 、 EXTI_Line5 、 EXTI_Line6 、 EXTI_Line7 、 EXTI_Line8 、 EXTI_Line9 、 EXTI_Line10 、EXTI_Line11 、 EXTI_Line12 、EXTI_Line13 、 EXTI_Line14 、 EXTI_Line15、
EXTI_LineCmd:ENABLE or DISABLE
EXTI_Mode:EXTI_Mode_Interrupt(中断响应)、EXTI_Mode_Event(事件响应)
EXTI_Trigger: EXTI_Trigger_Rising (上升沿触发)、 EXTI_Trigger_Falling(下降沿触发) 、EXTI_Trigger_Rising_Falling (双边沿触发)
——NVIC配置:
指定中断分组:NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2)
NVIC_PriorityGroup_0
NVIC_PriorityGroup_1
NVIC_PriorityGroup_2
NVIC_PriorityGroup_3
NVIC_PriorityGroup_4
初始化NVIC:(结构体)NVIC_InitTypeDef NVIC_InitStruct、NVIC_Init(&NVIC_InitStruct)
NVIC_IRQChannel:完整的STM32中断通过列表参考stm32f10x.h
NVIC_IRQChannelCmd:ENABLE or DISABLE
根据所选的中断分组类型,指定所选通道的抢占优先级和响应优先级
NVIC_IRQChannelPreemptionPriority:
NVIC_IRQChannelSubPriority:
——中断程序:
每个中断通道都对应一个中断函数,参考启动文件
EXTI15_10_IRQHandler
void EXTI15_10_IRQHandler(void)
{
//中断标志位判断,确定中断来源是否为EXTI14
if(EXTI_GetITStatus(EXTI_Line14) == SET)
{
CountSensor_Count++;
EXTI_ClearITPendingBit(EXTI_Line14);
}
}
CountSensor.c文件
#include "stm32f10x.h"
uint16_t CountSensor_Count;
void CountSensor_Init(void)
{
//开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//开启GPIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//开启AFIO时钟
//EXTI、NVIC时钟是一直打开的,不需要手动打开
//配置GPIO
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//EXTI,GPIO配置:浮空输入/上拉输入/下拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB , &GPIO_InitStructure);
//配置AFIO
//void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);引脚重映射
//void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);配置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_Falling;
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_Get()
{
return CountSensor_Count;
}
void EXTI15_10_IRQHandler(void)
{
//中断标志位判断,确定中断来源是否为EXTI14
if(EXTI_GetITStatus(EXTI_Line14) == SET)
{
CountSensor_Count++;
EXTI_ClearITPendingBit(EXTI_Line14);
}
}
CountSensor.h文件
#ifndef __COUNTSENSOR_H
#define __COUNTSENSOR_H
void CountSensor_Init(void);
uint16_t CountSensor_Get();
#endif
主函数
#include "stm32f10x.h"
#include "Delay.h"
#include "OLED.h"
#include "CountSensor.h"
int main(void)
{
OLED_Init(); //显示屏初始化
CountSensor_Init(); //红外传感器计数初始化
OLED_ShowString(1,1,"Count:"); //显示屏显示“Count:”
while(1)
{
OLED_ShowNum(1,7 ,CountSensor_Get(),5); //显示数字,CountSensor_Get()函数
}
}
2.旋转编码器计次
#include "stm32f10x.h" // Device header
int16_t Encoder_Count;
void Encoder_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_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 EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) == SET)
{
/*如果出现数据乱跳的现象,可再次判断引脚电平,以避免抖动*/
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 0)
{
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
{
Encoder_Count --;
}
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void EXTI1_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) == SET)
{
/*如果出现数据乱跳的现象,可再次判断引脚电平,以避免抖动*/
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
{
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 0)
{
Encoder_Count ++;
}
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
#ifndef __ENCODER_H
#define __ENCODER_H
void Encoder_Init(void);
int16_t Encoder_Get(void);
#endif
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Encoder.h"
int16_t Num;
int main(void)
{
OLED_Init();
Encoder_Init();
OLED_ShowString(1, 1, "Num:");
while (1)
{
Num += Encoder_Get();
OLED_ShowSignedNum(1, 5, Num, 5);
}
}