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
  • 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.

    Regards

  • 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

  • Hello,

    The above configuration has been verified to measure correctly external signal's  frequency. If you still face any issue, you can send your project to check the code and FSP configuration.

    Regards

  • RA2E1_Interrupt_test.rar

    the problem with the code is that if i enable the interrupt on irq it interrupts the code while the fan is spining and giving signal, if i disable the irq the timer doenst show me any values to frequency, since the fan will be spining all the time it cannot stop the whole code while it spins, i was thinking of enabling the interrupt only for the timer period and them disabling it for each fan in an order

  • Hello,

    Can you please explain the project more?

    I cannot understand why to disable the irq ( external interrupt irq or timer irq). If you do it, you will not be able to have speed feedback, in order to implement close loop control, unless if the motor is controlled by different mcu or is it is controlled sensorlessly and you just want to measure the speed using the speed sensor.

    Regards