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.

MSP432P401R: Timer A - Capture Mode for ultrasonic sensor HC-SR04

Part Number: MSP432P401R

Hello,

I need to configure the Timer A in capture mode to measure the echo pulse of the HC-SR04 sensor i'm using.

I'm sending a 10us pulse to the sensor (i tried less and more just in case but same results) and I'm waiting for the echo that should trigger my interruption, but that's not the case. When I force the capture pin (P2.4) to 3.3V and then to 0V, my breakpoints in the interruption are triggered correctly.

I really don't understand what's wrong with my code. There might be some error in my code that i'm not seeing.

By the way, my sensor is working since I tested it with a code for Energia, but i want to use CCS and the driverlib.

#include "driverlib.h"
#include <stdint.h>

/* Timer_A Continuous Mode Configuration Parameter */
const Timer_A_ContinuousModeConfig continuousModeConfig =
{
        TIMER_A_CLOCKSOURCE_SMCLK,           // SMCLK Clock Source
        TIMER_A_CLOCKSOURCE_DIVIDER_1,       // SMCLK/1 = 3MHz
        TIMER_A_TAIE_INTERRUPT_DISABLE,      // Disable Timer ISR
        TIMER_A_SKIP_CLEAR                   // Skup Clear Counter
};

/* Timer_A Capture Mode Configuration Parameter */
const Timer_A_CaptureModeConfig captureModeConfig =
{
        TIMER_A_CAPTURECOMPARE_REGISTER_1,        // CC Register 2
		TIMER_A_CAPTUREMODE_RISING_EDGE,          // Rising Edge and falling
        TIMER_A_CAPTURE_INPUTSELECT_CCIxA,        // CCIxA Input Select
        TIMER_A_CAPTURE_SYNCHRONOUS,              // Synchronized Capture
        TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE,  // Enable interrupt
        TIMER_A_OUTPUTMODE_OUTBITVALUE            // Output bit value
};

static void Delay(uint32_t loop)
{
    volatile uint32_t i;

    for (i = 0 ; i < loop ; i++);
}

int meas1 = 0;
int meas2 = 0;
int main(void)
{
	/* Stop watchdog timer */
	MAP_WDT_A_holdTimer();

	CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_24);                    // 24000000 Hz

	CS_initClockSignal(CS_MCLK,  CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1); // 24000000 Hz
	CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_8); //  3000000 Hz

	/* Configuring P2.4 as peripheral input for capture */
	GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN4,
		GPIO_PRIMARY_MODULE_FUNCTION);
	GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN5);

	/* Configuring Capture Mode */
	Timer_A_initCapture(TIMER_A0_BASE, &captureModeConfig);

	/* Configuring Continuous Mode */
	Timer_A_configureContinuousMode(TIMER_A0_BASE, &continuousModeConfig);

	/* Enabling interrupts */
	Interrupt_enableInterrupt(INT_TA0_N);
	Interrupt_enableMaster();

	/* Starting the Timer32 */
	Timer32_initModule(TIMER32_0_BASE, TIMER32_PRESCALER_1, TIMER32_32BIT, TIMER32_PERIODIC_MODE);
	Timer32_disableInterrupt(TIMER32_0_BASE);
	Timer32_setCount(TIMER32_0_BASE, 1);
	Timer32_startTimer(TIMER32_0_BASE, true);

	/* Starting the Timer_A0 in continuous mode */
	Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_CONTINUOUS_MODE);
	while(1)
	{
		GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN5);
	    Timer32_setCount(TIMER32_0_BASE, 24 * 10);
		while (Timer32_getValue(TIMER32_0_BASE) > 0); // Wait 10us
		GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5);
//software delays Delay(40000); Delay(40000); Delay(40000); Delay(40000); Delay(40000); Delay(40000); Delay(40000); Delay(40000); Delay(40000); Delay(40000); Delay(40000); Delay(40000); Delay(40000); Delay(40000); Delay(40000); } } void TA0_N_IRQHandler(void) { int rising = 0; Timer_A_clearCaptureCompareInterrupt(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1); if(P2IN&0x10) rising=1; else rising=0; if(rising) // Start { meas1 = Timer_A_getCaptureCompareCount(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1); } else { meas2 = Timer_A_getCaptureCompareCount(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1); } }

Thanks,

Olivier

  • Olivier,
    When I try out your code on a red MSP432P401R launchpad, and force the P2.4 input high/then-low, I see the capture as you describe. (My breakpoint hits that part of the code in the interrupt handler). I'm using the SimpleLink MSP432 SDK v 1.40.00.28. What version of the SDK are you using and what kind of Launchpad (Red or black)?

    PS-The output pulse is 12.8us. Is this what you intended?

    Regards,
    Bob Landers
  • I'm using the Black Launchpad. I checled in the Installation Details menu of CCS but I found nothing about which SDK i'm using. Can you provide more details of how to find it ?


    Regards,

    Olivier

  • I'm using the latest CCS6.2 versions.

    If i understood correctly MSP432Ware is no longer supported, and everything was migrated to SimpleLink. So i deleted my ti folder and reinstalled the CCS6.2 versions from scratch. Checks for updates and applied the updates. Then installed simplelink_msp432_sdk_1_40_00_28.

    In the ressource explorer and the Installation Details menu there's nothing about SimpleLink.

    Did i miss something ?

    Regards,
    Olivier

  • Olivier,

    Just a quick note, the black LaunchPad contains pre-production silicon. There have been numerous changes to the silicon since we released that two years ago in both software and hardware. Since then we have updated the device to be fully CMSIS compliant, changes to the driverlib in the ROM of the device and lots of changes to the way we support that. With the SimpleLink SDK, we only support Rev C and D on the MSP432. The black LP only has Rev B. Our recommendation in this case would be to upgrade to the Red Launchpad which can be found at ti.com/tool/msp-exp432p401r.

    You can find more information on Section 3 of this document: www.ti.com/.../slaa700

    Sorry for any inconvenience that this incurs. This is a product of making performance improvements to ensure the release silicon was performing at best, and in large we had to move support to those newer versions.

    It should be noted that SimpleLink SDK is also only supported for CCSv7, which the full version is free and can be downloaded from www.ti.com/.../ccstudio
  • Evan,

    I upgraded my CCS installation to the CCSv7 and SimpleLink SDK installation was indeed pretty easy (just a click).

    I understand that the black LP is no longer supported and it's about time to get the last one. Do you recycle those LPs? I mean I could send them to you if you're recycling the LP(not asking for an exchange with the red one).

    There was an obvious error in my code. Lines 44 and 45 should be

    GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P2, GPIO_PIN4, GPIO_PRIMARY_MODULE_FUNCTION);

    The echo pin is an INPUT not an OUTPUT.

    Regards,

    Olivier

**Attention** This is a public forum