20Mz PWM on a teensy ?

Status
Not open for further replies.

phil123456

Active member
Hello,

I try to generate a 20mh 50% duty cycle signal on the teensy 3.6

I can barely achieve 6Mhz, which is weird, since the teensy runs at 240Mhz

anyone sees something wrong with my code ?

thanks for your help

Code:
#define MCLK 9

void setup() {
  pinMode(MCLK, OUTPUT);
  CORE_PIN9_CONFIG = PORT_PCR_MUX(1); // no slew rate limit
  noInterrupts();  // look pretty for the oscilloscope trigger
}

void loop() {
    digitalWriteFast(MCLK, 0);
    digitalWriteFast(MCLK, 1);
}

Untitled.jpg
 
Two issues.

1: By default pinMode() turns on the slew rate limit feature. This greatly reduces noise and undesirable high speed effects when normal wires are used, but it also limits how fast the pin can change.

2: There is significant overhead after loop() runs. Use a while (1) loop to check the raw performance.

This code gives a 48 MHz waveforms on pin 9 when run on Teensy 3.6 overclocked to 240 MHz.

Code:
void setup() {
  noInterrupts();
  pinMode(9, OUTPUT);
  CORE_PIN9_CONFIG = PORT_PCR_MUX(1); // no slew rate limit
  while (1) {
    digitalWriteFast(9, HIGH);
    digitalWriteFast(9, LOW);
  }
}

void loop() {
}

Measuring this signal well, even with a high bandwidth oscilloscope, is tricky. The high speed edges create a lot of overshoot and ringing if a normal wire is used.

Here's a prior thread with more info.

https://forum.pjrc.com/threads/4187...lator-example)?p=132359&viewfull=1#post132359
 
You can also get a 20 MHz waveform from the PWM hardware, but not at 50% duty cycle. The timers run at 60 MHz (when Teensy 3.6 runs at any multiple of 60 MHz), so a 20 MHz carrier is only 3 timer clocks.

Code:
void setup() {
  analogWriteFrequency(9, 20000000);
  analogWrite(9, 128);
}

void loop() {
}
 
it works while the previous code freezes the teensy
but I now reach 4mhz
I try to find where to raise the saleae sampling rate
 
while the previous code freezes the teensy

If you turn off interrupts, the USB interrupt can't respond to Arduino's request to automatically reboot. Teensy is running, just not handling any interrupts when I/O occurs.

Expect to need to press the pushbutton on Teensy to reprogram.
 
At minimum you need 40Mhz - that's the minimum need for sampling 20MHz but can say nothing about timing details. For your pwm, I'd go for 100MHz absolutely minimum. If your saleae can do more, choose even more.
 
ok with the scope the signal goes to 2 Mz and 48Mz with digitalWriteFast

thanks gys

maybe Saleae cant go that fast becos of the usb port
 
Mine, a clone, can do 100MHz.
The newer official ones are capable of much much more for even more $$ :)

Edit: Crosspost
 
Just an FYI - With the Saleae logic analyzers - If you have a lot of channels being captured, your sample rate is lower.

For example with my Pro 8, with two channels, I can capture at 500mhz. but with all 8 only 250mhz... With my other non-pro versions, there is more of a change.
With 2 I can run 100mhz, but 3 drops to 50 and all 8 drops to 25... I have not tried the new software yet with my older 16 to see what speeds it can do.
 
Status
Not open for further replies.
Back
Top