Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 9 of 9

Thread: Teensy 3.1 for Frequency measurement

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    12

    Unhappy Teensy 3.1 for Frequency measurement

    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?

  2. #2
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    This is not a student's class assignment, right?

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,304
    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...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.

  4. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    12
    Quote Originally Posted by stevech View Post
    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.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,304
    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.

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    12
    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.

  7. #7
    Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    42
    Have you thought of going the hardware route. Might save an awful lot of work.

    Click image for larger version. 

Name:	F to V.jpg 
Views:	388 
Size:	29.0 KB 
ID:	1909

  8. #8
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    Quote Originally Posted by BriComp View Post
    Have you thought of going the hardware route. Might save an awful lot of work.

    Click image for larger version. 

Name:	F to V.jpg 
Views:	388 
Size:	29.0 KB 
ID:	1909
    Software approach, must less time consuming.

  9. #9
    Junior Member
    Join Date
    Jan 2014
    Posts
    12
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •