RA2E1 which timer to use?

I'm making a system that gets the signal of a hall sensor attached to a fan, the faster it spins the faster is the signal, i want to use the interrupts on the RA to count the rising edge and calculate the RPM with the amount of rising edges in a specific time, what timer should i use? i'm trying to use the R_GPT but when i StatusGet it giver me a tipe of data i cant use with an if statement, how can i do it and what is the best timer for this?

  • In your example, timer_status_t is a structure, so you are mismatching types.  The timer count would be in status.counter.

    A better way to do what you are attempting is to use the GPT with the input capture function.  It will latch the count on the rising or falling edge of the input (configurable) and that latched count would be available in the timer callback function when the input is triggered.  Calculate the difference in count between the  and you can determine the frequency of the input signal.

  • if i try to use the interrupt pin as capture A it gives me an error

  • What is the error?  If you mouse over the red block, it should tell you what is wrong.  You would need to configure "Capture A Source" or "Capture B Source" to use input capture.

  • added event link and it worked, i only need to set the input capture A or some other config?

  • Yes, and in the callback, look for the TIMER_EVENT_CAPTURE_A or TIMER_EVENT_CAPTURE_B event.  This if a fragment from code that is using input capture to do exactly what you are trying to do.  The pulse accumulator is for lower frequency signals that just count how many events have occurred:

    void ScCallback (timer_callback_args_t *pArgs)
        word wCapturedCount;
        if ((pArgs->event == TIMER_EVENT_CAPTURE_A) || (pArgs->event == TIMER_EVENT_CAPTURE_B))
            wCapturedCount = pArgs->capture;

  • Hello,

    As mentioned by dsherman26, you should use timer capture input.

    You need to calculate the external interrupt (hall signal)  frequency, in order to be able to calculate the speed on RPM.

    You should configure the input settings of timer in an appropriate way.

    To be more specific, you should define Start Source, Clear Source and Capture A Source to be ICU IRQ2. Don't use Stop Source for pulse measurement.

    Also, you should enable the overflow and capture A interrupt.

    Finally, you can add the code below, inside the timer callback function:

    float g_captured_time     = 0U;
    float pulse_time =0;
    uint32_t g_capture_overflows = 0U;
    float frequency;
    void timer_callback(timer_callback_args_t *p_args)
        /* TODO: add your own code here */
        if ((TIMER_EVENT_CAPTURE_A == p_args->event) || (TIMER_EVENT_CAPTURE_B == p_args->event))
              /* (Optional) Get the current period if not known. */
              timer_info_t info;
              (void) R_GPT_InfoGet(&g_timer1_ctrl, &info);
              uint64_t period = info.period_counts;
              /* The maximum period is one more than the maximum 32-bit number, but will be reflected as 0 in
               * timer_info_t::period_counts. */
              if (0U == period)
                  period = UINT32_MAX + 1U;
              g_captured_time     = ((period * g_capture_overflows) + p_args->capture) ;
              pulse_time = (float)(((float)g_captured_time)/((float)info.clock_frequency));
              frequency = 1/g_captured_time;
              g_capture_overflows = 0U;
          if (TIMER_EVENT_CYCLE_END == p_args->event)
              /* An overflow occurred during capture. This must be accounted for at the application layer. */

    Οn the hal entry you should call the R_GPT_Open and R_GPT_Enable functions.

    Hope it helps!



  • the code runs but i cant get the frequency variable value, it always get 0, when the fan is not moving it gives me frequency 0 and the leds flash, when i spin the fan the leds stop and the printing of the values too, when i stop the fan movement it prints the value and the leds continue to flash but the value printed is still 0

    i need the code to be constantly checking and reseting the timer, like it stays capturing the input for 100ms and them calculates the frequency and start over, there will be 4 fans connected simultaneously

  • Hello,

    Please change the frequency = 1/g_captured_time with frequency = 1/pulse_time and keep me informed.

    It is my mistake.


  • i didnt change, it still stops when it start spining and returns 0, the fan will be spining continuosly and with this code with the system stoping with the fan spining it will not work as i intend it, i was thinking of using a counter with the irq and count within the timer period and them calculate it everytime the timer loops, like
    interrupt_count/timer_period = spin_value
    interrupt_count = 0