How to invert 100khz PWM on Mega?

Hello
I came here because there was a problem while coding.
I saw several question, but I couldn't completely solve them.

#include <PWM.h>
int ABC = 8;
int BCD = 7;
int CDE = 3;
int DEF = 2;
int EFG = 12;
int FGH = 11;
int32_t Fre = 100000;

void setup()

{
  InitTimersSafe();
  Serial.begin(9600);

  SetPinFrequencySafe(ABC, Fre);
  SetPinFrequencySafe(BCD, Fre);
  SetPinFrequencySafe(CDE, Fre);
  SetPinFrequencySafe(DEF, Fre);
  SetPinFrequencySafe(EFG, Fre);
  SetPinFrequencySafe(FGH, Fre);

  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
}
void loop()

{
  pwmWrite(ABC, 127);
  pwmWrite(BCD, 127);
  pwmWrite(CDE, 127);
  pwmWrite(DEF, 127);
  pwmWrite(EFG, 127);
  pwmWrite(FGH, 127);

 delay(500);

}

In this code, I want to invert PWM in 12,11 pins.
but not working time delay or phase shift with high frequency.

Should I give up the PWM.h library?

Simplest way is to connect an external NOT gate or inverter.

Not sure what you mean by invert.
But try subtracting the number you want to use from 255

pwmWrite(ABC, 255 - 127);

Of course this is a square wave anyway so that is not going to do much for you.

I thought about it once.
However, I wanted to proceed without additional external elements.
(There are no space to add something)

Sorry for my poor explanation.
my mean is
image

Not sure if this will work...but what you want is a NOT operation. If any pin can be input/output both....say its pin 7 that has the main PWM and pin 11 needs the inverted version, you could write digitalWrite(11, !digitalRead(7))

What is the difference? And why would not what I suggested work?

Why do you want to actually do this?

Is this an X-Y problem?

X-Y problem

No, I didn't do it to tease you.
I've already tried what you suggested.
but same result
pwmWrite(ABC, 127);
pwmWrite(ABC, 255-127);

The reason why I want to invert the PWM signal is to create a sine waveform by adjusting duty using H Bridge.

Is this to drive stepper motors? If so then use a driver - these do the sine wave generation (current, not voltage) using PWM based on microstepping and current limit...

I'd start with the chip's datasheet to see if it's possible (I suspect it may be but you may need to compromise on which pins are used). If the chip can do it, program the timer(s) yourself, no need for a library.

I think I've tried it before, but it's night here, so I'll try it again tomorrow.

the 7,4 pins did not work properly except for pins 8, 3, 11, and 12.
(Maybe the timers are influenced by each other.)

I've never thought of a stepper motor.
The circuit has already been completed and must be solved only by coding. Thank you for your advice.

AFAIK the PWM is not the same for all pins. Check the UNO spec sheet. I'd still say you use an external driver to generate the required signals - these already have the H-bridge and everything implemented. All you need to do is provide pulses to the driver...

I'll open the datasheet and study more.

Would it not require two different clock sources which are synchronized to perfection to achieve an inverted PWM signal as wanted? Would it not be simpler to use a transistor for the inversion?

Yeah you're right.
Not all pins are the same. So I checked the mega spec, but I didn't understand it well because I was stupid.

Everyone is having a hard time because there's little information I wrote down.
I already designed the PCB board. There are no space to add something.
So I had to solve it only with coding.

Since everyone says it's better to add elements to a new circuit, I'll proceed like that.

I'll consider that, too. Thx

If you use a homemade H-bridge and apply a signal derived from a timer like that you will get a problem known as shoot through where for a brief time both top and bottom transistors are on, resulting in a short circuit of your power supply. Use a commercial H-bridge with only one direction input.

I experienced the problem you mentioned. I didn't know why my device was burnt out, but thanks to you, I found out.

This is solved using "dead-time" where for a brief period both MOSFETs are off. This is best implemented by drivers both for discrete or external H-bridges or as a complete driver cum H-bridge package.