Help getting Inputs to read faster

Background: I working on making a 24-30V solid State tesla coil with the most of the control and all of teh information gathering handled by an AVR be it a chip wit just C language or chip with arduino tidbits mixed in.

Right now though I’m have the an issue with getting inputs register/react to the high end frequencies I’m aiming for namely 800K tops, and by registering/reacting (as well as my basic goal) is to use a digital input to “capture” the rising and falling edge of the “resonant Frequency” wave form coming from a current transformer, then cause 1 output pin to go high while another goes Low, nad be able to “inject” or “add” a dead zone/time to the waveforms to avoid hte mosfets/igbts from both turning on at the same time and reducing the magnetic flux going into the secondary.

My original test code was:

void setup(){
pinMode(2,INPUT);
pinMode(4,OUTPUT);
pinMode(7,OUTPUT);
}

void loop(){
int i;

if(digitalRead(2)==1){
digitalWrite(7,LOW);  //make sure FET 2 is off
for(i=0;i<x;i++);        //clock cycle delay since micro delay would be and take too long, the x is just for a random number to adjust how long of a delay
digitalWrite(4,HIGH); //turn on FET 1 after delay to ensure FET 2 is off
}
else{
digitalWrite(4,LOW);  //make sure FET 1 is off
for(i=0;i<x;i++);        //clock cycle delay since micro delay would be and take too long, the x is just for a random number to adjust how long of a delay
digitalWrite(7,HIGH); //turn on FET 1 after delay to ensure FET 2 is off
}
}

this code was tested with 1KHz square and I was happy thinking it would work at higher frequencies, then decided to actually test that heory after calculating that without a topload my coil would actually need somewhere between 700KHz-1MHz, and low and behold using arduino lib’s the output maxed out about 25KHz with the input set to 800KHz and the duty cycle was off as well (yes teh duty cycle is rather important), then I remembered using timer registers directly for a differnet project to let me get more then 35KHz ( was actually able to get to 1MHz) by usingthe timer registers directly, so then I came to this code:

void setup(){
pinMode(2,INPUT);
pinMode(4,OUTPUT);
pinMode(7,OUTPUT);
}

void loop(){
int i;

if(digitalRead(2)==1){
PORTD = 0x10; //turn on FET 1 and Turn off FET 2
}
else{
PORTD = 0x80; //turn on FET 2 and turn off FET 1
}
}

At this point since I’m just trying to get teh code to read the input and switch the outputs fast enough I removed the For delays just to see how fast I/Os could handle, sadly I was only able to get to 38KHz but at least it was 50% duty cycle now.

Now I can’t seem to figure out how to get the If statement to work properly with directly reading the Input register, I have tried to use :

if ((PIND = B00000100) == 1) with an else statment
if (PIND = B00000100) with an else statement
if (PIND & B00000100) with an else statement and with another if statement of if (PIND & B00000000)

as well as having it check against the outputs to make sure it wasnt reading the register as B00010000 or B10000000 or other combinations and I’ve also tried using the serial print commands to help debug except they always return as 1, 111, 1111, or 101 depending on which if statement above was used, also I’ve tried to just get variable = PIND & B00000100 and have that pooled and had the variable used in the if statement which is wher I’m at now with this code:

void setup(){
  int i;
  pinMode(2, INPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  for(i=3;i<11;i++){
    pinMode(i,OUTPUT);
    digitalWrite(i,LOW);
  }
  //Serial.begin(9600);
}

int pin ;
void loop(){ 
  pin = PIND & B00000100;
  if(pin != 0x04){
    PORTB=B00010000; //ch1 on oscope
  }
  else{
    PORTB=B00100000; //CH2 on oscope
  } 
  //Serial.println(pin,BIN);
}

it seems like It will work upto a point but as I’m typing this i have my func gen outputing 700KHz and the outputs are showing 75KHz, theres some other coding I’ve tried (just can’t remember it) but all it did was cause the two outputs to "oscilate about 850-900KHz at non 50% duty cycles.

So If anyone has any ideas or suggestions about how to pool the PIND register in an if statement or some other option that will let me use the uno as 1 in → 1 output and 1 inverted output upto 800KHz, I’d be very thankful.

Also sorry for long winded wall of text and code.

You could maybe save a few cycles by putting a "while(1)" loop inside "loop()", and place all your code inside that.

The processor executes 17 clock cycles in 900KHz time. There is no way you can get 50% duty cycle at that frequency.

At 50% duty cycle, your frequency choices are... 1MHz, 888888, 800000, 727272, 666666, 615384, ...

In order to achieve that, you are going to have to use hardware driven PWM (which will require extra hardware to get an inverted output) or you are going to have to shut off timer 0 and learn some assembly.

You should consider using an ATtiny85. I believe it is designed for just this kind of application.