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;
}