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

Thread: Teensy 4.1 Interrupt basics to create a square wave signal

  1. #1
    Junior Member
    Join Date
    May 2021
    Posts
    10

    Teensy 4.1 Interrupt basics to create a square wave signal

    Hello everyone,

    I'm trying to get started with interrupt programming for a project in which I want to measure voltages and other things under real time conditions. First of all I wanted to try a very simple program, where the Teensy creates a square wave signal by toggling an output pin on every timer overflow. Therefore I used the following code:
    Code:
    #include <avr/io.h>
    #include <avr/interrupt.h>
    
    #define PWM_PIN 2
    
    volatile byte PWMState = LOW;
    
    void setup() {
      pinMode(PWM_PIN, OUTPUT);
    }
    
    ISR(TIMER0_OVF_vect)
    {
        if(PWMstate == LOW){
          PWMstate = HIGH;
        } else {
          PWMstate = LOW;  
            }
    }
    
    void loop() {
      digitalWrite(PWM_PIN, PWMstate);
    }
    Later I want to try a PWM signal, with adjustable pulse width and frequency, but first the square wave is already a problem.

    I'm not really sure, but I think I have to put the digitalWrite function into the ISR(){} or could the code above also work?
    The Arduino software throws an error at me, that it expects a constructor or destructor before "(" is used in ISR(TIMER0_OVF_vect). Did I do something wrong including the libraries or is something in the code missing?

    Am I on the right track with my approach?

    Thank you in advance

  2. #2
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    1,075

  3. #3
    Junior Member
    Join Date
    May 2021
    Posts
    10
    Thank you jonr. That helped

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,088
    Quote Originally Posted by MrZ80Dude View Post
    Later I want to try a PWM signal, with adjustable pulse width and frequency, but first the square wave is already a problem.
    You can also get adjustable frequency from the hardware PWM pins, using analogWriteFrequency() and analogWrite(). Details here:

    https://www.pjrc.com/teensy/td_pulse.html

Posting Permissions

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