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.

MSP430FR2532: Adding a longer delay (30 seconds) to LED2 while CAPT_appHandler() is active (Capacitive button)

Part Number: MSP430FR2532

Good Day Ti Forum,

I am attempting to add a longer delay to LED2 within the code below. I have attempted using __delay_cycles but maximum I can get up to is around 15 seconds. If I increase the value, the code compiles but doesnt function.

What I am trying to achieve is as follows,

1. User touches button (which goes into CAPT_apphandler())

2. Output LED2 (30 seconds always on, no toggle) AND Output LED1 (30 seconds, but rapid toggle, to reduce power consumption

3. Go back to sleep.

This is my code below;

/* --COPYRIGHT--,BSD
 * Copyright (c) 2017, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * --/COPYRIGHT--*/
//*****************************************************************************
// Development main.c for MSP430FR2633, MSP430FR2533, MSP430FR2632, and
// MSP430FR2532.
//
// This starter application initializes the CapTIvate touch library
// for the touch panel specified by CAPT_UserConfig.c/.h via a call to
// CAPT_appStart(), which initializes and calibrates all sensors in the
// application, and starts the CapTIvate interval timer.
//
// Then, the capacitive touch interface is driven by calling the CapTIvate
// application handler, CAPT_appHandler().  The application handler manages
// whether the user interface (UI) is running in full active scan mode, or
// in a low-power wake-on-proximity mode.
//
// The CapTIvate application handler will return true if proximity was
// detected on any of the sensors in the application, which is used here
// to control the state of LED2. LED1 is set while the background loop enters
// the handler, and is cleared when the background loop leaves the handler.
//
// \version 1.83.00.05
// Released on May 15, 2020
//
//*****************************************************************************

#include <msp430.h>                      // Generic MSP430 Device Include
#include "driverlib.h"                   // MSPWare Driver Library
#include "captivate.h"                   // CapTIvate Touch Software Library
#include "CAPT_App.h"                    // CapTIvate Application Code
#include "CAPT_BSP.h"                    // CapTIvate EVM Board Support Package

void main(void)
{
	//
	// Initialize the MCU
	// BSP_configureMCU() sets up the device IO and clocking
	// The global interrupt enable is set to allow peripherals
	// to wake the MCU.
	//
	WDTCTL = WDTPW | WDTHOLD;
	BSP_configureMCU();
	__bis_SR_register(GIE);

	//
	// Start the CapTIvate application
	//
	CAPT_appStart();

	//
	// Background Loop
	//
	while(1)
	{
		//
		// Run the captivate application handler.
		// Set LED1 while the app handler is running,
		// and set LED2 if proximity is detected
		// on any sensor.
		//
		
		if(CAPT_appHandler()==true)
		{
			LED2_ON;
			__delay_cycles(300000000);
			
		}
		else
			LED2_OFF;
		
		
		//
		// This is a great place to add in any 
		// background application code.
		//
		__no_operation();

		//
		// End of background loop iteration
		// Go to sleep if there is nothing left to do
		//
		CAPT_appSleep();
		
	} // End background loop
} // End main()

Any support/guidance will be appreciated.

  • Hi Ty,

    Here is a simple way to accomplish this provided the MCU runs in active mode only.  If you are using the WOP "wake-on-proximity" feature, change the CAPT_UserConfig.c application parameter .ui16InactivityTimeout = _30SEC = 1500. Doing this will force the MCU to remain in active mode long enough to get your desired effect.

    uint16_t LED_count;
    bool TouchFlag = false;
    
    #define _30SEC               (909)
    #define _125mSEC             (8)
    
    void main(void)
    {
    	//
    	// Initialize the MCU
    	// BSP_configureMCU() sets up the device IO and clocking
    	// The global interrupt enable is set to allow peripherals
    	// to wake the MCU.
    	//
    	WDTCTL = WDTPW | WDTHOLD;
    	BSP_configureMCU();
    	__bis_SR_register(GIE);
    
    	//
    	// Start the CapTIvate application
    	//
    	CAPT_appStart();
    
    	//
    	// Background Loop
    	//
    	while(1)
    	{
    	    if(CAPT_appHandler()==true)
    	    {
    
    	        if(TouchFlag == false)
                {
                    TouchFlag = true;
                    LED1_ON;
                    LED2_ON;
                }
    	    }
    
    	    if(TouchFlag == true)
    	    {
    	        LED_count++;
    
                /* Time to toggle LED1? */
                if(LED_count % _125mSEC == 0)
                {
                    LED1_TOGGLE;
                }
    
                /* Time to turn off both LEDs */
                if(LED_count == _30SEC)
                {
                    LED1_OFF;
                    LED2_OFF;
                    TouchFlag = false;
                    LED_count = 0;
                }
    	    }
    
    		//
    		// This is a great place to add in any 
    		// background application code.
    		//
    		__no_operation();
    
    		//
    		// End of background loop iteration
    		// Go to sleep if there is nothing left to do
    		//
    		CAPT_appSleep();
    		
    	} // End background loop
    } // End main()
    

  • Hi Dennis, thank you. I have updated and seems to be working as expected.

    So to clarify, I needed to adjust the CAPT_UserConfig.c inactivity timeout parameter according to however long I wish to have LED1 and LED2 active. Three questions;

    Q1 Combined with above adjustment I will have to adjust the below;

    #define _30SEC               (909)
    #define _125mSEC             (8)

    How do I change these constants? (I tried changing the 8 to a lower and higher number as the led toggle is too slow, and the device stopped recognizing my touch. I need it to flash rapidly)

    Q2

    Does the inactivity timeout parameter being increased have an effect on the power consumption? Im guessing yes.

    Q3

    What if I want the LED1 to rapidly blink for say 2-3 seconds (in the interest of optimizing power consumption) whereas the LED2 remains at 30seconds. How will I go about adjusting which parameters.

    Thank you again, you have been really helpful.

  • Hi Ty,

    I have to make a correction to my previous post regarding _30SEC = 1500 in my comment, this is wrong.  This value was based on .ui16ActiveModeScanPeriod = 20, not 33.  It is 909 in my code example, which is correct.  Sorry for any confusion.

    Also, my _125mSEC should be a value of 4, not 8 for the same reason.

    Yes, if you want the LEDs to remain active for 30 seconds you need to set the inactivity time to at least 30 seconds.

    Q1 - These #defines can only be changed (assigned a different value) during compile time.  If you want to be able to change these values 'dynamically' during run-time,  use variables instead.

    For example:

    uint16_t  _30sec = 909;
    uint16_t  _125msec = 8;

    I don't know why it stops recognizing touch in your setup.  It works perfectly on my BSWP setup.  I tested it pretty thoroughly. ;)

    I forgot to mention, make sure you allow enough time for your 30 second LED timing cycle to complete and turn off both LEDs before the MCU goes into WOP mode.  You can do this by setting the .ui16InactivityTimeout to something slightly longer than 30sec.  So instead of using 909 use 910.  Make sense? This guarantees the counter counts up to 900 and the routine will turn off both LEDs.

    Q2 - Yes.  The larger the timeout value, the longer the MCU remains in active mode.

    Q3 - To control how long you toggle LED2 you need to define another count timeout and add another flag.  You can use either a #define or variable, depending on if you want to change the value during run-time.  See the following example.

    Note: you can probably make the code a little easier to read and perform the same functionality by using a 'switch() statement'.  That can be an exercise for you to try ;)

    uint16_t LED_count;
    bool TouchFlag = false;
    bool ToggleFlag = false;
    uint16_t _30sec = 909;  // Note: set .ui16InactivityTimeout = 910 to allow LED cycle to finish
    uint16_t _2sec = 60;
    uint16_t _125msec = 4;
    
    
    void main(void)
    {
    	//
    	// Initialize the MCU
    	// BSP_configureMCU() sets up the device IO and clocking
    	// The global interrupt enable is set to allow peripherals
    	// to wake the MCU.
    	//
    	WDTCTL = WDTPW | WDTHOLD;
    	BSP_configureMCU();
    	__bis_SR_register(GIE);
    
    	//
    	// Start the CapTIvate application
    	//
    	CAPT_appStart();
    
    	//
    	// Background Loop
    	//
    	while(1)
    	{
    	    if(CAPT_appHandler()==true)
    	    {
    
    	        if(TouchFlag == false)
                {
                    TouchFlag = true;
                    ToggleFlag = true;
                    LED1_ON;
                    LED2_ON;
                }
    	    }
    
    	    if(TouchFlag == true)
    	    {
    	        LED_count++;
    
    	        /* Toggle LED1 for only 2 seconds */
    	        if(ToggleFlag == true)
    	        {
                    /* Time to toggle LED1? */
                    if(LED_count % _125msec == 0)
                    {
                        LED1_TOGGLE;
                    }
    
                    if(LED_count == _2sec)
                    {
                        ToggleFlag = false;
                        LED1_OFF;
                    }
    
    	        }
    
                /* Time to turn off both LEDs */
                if(LED_count == _30sec)
                {
                    LED1_OFF;
                    LED2_OFF;
                    TouchFlag = false;
                    LED_count = 0;
                }
    	    }
    
    		//
    		// This is a great place to add in any 
    		// background application code.
    		//
    		__no_operation();
    
    		//
    		// End of background loop iteration
    		// Go to sleep if there is nothing left to do
    		//
    		CAPT_appSleep();
    		
    	} // End background loop
    } // End main()
    

**Attention** This is a public forum