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

Thread: flip flop out put is quiet slow

  1. #1

    flip flop out put is quiet slow

    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;
    }
    Click image for larger version. 

Name:	untitled.jpg 
Views:	3 
Size:	338.7 KB 
ID:	18313

    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

  2. #2
    Senior Member
    Join Date
    Dec 2013
    Posts
    238
    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.

  3. #3
    Senior Member
    Join Date
    Dec 2013
    Posts
    238
    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);
      }
    }

  4. #4
    thx I'll give it a try

  5. #5
    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);
    }

  6. #6
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,722
    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;
      }
    }
    Click image for larger version. 

Name:	screenshot.jpg 
Views:	3 
Size:	29.9 KB 
ID:	18315

  7. #7
    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);
    }

  8. #8
    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)

    Click image for larger version. 

Name:	Untitled.jpg 
Views:	1 
Size:	114.1 KB 
ID:	18316

Posting Permissions

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