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 implement a system clock by software and create software counters

Other Parts Discussed in Thread: MSP430F5635, MSP430F5636

Hi

I am using MSP430F5635.

My application needs to implement different types of delays .

Few delays in terms of seconds , few in terms of milliseconds and few in terms of micro seconds and so on.

The Timer modules available in the controller is not sufficient .

How do i implement a system clock by software and create software counters ?

Thanks in advance.

Jenitta

  • Hi,

    This example allows you to manage 3 diferent services, at diferent rates.

    Best Regards,

    AES

    #include <msp430f5636.h>

    #define SERVICE_A_COUNT 2000;
    #define SERVICE_B_COUNT 200;
    #define SERVICE_C_COUNT 20;


    unsigned int counter_A;
    unsigned int counter_B;
    unsigned int counter_C;
    //******************************************************//
    // Main code
    //******************************************************//

    void main(void)
    {
      WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

      counter_A = SERVICE_A_COUNT;                // Service Counter A
      counter_B = SERVICE_B_COUNT;                // Service Counter B
      counter_C = SERVICE_C_COUNT;                // Service Counter C

      TA0CCTL0 = CCIE;                          // CCR0 interrupt enabled
      TA0CCR0 = 50;
      TA0CTL = TASSEL_2 + MC_1 + TACLR;         // SMCLK, upmode, clear TAR

      __bis_SR_register(LPM0_bits + GIE);       // Enter LPM0, enable interrupts
    }
    //******************************************************//
    // Service A code
    //******************************************************//
    void service_A(void)
    {
        __no_operation();
    }
    //******************************************************//
    //  Service B code
    //******************************************************//
    void service_B(void)
    {
        __no_operation();
    }
    //******************************************************//
    // Service C code
    //******************************************************//
    void service_C(void)
    {
        __no_operation();
    }
    //******************************************************//
    // Timer0_A0 interrupt service routine
    //******************************************************//
    // Timer0 A0 interrupt service routine
    #pragma vector=TIMER0_A0_VECTOR
    __interrupt void TIMER0_A0_ISR(void)
    {

        counter_A--;
        counter_B--;
        counter_C--;

        if (counter_A == 0)
            {
            counter_A = SERVICE_A_COUNT;
            service_A();
            }
        if (counter_B == 0)
            {
                counter_B= SERVICE_B_COUNT;
                service_B();
            }
        if (counter_C == 0)
            {
                counter_C = SERVICE_C_COUNT;
                service_C();
            }

    }


  • AES, that's a nice starting point for multi-purpose timers. Especially for longer delays, as teh coutners are multiplied with the timer cycle.

    Next step is to use the CCR units for generating independent interrupts for different trigger frequencies. Just let the timer run free, and when a CCR unit triggers, add the 'selay' value for this specific interrupt to the CCR value. So e.g. CCR0 triggers at 1000,2000,3000, 65000,464,1464 etc. Of course for this the delay needs to be smaller than a complete timer cycle.

  • Hi,

    The SMCLK is 1.048576 MHz . So it counts 50 in approximately 50 ms .

    The ISR gets triggered every 50 ms .

    By 2000 we are creating 2000*50 = 10000 ms delay

    By 200 we are creating 200 * 50 = 1000 ms = 1 second delay

    By 20 we are creating 20*50 = 100 ms delay .

    Is my understanding correct ?

    Thanks a lot for your kind reply .

    Jenitta

     

  • Hi,

    Yes, it is correct.

    Best Regards,

    AES

  • Shouldn't it be microseconds instead of milliseconds? Or did I miss a divider somewhere?

  • Bernhard Weller said:
    Or did I miss a divider somewhere?

    No.

    Indeed, it counts from 0 to 50 (51 steps!) in 48,637µs (=20560Hz)

**Attention** This is a public forum