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?

Parents
  • 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. */
              g_capture_overflows++;
          }
    }

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

    Hope it helps!

    Regards,

    AL_Renesas

Reply
  • 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. */
              g_capture_overflows++;
          }
    }

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

    Hope it helps!

    Regards,

    AL_Renesas

Children