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.

CC2541: CC2541 timer 4 setup and interrupt function

Part Number:

Hi! I'm  trying to use timer 4 to generate an 1MHz interrupt to count microsseconds precisely, however it's not working well. Here's my code:

//Timer 4 Setup function
void Timer4_init(void)
{
    //******************** Timer4, channel 
    //  Timer 4 channel 0 compare value.
    
    CLKCONCMD = (CLKCONCMD & ~CLKCON_TICKSPD) | CLKCON_TICKSPD_32M;
    T4IE = 1;
  /***************************************************************************
       * Timer 4 setup 
       *
       * Timer 4 channel 0 compare value. Initiates the Timer terminal count value
       * to 224. This value is reseted at each Timer compare match interrupt.
       */
    T4CC0 = e0;//224 -> 256 - (32MHz/(Prescaler*Target_Frequency)): Target_frequency=1MHz
    /* Timer 4 channel 0 (T4CCTL0) configuration:
      * - Channel 0 interrupt enabled.
      * - Compare mode.
      */
      T4CCTL0 = T4CCTLn_IM | T4CCTLn_MODE;

    /* Timer 4 control (T4CTL) configuration:
     * - Prescaler divider value: 1.
     * - Modulo mode.
     * The Timer is also cleared and started.
     */
    T4CTL = T4CTL_DIV_1 | T4CTL_MODE_MODULO | T4CTL_CLR | T4CTL_START;

}
//Interrupt function
#pragma vector = T4_VECTOR
 __interrupt void t4_isr(void)
{
  //     // Clears the module interrupt flag.
  T4OVFIF = 0;
    
  static uint16 micros = 0;
   
  micros ++;
  
  // Clears the CPU interrupt flag. 
  T4IF = 0;
   
}

  • BRUNO CHAVES said:

    Part Number: CC2541

    Hi! I'm  trying to use timer 4 to generate an 1MHz interrupt to count microsseconds precisely, however it's not working well. Here's my code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    //Timer 4 Setup function
    void Timer4_init(void)
    {
        //******************** Timer4, channel
        //  Timer 4 channel 0 compare value.
        
        CLKCONCMD = (CLKCONCMD & ~CLKCON_TICKSPD) | CLKCON_TICKSPD_32M;
        T4IE = 1;
      /***************************************************************************
           * Timer 4 setup
           *
           * Timer 4 channel 0 compare value. Initiates the Timer terminal count value
           * to 224. This value is reseted at each Timer compare match interrupt.
           */
        T4CC0 = e0;//224 -> 256 - (32MHz/(Prescaler*Target_Frequency)): Target_frequency=1MHz
        /* Timer 4 channel 0 (T4CCTL0) configuration:
          * - Channel 0 interrupt enabled.
          * - Compare mode.
          */
          T4CCTL0 = T4CCTLn_IM | T4CCTLn_MODE;
        /* Timer 4 control (T4CTL) configuration:
         * - Prescaler divider value: 1.
         * - Modulo mode.
         * The Timer is also cleared and started.
         */
        T4CTL = T4CTL_DIV_1 | T4CTL_MODE_MODULO | T4CTL_CLR | T4CTL_START;
    }
    //Interrupt function
    #pragma vector = T4_VECTOR
     __interrupt void t4_isr(void)
    {
      //     // Clears the module interrupt flag.
      T4OVFIF = 0;
        
      static uint16 micros = 0;
       
      micros ++;
      
      // Clears the CPU interrupt flag.
      T4IF = 0;
       
    }

    BRUNO CHAVES said:

    Part Number: CC2541

    Hi! I'm  trying to use timer 4 to generate an 1MHz interrupt to count microsseconds precisely, however it's not working well. Here's my code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    //Timer 4 Setup function
    void Timer4_init(void)
    {
        //******************** Timer4, channel
        //  Timer 4 channel 0 compare value.
        
        CLKCONCMD = (CLKCONCMD & ~CLKCON_TICKSPD) | CLKCON_TICKSPD_32M;
        T4IE = 1;
      /***************************************************************************
           * Timer 4 setup
           *
           * Timer 4 channel 0 compare value. Initiates the Timer terminal count value
           * to 224. This value is reseted at each Timer compare match interrupt.
           */
        T4CC0 = e0;//224 -> 256 - (32MHz/(Prescaler*Target_Frequency)): Target_frequency=1MHz
        /* Timer 4 channel 0 (T4CCTL0) configuration:
          * - Channel 0 interrupt enabled.
          * - Compare mode.
          */
          T4CCTL0 = T4CCTLn_IM | T4CCTLn_MODE;
        /* Timer 4 control (T4CTL) configuration:
         * - Prescaler divider value: 1.
         * - Modulo mode.
         * The Timer is also cleared and started.
         */
        T4CTL = T4CTL_DIV_1 | T4CTL_MODE_MODULO | T4CTL_CLR | T4CTL_START;
    }
    //Interrupt function
    #pragma vector = T4_VECTOR
     __interrupt void t4_isr(void)
    {
      //     // Clears the module interrupt flag.
      T4OVFIF = 0;
        
      static uint16 micros = 0;
       
      micros ++;
      
      // Clears the CPU interrupt flag.
      T4IF = 0;
       
    }

  • Please take a look at the examples found here:

    Siri