Teensy 3.5 Weird Timer Issues ( TeensyDelay )

Status
Not open for further replies.

vimassive

Member
I download TeensyDelay reference here https://www.pjrc.com/teensy/td_timing_IntervalTimer.html
Download source https://github.com/luni64/TeensyDelay

Arduino 1.8.9

Board: Teensy 3.5

GOAL:
When PIN 11 detects falling edge, set Pin 6 HIGH at some time in the future. In this case 10 us.
After PIN 6 is set HIGH, 500us later Pin 6 is set LOW.

Basically a one shot pulse.

If I add a small delay 10 - 100 ms delay in the main loop of the not working code below, it kind of works but the signal is not smooth like I want it to be.


I want to trigger timers in the main loop. Why is this code behaving this way? Can someone please explain?
What do I need to do to correct this weird behavior?


#### NOT WORKING ####

#include "TeensyDelay.h"

// Trigger pin
int trigger = 11;

volatile int pulse = 0;


// callback function for channel 0
void callback_A()
{
Serial1.println("Callback A");
digitalWriteFast(6,HIGH);
TeensyDelay::trigger(500,4);
}

// callback function for channel 1
void callback_B()
{
pulse = 0;
Serial1.println("Callback B");
digitalWriteFast(6,LOW);
}

void event()
{
pulse = 1;
}

void setup()
{
pinMode(6, OUTPUT);
pinMode(trigger, INPUT_PULLUP);
Serial1.begin(115200,SERIAL_8N1);
Serial1.println("Timer Debug: ");
TeensyDelay::begin();
TeensyDelay::addDelayChannel(callback_A,0); //setup channel 0
TeensyDelay::addDelayChannel(callback_B,4); //setup channel 1
attachInterrupt(digitalPinToInterrupt(trigger), event, FALLING);
}

void loop()
{
if(pulse)
{
//Serial1.println("Main Loop: ");
TeensyDelay::trigger(10,0);
}
//delay(100);
}


###### Working Code ######

#include "TeensyDelay.h"

// Trigger pin
int trigger = 11;


// callback function for channel 0
void callback_A()
{
Serial1.println("Callback A");
digitalWriteFast(6,HIGH);
TeensyDelay::trigger(500,4);
}

// callback function for channel 1
void callback_B()
{
Serial1.println("Callback B");
digitalWriteFast(6,LOW);
}

void event()
{
TeensyDelay::trigger(10,0);
}

void setup()
{
pinMode(6, OUTPUT);
pinMode(trigger, INPUT_PULLUP);
Serial1.begin(115200,SERIAL_8N1);
Serial1.println("Timer Debug: ");
TeensyDelay::begin();
TeensyDelay::addDelayChannel(callback_A,0); //setup channel 0
TeensyDelay::addDelayChannel(callback_B,4); //setup channel 1
attachInterrupt(digitalPinToInterrupt(trigger), event, FALLING);
}

void loop()
{

}


Scope working

working.png

Scope not working

not_working.png
 
In your callbackA you trigger channel 4? You only assigned two channels. There is an example showing how to handle more than one channel.
Btw. You can directly trigger the channel from your pin interrupt handler. No need to do that via your pulse flag
 
Sorry, forget #2, I didn't notice that you actually did reserve channel 0 and channel 4.
Can't test it right now but it seems to be the following problem:

When pulse is true you trigger channel 0 with a delay of 10µs but don't set pulse to false.
-> Next time you enter loop, pulse is still true, thus you retrigger the channel.
-> The callback is never called because you permanently retrigger the channel
It works with the delay since that will wait long enough to make sure that the second callback is called (which resets pulse)

Fix: In loop set pulse to false directly before or after triggering the channel. Or more elegant: trigger the channel from your pin interrupt handler (event())
 
@luni Thanks for your help.

I made the change and it now works as intended.

void loop()
{
if(pulse)
{
pulse = 0;
//Serial1.println("Main Loop: ");
TeensyDelay::trigger(10,0);
}
//delay(100);
}


@luni I know depending on which timer is selected up to a maximum of 8 channels can be configured per Timer.

Do you have any plans to design this timer to use multiple Timers at the same time?

Thanks
 
Status
Not open for further replies.
Back
Top