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.

interfacing ultrasonic sensor hcsr-04 with msp430g2553

Other Parts Discussed in Thread: MSP430G2553, CC430F5137

hello sir , i have written the following code to get the distance from ultrasonic sensor :

#include<msp430g2553.h>
unsigned int duration;
unsigned int distance;
void configureclocks();
void main()
{
WDTCTL = WDTPW + WDTHOLD;
configureclocks();

P1DIR |=BIT4; // TRIGGER PIN OF HC-SR04
P1DIR &=~BIT5; // INPUT FROM HC-SR04
P1IN=00;


while(1)
{

P1OUT |= BIT4;
_delay_cycles(80);
P1OUT &= ~BIT4;

while(P1IN && BIT5)
{
duration++;
}
distance = (duration/2) / 29.1;
}


}
void configureclocks()
{
if (CALBC1_8MHZ ==0xFF || CALDCO_8MHZ == 0xFF)
{
while(1); // If cal constants erased,
} // trap CPU!!

BCSCTL1 = CALBC1_8MHZ; // Set range
DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation
BCSCTL3 |= LFXT1S_2 + XCAP_3; // clock system setup
IFG1 &= ~OFIFG;
// _bis_SR_register(SCG1 + SCG0);
BCSCTL2 |= SELM_0 + DIVM_3;
}

i am able to run the code and i am using "add watch expression" feature of code composer studio to know the value of "duration" and "distance" .

ccs is displaying 0 value in distance variable and the value of "duration" goes on updating each time , i run and suspend the code . 

and the main problem is that , i am getting these updated values of "duration" even when i am not connecting the sensor to the launchpad .

i think , the program counter is not coming out of the second while() loop and it is continuosly updating "duration" value .

but the program counter should enter the second  while() loop , only when P1IN && BIT5 , condition is satisfied , but i dont understand how this condition is satisfied even when i am not connecting the sensor .

please help . 

  • > while(P1IN && BIT5)
    This is a logical expression, which is true if both sides are non-zero. BIT5=32, which is always non-zero. P1IN is probably mostly floating (unconnected inputs), and with high probability at least one bit is non-zero. Thus this expression is always "true". I suspect you wanted instead:

    > while(P1IN & BIT5)

  • hello raju vadarevu.. i am trying to interface ultra sonic sensor to cc430f5137(integrated with msp430) which is similar to interfacing ultrasonic sensor to msp430 which you have did already. hope you got desired output. please help me.. i want the code to do this which can be executed well and i dint understand that configuring the clocks part in your program. why do we need that. please help me.. thanx in advance..

**Attention** This is a public forum