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.

How to calculate toggling rate (or generate Delay) for 1 Sec.for led using Timer .

Other Parts Discussed in Thread: MSP430G2231

Hello Sir,

        I am new to MSP430, using TI MSP Launchpad with MSP430G2231.I am trying with different example code provided with Launchpad.

I want to know,how to calculate toggling rate of led(or specific delay of 1sec.) using Timer,there is any formula.

 

Like 8051,we have to divide Machine Cycle period i.e 1.085u Sec (with 11.0592MHz Crystal)  by delay we want.& load these hex value in TH1 & TL1.

How it's calculation depends upon clock(i.e.MCLK,ACLK,SMCLK) in MSP430.

 

  • Well, on a 11MHz signal, it is difficult to get a 1s delay without further software support.

    11MHz means 111059200 crystal ticks per second. Yet the timer has only a 16 bit counter and can only make delays up to 65536 ticks.

    Yu can set up a tierm ISR that is called on every tiemr overflow. Which pappens 1694.628906 times per second. Count a variable up inside the ISR and when it reaches 1695 (rounded up) , one second (and a few microseconds) have passed.

    You can divide the crystal frequency before passing it to the timer, by using the ACLK or SMCLK dividers (thsi will reduce the clock speed globally for all MSP modules) and also by dividing it in the timer itself. Yet  you need to divide by 11 bit, which isn't possible (there are only 6 or 8 bits you can use for dividing the crystal frequency - depends on the MSP). Also, the chunkyness of the individual interrupts increases, giving you an increasingly inaccurate timing.

    You can use the CCR0 unit to limit one timer cycle from 65536 to something less, something that gives an integer result if you divide your crystal frequency by it.

    I guess, for best results, you'll end up with a combination of all three.

    personally, I set up my system to have a timer interrupt every millisecond. So together with a counter variable, I cna time every interval from 1ms to 65 seconds with 1ms granularity. For smaller , more precise intervals I use the CCR1 register and can time delays from ~6 microseconds (code overhead) to 65ms.

    P.s.: Where did you get these 'TH1' and 'TH2' register names? I cannot find them in the G2231 datasheet or the 2x users guide. Is it form the example? Then you should know that these examples are often just for exactly this one use on exactl this one device. Adjusting them for own projects often won't work as expected. It's better to read the datasheet and users guide, then try to understand hwo the example works, and then write your own code.

     

  • sorry for miss questioning,i just correlating the term with basic 8051 microcontroller,so TH1 & TL1(belongs to 8051 uC)  not belongs to MSP430G2231,no issue for that.

    11.0592MHz crystal also belongs to 8051 microcontroller, i am trying ti question you by taking reference of it.

    But in MSP I think,we use 32.678kHZ for ACLK,1.1MHz-16MHz for DCO &12 KHz for SMCLK.

    So by using these CLK's  how it is possible in calculation of delay generation.

  • bhushan kulkarni said:
    TH1 & TL1(belongs to 8051 uC)

    Ah, yes, I dimly remember. I already thought so, but I cannot confirm something I only guessed :)

    bhushan kulkarni said:
    11.0592MHz crystal also belongs to 8051 microcontrolle

    Did it? I've seen it clocked by many different setups, but I guess it's the nicest base frequency for any usual serial baudrate.
    The MSP, however, can deal with fractional frequencies for baudrates and it does fairly well. So cheap 8MHz and 16MHz crystals aren't a problem - and they are nicer for 'round' timings such as ms and µs,

    bhushan kulkarni said:
    But in MSP I think,we use 32.678kHZ for ACLK,1.1MHz-16MHz for DCO &12 KHz for SMCLK.

    Soem MSPs have limited assignments for A/M/SMCLK, but the newer ones can use any clock source for any of the three.
    I wouldn't, however, use VLO (12kHz) for SMCLK. First, it is very inaccurate, then it is of no use for almost everything. Its only purpose is to provide at least a minimum clock in extreme low-power mode (if timing is completely unimportant), adn as emergency fallback if everything else fails.
    If you have a consistent low-frequency ACLK, there is no need to use the VLO.

    If you have ACLK running on 32768Hz, it is easy to get a 1s delay.

    set timerA into up mode, ACLK source, no divider.
    set TACCR0 to 32767.
    enable CCR0 interrupt

    The timerA0 interrupt service routine will be called every second.

    Either you set a global variable to false in your main and wait for the timer ISR setting it back to true, or you enter LPM and let the tiemr ISR wakeup the device (and main will sleep and continue after the itnerrupt).

    In both cases, you'll have t reset the timer counter before waiting, so you can be sure that complete 1s interval starts when you want it to start. (else you'd get a 1s timer tick that eplapses in 1s intervals, but you won't get a 1s delay if you start waiting in the middle between two ticks).

    A different approach is to set up the timer so it 'ticks' in much shorter intervals and you program your delay to wait a certain number of ticks. This still gives you a consistent timeline, but reduces the 'jitter' to +-1 tick.
    As I already wrote, my setup is a 1ms tick, so I can get any delay from 1ms to 65s (with +0-1ms accuracy). Since 32768Hz isn't a multiple of 1000Hz, it is not the best base frequency for this setup. 1MHz is better, which can be derived from a 16MHz crystal clock divided by 16 etc.

    Compared to the 8031, the MSP has by magnitudes more powerful hardware modules, so if oyu plan moving forwared, you should abandon the old strategies used for the 8031 and open your rmind for some new approaches to do the same, but more efficiently, and maybe completely in hardware instead by software.

**Attention** This is a public forum