// https://wokwi.com/projects/391675696567211009
// Modified from https://wokwi.com/projects/354767518622045185
// and
// https://forum.arduino.cc/t/complementary-pwm-signals-at-50-khz-with-dead-time/1081473/6
// Code by John Wasser, modified for Wokwi & Scope by DaveX
// Generating Two 180° Out of Phase adjustable duty cycle
// Waves on Timer1 of an 
// Arduino UNO (Pins 9 and 10)
// Written January 23rd, 2023 by John Wasser. modified 2024-03-07
// For register details see:
// https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf

const unsigned TOP = 8000;    //f = 16000000/(2*N*TOP), TOP=16e6/(2*N*f) 
int delta = 10;

void setup()
{
  Serial.begin(115200);
  // Stop Timer/Counter1
  TCCR1A = 0;  // Timer/Counter1 Control Register A
  TCCR1B = 0;  // Timer/Counter1 Control Register B
  TIMSK1 = 0;  // Timer/Counter1 Interrupt Mask Register
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);

  // Set Timer/Counter1 to Waveform Generation Mode 8:
  // Phase and Frequency correct PWM with TOP set by ICR1
  TCCR1B |= _BV(WGM13);                 // WGM=8
  TCCR1A |= _BV(COM1A1);                // Normal PWM on Pin 9
  TCCR1A |= _BV(COM1B1) | _BV(COM1B0);  // Inverted PWM on Pin 10

  ICR1 = TOP;

  // Start timer by setting the clock-prescaler bits to non-zero
  TCCR1B |= 0b001 << CS10;   // index into N={0,1,8,64,256,1024}
}

void loop() 
{
  report();
  adjust();
}

void report(void){
  const uint32_t interval = 1000;
  static uint32_t last = -interval;
  uint32_t now = millis();
  if(now - last >= interval){
    last = now;
    Serial.print(TCNT1);
    Serial.print(" ");
  }
}

void adjust(void){
  // Adjust the duty cycles
  const uint32_t interval = 50;
  static uint32_t last = -interval;
  uint32_t now = millis();
  static int lastPot = -1;
  if(now - last >= interval){
    last = now;
    uint16_t pot = analogRead(A0);
    if(pot != lastPot){
      lastPot = pot;
      OCR1A = (ICR1*long(pot))/1023;
      OCR1B = ICR1 - OCR1A;
      Serial.print("pot:");
      Serial.print(pot);
      Serial.print(", ");
      Serial.print("OCR1A:");
      Serial.print(OCR1A);
      Serial.print(", ");
      Serial.print("Duty%:");
      Serial.print(OCR1A*100.0/ICR1);
      Serial.print(", ");
    }
  }
}
Loading chip...chip-scope