Generating a 2 MHz clock signal from a Teensy 4.0

Harm

New member
I need a 2 MHz clock pulse, which I tried to do with PWM using:

analogWriteFrequency(14, 2000000);
analogWrite(14, 128);

This doesn't work. It looks like the maximum frequency I'm able to generate this way is 100 kHz on a Teensy 4.0.

So my question is: what is the best way to generate a 2 MHz clock pulse on a Teensy 4.0?
 
Thank you Defragster. Unfortunately my project still doesn't work and my cheap oscilloscope can't measure such high frequencies, so I can't check if the clock signal is correct or it is another issue. I think my next step will be to program another microcontroller I've laying around (a Raspberry Pi Pico) to work as a frequency counter to check the pulse.
 
Hi,
If you can connect your PWM output to pin 9 on your Teensy 4.x, you can use the FreqCount library to check the frequency,
All the best,
Alan
 
I'm running it on a Teensy 4.0 right now. Here's what my scope sees on pin 14.

file.png
 
I'm running it on a Teensy 4.0 right now. Here's what my scope sees on pin 14.

...

Paul, posting complete code in use would be cool - though it is just expected to be the res(6) line before the p#1 two lines.

with a T_4.1: Today's new DVM does Freq Hz counting and gives a ballpark number of 1.999 for the 2 MHz - so it wasn't accurate enough to rush and report success, a loop() {toggle} gave expected results ... and I got distracted seeing if I could drop resolution and get higher Hz counts - only seemed to get write of 37M/2(18.75MHz) on pin at 2 bit res, and measured 0 on the full 37.5M pin. The 0 Hz pins show ~'half voltage' - book doesn't quote Max usable Hz.

I played with a T_4.1 since I got a new DVM today - a $24 unit that seems better than others on hand - except all black case. But has NonContact AC detection too. And lights the Probe connect ports for the selected function!

It is '6000 Counts' manual ranging which means I get better range and values it seems not having to use 2V or 20V of the old Sparkfun or other - so should be better for measuring low current.
 
Works for me too.

Here is complete example using FreqCount to measure frequency on pin 14. (For Teensy 4, gateinterval is in microseconds ! Who knew. web page documentation needs updating.) Jumper pin 9 to pin 14.
Code:
#include <FreqCount.h>

void setup() {
  Serial.begin(57600);
  while (!Serial);
  analogWriteFrequency(14, 2 * 1000000); // test jumper 9 to 14
  analogWrite(14, 128);

  FreqCount.begin(1000000);  // 1 second gateinterval for T4
}

void loop() {
  if (FreqCount.available()) {
    unsigned long count = FreqCount.read();
    Serial.println(count);
  }
}
Sketch reports 2000000 every second. No real need to mess with analogWriteResolution(), as the teensy core adjusts duty value based on frequency (hardware/teensy/avr/cores/teensy4/pwm.c)
 
Thank you all for your feedback. I tested it and it works and then I tested it running through a TXS0108E level converter and it still worked and then I reconnected my set-up including a feed-back to pin 9 for testing and all of a sudden everything works :)

No idea what I've changed. Or perhaps something on the breadboards didn't connect properly. Anyway: it work.

Just to tell what I'm doing: I'm reworking an old Teensy 3.x project called YM2159Synth (https://github.com/trash80/Ym2149Synth) to drive an AY-3-8910 music chip from a Teensy 4.0 (through the aforementioned level converter and an MCP23017 i2c expander. My idea is to feed the audio output back into the Teensy using the audio board to add effects.
 
Back
Top