STM32 software key debounce

introduction

  Mechanical switches are typically elastomeric button switch used, when the mechanical contact is open, is closed, since the spring action of a mechanical contact, a key switch is not turned on immediately when closed stable, nor upon opening It will suddenly turned off completely, but closed off and the moment along a series of jitter, as shown in FIG.

    

analysis

  Closing button stabilization is determined by the length of time the operator, usually more than 100ms, it can quickly press deliberately reach about 40-50 ms, difficult to lower. Jitter is a time determined by the mechanical properties of keys, usually in less than 10ms, in order to ensure a program key or closed in response to disconnect only once, it must be processed key debounce.

method one

  In most cases, we use software program that is used to achieve the elimination of shaking. The easiest debounce principle, that is, when the detected key state change, wait for a delay time of about 10ms, so that once again shake the disappearance of key state detection, if the detected state with just the same, we can confirm button It has a stable operation. Practical applications, the limitations of this approach large (poor real-time).

Method Two

  Enabling a timer interrupt every 2ms into an interrupt, scan a key state and stored, after eight consecutive scans to see if it eight consecutive times in key state is the same. 8 key time is about 16ms, 16ms within this state if the key has been consistent, it is now possible to determine the keys in a stable stage, rather than in jitter phase, as shown in Figure 2.

    

Piece of code

//外部中断触发(下降沿)
void EXTI15_10_IRQHandler(void)
{
    if ( RESET != EXTI_GetFlagStatus( EXTI_Line5 ) )
    {
        static u32 lasttime = 0;

        if( (g_tickCount - lasttime) > 100 )
        {
            T_1min_cnt = TIM2OpenTime;
            TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
            lasttime = g_tickCount;
        }
        EXTI_ClearITPendingBit( EXTI_Line5 );
    }
}

 

/* Tim2 部分中断服务函数 */
void TIM2_IRQHandler(void)//250us
{   
    if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
        T_250us_cnt++;
        if ( T2_250us_cnt >= 8 )//2ms IRQ
        {
            T2_250us_cnt = 0;
            static u8 Keybuf1 = 0xff,WG_AKeyBuf = 0xff;

            Keybuf1 = ( ( Keybuf1 << 1 ) | GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_5) );// buffer left one, and the current value of the least significant bit shifted 

            IF ( 0x00 == Keybuf1) // . 8 consecutive scans are 0, i.e., are detected within 16 milliseconds pressed state, i.e., that key press 
            { 
                Key1Sta = 0 ; 
            } 
            the else  IF ( 0xFF == Keybuf1) // key bounce 
            { 
                Key1Sta = . 1 ; 
            } 
            the else // otherwise it indicates that the key state is not yet stabilized, no value is updated to the variable KeySta
                 // Key1Sta =. 1; / / default value    
        } 
        TIM_ClearITPendingBit (TIM2, TIM_FLAG_Update);  
    }
}

 

Guess you like

Origin www.cnblogs.com/jiangzhaowei/p/11237331.html