We want the duty cycle of the pwm out to be proportional to the delay in timing of both square signals, if its leading it will output on one ocr pin if it is lagging, it goes to the other, based on which value comes first. Also we have to wait for the program to change both values of the signals, PORTA and PORTB, to compare the delay, which i think its not doing correctly. for testing purposes we're inputting 1 hz square waves so we can see the output, but for the real deal it should be 40khz square waves, but its not working, and I am also not great with arduino (my first time) I am not sure why our code isn't working properly. Is there another way to do this or what should we try and change? Thanks!
#include <avr/io.h>
#include <avr/interrupt.h>
#define CLK 16000000UL
bool t1_changed = false;
bool t2_changed = false;
void setup() {
// Serial.begin(115200);
noInterrupts();
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);
TCCR2B = _BV(CS22);
OCR2A = 0;
OCR2B = 0;
TCNT2 = 0;
//TIMSK2 |= (1 << TOIE2);
interrupts();
attachInterrupt(0, t1_ISR, RISING); //enable interrupt service routines on pins 2 and 3
attachInterrupt(1, t2_ISR, RISING);
}
volatile unsigned int t1;
volatile unsigned int t2;
//volatile unsigned int oldt1, oldt2;
void loop() {
// noInterrupts();
//noInterrupts();
//t1_changed = false;
//t2_changed = false;
PORTC=PORTB-PORTA;
if(SREG & 4)
{
OCR2A = (PORTB ^ PORTA); //duty cycle 50%
OCR2B = 0;
}
else if(PORTA == PORTB)
{
OCR2A = 0;
OCR2B = 0;
}
else
{
OCR2A = 0;
OCR2B = (PORTA ^ PORTB);
//OCR2A = 0;
}
cli();
// t1_changed=true;
//t2_changed=true;
//interrupts();
}
void t1_ISR()
{
//Serial.println("ISR");
PORTA = TCNT2;
//t1_changed = true;
Serial.println("PORTA is:");
Serial.println(PORTA);
//oldt1=t1;
}
void t2_ISR()
{
//Serial.println("ISR2");
PORTB = TCNT2;
// t2_changed = true;
Serial.println("PORTB is:");
Serial.println(PORTB);
// oldt2=t2;
}