inverted PWM

I need two square waves pwm outputs 180 deg out of phase..
can this be done??
I am using mega2560..i can set analogWrite to several pins but i dont see a way to invert one pin output..any ideas?? and thankyou

Add a transistor to the output of one PWM:

i dont see a way to invert one pin output..any ideas??

Feed the PWM output to a digital input pin, read that pin and output the inverted version to another digital pin ? Not sure about the overhead of doing this.

I dont have 'time' to read a pin, then invert that pin.. i am hoping for a pwm internal
solution if there is one..
thanks

I would say high, and possibly not workable. Maybe workable at 490 Hz, sure to be a lot of jitter on the inverted signal tho due to code that runs in main() behind the scenes, code for loop(), code for digitalRead and digitalWrite (can be reduced with direct port manipulation), code for millis/micros interrupts, and code for the rest of the sketch.

Discrete hardware would be much better than all that.
Almost better off with blink without delay and just driving two outputs there. This should be pretty close:

unsigned long currentMicros;
unsigned long highStartTime;
unsigned long highDuration = 8; // = 1/255 of 490.196 Hz
unsigned long totalPeriod = 2040; // 1/2040uS = 490.196 hz
unsigned long previousMicros;
byte highOutput =1;
void setup(){
pinMode (2, OUTPUT);
digitalWrite (2, LOW);
pinMod (3, OUTPUT);
digitalWrite (3, HIGH);
highStartTime = micros();
previousMicros = highStartTime;
}
void loop(){
currentMicros = micros();
if ( (highOutput ==1) && ( (currentMicros - highStartTime) >= highDuration) ){
PIND = PIND | 0b00001100; // flip D2 & D3 by writing to Input register
highOutput = 0;
}
if ((highOutput == 0) && ( (currentMicros - previousMicros) >= totalPeriod) ) {
PIND = PIND | 0b00001100; // flip D2 & D3 by writing to Input register
highOutput = 1;
highStartTime = currentMicros; // set up for next transition
previousMicros = currentMicros + totalPeriod; // set up for next transition
}
}

Twins?

http://forum.arduino.cc/index.php?topic=373780.0

Yeah my problem is i cant do the inversion in the mainloop..i am using the two pwms for that reason, so they run continuous and never fluctuate.. i am using them as two AC inputs to a full wave bridge..the bridge then provides V- to a LM358 op amp..so i generate a negative voltage supply for the op amp..thats why i need 180 deg out of phase..
thanks..

OldSteve:
Twins?

http://forum.arduino.cc/index.php?topic=373780.0

No longer

AWOL:
No longer

Thanks. :slight_smile:

If you don't want to use a transistor, maybe a [u]7404 Inverter[/u]?

Or a modern part, less power hungry version, or one of the 1-gate variants:
http://www.digikey.com/product-search/en?vendor=0&keywords=74hc04
Or a faster version (less propagation delay), 74AC04
http://www.digikey.com/product-search/en?vendor=0&keywords=74ac04&stock=1

i was hoping for a software 'fix'...i have run out of board space and already am using 1206 smds!!
it works with just two pwms in sync but would be better of course if one was out of phase with the other..
thanks for all the great ideas from all!!

If you are driving a power bridge, like for a motor, the simple inversion like the textbook says is dead wrong. Use a proper motor driver chip with proper MOSFET drivers.

But you seem to be using this as a voltage inverter to derive an opamp supply rail. How many milliamps? A single ended output should be all you need for that. Or use a dedicated chip like ICL7660.

it must be low low current requirement…it only has to supply one LM358…and both op amps have hi R resistors all around…i would think several ma of negV supply is sufficient…thats why i chose to use 2 pwm output pins… they are rated at 10ma each… if i could get 180 phase on one, it would be all good…
thanks everyone…
i tried to attach schematic…not sure it made it…

You can do this directly from the timer hardware. Look at section 15, 16, and 18 in the Holy Datasheet. For timer 1 (the other will do similar) you can set the COM1A1 - COM1A0 bits to 1 and 0 so it clears the OC1A pin on compare match and sets it at BOTTOM. Then set COM1B1 - COM1B0 to 1 and 1 to get the opposite action on the OC1B pin. If you set both compare registers to the same value then the pins toggle at the same time but in opposite directions. That's for fast PWM mode (Table 16-2). Basically the same thing in phase correct and frequency correct modes. (Table 16-3). See sections 15 and 18 for the same stuff with timers 0 or 2.

wow.. thankyou .. i havent worked with the mega2560 pwms much yet..its pretty complicated to me..
i will try and see what u have suggested.. thanks again..i'll get back to this thread know what i findout..

backwoodsjack:
wow.. thankyou .. i havent worked with the mega2560 pwms much yet..its pretty complicated to me..
i will try and see what u have suggested.. thanks again..i'll get back to this thread know what i findout..

Oh snap, I linked you to the datasheet for the 328P like the UNO. But you'll find similar in the datasheet for the 2560.

Hi,

The circuit you provided was nowhere high enough in definition, can you resend it at higher res please.
Can you tell us what your application is please.
What is input to the arduino, or is the arduino just a fixed generator of two out of phase square waves?

Thanks, Tom.... :slight_smile:

Hi Tom… sorry for the poor resolution… heres a higher export-- from cadsoft Eagle–

hope it is better–

the solarShield is simply an interface board between the mega2560 with 3.2" LCD and solar panels…
current sense and battery voltage are detected by the LM358…thats what the pwms are for-- charge pumping up(or down?) a negative power rail for the LM358…

I am having trouble locating the nomenclature for the bits i need to set in the timer registers for pins 9 and 10…it is timer 2 i know…

my circuit does provide several volts negative even with both pins ‘in sync’… which appears to be headroom to make things work, however, i want to get it right if i can… thanks for all the terrific help…
this forum is powerful… thankyou all…
if the circuit is still not usable, i can increase resolution more, but it crashes at some point… not sure what is going on… i am running ubuntu14.04 on an old T43 laptop…

backwoodsjack:
I am having trouble locating the nomenclature for the bits i need to set in the timer registers for pins 9 and 10..it is timer 2 i know..

Do you have the datasheet for the 2560? They're all listed there with what they do in the different modes.

With Timer 2 you're interested in section 20. Specifically tables 20-3 and 20-4.