学前班
最后登录1970-1-1
在线时间 小时
注册时间2018-10-19
|
最近刚开始学习STM32,学习到了按键中断这一章节,写了一段代码,但是按键没有反应,不知道哪里除了问题,LED灯是PB0,按键是PA0头文件
#ifndef __KEYIT__H__
#define __KEYIT__H__
#include"stm32f10x.h"
#define LED_T (GPIOB->ODR^=GPIO_Pin_0)
void RCC_Config(void);
void LED_Config(void);
void NVIC_Config(void);
void EXTI_Config(void);
#endif /*__KEYIT__H__*/
#include"keyit.h"
#include"stm32f10x_gpio.h"
#include"stm32f10x_rcc.h"
#include"stm32f10x_exti.h"
#include"misc.h"
#include"stm32f10x.h"
//RCC时钟初始化
void RCC_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
}
//LED配置函数
void LED_Config(void)
{
GPIO_InitTypeDef GPIO_Initstructure;
GPIO_Initstructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Initstructure.GPIO_Pin=GPIO_Pin_0;
GPIO_Initstructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_Initstructure);
}
//NVIC
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_Initstructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_Initstructure.NVIC_IRQChannel=EXTI2_IRQn;
NVIC_Initstructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_Initstructure.NVIC_IRQChannelSubPriority=1;
NVIC_Initstructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_Initstructure);
}
//EXTI
void EXTI_Config(void)
{
EXTI_InitTypeDef EXTI_Initstructure;
GPIO_InitTypeDef GPIO_Initstructure;
NVIC_Config();
GPIO_Initstructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Initstructure.GPIO_Pin=GPIO_Pin_0;
GPIO_Init(GPIOA,&GPIO_Initstructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_Initstructure.EXTI_Line=EXTI_Line2;
EXTI_Initstructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_Initstructure.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Initstructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_Initstructure);
}
//main函数
#include"stm32f10x.h"
#include"keyit.h"
int main(void)
{
RCC_Config();
LED_Config();
EXTI_Config();
GPIO_SetBits(GPIOB,GPIO_Pin_0);
while(1)
{
}
}
//中断处理函数
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2)!=RESET)
{
LED_T ;
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
|
|