外部中断配置
#include "stm32f10x.h" // Device header
uint16_t CountSensor_Count;
void CountSensor_Init(void)
{
//RCC--> GPIO--> AFIO--> EXTI--> NVIC五步
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //开启GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //开启AFIO时钟
/******************************************
EXTI和NVIC默认时钟是打开的
******************************************/
//配置GPIOB端口模式和初始化
GPIO_InitTypeDef GPIO_InitStructure; //定义一个结构体变量
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //配置GPIO的模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; //选择需要的GPIO
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;// 配置GPIO的速度
GPIO_Init(GPIOB,&GPIO_InitStructure); //对GPIO进行初始化
//配置AFIO端口模式和初始化
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14); //打通AFIO的第14根数据选择器,开始进入EXTI电路
//配置EXTI的电路
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的电路
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //NVIC分组
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级(分组2)
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级
NVIC_Init(&NVIC_InitStructure);
}
uint16_t CountSensor_Get(void)
{
return CountSensor_Count;
}
void EXTI15_10_IRQHandler(void) //中断函数的入口函数在Start中的startup_stm32f10x_md.s里面
{
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0)
{
CountSensor_Count++;
}
EXTI_ClearITPendingBit(EXTI_Line14);
}
int main(void)
{
OLED_Init();
CountSensor_Init();
OLED_ShowString(1,1,"Count:");
while (1)
{
OLED_ShowNum(1,7,CountSensor_Get(),5);
}
}
实验总结:本实验主要利用外部中断来对传感器模块进行计次,外部中断触发计次累计,通过检测B14的下降沿来触发计次!