Bravo
Bravo

Reputation: 31

Guide for multiple function for 1 button of MSP430F427 microcontroller

I have 2 buttons, and each button has multiple function. They are pulled-up externally. I am using external oscillator 32kHz.

Button A (P1.2)

functions of button A:

  1. a press on Button A would make the device be on

  2. a hold for 5 seconds will turn off the device

  3. a press on Button A (while on counting operation) would reset the counter

Button B (P1.3) functions of button B:

  1. a press on Button B will light LED (P1.1) and LED will turn off after 30 seconds.

  2. a hold for 5 seconds will change its unit count (inches on first hold, cm in the next hold)

for now I have tried the button B as it is more easy for me for it does not involve LPMx/sleep mode.

volatile uint16_t ctr1 = 0;
volatile uint16_t ctr2 = 0; 
volatile uint16_t ctr3 = 0;    //counter for LED ON

void Init_Timer_A (void) 
{ 
// Timer_A0 Configuration 
TA0CCTL0 = CCIE;             // Enable Timer_A interrupt 
TA0CCR0 = 33;                // Set the Timer_A count limit 1ms intervals 
TA0CTL = TASSEL_1 + MC_1;    // ACLK from watch crystal 32.768 kHz, Up Mode
}

pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A (void) 
{ 
if (counter1_enable_flag) 
    { 
    if (ctr1 <= 5000) 
    { 
        ctr1++; 
        if (ctr1 >= 100) 
        { 
            ctr3++; 
            if (ctr3 <= 4000) 
            { 
                P1OUT |= BIT1; 
            } 
            else 
            { 
                P1OUT &= ~(BIT1); 
            }
        }
    }

    else
    {
        P1OUT |= BIT1;
        ctr1 = 0;
    }

    }
}

int main(void) 
{ 
    initialize(); 
    GPIO_init(); 
    Init_Timer_A();

    __enable_interrupt();

    while (1)
    {
        if ((P1IN & BIT2) == 0)    // 1-0 | Button1 (P1.2) is pressed
        {
        counter1_enable_flag = 1;
        }
    }
}

The goal is to make the LED light up for 30 seconds for a press of button, and when I hold the button for 5 seconds (it should do a certain function) but to make it simple I just want it first to make the LED stay on/high.

if (ctr3 <= 4000) in this line of code, I can only light up the LED for 4 seconds, when I make it 5000, it stays on and doesn't turn off.

Moreover, if I include the else statement, I can see that it LED blink every 5 seconds. Also, if I push the button it does not light up for 4 seconds anymore. It just blink every 5 seconds.

If I exclude the else statement, the first push of button lights up the LED for 4 seconds and it doesn't blink every 5 seconds, however, it does not light up if I pushed the button it the next time.

Upvotes: 0

Views: 30

Answers (0)

Related Questions