AVR128Dx - Measure external fan RPM using timers

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

HI ..

New to the AVR128Dx series.  It has a ton of features I love but it's always difficult to use them out without an example or two. 

 

I want to measure an external fan using it's tach out signal, typical 2500RPM.  I could use PWM to control it but that's for another exercise.  For now, just measure the RPM.  Assume that it's been treated to create a nice pulse.  I've found a few examples that use the timers but not in this external clock mode.  For example,

https://www.avrfreaks.net/forum/...

 

I'm thinking of using 2 timers ...

TCB:  Feed filtered RPM via and external pin and set it to count the pulses (even with a 3-pole rotor, it should not exceed the 16-bit counter)

TCA:  Create an interrupt every second.  The ISR will snapshot TCB and clear it for the next 1 second interval. 

 

A foreground task will then process the count.

 

Has anyone seen this example around?  Or do this a different way?

 

Thanks in advance for any advise ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi and thanks!

 

The

           Using Periodic Interrupt Mode | Getting Started with Timer/Counter Type A (TCA)

 

definitely helps the second half of my query ...  and I'm now looking through the Microchip Github to see if I can find an external clk source counter example ...

 

Thanks again ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to the Forum.

 

The Dx series is a gret new addition to the uC lineup!

 

In the AVR..DB.. data sheet, as an example, I suspect one way to measure the RPM pulses is to set up TimerCounter Type A in "Count on Event" mode.

 

If you have worked with the Xmegas then you might be familar with the "Event System".

Otherwsie this would be a great project with which to learn about it.

It is somewhat similar to an "extra" bus routing some control signals around the uC, between the various modules.

 

Have a look at the Events section of the T/C Type A, and also the other sections on the Event System.

I suspect, (I've not done this yet), you would configure an Input Pin to trigger an Event on a leading (rising) edge from the RPM pulse stream.

 

When properly configured the Event will cause the T/C-A to increment by one count.

 

You will still use your other T/C ISR to read the T/C-A that is holding the count, and then reset it to 0, and then restart your counting interval.

 

Note that since the fan speed isn't likely to change too much or too fast, you could also have a counting interval of 1/4 Sec or 1/10th Sec, etc.

 

Make sure that your RPM signal is 0/3V or 0/5V, depending upon what you are running the micro's power supply at.

 

Good luck with your project.

 

JC

 

Edit:Typo

 

 

 

Last Edited: Tue. May 4, 2021 - 02:59 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

>TCB:  Feed filtered RPM via and external pin and set it to count the pulses (even with a 3-pole rotor, it should not exceed the 16-bit counter)

>TCA:  Create an interrupt every second.  The ISR will snapshot TCB and clear it for the next 1 second interval. 

 

TCB is basically all you need. Use one of several capture modes, for example the 'Input Capture Frequency Measurement' mode. Setup a pin (tach in) as an event generator, and the tcb as its user. Setup the CAPT and OVF interrupts, where the OVF will keep track of overflows as you will be dealing with relatively long times for the timer clock speed in use (PER or PER/2). The CAPT interrupt will get a counter value captured and the overflow count will add to the info. You now have a period count, and since you know the clock speed you can figure out rpm.

 

So for example a CAPT irq occurs, your isr reads the CCMP register to get the capture value and also check your overflow count. If a 4MHz clock, and a 60RPM signal, a capture value may be 2304 and the overflow count 61. So you have a 32 bit value of  0x003D0900 (61*65536 + 2304). At 2500RPM, 0x000017700 (1*65536 + 30464). Clear the overflow count when done reading ccmp as the counter already is counting from 0 now. There may be 1 instance when both the capt and ovf irq are set where the ovf should be ignored- if they occur at the same time where the ccmp value is at the max 65535 (?). Since any capt also clears the counter in this mode, this may be the only thing to watch out for as all other cases the counter resets so would not get to the ovf in the time it takes to get to the isr.

 

rpm = 60*clk/cnt

rpm = 60*4000000 / 0x3D0900 = 60 

rpm = 60*4000000 / 0x17700 = 2500

 

Every pulse you get an rpm value you can calculate, and if you want to do averaging across multiple captures, you can do that also. If your overflow count value gets larger than a certain value, you know you have trouble (0 rpm, or less than some minimal value you should see). You also have a natural update rate in effect- slow rpm's update slower, faster rpm's update faster.

 

Read the TCB chapter and the EVSYS chapter. PinXn as event generator -> TCB CAPT event user -> TCB CAPT.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DocJC wrote:
I suspect one way to measure the RPM pulses is to set up TimerCounter Type A in "Count on Event" mode.

 

The AVR-Dx have upgraded type B counters, that among other things can now count events. Since type A timers are more valuable, I'd use a type B to count the tach pulses over a certain time, or the frequency measure method described in #5.