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

Thread: Counting external pulses in counters: Teensy 3.2, Teensy 4.0 or something else?

  1. #1

    Counting external pulses in counters: Teensy 3.2, Teensy 4.0 or something else?

    I want to count two or three external signals that deliver pulses to the MCU, without ever missing one count.

    Currently I am using a Arduino Mega 2560, but sadly it only has one counter input connected (T5 to pin 47). I have read that the Seeduino mega does break out more counters, but I am not sure how easy it would be to get one in Europe.

    Now I understand that counters are very hardware dependent, and will probably look completely different on the ARM CPUs of Teensy 3.x or 4.0. Even on the AVR it was probably the most complex feature I ever used.

    How difficult is it to do stuff like this (count the pulses on a pin, trigger an interrupt when count reaches 1000) on the Teensy? And should I choose the 3.2 or the new 4.0, or will it make no practical difference?

    Thanks in advance,
    Ralph

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,735
    T_3.2 has one pin for FreqCount. It seems three were available on Teensy 4 - two edge pins and one bottom pad - but I'm not sure if they can work in parallel or if they overlap? And only one was mapped to FreqCount at this time.

    How far apart are the pulses to each pin? Is the processor doing much else after seeing the target counts?

  3. #3
    Thanks, defragster! One of the signals is 1kHz or so, one is 1Hz, maybe later some third signal will be added in the same range.

    And I do not need frequencies, just aggregated totals from the last manual reset of the counter, if that makes a difference.

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,735
    Just checking frequencies because the Teensy could just trigger an interrupt on each pulse and count them at those low rates without missing anything. If they were near or over 1MHz that could cause misses at some point - though the T4 could handle much faster.

    T4 would do it easily with interrupt on RISE for each of three pins - and each pin with an _isr() could count and act accordingly.

  5. #5
    Senior Member
    Join Date
    Apr 2019
    Posts
    141
    Quote Originally Posted by defragster View Post
    Just checking frequencies because the Teensy could just trigger an interrupt on each pulse and count them at those low rates without missing anything. If they were near or over 1MHz that could cause misses at some point - though the T4 could handle much faster.

    T4 would do it easily with interrupt on RISE for each of three pins - and each pin with an _isr() could count and act accordingly.
    use FreqMeasureMulti

    works fine on T3.2

  6. #6
    Quote Originally Posted by Gadget999 View Post
    use FreqMeasureMulti

    works fine on T3.2
    Thanks! I will have a look at that code.

    Is there a direct equivalent to the 16 bit counters in e.g. the Teensy 2.0 (feeding an input to the T1 pin, counting in CTC mode, comparing to OCR)?

    There does seem to be a Low Power Timer (there is only one of these?) and the FlexTimer (several of them), but can I hook them up to an external source, and count without generating an interrupt upon each external pulse, but only on coute
    Is there a tutorial similar to https://www.robotshop.com/community/...terrupts/13072 for the ARM family on the Teensy 3.2 or 4.0 on how to setup timers, what input pins they are linked to, etc.?
    Even if with the faster CPU doing everything with interrupts is very feasible, I want to learn how to use timers -- if possible, and if it is not orders of magnitude more complicated than on AVR architecture.

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,577
    Quote Originally Posted by ausserirdischesindgesund View Post
    There does seem to be a Low Power Timer (there is only one of these?) and the FlexTimer (several of them), but can I hook them up to an external source, and count without generating an interrupt upon each external pulse,
    Yes, the low power timer supports this. It's used by the FreqCount library, so that should be your first place to look for example code. Of course, the reference manual gives all the details about the timers. The nice thing about LPTRM is it's fairly simple, not loaded with advanced PWM features that take hundreds of pages to document.

    If you read the ref manual, you'll also see the FTM timers can theoretically do counting of external pulses. But you can't get access to the pins needed to give them a clock signal. Hopefully knowing that now can save you from wasting time exploring the huge FTM chapter, only to later discover scattered across other chapters that you can't actually use it.

    It definitely does work with LPTRM, as FreqCount demonstrates.

  8. #8
    Quote Originally Posted by PaulStoffregen View Post
    Yes, the low power timer supports this. It's used by the FreqCount library, so that should be your first place to look for example code. Of course, the reference manual gives all the details about the timers. The nice thing about LPTRM is it's fairly simple, not loaded with advanced PWM features that take hundreds of pages to document.

    If you read the ref manual, you'll also see the FTM timers can theoretically do counting of external pulses. But you can't get access to the pins needed to give them a clock signal. Hopefully knowing that now can save you from wasting time exploring the huge FTM chapter, only to later discover scattered across other chapters that you can't actually use it.

    It definitely does work with LPTRM, as FreqCount demonstrates.
    Thanks a lot for this, I had looked at the two chapters a bit, but compared to the Atmel timer documentation, it is probably 10 times as much information, and somewhat different terminology again.

    I suppose I should arrive in the 21st century, and give up my 8bit habit ; Thanks!

Posting Permissions

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