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.

MSP430FR2355: Basic timer read/reset/control operations

Part Number: MSP430FR2355


My goal is to:

-configure a timer period (somewhere in the 0.1 to 1ms range)

-read a timer value (in my port pin ISR)

-reset the timer back to zero 

Can you help me do those 3 things?

I see there are 3 16bit timers on this device, are there differences between them/cases for using one over the other for this?

My system clock is 8Mhz. 

Apologies if this is a redundant post; seems like a basic operation but my search turned up nothing.Thanks!

  • Your best bet is to download the timer example code from TI resource explorer. The Data Sheet and Users manual can also help explain the differences between the timers.

  • Hi John,

    For basic timer concepts we have created these Academy Examples which can assist Timer Academy.

    For your device specific we have examples for timers as well which can be found here MSP430FR2355 Example Code.

    And resources like the datasheet and the user's guide can give you further information.

    Regards,

    Luke

  • Thank you. Reading through these examples, I see I can use the "capture" feature to accomplish my ultimate goal (pulse frequency measurement) if it's the only way to do it.

    However, I would like a software event to be able to read and reset the timer. (Ideal for portability from my dsPIC33 projects). On dsPIC33 for example I could have this:

    (on port interrupt)

    myValue = TMR1;

    TMR1_reset();

    doStuffWith(myValue);

    With the examples I've seen here, it looks like I need to tie my "port interrupt" signal to a Capture/Compare pin, then accumulate a specific number of "capture" values in a buffer; trigger an interrupt on the buffer status; average the collected values in the buffer, then proceed to use the average elapsed time.

    Is there a way to use the msp430 timer like the dsPIC timer, as described above? I suspect I'm only failing to identify the mnemonic for querying the timer counter value.

  • Timer capture is logically equivalent to reading (e.g.) TB0R in a pin interrupt, but without the software latency. [The equivalent mechanism for the dsPIC33 is described in (e.g.) (DS70005371B) Sec 10.4.] Generally you would not reset the timer on each capture, rather use Continuous mode and subtract successive capture values. (This would be true for the pin-interrupt method as well.) The only real reason not to use capture would be if you don't have access to any of the designated capture input (trigger) pins.

    Example msp430fr235x_tb0_capture.c shows the setup rather succinctly. You don't need to save the captured value (TB0CCR0 in this case) in a buffer, you can process it immediately in the ISR if you prefer. The example triggers on an internal signal (TB0.CCI0B=ACLK), so you would need to choose an external pin and adjust the setup accordingly. (P1.6=TB0.1=TB0.CCI1A->TB0CCR1 seems like a possibility [Ref data sheet (SLASEC4C) Table 6-63].)

    Before you get too deep into this: How fast is your signal? Using capture for frequency-counting a slow signal -- say, <10kHz -- works well, but for a fast signal you should use a frequency-counting method with (e.g.) TB0CLK.

  • Thank you Bruce, I will use this method. Will follow up here if I struggle.

    My signal is slow (0-1000hz) so Capture will be suitable. Ideally I'd also want a "timeout" such that if, say, 3 seconds pass without a falling edge, the ISR does something else. I think there is enough information here for me to make that work. I appreciate your help.

  • One method I've used:

    1) Adopt an unused CCR in the same timer, e.g. CCR2

    2) Set TB0CCTL2 = CCIE to request a compare interrupt

    3) At each capture, store the captured value in TB0CCR2.

    4) If you ever get a CCR2 interrupt, that means that the timer has cycled completely (64K ticks) and you're at the limit of your capture timing -- this is as good a timeout as any. 

  • An update here for anyone else who has a similar need.

    My first mistake was trying to use the datasheet, rather than the users guide, to find the correct mnemonics. The MSP430 user's guide has the configuration word contents and "friendly names" similarly to what I was used to seeing in the dsPIC33 datasheets, and couldn't find in the MSP430 DS.

    While Bruce's approach of capture interrupt is more correct, I first performed a line-by-line port of my old code to MSP430 using the approach laid out in my original post. This turned out to be very simple, once armed with the user's guide:

    In main():

    TB0CTL |= TBSSEL_1 | ID_3 | MC_2 | TBCLR; //Timer B Control:
    //TBSSEL_2 Clock Source Selection:Alternatives are SMCLK, ACLK, TBxCLK, INCLK.
    // MC_2 Mode set: Continuous. This starts the timer. Alternatives are 00-STOP, 01=UP, 11=UPDN
    // TBCLR resets the clock. The bit returns to 0 automatically.
    //CLOCK IS RUNNING NOW

    Then in an interrupt triggered by a port pin:

    TB0CTL &= ~MC_3; // STOP THE TIMER
    doStuffWith(TB0R); //read the 16bit timer value and do stuff with that value
    TB0CTL |= MC_2 | TBCLR; //RESET THE TIMER
    TB0CTL |= MC_2; //START THE TIMER again from zero

    I recognize that the capture approach will be more accurate and just as simple; but this was a good learning exercise and is intuitive. Hope it helps someone.

**Attention** This is a public forum