03:【江科大stm32】:外部中断EXTI的使用

外部中断EXTI的使用

  • 1、按钮通过外部中断控制LED
  • 2、旋转编码器计次

1、按钮通过外部中断控制LED

/*
	外部中断按键控制LED,使用外部中断EXTI0进行按键控制LED的亮灭
*/
#include "stm32f10x.h" 
int main(void)
{
	//1.对PB0引脚进行配置,按键连接的引脚
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitTypeDef GPIOInitStruct;
	GPIOInitStruct.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
	GPIOInitStruct.GPIO_Pin = GPIO_Pin_0;
	
	GPIO_Init(GPIOB,&GPIOInitStruct);
	
	//对PA0进行配置,LED连接的引脚
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIOInitStruct.GPIO_Mode = GPIO_Mode_Out_OD;//开漏输出
	GPIOInitStruct.GPIO_Pin = GPIO_Pin_0;
	GPIOInitStruct.GPIO_Speed = GPIO_Speed_2MHz;
	
	GPIO_Init(GPIOA,&GPIOInitStruct);
	
	//2.对AFIO进行配置,AFIO的功能:①复用功能重映射,②中断引脚选择
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);//选择PB0通道进行外部中断
	
	//3.对EXTI进行配置
	EXTI_InitTypeDef EXTIInitStruct;
	EXTIInitStruct.EXTI_Line = EXTI_Line0;//选择EXTI0
	EXTIInitStruct.EXTI_Mode = EXTI_Mode_Interrupt;//中断模式
	EXTIInitStruct.EXTI_Trigger = EXTI_Trigger_Rising;//上升沿触发中断
	EXTIInitStruct.EXTI_LineCmd = ENABLE;//使能中断屏蔽
	
	EXTI_Init(&EXTIInitStruct);
	
	//4.对NVIC进行配置
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//优先级的分组,0抢占,4子优先
	NVIC_InitTypeDef NVICInitStruct;
	NVICInitStruct.NVIC_IRQChannel = EXTI0_IRQn;//选择中断来源
	NVICInitStruct.NVIC_IRQChannelPreemptionPriority = 0;//抢占为0
	NVICInitStruct.NVIC_IRQChannelSubPriority = 0;//子优先级为0
	NVICInitStruct.NVIC_IRQChannelCmd = ENABLE;//使能NVIC
	
	NVIC_Init(&NVICInitStruct);
	while(1)
	{
		
	}
}

//中断函数
void EXTI0_IRQHandler(void)
{
	if(EXTI_GetFlagStatus(EXTI_Line0) == SET)//判断EXTI0中断挂起寄存器的值
	{
		EXTI_ClearITPendingBit(EXTI_Line0);//EXTI0的中断挂起寄存器置位
		if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_0) == RESET)//如果是点亮的
		{
			GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);//熄灭
		}
		else
		{
			GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);//点亮
		}
	}
}

2、旋转编码器计次

/*
	通过外部中断使用旋转编码器计次
*/

#include "stm32f10x.h"                 
#include "OLED.h"

uint32_t count = 0;
int main(void)
{
	OLED_Init();
	OLED_Clear();
	
	//1.旋转编码器的B连接单片机的PB1,A连接单片机的PB0,对引脚的初始化
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitTypeDef GPIOInitStruct;
	GPIOInitStruct.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
	GPIOInitStruct.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1;
	GPIO_Init(GPIOB,&GPIOInitStruct);
	
	//2.对AFIO进行配置
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
	
	//3.对EXTI进行配置
	EXTI_InitTypeDef EXTIInitStruct;
	EXTIInitStruct.EXTI_Line = EXTI_Line0 | EXTI_Line1;
	EXTIInitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTIInitStruct.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发
	EXTIInitStruct.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTIInitStruct);
	
	//4.对NVIC进行配置
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
	NVIC_InitTypeDef NVICInitStruct;
	NVICInitStruct.NVIC_IRQChannel = EXTI0_IRQn;
	NVICInitStruct.NVIC_IRQChannelPreemptionPriority = 0;
	NVICInitStruct.NVIC_IRQChannelSubPriority = 0;
	NVICInitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVICInitStruct);
	
	NVICInitStruct.NVIC_IRQChannel = EXTI1_IRQn;
	NVICInitStruct.NVIC_IRQChannelPreemptionPriority = 0;
	NVICInitStruct.NVIC_IRQChannelSubPriority = 0;
	NVICInitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVICInitStruct);
	
	while(1)
	{
		OLED_ShowSignedNum(1,1,count,5);
	}
}

//中断函数
void EXTI0_IRQHandler(void)
{
	if(EXTI_GetFlagStatus(EXTI_Line0) == SET)
	{
		EXTI_ClearITPendingBit(EXTI_Line0);//清除中断状态
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0)
			 count++;
	}
}

void EXTI1_IRQHandler(void)
{
	if(EXTI_GetFlagStatus(EXTI_Line1) == SET)
	{
		EXTI_ClearITPendingBit(EXTI_Line1);//清除中断状态
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == 0)
		  count--;
	}
}

你可能感兴趣的:(#,江科大【标准库】,stm32,单片机,嵌入式硬件)