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.

PWM and measure distance by SRF05

Hi everyone. I have a problem with my code. When I do each part. It works. But when i do them together. They don't work. I don't know why. Could anyone  explain to me why it does not work.  Thanks so much.

 

Part 1. I measure distance by SRF05. This is my code:

#include <msp430g2553.h>    
#define	trigger		BIT1;
#define	 echo		BIT2;
unsigned int RisingEdge;
unsigned int FallingEdge;
unsigned int PulseTimes;
unsigned short First;
unsigned int distance;

void timer_init(void);
void do_khoang_cach(void);

void main(void)
{
	WDTCTL = WDTPW | WDTHOLD;
	BCSCTL1 = CALBC1_1MHZ;
	DCOCTL =  CALDCO_1MHZ;

	P1DIR |= trigger;		// trigger ouput
	P1OUT&=~trigger;
	//P1DIR&=~BIT3;
	P1IE|=echo;
	P1IFG&=~echo;
	P1SEL |= echo;
	P1DIR|= BIT4;
	
	TACTL = TASSEL_2 + MC_2 ;						// continuos mode, 0 --> FFFF
	TACCTL1 = CM_3 + CAP + CCIS_0 + SCS+ CCIE;		// falling edge & raising edge, capture mode, capture/compare interrupt enable
	TACCTL1 &= ~ CCIFG;

	First=0;
	_BIS_SR(GIE);			//ngat toan cuc

	while(1)
	{
		do_khoang_cach();
		__delay_cycles(30000);		//  30ms time out
	}

}

void do_khoang_cach(void)
{
	P1OUT |= trigger;
	__delay_cycles(20);
	P1OUT &= ~trigger;
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt void Timer0_A1 (void)
{
switch (TA0IV)
		{
		case (TA0IV_TACCR1):

		{
		if( First == 0)
		{
			RisingEdge = TACCR1;
			First =1;
					}
		else
		{
			FallingEdge = TACCR1;
			PulseTimes = FallingEdge - RisingEdge;
			First = 0;
			TACCR1 = 0; //
			distance =  (PulseTimes)/58;

	}

		if(distance <20)
			P1OUT|=BIT4;
		else
			P1OUT&=~BIT4;
	}
}
}

Part 2. I do 2 PWM and this is my code: 

#include <msp430g2553.h>

void PWM1();
void PWM2();

void Port_init();


void PWM1()
{
	TA1CCR1 =100;
	TA1CCR2 = 100;
}
void PWM2()
{
	TA1CCR1 =50;
	TA1CCR2 = 50;

}

void Port_init()
{
	P2DIR|= BIT1+ BIT4;  // P2.4 + P2.1 Output
	P2SEL|= BIT1+ BIT4;		// P2.1 + P2.4 is PWM

	TA1CTL = TASSEL_2 + MC_1 ;
	TA1CCR0 = 100;
	TA1CCTL1 = OUTMOD_7;
	TA1CCTL2 = OUTMOD_7;
}

void main (void)
{
	WDTCTL = WDTPW + WDTHOLD;		// Stop WDT
	Port_init();
	PWM2();
	_BIS_SR(LPM0_bits + GIE);


}

And when i do they together 

#include <msp430g2553.h>    /////////DA CHAY
#define	trigger		BIT1;
#define	 echo		BIT2;
unsigned int RisingEdge;
unsigned int FallingEdge;
unsigned int PulseTimes;
unsigned short First;
unsigned int distance;

void timer_init(void);
void do_khoang_cach(void);
void PWM1();
void PWM2();

void Port_init();

void do_khoang_cach(void)
{
	P1OUT |= trigger;
	__delay_cycles(20);
	P1OUT &= ~trigger;
}

void PWM1()
{
	TA1CCR1 =100;
	TA1CCR2 = 100;
}
void PWM2()
{
	TA1CCR1 =50;
	TA1CCR2 = 50;

}

void Port_init()
{
	P2DIR|= BIT1+ BIT4;  // P2.4 + P2.1 Output
	P2SEL|= BIT1+ BIT4;		// P2.1 + P2.4 is PWM

	TA1CTL = TASSEL_2 + MC_1 ;
	TA1CCR0 = 100;
	TA1CCTL1 = OUTMOD_7;
	TA1CCTL2 = OUTMOD_7;
}

void main(void)
{
	WDTCTL = WDTPW | WDTHOLD;
	BCSCTL1 = CALBC1_1MHZ;
	DCOCTL =  CALDCO_1MHZ;

	P1DIR |= trigger;		// trigger ouput
	P1OUT&=~trigger;
	
	P1IE|=echo;
	P1IFG&=~echo;
	P1SEL |= echo;
	P1DIR|= BIT4;
	
	TACTL = TASSEL_2 + MC_2 ;						// continuos mode, 0 --> FFFF
	TACCTL1 = CM_3 + CAP + CCIS_0 + SCS+ CCIE;		// falling edge & raising edge, capture mode, capture/compare interrupt enable
	TACCTL1 &= ~ CCIFG;

	First=0;
	_BIS_SR(GIE);			//ngat toan cuc

	while(1)
	{
		do_khoang_cach();
		__delay_cycles(30000);		//  30ms time out
	}
	
		
		Port_init();
		PWM2();
		_BIS_SR(LPM0_bits + GIE);

}

#pragma vector=TIMER0_A1_VECTOR
__interrupt void Timer0_A1 (void)
{
switch (TA0IV)
		{
		case (TA0IV_TACCR1):

		{
		if( First == 0)
		{
			RisingEdge = TACCR1;
			First =1;
					}
		else
		{
			FallingEdge = TACCR1;
			PulseTimes = FallingEdge - RisingEdge;
			First = 0;
			TACCR1 = 0; //
			distance =  (PulseTimes)/58;

	}

		if(distance <20)
			P1OUT|=BIT4;
		else
			P1OUT&=~BIT4;
	}
}
}

 

 

**Attention** This is a public forum