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.

ultrasonic sensor



i tried a program for my ultrasonic sensor to measure the distance n if it is less than 10 then buzz the buzzer with higher freq n if more, then with lower freq. this is the code, i have been trying. but it aint working.

plz help me out.

thanku in adv!1

#include <msp430.h>

#define echo BIT1
#define trig BIT2
#define led1 BIT3
#define led2 BIT4
#define buzz BIT6

unsigned int measure = 0;
unsigned int measure_1 = 0;
unsigned int measure_2 = 0;
unsigned int up;
unsigned int i = 0;

void config_init(void)
{
WDTCTL = WDTPW + WDTHOLD;

P1DIR |= trig + led1 + led2;
P1OUT &=~ trig + led1 + led2;
P1DIR &=~ echo;
P1SEL = echo;
P1SEL2 = echo;

BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
BCSCTL2 &=~ SELS;
BCSCTL2 |= DIVS0;
TACTL = TASSEL_2 + MC_2 + ID_0;
TA0CCTL0 = CM_3 + CCIE + CAP + SCS +CCIS_0;

_bis_SR_register(GIE);
}
void uart_init(void)
{
UCA0CTL1 = UCSSEL_2;
UCA0MCTL = UCBRS0;
UCA0BR0 = 104;
UCA0BR1 = 0;
IE2 |= UCA0RXIE + UCA0TXIE;
UCA0CTL1 &=~ UCSWRST;
}
int main(void)
{
config_init();
uart_init();
while(1)
{
up = 1;
P1OUT &=~ trig;
_delay_us(2);
P1OUT |= trig;
_delay_us(10);
P1OUT &=~ trig;
_delay_ms(1000);
}
return 0;
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
while (!(IFG2 & UCA0TXIFG));
UCA0TXBUF = UCA0RXBUF;
}
#pragma vector= TIMER0_A0_VECTOR
__interrupt void Timer0_A0 (void)
{
if(!(TA0CCTL0 & CM_3))
{
measure_1 = TACCR0;
TA0CCTL0 = CM_3;
}
else
{
if(up)
{
measure_1 = TACCR0;
}
else
{
measure_2 = TACCR0;
measure = (measure_1 - measure_2)/58;
if(measure < 10)
{
P1OUT |= led1;
while(1)
{
P1OUT ^= buzz;
for(i=0; i<1000; i++);
}
}
else
{
P1OUT |= led2;
while(1)
{
P1OUT ^= buzz;
for(i=0; i<10000; i++);
}
}
}
}
TACTL &= ~TAIFG;
}

  • plz help asap!!!! thanks a lot

  • You should perhaps tell in detail what you try to do with the code (so people can follow your path of thoughts). Also, it usually helps if the code is readable. The editor has a special function for adding code (with syntax highlighting and preserving indentation). It is the icon in the bottom-right of the toolbar. Perhaps you can edit your post.

     A general hint, you shouldn’t arithmetically add bit values. Use a bitwise OR for this. The compiler won’t complain, but you can easily mess-up things by accidentally adding a bit twice. (you can’t OR a bit twice)

     What does _delay_ms() do? Hopefully not a FOR loop, as this might not lead to the desired result when the compiler optimization level changes.

     Now for the main problem:
    You cannot set UCA0xXIE while SWRST is still set (SWRST clears the IE bits). You need to do it after you cleared SWRST. Currently, your UART interrupts are not active.

**Attention** This is a public forum