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