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
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
}
}
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?
No longer
AWOL:
No longer
Thanks.
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:
Or a faster version (less propagation delay), 74AC04
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..
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..
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....
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..
Lost in TimerWonderLand...
Im not understanding why there is three WGMs
I have this so far..
TCCR2A = _BV(COM2A1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(COM2A1) | _BV(COM2A0) |_BV(WGM22);
delay(10);
analogWrite(AC1,127); //pin9
analogWrite(AC2,127); //pin10
Don't bother with the analogWrite after all this. They should be running.
If you don't call analogWrite() be sure to set pinMode(pin,OUTPUT) for the two output pins.
yeah i have pinMode setup for both pins 9 and 10 along with the analogWrites..
I found a great register summary on page 401 of the 2560 atmel datasheet DeltaG linked to..
It shows the WGM stuff more or less..
This is a great help..I will see if i can put it all together from all this help.. Thankyou all..