Please check my code for errors, How do I generate Generate High Freq Square waves(at least 200KHz) with ArduinoMega2560 CTC Mode

Firstly, is there a way I could achieve 200KHz PWM signals with Fast PWM Mode?...I gues the max is 62KHz with Pins 4 and 13?..using Timer0.

So I tried using CTC mode which gives me the fredom to generate as much freq as I want, but in folowing the ArduinoMega2560 datasheet, I have tried several things but its not working.

I believe interrupt flag is optional?..Since I already instructed my counter TCNT2 to toggle when OCR2A and OCR2B values are reached.

Please I would appreciate your input as I aim to drive a halfbridge MOSFET driver with the signals generated.

void setup() {
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
TCCR2A |=B01010010 ; // CTC mode activated, Toggle OC2A and Toggle OC2B
TCCR2B |=B00000111;//Prescaller set
TCNT2=0;//Start Counting ffrom 0
OCR2A=127;//Set TOP value for pin10
OCR2B=127;//Set TOP value for pin9

}
//sei(); // enable interrupts globally
//TIMSK2 = B0000010; // interrupt when timer2 == OCR2A
void loop() {

}

Do you need square wave or rectangular wave?

Don't use bitwise OR, set all bits in the register explicitly. You don't know what was previously in it.

What frequency did you want? You say "High Frequency" but you set the Clock Select bits for a prescale of 1024!

// Sketch for Arduino MEGA 2560
void setup()
{
  pinMode(9, OUTPUT);  // OC2B
  // pinMode(10, OUTPUT);  // OC2A (Can't be used with WGM=2)

  // Clear the control registers to stop the timer
  TCCR2A = 0;
  TCCR2B = 0;
  TIMSK2 = 0; // Disable all Timer2 interrupts
  TCNT2 = 0; //Start Counting ffrom 0

  // Set Waveform Generation Mode 2 (CTC with TOP in OCR1A)
  TCCR2A |= _BV(WGM21);

  // Enable toggle OC2B on compare match
  TCCR2A |= _BV(COM2B0);

  // Set TOP for 128 cycles
  // 16 MHz / 128 = 125 kHz CTC
  // The toggle on OC2B is 62.5 kHz
  OCR2A = 127;

  // Set OC2B to toggle at count 0
  OCR2B = 0;

  // Set the Clock Select bits to start the timer with prescale = 1
  TCCR2B |= _BV(CS20);
}

void loop() {}

I need a square wave..

Thank you...I found out this was the issue that has kept me for 4days on this project...

Thank you. Also, I need the waves to be complementary and a deadtime between them, do you think that is posible?

I already was able to get a complementary square wave by setting
TCNT2=80:
OCR2A=39:
OCR2B=0;

However, it seems CTC works on 50% Duty Cycle...

My aim actually was to get a variable PWM 200KHz Complementary Signals with Deadtime in between.

However Fast PWM only gave max 62KHz, so I had to depend on CTC mode...

I would appreciate further insight if anyone could help.

Thank you.

Thank you for your reply...

I was able to achieve a 200KHz Complementary Square waves at 50% Duty Cycle CTC Mode.

However, my final target is a variable PWM 200KHz Complementary signals with a deadtime between signals; to ensure ZVS.

The timer alone can't insert deadtime.

Is there a way I could go around this?..

You could generate the signal with code, in the main loop, but the processor would not be able to do much, or anything else. Or, have the timer generate half of the complementary pair and interrupt the processor to generate the other half, with dead time.

I left the office for now, I would try this again on Tuesday..I just hope I get done with this so face other issues of feedback and control...

Thank you thus far for your replies..

I am still open to any suggestion that could help me with my project...

Maybe.

How much dead time?

Timer2 has no way to set TOP and also use OC2A for PWM. I think the way to do it is to use Timer 1, 3, 4, or 5 and WGM 8 or 10 (Phase Correct PWM with TOP in ICR1).

16 MHz / 200 kHz / 2 = 40, so TOP is 39.

To get complimentary PWM with dead time, set one channel to COM 0b10 (Turn off when counting up, on when counting down) and the other to COM 0b11 (Turn on when counting up and off when counting down). Make the first compare a lower value. When counting up, way the first channel turns off first, then the second channel turns on. When counting down, the second channel turns off, then the first channel turns on.

An FYI: Motor Control Pulse Width Modulator (MCPWM) - ESP32 - — ESP-IDF Programming Guide latest documentation (espressif.com)

Dear John,

Thank you for this reply.

I just need a sufficient deadtime to allow ZVS.

So Phase correct mode may be the way?..I will check on tuesday; if I can achieve 200KHz with Phase correct Mode and get what I want, I would be glad.

I would be at the office next week tuesday to attempt the suggestions.

Thank you.

Is that something you can calculate or measure?

Since I am switching at 200KHz, Period is 5uS. a dead time of 0.5us before rise and 0.5uS after fall is sufficient...

So 8 clock ticks (0.5 uS) of deadtime. Set OCR1A to 16 and OCR1B to 24.

OC1A: ----____________--------____________--------______
OC1B: ______--------____________--------____________----

Ok..I will check that...

Thank you or your replies.. :))

Dear John,,

Thank you.

This works.

The new issue I have not is how to smoothen my signals.

I have switching spikes of 2V and this causes a false triggering of my HS and LS MOSFETS.

Is there a way I could smoothen my signals by programming?..I dont want to include any hardware filters.