Frequency Counter

jerryk

Member
Hello!

I'm working on a project with a Teensy 4.1. Part of it is a frequency counter. Doesn't have to be super accurate. I need to count up to 50MHz to an accuracy of maybe 10kHz.

I found an example on github:

https://github.com/manitou48/teensy4/blob/master/gpt2_count.ino

The general principle is that you set GPT2 to use an external clock, and set up
an IntervalTimer object to read GPT2 at the gate time.

Unfortunately, it poops out above about 10MHz. Apparently, there is an internal clock ( clk_ipg ), and you cannot count anything higher than 1/4 the frequency of that clock. Even the prescaler doesn't work any faster.

The datasheet is coy about the frequency of clk_ipg, but I suspect 40MHz.
Any way to set it faster? Any other way to count that's not subject to the 1/4 clk_ipg limitation?

Right now, I'm looking at a string of D flip flops....

- jerryk
 

No, didn't know about that. I just looked at it now. Apparently, it uses the qtimers, which I guess do not have the frequency limitation of the GPTs. Alas, I don't think I can use it. There is a very limited subset of pins that can be used with the Qtimers.
And the only free one - the one that you used - on the card I'm using - is dedicated to the data/command pin on the SPI interface going to the LCD.

...Although I *could* change the data/command pin - that's specified in software. Then a couple of trace cuts and little green wires....
 
OK, I successfully got the display working off D7, thus freeing up D9 for the
frequency counter. One strangeness - the display doesn't come up when I cycle
power on the card.

But the display does come up when I upload the sketch to the Teensy via USB.
It also comes up if I hit the reset pin ( on the display card ) with a momentary
short to ground.

Right now, the display "reset" is not connected to anything except a couple of
grounded capacitors, one 10uF, and also a 0.1uF. Might be interesting to actually
connect it to a processor i/o pin.
 
One strangeness - the display doesn't come up when I cycle
power on the card.

Try adding a delay before you initialize the display.

Long ago, before a 300 ms delay was added in the startup code, this used to be a very common problem where Teensy boots up too quickly, before other hardware is ready. Maybe your display is taking longer than that 300ms startup before it's ready to use?
 
Try adding a delay before you initialize the display.

Tried that, no joy.
----------------- snip -----------------
void init_spi( void )
{
delay( 500 );
spi_bus = new Arduino_HWSPI( 7, 10 );
delay( 500 );
gfx = new Arduino_ILI9341( spi_bus, TFT_RST, 1 ); //landscape mode
delay( 500 );
}
----------- endsnip --------------------

Again, I get normal display after uploading the sketch. But when I cycle power
by unplugging the USB, it gives me a white screen. It worked normally on cycling power when I was using digital pin #9. But with digital pin #7, this is happening.
The serial monitor shows that the program is running normally otherwise. The touch is working.
 
On the off chance that there was something magical about digital pin 7, I changed it to 32 - which was closer to the LCD connector anyway. No joy. Same-o same-o. Works on download, doesn't work on power cycle.
 
And last night I tried moving the green wire back to digital output 9, where it had originally been. NO CHANGE. The system acts just like it did with pins D7 and D32. Comes up normally on download, white screen on power cycle.

The only substantive change I can think of is that I moved the invocation of the constructors - of the SPI and
GFX - to an actual subroutine, instead of being at the top level - so I could have control of the timing - to stick
delays before actual invocation.

Clearly, there's something else that I did - that I forget. Unfortunately, not using a version control system, and I can't just back it up.
 
And last night I tried moving the green wire back to digital output 9, where it had originally been. NO CHANGE. The system acts just like it did with pins D7 and D32. Comes up normally on download, white screen on power cycle.

*** Found it! Totally out of left field. The cap that gives the video card a proper reset - had failed. By total coincidence,
at the same time I was doing this pin# change. A 10uF surface mount electrolytic; it was showing .1nF on my multimeter. I bridged
it with a 10uF tantalum, and all is now well.

Back to the Qtimers...

- jerryk
 
Back
Top