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

Thread: How to use digital outs as a square wave generators?

  1. #1

    How to use digital outs as a square wave generators?

    I am designing a music synthesizer that will need multiple square wave outputs at different frequencies from multiple Teensy4/4.1. As I need a maximum number of outputs I thought about using digits outs, as there is more of them than PWM outs. How do I write some code to define the frequency of a square wave from the digital outs?

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,111
    With PWM, up to 18 different frequencies are possible on Teensy 4.0.

    https://forum.pjrc.com/threads/59677...is-it-possible

    With the audio library, you can synthesize many more waveforms (hundreds on Teensy 4) not just limited to square waves, then filter, shape, and mix them. The best place to start is this tutorial, especially part 2-10 starting on page 20.

    https://www.pjrc.com/store/audio_tutorial_kit.html

  3. #3
    Is it possible to use all 31 PWM outs for 50/50 duty cycle square waves without PWM?

  4. #4
    Senior Member
    Join Date
    Mar 2016
    Posts
    133
    why would you want to? the answer is, yes. With sufficient software but that looks a lot harder than using the hardware exactly as it was designed to do.

  5. #5
    Quote Originally Posted by PhilB View Post
    why would you want to? the answer is, yes. With sufficient software but that looks a lot harder than using the hardware exactly as it was designed to do.
    Which part of my question are you referring to? Having 31 outputs or the PWM part?

  6. #6
    Senior Member
    Join Date
    Mar 2016
    Posts
    133
    Taken as whole. But to put a point on it, why no PWM? Does a 50% PWM wave form not meet your application needs?

  7. #7
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    6,932
    One thing that is making this harder to follow, as this logical thread has been winding through several threads, so unless you dig around you have no clue what the issues are.

    That is Nick would like a lot of square waves all at different frequencies. With PWM you can only get about 13 or so unique PWM frequencies. That is several of the PWM pins share using the same clock, so restricts how many you can do easily.

    Yes you can do it software wise, but that can be more complicated and probably not as accurate.

    Again I don't know how much of a range of different square waves you need, are they fixed on each pin or do they vary...

    But for example you could create a timer such as IntervalTimer, that if several of the pins were multiples of a common frequency you could have one timer try to handle multiple ones. where you keep a counter for each one, to know if you should update the state of a pin on a pin on this interrupt or not...

    That is suppose you have 4 pins, that you can setup that you should change: 1 on every interrupt, 1 on every other, 1 on 3rd 1 on 4th...

    you then have the interrupt handler do some simple logic like:

    Code:
    void timer_int() {
        digitalInvertFast(pin0);  // Note function does not exist but should)
        static uint8_t cd1 = 2;
        if (!(--cd1) {
            digitalInvertFast(pin1);  // Note function does not exist but should)
            cd1 = 2;
        }
        static uint8_t cd3 = 3;
        if (!(--cd2) {
            digitalInvertFast(pin1);  // Note function does not exist but should)
            cd2 = 3;
        }
    ...
    }
    Again this would not be perfect as there is logic makes slight delays...
    But there are tricks. If all are on the same hardware GPIO port, you could setup masks
    for each cycle through. For example you could maybe create an array of 4 items here that
    has the InvertPins Mask for that port for each cycle through this list, so you increment, in this case
    with 4 easily wrap around, and then simply store that array item into the Invert register...

    But again this is easier for fixed things... But maybe could be done along the line Paul did with the
    OctoWS2811... using DMA... But this is probably not for the feint at heart

  8. #8
    Senior Member
    Join Date
    Mar 2016
    Posts
    133
    OK, fair enough. Though the high order bit here appears to be desire to not use PWM, though, I could be misunderstanding it. Would like to understand what the needs are.

    And, if 18 unique frequencies isn't enough, I would use 18 PWMs and add more via some sort of timer based scheme like you suggest. A lot depends on how much jitter the application can tolerate.

  9. #9
    Quote Originally Posted by KurtE View Post

    That is Nick would like a lot of square waves all at different frequencies. With PWM you can only get about 13 or so unique PWM frequencies. That is several of the PWM pins share using the same clock, so restricts how many you can do easily.

    Again I don't know how much of a range of different square waves you need, are they fixed on each pin or do they vary...
    I should have mentioned the square waves are audio pitches. I don't see why they is an issue sharing PWM frequencies as they are not the same as the audio frequency. The PWM is like a high frequency carrier for the audio. It should be possible to get many different audio frequencies all using the same PWM carrier frequency.
    (edit) I just realised that you were thinking of generating the square waves directly with the the PWM signal. I was thinking they would be generated similar to how you would make an arbitrary analog wave using many PWM pulses per audio wave.


    I am designing a polyphonic synthesizer in a micro-tonal tuning (just intonation) with 22 notes per octave. Most of the circuit will be analog. Using digital square waves as starting point for pitch accuracy. It will be true polyphony so I need all pitches available at the same time in parallel. It would be nice to minimise the number of Teensy boards required.

    There will be a separate pitch bend for every note (via analog ins).

    There will be one input per Teesny to shift every note on the synthesizer by the same chromatic number of semitones (for selecting the root pitch in semitones of the just intonation tuning system).

    Each note will have 2 square wave outputs one fixed and a 2nd modulating in phase relative to the 1st one via a 1 LFO input on each Teensy.

    My analog circuit also requires an additional signal per note that represents the pitch of that note as a DC voltage, I can do this with a rectangle wave of a high frequency with a pulse width that is proportional to the note frequency.
    Last edited by Nick-Inventor; 05-25-2020 at 10:35 PM.

Posting Permissions

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