flip flop out put is quiet slow

Status
Not open for further replies.

bob1984

Member
Hello,

I need to generate a +- 22MHZ pulse

and strangely enough digitalWrite functions are extremely slow compared to the teensy 3.6 working frequency (240Mhz)

Code:
#define MCLK 9

byte pulse=0;

void setup() {
  pinMode(MCLK,OUTPUT);
}

void loop() {
  digitalWrite(MCLK,pulse); pulse=1-pulse;
}

untitled.jpg

with this code I can only achieve around 600khz

I know I can do pwm, but out of curiosity, why is it so slow ???

thanks
 
Try digitalWriteFast.
There is of course other caveats such as loop() overhead and pin slew rate limiting, which is on by default on teensy 3.x.
 
;) From LINK: STRANGE GPIO speed perfomance with digitalWriteFast() (ring oscillator example)
Code:
void setup() {
  pinMode(9, OUTPUT);
  CORE_PIN9_CONFIG = PORT_PCR_MUX(1); // no slew rate limit
  noInterrupts();  // look pretty for the oscilloscope trigger
}

void loop() {
  while (1) {
    digitalWriteFast(9, HIGH);
    digitalWriteFast(9, LOW);
    digitalWriteFast(9, HIGH);
    digitalWriteFast(9, LOW);
    digitalWriteFast(9, HIGH);
    digitalWriteFast(9, LOW);
    digitalWriteFast(9, HIGH);
    digitalWriteFast(9, LOW);
  }
}
 
apparently digitalWriteFast does not output anything , I get a flat line until I replace it by the usual usual digitalWrite

I tried with pwm, I barely reach 10Mhz and the signal is more a sine than a CLK signal

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
  analogWriteFrequency(MCLK, 10000000);
  analogWrite(MCLK, 128);
}
 
Works for me...
I modified your code slightly as to remove the overhead of calling main:
Code:
#define MCLK 9

byte pulse = 0;

void setup() {
  pinMode(MCLK, OUTPUT);
}

void loop() {
  while (1) {
    digitalWriteFast(MCLK, pulse); pulse = 1 - pulse;
  }
}
screenshot.jpg
 
I could manage to have 20Mhz !! but the signal barely comes back to zero even with a pull down

Code:
void setup() {
  pinMode(MCLK,OUTPUT);
  CORE_PIN9_CONFIG = PORT_PCR_MUX(1); // no slew rate limit
  noInterrupts();  // look pretty for the oscilloscope trigger
  analogWriteResolution(2);
  analogWriteFrequency(MCLK, 20000000 );
  analogWrite(MCLK, 2);
}
 
yes I tried your code, it works ... now the signal is really gross (no matter the code)

I actually try to have a CS5343 working (as master, at first) becos so far when the teensy is the master, the chip does not answer at all

and looking at the clock, no wonder why (I'll try with a digital analyser as you did)

Untitled.jpg
 
Status
Not open for further replies.
Back
Top