Ok, so I have been reading quite a bit from your references and have now found out how to make:
(1) - a jitter-free square waveform using hardware timers (this is the only signal that seems to be truly "jitter-free" that I have been able to make) - adapted from gammon
(Now, learn how to use Code tags please!)
** **/////////////////////////////////////////////////////////////////////////////////////////////////////////////////** **
#include <avr/sleep.h>
#include <util/delay.h>
double inputDel = 80; //us
double inputDur = 40; //us
ISR (TIMER1_COMPA_vect)
{
_delay_us(inputDel); // delay before rise time
PORTB |= B00010000;
_delay_us(inputDur); // duration of high time
PORTB = B00000000;
}
void setup ()
{
DDRB |= B11111111; // digital pins -,-,13,12,11,10,9,8 as output
DDRD = B00000000; // digital pins 7,6,5,4,3,2,1,0 as input
// stop timer 0
TCCR0A = 0;
TCCR0B = 0;
// stop timer 1
TCCR1A = 0;
TCCR1B = 0;
TCCR1B = bit (WGM12) | // CTC
bit (CS10); // prescaler of 1
// 1e9 / 22050 / 62.5 = 725.6 - round down, then subtract 1
OCR1A = 800;
TIMSK1 = bit (OCIE1A); // interrupt on compare A
set_sleep_mode (SLEEP_MODE_IDLE);
} // end of setup
void loop ()
{
sleep_mode ();
} // end of loop
** **/////////////////////////////////////////////////////////////////////////////////////////////////////////////////** **
(2) - a CLOSE to jitter-free signal using external interrupts. This is extremely close to what I need, but I really need the "jitter free" signal as in the one generated by hardware timers in the previous example.
[b]/////////////////////////////////////////////////////////////////////////////////////////////////////////////////[/b]
#include <util/delay.h>
#include <avr/sleep.h>
const byte interruptPin = 2;
const byte outputPin = 12;
double testDel = 10;
void trigger () {
/////////////////////////////////////////////////////////////////////////////
// PORTB |= B00010000; // show when signal is recieved
// PORTB &= B11101111;
/////////////////////////////////////////////////////////////////////////////
_delay_us(testDel);
PORTB |= B00010000; // pin12 high
_delay_us(testDel);
PORTB = B00000000;
}
void setup() {
DDRB |= B11111111; // digital pins -,-,13,12,11,10,9,8 as output
DDRD = B00000000; // digital pins 7,6,5,4,3,2,1,0 as input
attachInterrupt(digitalPinToInterrupt(interruptPin),trigger,RISING);
set_sleep_mode (SLEEP_MODE_IDLE);
}
void loop() {
sleep_mode ();
}
[b]/////////////////////////////////////////////////////////////////////////////////////////////////////////////////[/b]
I am reaching out for your expertise for help in getting the rest of the way.
I need to either:
(1) - adapt the hardware timer version to initiate on an external trigger (ie. pin 2 change) -- I am relatively new to hardware timers, so I'm not sure how to do this one.
-or-
(2) - try to remove the residual jitter from the interrupt version (I don't know where it's coming from now).
Thanks,
mundu