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 .