STM32通过按键中断点亮led灯

这是我学习的一些总结性作业

首先是led头文件,其中调用的一些库是原子的我只是借鉴一下 侵删

#ifndef __LED_H
#define __LED_H	 
#include "sys.h"

#define LED PCout(13)	// PA8


void LED1_Init(void);//³õʼ»¯

		 				    
#endif

led的c文件

#include "led.h"


//初始化PB5和PE5为输出口.并使能这两个口的时钟		    
//LED IO初始化
void LED1_Init(void)
{
 
 GPIO_InitTypeDef  GPIO_InitStructure;
 	
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);	 //使能PC端口时钟
	
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;				 //LED0-->PA.8 端口配置
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO口速度为50MHz
 GPIO_Init(GPIOA, &GPIO_InitStructure);					 //根据设定参数初始化GPIOA.8
 
 GPIO_Init(GPIOC, &GPIO_InitStructure);	  				 //推挽输出 ,IO口速度为50MHz
}

之后是中断的头文件

#ifndef _EXTI_H_
#define _EXTI_H_
#include "sys.h"
void EXTI_KEY_Init(void);//³õʼ»¯
           
#endif

中断的c文件

#include "EXTI.h"

void EXTI_KEY_Init(void)
{
//配置PB11为下降沿触发
GPIO_InitTypeDef GPIO_InitStructure;
	EXTI_InitTypeDef EXTI_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
//配置管脚时钟和复用时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
//配置GPIO口为上拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;				 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 		
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		
GPIO_Init(GPIOC, &GPIO_InitStructure);	
//配置GPIO为外部中断模式
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource11);
	
	//配置EXTI中断触发模式

	EXTI_InitStructure.EXTI_Line = EXTI_Line11;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);

//配置EXTI中断优先等级
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);


}

主函数

#include "sys.h"
#include "EXTI.h"
#include "led.h"
#include "KEY.h"
#include "delay.h"
u8 t=0;	  
int main(void)
{
	
	delay_init();

NVIC_Configuration();
LED1_Init();
EXTI_KEY_Init();

	while(1)
	{ 
		
	LED = 1;
	
	
	}
}
经验分享 程序员 微信小程序 职场和发展