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.

Generate 2 pulses using Timer A of MSP430F1132

Other Parts Discussed in Thread: MSP430G2112

Assuming HF XTAl = 6.5MHz, I generate toggle rate: P2.2 =TACCR0 =  6.5MHz/2x26 = 125kHz, P2.4 = TACCR2 = 6.5/4x26 = 62.5kHz. But when I set these value in my source code for TACCR0=26 and TACCR2=52, I get the toggle rates with the same value = 50Hz. When I change TACCR0 = 500 and TACCR2 = 1000, the toggle rate is 6.5kHz and 3.23kHz. I don't know the reason why. Please help me!

This is my source code:

 

 BCSCTL1 |= XTS + XT2OFF;                   

// turn external oscillator on

    do

    {

      IFG1 &= ~OFIFG;                   // Clear OSCFault flag

      for (i = 0xFF; i > 0; i--); // Time delay for flag to set

    }

    while ((IFG1 & OFIFG) == OFIFG);    // OSCFault flag still set?

    BCSCTL2 |= SELM_3 + SELM0 + SELS;           //  MCLK = SMCLK = HF LFXT1 (safe)

  P2SEL |= (BIT2+BIT4);

  P2DIR |= (BIT2+BIT4);

  TACCTL0 = OUTMOD_4 + CCIE;

  TACCTL2 = OUTMOD_4 + CCIE;

  TACCR0 = 0; 

  TACCR2 = 0;

  TACTL = TASSEL_1 + MC_2 + TAIE;

  

  _enable_interrupts();

}

#pragma vector =TIMERA0_VECTOR

__interrupt void Timer_A0(void)

{

TACCR0 += 26;

}

#pragma vector =TIMERA1_VECTOR

__interrupt void Timer_A1(void)

{

switch (TAIV)

{

case 2:

{

        break;

}

case 4:

{

TACCR2 += 52;

break;

}

}


}

  • Hi Thuc,

    I suspect that the software overhead in the ISRs could be to blame here since the timer count you are going for is so low (only 26 cycles between your ISRs). This would explain why when you lengthened your count in your TACCRx registers, the timer began to behave as expected. If the ratio of cycles spent in the ISRs to cycles between ISRs (based on your TACCRx value) is too high, then your ISRs are going to be preventing timely interrupt service and thus affecting your timer output when you are using continuous mode. (This does not apply in up/down mode since in up/down mode everything is done in hardware - no ISRs needed).

    I have been doing some experimenting myself lately with generating multiple PWM frequencies as well as duty cycles all off of a single timer module in continuous mode. As part of this I've done a little testing to determine a general guideline or "rule-of-thumb" for the maximum frequencies that can be reliably achieved with this method. There is a cutoff point that varies depending on the number of TACCRx registers you are using due to the latency added by your ISR code, which includes for each separate frequency generated:

    •  the ISR entry time,
    • wake-up time if coming from low power mode,
    •  the decision logic for determining which TACCRx register triggered the interrupt (switch structure)
    • and then adding the period offset to the TACCRx register to generate the new value.

     All of this adds time spent in the ISR where other interrupts can't come in, and the more frequencies you are generating the greater the effect (because you will be in the ISR a larger % of the time). Having any other ISRs in your system will also affect how high of a timer frequency you can generate with continuous mode because they can prevent you from servicing your timer interrupts.

    In my own experimentation, I was creating the same frequency on all TACCRx registers as a sort of "worst case" and then seeing on a scope where the lowest priority signal furthest down the ISR would start becoming unreliable. I found that for my code in the case for generating two signals, you would start to get unreliable results if you used a period count of less than TACCRx += 50 when sourcing the timer from the same source as MCLK (your 26 case applies here). This value seemed to be consistent no matter what MCLK frequency was used, as it is really an indicator more dependent on the ratio of timer counts between interrupts  to cycles spent in the ISR. Note that this value is just a guideline, as it will depend on how you have written your ISR code and could vary with different output frequency combinations.

    So for generating your 125kHz signal, you will probably need to use a higher frequency clock source for your timer or to use a second timer module (unfortunately F1132 only has one timer). Alternately, since your periods are direct multiples of each other, you could try using the timer in Up/Down mode so you don't have to do anything in the ISR to reload TACCR0, just set it to 26 at the beginning of the program. Then in the TACCR0 ISR you could toggle your second signal in software on every other pass. This still may not work with such a low timer count but it is something you could try.

    Best of luck!

    Katie

  • Thanks Katie for your useful explanation in detail.

    Actually, I've already tried with UP mode. But the results were not much different, they did not follow my calculation. It seems to me that it's difficult to generate high output frequency for MSP430. Anyway, I will do more and more test. Thank you!

  • Hi Thuc,

    Normally the MSP430 timer would be able to produce a single125kHz signal using up mode without problem, however, the difficulty in your situation is that you need two different frequencies off the same timer and they are relatively close to the source clock frequency. The timers are generally used in this fashion: one main timer frequency set by TACCR0 and the other TACCRx registers setting the duty cycle; other frequencies are typically generated on other timer modules. The problem only comes into play when you start using software to set multiple frequencies on a single timer module in continuous mode and are trying to set a frequency that is close to your operating frequency. Unfortunately, this happens to match up with your particular needed use-case.

    I would suggest this alternative: If you are trying to produce a high-frequency signal close to your system clock frequencies (and it just needs to be 50% duty cycle, not a special PWM signal) instead of using the timer module you might want to just divide down one of your clocks. For example, if you set your DCO to 1MHz operation, set it as the source for SMCLK and set your DIVSx bits to "/8" operation, you could then output SMCLK on a pin and it would be your 125kHz signal (1MHz/8 = 125kHz). Then you could use the timer module to generate the 62.5kHz signal in up mode off of your 6.5MHz crystal.

    If this isn't possible in your application, then you may not have quite the right MSP430 for the job, and could look at switching to an MSP430 part with two timer modules or a higher frequency MCLK. For example, you could look at our value line parts, many of which would fall into this category - to get a similar feature set but with up to 16MHz DCO freq, perhaps the MSP430G2112 would be a good option?

    Hope this helps and best of luck!

    Regards,

    Katie

**Attention** This is a public forum