Other Parts Discussed in Thread: ENERGIA
Tool/software:
Hi,
I want to generate a pulse signal using the MSP430FR2311. The pulse should have a width of 1 µs and a period of 1 second. I'm using TimerA to generate the 1 µs pulse and TimerB to generate the 1-second period. To keep it simple, I'm using the polling method.
Initially, I tried using the _delay_cycles() function to generate the 1 µs pulse, but it didn’t give accurate results. So, I switched to using timer registers for more precise control.
I'm using Code Composer Studio (CCS) for development. However, the IDE doesn't recognize the timer registers — it throws an error like:
"TA0CCR1" is undefined. I am also getting error for the variable i defined in the for loop.
I also tried using the Energia IDE, but the issue remains the same — the timer registers are not recognized.
Could anyone help me resolve this issue?
Thank you!
Below is the code:
#include <msp430.h>
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
// Configure P1.0 as output
P1DIR |= BIT0;
P1OUT &= ~BIT0;
// Use TimerA for pulse generation (1 µs) and TimerB for period (1 s)
// TimerA
TA0CTL = TASSEL__SMCLK | MC__STOP | TACLR; // Select SMCLK, keep timer stopped
TA0CCR1 = 1; // Toggle after 1 µs
// TimerB
TB0CTL = TBSSEL__SMCLK | MC__STOP | TBCLR;
TB0CTL |= ID__8; // Set prescaler = 8
TB0CCR0 = 12500; // 1 second (assuming 1 MHz SMCLK)
while (1)
{
TA0R = 0;
TA0CTL |= MC__UP; // Start TimerA in up mode
P1OUT |= BIT0; // Set output pin
while (TA0R != TA0CCR1) {}
TA0CTL &= ~MC__UP; // Stop TimerA
P1OUT &= ~BIT0; // Clear output pin
for (int i = 0; i < 10; i++)
{
TB0R = 0;
TB0CTL |= MC__UP; // Start TimerB
while (TB0R != TB0CCR0) {}
TB0CTL &= ~MC__UP; // Stop TimerB
}
}
return 0;
}