野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8811|回复: 1

STM32 外部 按键中断 问题

[复制链接]
发表于 2019-3-13 09:31:28 | 显示全部楼层 |阅读模式
各位大佬 是这样的 我想做一个 基于STM32F103C8T6 的按键外部中断 以GPIOB Pin 0 作为中断 选择 0中断线  ,GPIOA Pin 0 作为led 输出 主程序中 led 为灭的状态 ,监测到中断后 进行亮灯操作,经过操作,可以实现功能,但是测试的时候 发现 我按GPIOB 的Pin 1 引脚外连的按键 也可以引起中断,请问这是为什么呢,如何解决这个问题呢?

  1. [mw_shl_code=c,true]#include "stm32f10x.h"                  // Device header
  2. #include "stm32f10x_gpio.h"             // Keil::Device:StdPeriph Drivers:GPIO
  3. #include "stm32f10x_exti.h"             // Keil::Device:StdPeriph Drivers:EXTI

  4. void delay(u32 t)
  5. {
  6.   while(--t);
  7. }

  8. void ledGpioInitConfig()  //led初始化 选择GPIOA Pin_0 作为推挽输出
  9. {
  10.   GPIO_InitTypeDef GPIO_InitStructure;
  11.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  12.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  13.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  14.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  15.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  16. }

  17. void nvicInitConfig() //初始化 嵌套向量中断控制器
  18. {
  19.         NVIC_InitTypeDef NVIC_Initstructure;
  20.        
  21.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  22.        
  23.         NVIC_Initstructure.NVIC_IRQChannel = EXTI0_IRQn;
  24.         NVIC_Initstructure.NVIC_IRQChannelPreemptionPriority = 0;
  25.         NVIC_Initstructure.NVIC_IRQChannelSubPriority = 1;
  26.         NVIC_Initstructure.NVIC_IRQChannelCmd = ENABLE;
  27. }

  28. void extiGpioBPin1InitConfig() //初始化 外部中断配置 选择 GPIOA Pin 0 作为外部按键 且下降沿有效
  29. {
  30.   GPIO_InitTypeDef GPIO_InitStructure;
  31.   EXTI_InitTypeDef EXTI_InitStructure;

  32.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
  33.        
  34.         nvicInitConfig();
  35.        
  36.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  37.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  38.         GPIO_Init(GPIOB,&GPIO_InitStructure);
  39.        
  40.         EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  41.         EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  42.         EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  43.         EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  44.        
  45.         GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource0);
  46. }

  47. void EXTI0_IRQHandler() //中断服务程序控制 led亮
  48. {
  49.         if(EXTI_GetITStatus(EXTI_Line0) != RESET)
  50.         {
  51.                 GPIO_SetBits(GPIOA,GPIO_Pin_0);
  52.                 delay(0xFFFFF);
  53.                 EXTI_ClearFlag(EXTI_Line0);
  54.         }
  55.         EXTI_ClearITPendingBit(EXTI_Line0);
  56. }

  57. int main()
  58. {
  59.         ledGpioInitConfig();
  60.         extiGpioBPin1InitConfig();
  61.         while(1)
  62.         {
  63.                 GPIO_ResetBits(GPIOA,GPIO_Pin_0);
  64.                 delay(0xFFFFF);
  65.         }
  66. }
  67. [/mw_shl_code]
复制代码

回复

使用道具 举报

发表于 2019-3-13 16:08:56 | 显示全部楼层
用调试的方式看看有按PB1的时候有没有进中断
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-6 00:40 , Processed in 0.031456 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表