Tool/software:
Hello,
I'm trying to limit the number of pulse outputs to 10, but it's not working well, so I'm asking for help.
The system clock is 8 MHz, and I configured TA0 as shown below so that the two outputs are 180 degrees out of phase:
void initPulseGenerator(void)
{
// Stop the watchdog timer
WDTCTL = WDTPW | WDTHOLD;
// Disable the default high-impedance mode
PM5CTL0 &= ~LOCKLPM5;
// Configure P7.4 for TA0.1 and P7.7 for TA0.2
// (Check that these pins do not require port mapping on your board.)
P7DIR |= BIT4 | BIT7; // OUTPUT DIRECTION
P7SEL0 |= BIT4 | BIT7; // Timer function
P7SEL1 &= ~(BIT4 | BIT7); // Clear to 0 for TA0.x
// Stop Timer_A0 during configuration
TA0CTL = MC__STOP;
// With SMCLK at 8MHz, one clock is 125ns.
// To get a 1MHz output (period = 1µs = 8 clocks), set CCR0 = 7 (timer counts 0..7).
TA0CCR0 = 7;
// For a 50% duty cycle, set both CCR1 and CCR2 to 4.
TA0CCR1 = 4;
TA0CCR2 = 4;
// Configure PWM output modes:
// TA0CCTL1: OUTMOD_3 (set/reset) => high at timer start, low after CCR1
TA0CCTL1 = OUTMOD_3;
// TA0CCTL2: OUTMOD_7 (reset/set) => low at timer start, high after CCR2
TA0CCTL2 = OUTMOD_7;
}
Right now, it keeps generating pulses continuously.
In the code below, I'm trying to clear the interrupt flag when it triggers and stop after 10 repetitions, but the signal just stays HIGH.
void startPulseGenerator(void)
{
TA0R = 0; // Reset timer counter
TA0CTL = TASSEL__SMCLK | MC__UP | TACLR;
// Wait for exactly 10 pulse cycles
int cycles = 0;
while (cycles < 10)
{
while (!(TA0CCTL1 & CCIFG)); // Wait for rising edge of PWM (CCR1 match)
TA0CCTL1 &= ~CCIFG; // Clear flag
cycles++;
}
stopPulseGenerator();
}