首页 > 其他分享 >STM32外部中断(EXTI)控制LED亮灭的代码

STM32外部中断(EXTI)控制LED亮灭的代码

时间:2022-12-16 07:44:21浏览次数:42  
标签:NVIC LED 亮灭 void STM32 InitStruct GPIO EXTI

led.c

#include "led.h"

void LED_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);
}

void LED_Toggle(void)
{
	GPIOB->ODR ^= GPIO_Pin_0;
}

led.h

#ifndef __LED_H
#define __LED_H

#include "stm32f10x.h"

void LED_Config(void);
void LED_Toggle(void);

#endif

exti.c

#include "exti.h"
#include "led.h"

static void NVIC_Config(void)
{
	NVIC_InitTypeDef NVIC_InitStruct;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitStruct.NVIC_IRQChannel=EXTI0_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
	NVIC_Init(&NVIC_InitStruct);
}


void EXTI_Config(void)
{
	EXTI_InitTypeDef EXTI_InitStruct;
	
	NVIC_Config();
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	
	EXTI_InitStruct.EXTI_Line=EXTI_Line0;
	EXTI_InitStruct.EXTI_LineCmd=ENABLE;
	EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling;
	EXTI_Init(&EXTI_InitStruct);
}

void EXTI0_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line0)) LED_Toggle();
	EXTI_ClearITPendingBit(EXTI_Line0);
}

exti.h

#ifndef __EXTI_H
#define __EXTI_H

#include "stm32f10x.h"

void EXTI_Config(void);

#endif

main.c

#include "stm32f10x.h"
#include "led.h"
#include "exti.h"

int main(void)
{
	LED_Config();
	EXTI_Config();
	
	while(1)
	{
	
	}
}

标签:NVIC,LED,亮灭,void,STM32,InitStruct,GPIO,EXTI
From: https://www.cnblogs.com/Sound-Sleep/p/16986435.html

相关文章