Skip to main content

Keypress Debounce MSP430

 Normally keypresses are not direct change from one state to other , instead its a change with bit noise in between where the state seems undefined. To avoid the situation the common method is to recheck after certain delay 

if(PINState==Pressed)

{
delay(few_microsec);
if(PINState==Pressed)
    {
    ActionKeyPress();
    }
}

But if we are dealing with interrupt

PIN_ISR()
{
setkeypressflag();
}

main()
{
if (keypressflagset)
    if(PINState==Pressed)
    {
    DoKeyPressAction();
    }
}

Now the most advanced one with timer or WatchdogTimer

#pragma vector=PORT1_VECTOR
__interrupt void P1_Function()
{
    count=0; //Reset count
    TACTL|=TASSEL_2+MC_1+TAIE; //Start Timer0 with SMCLK clock source, UP mode and enable overflow interrupt
    state=(P1IN&BIT3)>>3; //Save the state of the switch
    P1IE&=~BIT3; //Disable interrupt on P1.3, now the Timer will take care of Debouncing
    P1IFG&=~BIT3; // Reset Port1 interrupt flag
}

#pragma vector=TIMER0_A1_VECTOR
__interrupt void TMR0()
{
    if(TAIV==TA0IV_TAIFG)   //Check if Timer overflow caused the interrupt
                            //This would be required in projects where multiple interrupts have
                            //the same interrupt vector. Here it is only optional.
        {
            if(state==((P1IN&BIT3)>>3)) //If the state of the LED is the same
                count++; //Increment the counter variable
            else
            {
                count=0; //If not same, reset the counter variable
                state=((P1IN&BIT3)>>3); //And save the present state of the switch
            }
        if(count==10) //If the state has been consistently the same
            {
            if(state==0) //If the switch was pressed
                P1OUT^=BIT0; //Toggle the LED
            P1IE|=BIT3; //We have handled the debouncing, now we again enable interrupt on P1.3, for it to again detect switch bounce
            TACTL=0; //Stop the Timer
            TACTL|=TACLR; //Clear the Timer counter
            }

            TACTL&=~(TAIFG); //Reset the interrupt flag
        }
}

Comments

Popular posts from this blog

Questions

1. What is a Stub function? Ans : A function without any definition which presents no error when called. 2. Why there are two ld scripts generated by STM32Cube IDE? Ans: The CubeIDE always presents two different LDscripts on for generating the executable for Debugging which goes to RAM another for normal code upload that goes to flash memory. 3. What is Supervisory Mode/ privileged mode ? Ans : When you are using a Kernel then there are two modes user mode and privileged mode. In User mode the applications cant have system calls. 4. What is "make -j6" ? Ans : Make has an argument for number of jobs, so when you add -j6 it will create 6 different compiler instances, so that the systems could use multicore to the fullest.  Linux people generally do add "-j $(nproc)" where nproc is a command line which returns number of processors attached. 5. What is weak attribute ? Ans : Weak attribute used to denote weak symbols which helps linkers to choose one function out of mul...

#@$%

  #include  "msp430x21x2.h"     #define PWM_OUTPUT              0x03 #define DUTY_CYCLE              TA0CCR2 #define MAX_LIMIT               300   /* #define no_of_samples           1 #define duty_cycle_limit        1000 #define temp_sensor             0xA000   #define panel_selection_cutoff  250 */ #define PV_VOLTAGE              INCH_0 #define PV_CURRENT              INCH_1 #define BATT_VOLTAGE            INCH_2 #define BATT_CURRENT            INCH_3   #define UPPER_CRG_LVL           0x0105 // 0x00FF // 0x00F5 //0x023A #define LOWER_CRG_LVL   ...

Preparing for embedded systems interview

  Preparing for a technical interview in embedded systems involves a wide range of topics, as the field encompasses both hardware and software components. Here are some key areas and example questions that you might encounter in such an interview: Basic Concepts and Theory: What is an embedded system? Can you explain the difference between a microprocessor and a microcontroller? Describe the various types of memory in an embedded system. Programming and Software Design: 4. How do you write an interrupt service routine in C? 5. Explain the concept of a real-time operating system (RTOS). How does it differ from a general-purpose operating system? 6. What are the different states of a thread in an RTOS? Hardware and Interfacing : 7. How does SPI communication work? What about I2C communication? 8. Explain the concept of GPIO (General-Purpose Input/Output). How would you use it in an embedded application? 9. What are interrupts, and how are they handled in embedded systems? Low-Level P...