# Phase Shifting and attachInterrupt

I found the below code some where on the forum. It does a nice job of 180 degree phase shift on a 40Khz on pin9 and 10 on Uno using a pot on A0.

Question is how can I use an attachInterrupt function from an external square wave input to get an intermittent output of this code? If this is possible at all!

Thanks.

``````//http://www.rei-labs.net/arduino-generating-two-fast-phase-offset-signals/
// code by dejko1, rei-labs.net

void setup() {
PORTB=0x00;
DDRB=0x06;

ICR1=199; // to vary frequency was 199
OCR1A=0;
OCR1B=100;
TCNT1=0;
TCCR1A=0x50;
TCCR1B=0x19;
}

int j=0;
int a;

void loop() {
delay(100);

j = (a/3);      // adjust this as frequency goes down was (a/4)
if(j>199) j=199; // must adjust these with frequecny

//update ocr registers with the value
TCCR1B=0x18;
if((PINB&0x02)!=00) TCCR1C=0x80;
if((PINB&0x04)!=00) TCCR1C=0x40;
TCNT1=0;
OCR1B = j;
if(j==0) TCCR1C=0x40;
TCCR1B=0x19;
}
``````

Question is how can I use an attachInterrupt function from an external square wave input to get an intermittent output of this code?

I don't understand the question.

You can, of course, use attachInterrupt() to define an interrupt handler to be called when an external interrupt happens.

You can do what you want in the interrupt handler. It sounds like you want to set a flag indicating that the interrupt happened.

You can check that flag to determine whether to diddle with the pins, etc. But, it isn't clear what the external interrupt is going to come from, why you want to diddle with the pins, or not, based on that external source, or what will disable (or enable) the action if the action is enabled (or disabled) by the external interrupt.

What is the frequency of the external square wave ?

Thanks for the response!

The input is coming from a separate Uno with frequency of 10Khz. What I am trying to do is get a synchronized out put of the initial code, to turn on at the rising edge and turn of at the falling edge of the 10Khz input signal.

My problem is I cant get me both in sync on my scope!

Picture show the input signal in purple, also there is a lot of jitter when I modified the code.

Here is a modified of the code I tried:

``````//http://www.rei-labs.net/arduino-generating-two-fast-phase-offset-signals/
// code by dejko1, rei-labs.net

int frequency = 199;

int buttonPin = 2;
int buttonState = 0;
void setup() {

PORTB=0x00;
DDRB=0x06;

ICR1=frequency; // to vary frequency was 199
OCR1A=0;
OCR1B=100;
TCNT1=0;
TCCR1A=0x50;
TCCR1B=0x19;

attachInterrupt(digitalPinToInterrupt(buttonPin), stateChange, CHANGE);
}

void loop() {

}

void stateChange()
{

if (buttonState == HIGH)
{
int j=0;
int i=0;
int a;
int b;

delay(50);
i = (b/2);
j = (a/2.5);      // adjust this as frequency goes down was (a/3)
if(j>frequency) j=frequency; // must adjust these with frequecny

//update ocr registers with the value
TCCR1B=0x18;
if((PINB&0x02)!=00) TCCR1C=0x80;
if((PINB&0x04)!=00) TCCR1C=0x40;
TCNT1=0;
OCR1B = j;
ICR1 = i;
if(j==0) TCCR1C=0x40;
TCCR1B=0x19;

}
}
``````

Here is the image

You have a delay call in your ISR. delay relies on interrupts that are turned off inside your ISR. That is liable to lock things up. At the very least it won't work properly.

Any suggestions?

dannyg21:
Any suggestions?

Refactor the code to get the delay out of the ISR. Let the ISR set a flag and let something in the loop function react to it.

I'm unclear from the scope image if you are using the 10khz square wave as interrupt input or are you using some sort of button to try and trigger the changes.

You will have difficulty getting response to the rising and falling edge of a 10khz square wave input with the two analog reads to change frequency and offset as they take about 100us each unless you change the ADC prescaler.

What are you doing that requires you to change the offset square waves at 10khz?

Trigger is from the 10KHz input. Im trying to make a pwm pahse shift controller so i can play around with few coils i have.

Delta_G, what do u mean by ISR?

Interrupt Service Routine.

In your case that's the stateChange function.

Oh, the "delay(50)", hmmm

Thank 4 the response. That makes sense, let me play around with that and see what i can get.