找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1173|回复: 7
打印 上一主题 下一主题
收起左侧

STM32单片机外部中断按键抖动不好消除,有什么办法解决?

[复制链接]
跳转到指定楼层
楼主
ID:302293 发表于 2023-9-20 16:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/**
  ******************************************************************************
  * @file    main.c
  * @author  Alexander
  * @version V1.0.0
  * @date    04-January-2023
  * @brief   Main program body

  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/** @addtogroup HK32F030Mxx_StdPeriph_Examples
  * @{
  */
        
/** @addtogroup EXTI_Example
  * @{
  */        

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
EXTI_InitTypeDef   EXTI_InitStructure;
GPIO_InitTypeDef   GPIO_InitStructure;
NVIC_InitTypeDef   NVIC_InitStructure;

/* Private function prototypes -----------------------------------------------*/
static void EXTI4_Config(void);
static void EXTI1_Config(void);

void Delay(__IO uint32_t nCount)
{
  for(; nCount != 0; nCount--);
}
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (KEIL_startup_hk32f030m.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_hk32f030m.c file
     */
  /* Initialize LEDs mounted on EVAL board        */
  HK_EVAL_LEDInit(LED1);
        HK_EVAL_LEDInit(LED2);
        HK_EVAL_LEDInit(LED3);
        
        /* Configure PC4 in interrupt mode */
        EXTI4_Config();
        
        /* Configure PA1 in interrupt mode */
        EXTI1_Config();
        
  /*  Delay(838709/5);Infinite loop */
  while (1)
  {
Delay(838709/5);
  }
}

/**
  * @brief  Configure PC4 in interrupt mode
  * @param  None
  * @retval None
  */
static void EXTI4_Config(void)
{
  /* Enable GPIOC clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

  /* Configure PC4 pin as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* Enable SYSCFG clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  /* Connect EXTI4 Line to PA0 pin */
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource4);

  /* Configure EXTI4 line */
  EXTI_InitStructure.EXTI_Line = EXTI_Line4;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  /* Enable and set EXTI4 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/**
  * @brief  Configure PA1 in interrupt mode
  * @param  None
  * @retval None
  */
static void EXTI1_Config(void)
{
  /* Enable GPIOA clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  /* Configure PA1 pins as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Enable SYSCFG clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

  /* Connect EXTI1 Line to PA1 pin */
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource1);


  /* Configure EXTI1 line */
  EXTI_InitStructure.EXTI_Line = EXTI_Line1;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  /* Enable and set EXTI1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}



void EXTI1_IRQHandler(void)
{
  if(EXTI_GetITStatus(EXTI_Line1) != RESET)
  {


                /* Toggle LED2 */
    HK_EVAL_LEDToggle(LED2);

    /* Clear the EXTI line 0 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line1);
  }
}

/**
  * @brief  This function handles External line 4 interrupt request.
  * @param  None
  * @retval None
  */
void EXTI4_IRQHandler(void)
{
  if(EXTI_GetITStatus(EXTI_Line4) != RESET)
  {
    /* Toggle LED1 */
    HK_EVAL_LEDToggle(LED1);

    /* Clear the EXTI line 4 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line4);
  }
}
















#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */

/**
  * @}
  */


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:634587 发表于 2023-9-21 08:32 | 只看该作者
加个时间判断,一定毫秒内的触发忽略掉。
回复

使用道具 举报

板凳
ID:161164 发表于 2023-9-21 09:51 | 只看该作者
按键用啥中断?用定时扫描不是更好吗?
一定要用中断就在进中断后关闭中断
等引脚电平恢复空闲时再开启中断
回复

使用道具 举报

地板
ID:829222 发表于 2023-9-22 10:09 | 只看该作者
按钮电平状态为0时,触发状态ButtonFlag为1,然后再在定时器中断中进行时钟累加,如果在任意时刻,按钮状态为1,复位ButtonFlag为0,清空定时器累加数据,当定时器累加数据大于设定阈值,判断为按钮按下有效,我一直都是这么处理的
回复

使用道具 举报

5#
ID:517951 发表于 2023-9-25 07:28 | 只看该作者
可以增加一个1nf的电容到地, 做下硬件滤波. 成本没多少, 效果还可以,对软件延时就没那么要求高了.
回复

使用道具 举报

6#
ID:883242 发表于 2023-9-25 16:41 | 只看该作者
就不应该用EXTI,用TIM。
回复

使用道具 举报

7#
ID:1034262 发表于 2023-9-25 17:19 | 只看该作者
按键这种慢速事件就不应该用外中断。
回复

使用道具 举报

8#
ID:1080935 发表于 2023-9-26 08:32 | 只看该作者
确实不该用中断处理按键,状态机解决,定时或延时均可,延时可以用循环中程序的时间。
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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