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.

help with timers

Other Parts Discussed in Thread: MSP430G2553

i am trying to interface ultrasonic sensor with msp430 using timers . i am unable to rectify errors .here is the code i wrote from the ti's user guide.

// interface code for the msp430 and the ultra sonic sensor hc-sr04
// Using IO trigger for at least 10us high level signal,
// The Module automatically sends eight 40 kHz and detect whether there is a
//pulse signal back.
// IF the signal back, through high level , time of high output IO duration is
//the time from sending ultrasonic to returning.
//Test distance = (high level time×velocity of sound (340M/S) / 2


// trigger input pulse will be connected to pin p2.1
// echo output signal to pin p2.2


#include<msp430g2553.h>
void TRIGGER(void);
void main(void)
{ unsigned int i=0;

//stop the watchdog timer
WDTCTL = WDTPW + WDTHOLD;
//PORT 2 SETTINGS
P2DIR=BIT1+~BIT2;
//configuring the timer_a
DCOCTL = CALDCO_16MHZ; // configure dco clock to 16mhz
BCSCTL2=BIT4+BIT2+BIT1; // MCLK=DCO DIVIDER /2 ,,,, SMCLK=DCO DIVIDER / 8
// TIMER_A clock source is smclk

TACTL=TASSELX+IDX+MCX+TAIE;
TAR=0X14;
P2OUT=~BIT1;
TRIGGER();
// now read the echo signal
MCX=0x00;
TACTL=MCX;

while(P2OUT.BIT2==1)
{
MCX=0x02;
TACTL=MCX;

}
MCX=0x00;
TACTL=MCX;
i=TACCR0;
// i contains the echo signal high time
}
void TRIGGER(void)
{
MCX=01;
TACTL=MCX;
while(TAIFG==0)
{
P2OUT=BIT1;
}
P2OUT=~BIT1;
}

  • >P2DIR=BIT1+~BIT2;
    This actually (under the rules of binary arithmetic) sets P2.1 to be input and P2.2 to be output. This matches your comments but not your code. My guess is that you really wanted:

    P2DIR |= BIT1; P2DIR &= ~BIT2;

    >DCOCTL = CALDCO_16MHZ; // configure dco clock to 16mhz

    You're setting DCOCTL without also setting BCSCTL1=CALBC1_16MHZ. You're probably running at something not far from 1MHz.

    > BCSCTL2=BIT4+BIT2+BIT1; // MCLK=DCO DIVIDER /2 ,,,, SMCLK=DCO DIVIDER / 8
    I'll have to trust you on this one. If you want anyone to check this, I suggest you use the BCSCTL2 bit names. (Hint: They're right there in the book.)

    > TACTL=TASSELX+IDX+MCX+TAIE;
    You're enabling TAIE, but I don't see a TIMER0_A1_VECTOR ISR for it.
    Your program will crash at some time dependent on what TASSELX, IDX, and MCX are.

    I don't see any declaration for TASSELX, IDX, nor MCX. I can guess some possibilities, but that's not the same thing.

    > while(TAIFG==0)
    Since TAIFG is 1, this test is never true, so this loop never executes, and P2.1 never goes high. I suspect this is important.

  • Thank you Sir, for your suggestion i made those changes . i still haven't tried the code on the LP. Because i don't want to burn up the LP. The following are the changes . Please suggested if any mistakes are still present in it.

    // interface code for the msp430 and the ultra sonic sensor hc-sr04
    // Using IO trigger for at least 10us high level signal,
    // The Module automatically sends eight 40 kHz and detect whether there is a
    //pulse signal back.
    // IF the signal back, through high level , time of high output IO duration is
    //the time from sending ultrasonic to returning.
    //Test distance = (high level time×velocity of sound (340M/S) / 2


    // trigger input pulse will be connected to pin p2.1
    // echo output signal to pin p2.2


    #include<msp430g2553.h>
    void TRIGGER(void);
    void main(void)
    { volatile unsigned int i=0;

    //stop the watchdog timer
    WDTCTL = WDTPW + WDTHOLD;
    //PORT 2 SETTINGS
    P2DIR|=BIT1;
    P2DIR&=~BIT2;
    //configuring the timer_a
    DCOCTL = CAL_DCO_16MHZ;
    BCSCTL1= CAL_BC1_16MHZ; // configure dco clock to 16mhz
    BCSCTL2=BIT4+BIT2+BIT1; // MCLK=DCO DIVIDER /2 ,,,, SMCLK=DCO DIVIDER / 8
    // TIMER_A clock source is smclk

    TACTL|=TASSEL0+ID0+MC0+TAIE;
    TAR=0X14;
    P2OUT&=~BIT1;
    TRIGGER();
    // now read the echo signal
    TACTL&=MC_0; // stop the timer


    while(P2OUT&BIT2==1)
    {

    TACTL|=MC_2; // continuous up mode

    }

    TACTL&=MC_0;
    i=TACCR0; // taccro contains the count value of high pulse time store for
    // store for further processing
    // i contains the echo signal high time
    }
    void TRIGGER(void)
    {
    P2OUT|=BIT1;
    TACTL|=MC_1;

    }
    #pragma vector= TIMER0_A1_VECTOR
    __interrupt void TA0_ISR(void)
    {
    P2OUT&=~BIT1;
    }

**Attention** This is a public forum