Teensy 3.1 for Frequency measurement

Status
Not open for further replies.

digi.ctrl

Member
In one of my projects, I need to measure frequency of external digital pulses. The pulse rate could be up to 1 MHz with duty cycle of 30 - 70%. How should I go about it? Which pin to use for external input? How to configure 32 bit counter? Also, how to generate precise 1 sec gate period?
 
This project might be much easier on a Teensy 2.0, where you can use the FreqCount library. Eventually I'll port this to 3.1, but I'm currently focusing on the audio library, so it won't happen for some time.

If you want to try, you'll have to dig into the details of using the chip's timers directly. It is possible. For example, here's an excellent page about using the input capture (as the FreqMeasure library does... which is best for lower frequencies)

http://www.digitalmisery.com/2013/06/timer-input-capture-on-teensy-3-0/

While the 3.1's timers are much more advanced, they work similarly to those on 2.0, just with a lot of extra features. You can probably use the FreqCount library as a general guide to the basic technique, but there's no escaping the need to dig into the vast reference manual to learn how the timers actually work.
 
This is not a student's class assignment, right?
Sorry, the post might look to be very basic. But, I didn't want any tutorial. Actually, my requirement is much complicated than simple frequency measurement. But, I wanted to start with the basic. It is clear from Paul's post that there is no easy way to do FreqCount in Teensy 3.x. I have gone through the manual (FTM in Chapter 36). My first quarry was how to make 32 bit counter from 16 bit ones. As, there is no library available for Teensy 3.x on counter, I expect a reply from anybody who has used counter.
 
Extending a 16 bit counter to 32 bits is done pretty much the same way on Teensy 3.1 as all other microcontrollers. You enable an interrupt to occur when the 16 bit timer overflows from 0xFFFF back to zero. In the interrupt, you increment the upper 16 bit variable.

Reading all 32 bits properly can be tricky. Again, this issue is basically the same on all microcontrollers. The simplest approach is usually to disable interrupts, read the timer, then read the upper 16 bits, and read the timer again. Then re-enable interrupts and check if the two timer readings are equal. If the 2 timer reading were equal, or the second is greater than the first, you can safely conclude the upper 16 bits didn't roll over while you were reading. If the second reading is less, usually the simplest thing is to just retry.
 
Thanks Paul. After a long time, I received a reasonably simple answer. Actually, I need two 32 bit counters and some synchronization. As, it might be difficult to do using only Teensy, I started looking for open source, low cost FPGA boards.
 
Have you thought of going the hardware route. Might save an awful lot of work.

F to V.jpg
 
Can I get a hint to deal with two 32 bit counter (max 10 MHz) and synchronization (D-FF) using Teensy 3.1 & software? Actually, now I am considering Cypress PSoC instead of uC+FPGA.
 
Status
Not open for further replies.
Back
Top