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.

CCS/MSP-EXP430F5529: MSP-EXP430F5529

Part Number: MSP-EXP430F5529

Tool/software: Code Composer Studio

Hello

i want to read ultra sonic data using msp430f5529

i found the following code it works fine 

i want to know the distance value (how far is the obstacle )  i don't know how to find it in the code 

please help

here is the code 

#include <intrinsics.h>
#include <stdint.h>
#include <msp430.h>

#define TRIGGER_PIN BIT1   // P6.1
#define ECHO_PIN BIT3  // P1.3
#define LED_PIN BIT0   // P1.0
#define DISTANCE_THRESHOLD 60  // cm
#define MEASURE_INTERVAL 2048  // ~250 ms

void triggerMeasurement() {
    // Start trigger
    P6OUT |= TRIGGER_PIN;

    // Wait a small amount of time with trigger high, > 10us required (~10 clock cycles at 1MHz MCLK)
    __delay_cycles(10);

    // End trigger
    P6OUT &= ~TRIGGER_PIN;
}

int main(void) {

    WDTCTL = WDTPW | WDTHOLD;

    // Configure trigger pin, low to start
    P6DIR |= TRIGGER_PIN;
    P6OUT &= ~TRIGGER_PIN;

    // Configure LED, off to start
    P1DIR |= LED_PIN;
    P1OUT &= ~LED_PIN;

    // Configure echo pin as capture input to TA0CCR2
    P1DIR &= ~ECHO_PIN;
    P1SEL |= ECHO_PIN;

    // Set up TA0 to capture in CCR2 on both edges from P1.3 (echo pin)
    TA0CCTL2 = CM_3 | CCIS_0 | SCS | CAP | CCIE;

    // Set up TA0 to compare CCR0 (measure interval)
    TA0CCR0 = MEASURE_INTERVAL;
    TA0CCTL0 = CCIE;

    // Set up TA0 with ACLK / 4 = 8192 Hz
    TA0CTL = TASSEL__ACLK | ID__4 | MC__CONTINUOUS | TACLR;

    uint16_t lastCount = 0;
    uint32_t distance = 0;

    for (;;)
    {
        triggerMeasurement();

        // Wait for echo start
        __low_power_mode_3();

        lastCount = TA0CCR2;

        // Wait for echo end
        __low_power_mode_3();

        distance = TA0CCR2 - lastCount;
        distance *= 34000;
        distance >>= 14;  // division by 16384 (2 ^ 14)

        if (distance <= DISTANCE_THRESHOLD)
        {
            // Turn on LED
            P1OUT |= LED_PIN;
        }
        else
        {
            // Turn off LED
            P1OUT &= ~LED_PIN;
        }

        // Wait for the next measure interval tick
        __low_power_mode_3();
    }
}

#pragma vector = TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR (void) {
    // Measure interval tick
    __low_power_mode_off_on_exit();
    TA0CCR0 += MEASURE_INTERVAL;
}

#pragma vector = TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR (void) {
    // Echo pin state toggled
    __low_power_mode_off_on_exit();
    TA0IV = 0;
}

  • Hello,

    The variable "distance" is the measured distance which will be compared with DISTANCE_THRESHOLD to turn on/off LED.

            if (distance <= DISTANCE_THRESHOLD)
            {
                // Turn on LED
                P1OUT |= LED_PIN;
            }
            else
            {
                // Turn off LED
                P1OUT &= ~LED_PIN;
            }

     

  • Hello Winter Yu and Ling Zhu2 

    i really appreciate your kidness

    i have another question please
    why to set the MEASURE_INTERVAL to 2048
    and could anyone explain me the following equation i didn't get exactly how to find the distance from this equation
    distance *= 34000;
    distance >>= 14; // division by 16384 (2 ^ 14)


    i know that distance = duration * 0.034/2;

  • 1. You set up TA0 with ACLK / 4 = 8192 Hz, so set  the MEASURE_INTERVAL to 2048 to get a 250ms measurement interval which means 4Hz sample rate. This measurement interval is based on power consumption, sample rate and minimum time per measurement requirements. 

    2. Since the TA0 clock is ACLK/4 = 8192Hz = 2^13, the duration is (TA0CCR2 - lastCount)/2^13. So 

     distance = (TA0CCR2 - lastCount)/2^13 * 34000/2 cm = (TA0CCR2 - lastCount)/2^14 * 34000 cm.

**Attention** This is a public forum