工具与软件:
您好!
我想将脉冲输出的数量限制为10、但效果不佳、因此需要了我的帮助。
系统时钟为8 MHz 、我按如下方式配置 TA0、以便两个输出为180度异相:
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;
}
现在、它持续生成脉冲。
在下面的代码中、我将尝试在中断标志触发时清除中断标志、并在重复10次后停止、但该信号只是保持高电平。
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();
}