找回密码
 立即注册

QQ登录

只需一步,快速开始

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

hk32f030m单片机定时器消抖,解决按键按下读取电平抖动

[复制链接]
跳转到指定楼层
楼主
ID:302293 发表于 2023-9-21 11:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
      用定时器按一定时间间隔扫描按键

  1. #include "main.h"
  2. #define A3_OFF GPIO_SetBits(GPIOA,GPIO_Pin_3);
  3. #define A3_ON GPIO_ResetBits(GPIOA,GPIO_Pin_3);
  4. #define u8 uint8_t
  5. #define KEY0_Read   GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)
  6. unsigned char key0_flag = RESET;
  7. #define BSRR_VAL 0x0006
  8. uint8_t a=9;
  9. /* Private macro -------------------------------------------------------------*/
  10. /* Private variables ---------------------------------------------------------*/
  11. GPIO_InitTypeDef        GPIO_InitStructure;
  12. u8 Key_left=1,Key_up,Key_right, Key_down;
  13. /* Private function prototypes -----------------------------------------------*/
  14. /* Private functions ---------------------------------------------------------*/
  15. static uint8_t fac_us = 0;
  16. static uint16_t fac_ms = 0;
  17. /**
  18.   * @brief  Main program.
  19.   * @param  None
  20.   * @retval None
  21.   */


  22. void KEY_Scan(void);
  23. void Delay(__IO uint32_t nCount)
  24. {
  25.   for(; nCount != 0; nCount--);
  26. }

  27. void delay_ms(uint16_t nms)
  28. {                                    
  29.         uint32_t temp;        
  30.    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
  31.     fac_us = SystemCoreClock/8000000;
  32.     fac_ms = (uint16_t)fac_us*1000;
  33.   if( nms > 0 )
  34.   {
  35.     SysTick->LOAD=(uint32_t)nms*fac_ms;             //时间加载(SysTick->LOAD为24bit)
  36.     SysTick->VAL =0x00;                             //清空计数器
  37.     SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;        //开始倒数  
  38.     do
  39.     {
  40.       temp=SysTick->CTRL;
  41.     }
  42.     while(temp&0x01&&!(temp&(1<<16)));               //等待时间到达   
  43.     SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;         //关闭计数器
  44.     SysTick->VAL =0X00;                              //清空计数器                     
  45.   }
  46. }

  47. void KEY_GPIO_Init(void)
  48. {
  49.   GPIO_InitTypeDef GPIO_InitStructure;
  50.   
  51.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  52.   
  53.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  54.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  55.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  56.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  57.   GPIO_InitStructure.GPIO_Schmit = GPIO_Schmit_Disable;
  58.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  59.   
  60.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  61. }
  62. void GPIO_CONIG()
  63. {
  64.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  65.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7| GPIO_Pin_2| GPIO_Pin_3;
  66.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  67.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  68.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  69.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  70.         
  71.         
  72.         GPIO_Init(GPIOC, &GPIO_InitStructure);


  73. }
  74. void TIM2_Configuration(void)
  75. {
  76.    
  77.   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  78.   TIM_OCInitTypeDef  TIM_OCInitStructure;
  79.   NVIC_InitTypeDef NVIC_InitStructure;
  80.   
  81.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  82.   
  83.   /* Time base configuration */
  84.   TIM_TimeBaseStructure.TIM_Period = 842;    // 32M/38K = 842.10
  85.   TIM_TimeBaseStructure.TIM_Prescaler = 0;
  86.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  87.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  88.   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  89.   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  90.   /* PWM1 Mode configuration: Channel2 */
  91.   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  92.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  93.   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
  94.   TIM_OCInitStructure.TIM_Pulse =421;
  95.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  96.   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

  97.   TIM_OC1Init(TIM2, &TIM_OCInitStructure);

  98.   TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);

  99.   TIM_ARRPreloadConfig(TIM2, ENABLE);
  100.   
  101.   //中断优先级NVIC设置
  102.         NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  103.         NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  104.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  105.         NVIC_Init(&NVIC_InitStructure);  
  106.   
  107.   TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

  108.   /* TIM2 enable counter */
  109.   TIM_Cmd(TIM2, ENABLE);   
  110.   
  111.   TIM_CtrlPWMOutputs(TIM2, ENABLE);
  112. }
  113. //定时器3中断服务程序
  114. void TIM2_IRQHandler(void)   //TIM2中断
  115. {
  116.         if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)  //检查TIM2更新中断发生与否
  117.     {
  118.                     TIM_ClearITPendingBit(TIM2, TIM_IT_Update  );  //清除TIMx更新中断标志
  119. //        NEC_IR_Remote_Periodic_Task();
  120.         KEY_Scan();
  121.     }
  122. }



  123. void KEY_Scan(void)
  124. {
  125.   static unsigned int key0_cnt = 0;
  126.   
  127.   if( RESET == KEY0_Read )
  128.   {
  129.     key0_cnt ++;
  130.    
  131.     if( 380 == key0_cnt )
  132.     {
  133.       key0_flag =1;
  134.     }
  135.    
  136.     if( key0_cnt > 38000 )
  137.     {
  138.       key0_cnt = 38000;
  139.     }
  140.   }
  141.   else
  142.   {
  143.     key0_cnt = 0;
  144.   }
  145. }
  146. int main(void)
  147. {

  148. KEY_GPIO_Init();
  149. TIM2_Configuration();        
  150. GPIO_CONIG();
  151.           GPIO_SetBits(GPIOC,GPIO_Pin_7);
  152.         while(1)
  153.         {


  154. //    GPIO_SetBits(GPIOC,GPIO_Pin_7);
  155. //          Delay(838709);//500ms
  156.                

  157.                
  158. //                Delay(838709);

  159. //else if(key!=1)
  160. //           GPIO_ResetBits(GPIOC,GPIO_Pin_7);

  161.           if( 1 == key0_flag )

  162.            {
  163.                 GPIO_Toggle(GPIOC,GPIO_Pin_7); key0_flag=0;
  164.                  }
  165.         
  166.         }

  167. }


复制代码

原理图: 无
仿真: 无
代码: GPIO_IOToggle.7z (19.72 KB, 下载次数: 1)
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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