stm32引脚中断
中断由GPIOB_PIN_14引脚的下降沿引起并计数,计数结果在OLED屏上显示。
引脚中断函数配置流程
1、外设时钟使能(端口、AFIO)
2、外设初始化(GPIO、AFIO、EXTI、NVIC)
3、设置中断函数
具体配置
1、外设时钟使能(端口、AFIO)
根据所需要使用到的外设设置时钟
例如:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/*所使用的GPIOB口在!APB2外设上*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/*所使用的AFIO口在!APB2外设上*/
/*具体外设所需要的配置函数还需要查看RCC.h函数库*/
2、外设初始化
初始化所需要使用到的外设
例如:
//GPIO初始化
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;//14号引脚,对应所接入的引脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);
/* 选择GPIOB的P14口连接上EXTI外设,即连接GPIOB的P14到AFIO的通道
需要注意的是所有Px(x=0~15)口到EXTI的外设的线路只有一条,也就是说如果选择了 GPIOB_PIN_14就不能选择GPIOA_PIN_14。
*/
//EXTI初始化
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line14;//14号线
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure);//初始化EXTI外设
//NVIC初始化
/*以下函数在misc.h函数库中*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//优先级分组
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
/*
选择输出线路,若有共用线路则需要在中断函数中判断是哪一个线路触发了中断
*/
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//优先级设置,具体查看分组
NVIC_Init(&NVIC_InitStructure);
总结:
1、配置GPIO端口(GPIO端口初始化,连接AFIO中断线)
2、配置EXTI(EXTI初始化)
3、配置NVIC(优先组设置、NVIC初始化)
3、中断函数配置
查看starup_stm32f103x中中断向量表的名字
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line14) == 1)//若标志位为1则进入中断
{
//CountSensor_Count++;
EXTI_ClearITPendingBit(EXTI_Line14);//清除标志位,否则会在中断卡死
}
}
代码配置
1、main函数
#include "stm32f10x.h" // Device header
#include "OLED.h"
#include "CountSensor.h"
int main(void)
{
OLED_Init();
CountSensor_Init();
OLED_ShowString(1,1,"Count");
while(1)
{
OLED_ShowNum(1,7,CountSensor_Get(),5);
}
}
2、CountSensor.c文件
#include "stm32f10x.h" // Device header
uint16_t CountSensor_Count;
void CountSensor_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_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line14;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
uint16_t CountSensor_Get(void)
{
return CountSensor_Count;
}
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line14) == 1)
{
CountSensor_Count++;
EXTI_ClearITPendingBit(EXTI_Line14);
}
}
3、CountSensor.h文件
#ifndef __COUNT_SENSOR_H
#define __COUNT_SENSOR_H
void CountSensor_Init(void);
uint16_t CountSensor_Get(void);
#endif