This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Initialize and assign buttons in SPPLEDemo

Hi,

I'm attempting to initialize and assign a button to trigger a function in the SPPLEDemo and I'm having problems. I would like to use the following code to put the LCD in standby by pressing a button on the exp-msp430f5438a experimenter board but it is not working. I've tried running it the mainthread() as well as inside the SPPLEDemo with no luck. Everything else works greats.

All I need to do is trigger a function by pressing a button. If there is an easier way please let me know.

#include hal_Buttons.h

#include hal_Board.h

volatile unsigned char buttonsPressed;

halButtonsInit(BUTTON_ALL);

halButtonsInterruptEnable(BUTTON_ALL);

buttonsPressed = halButtonsPressed();

 if (!buttonsPressed)

{

halLcdSetBackLight(0);

halLcdStandby();

}

Best regards,

Forrest Kimbriel

  • Hi Forrest,

    I didn't see hal_button.h in the SPPLEDemo, so I'm guessing you copied hal_button.c and hal_board.c in from the MSP-EXP430F5438 experimenter board demo software? For button functionality, that project uses not only hal_button.c and hal_board.c, but you'll also need a few more things from UserExperience_F5438A.c. You will need the PORT2_ISR, which is the interrupt handler for the GPIO ports - this will handle the button interrupts. You'll need startWDT() which is a function called by the PORT2_ISR for debouncing, and also the WDT_ISR which handles the WDT interrupt after the debouncing period ends.

    You'll need to add these things to your code. If you go to low power mode, and push a button, it will wake the device from low power mode and at that point you'll want to do your code that is checking the buttonsPressed variable. You can see something like this is done in the function activeMenuMode in UserExperience_F5438A.c.

    Regards,
    Katie
  • Hi Katie,

    I think I've added all the necessary code but, when I press the button it doesn't work.



    I've added the following code in the beginning of SPPLEDemo.c around line 200. Not inside any function.


    volatile unsigned char buttonsPressed;
    volatile unsigned char buttonDebounce;

    void startWDT()
    {
    //WDT as 250ms interval counter
    SFRIFG1 &= ~WDTIFG;
    WDTCTL = WDTPW + WDTSSEL_1 + WDTTMSEL + WDTCNTCL + WDTIS_5;
    SFRIE1 |= WDTIE;
    }

    #pragma vector=WDT_VECTOR
    __interrupt void WDT_ISR(void)
    {
    if (buttonDebounce == 2)
    {
    buttonDebounce = 1;
    SFRIFG1 &= ~WDTIFG;
    SFRIE1 &= ~WDTIE;
    WDTCTL = WDTPW + WDTHOLD;
    }
    }

    #pragma vector=PORT2_VECTOR
    __interrupt void Port2_ISR(void)
    {
    if (buttonDebounce == 1)
    {
    buttonsPressed = P2IFG;
    buttonDebounce = 2;
    startWDT();
    __bic_SR_register_on_exit(LPM3_bits);
    }
    else if (0 == buttonDebounce)
    {
    buttonsPressed = P2IFG;
    __bic_SR_register_on_exit(LPM4_bits);
    }

    P2IFG = 0;
    }






    I've added the following code under the function "static void UserInterface_Selection(void){ }"

    //Initialize buttons
    buttonDebounce = 1;
    halButtonsInit(BUTTON_ALL);
    halButtonsInterruptEnable(BUTTON_ALL);





    And lastly I've added the following code under the function "static void BTPSAPI GAP_LE_Event_Callback(unsigned int BluetoothStackID, GAP_LE_Event_Data_t *GAP_LE_Event_Data, unsigned long CallbackParameter)"

    if (buttonsPressed)
    {
    switch (buttonsPressed)
    {
    case BUTTON_UP: break;
    case BUTTON_DOWN: break;
    case BUTTON_SELECT: break;
    case BUTTON_S2: halLcdSetBackLight(0);halLcdStandby(); break;
    case BUTTON_S1: halLcdSetBackLight(0);halLcdStandby(); break;
    default: break;
    }
    // timeOutCounter = 0;
    }



    I get no errors but, it doesn't work when I press s1 or s2 buttons. Is there a simpler way to just initialize a button and use it in the code to trigger a function?

    Best regards,
    Forrest Kimbriel
  • Hi Forrest,

    Unfortunately I'm not familiar at all with the Bluetopia code, so I'm not sure if your last part (switch statement for checking which button is pressed) should really go in the GAP_LE_Event_Callback that you mentioned - I'd guess this is not the right place. I think you will want to post in the Bluetooth CC256x forum:

    They should know their scheduler better to know where you should add the code to check button presses after the button ISR has fired. I think you'll have the same problem even with simpler button code if we don't figure out the right place to put the button handling in relation to the Bluetopia stack scheduler.

    Regards,

    Katie

**Attention** This is a public forum