Inverted Square Wave Help

Please HELP!

Does any one know how to invert a PWM signal comming out of pin 9 on to pin 10?

Below is what I have so far messing around in the .ino and the attached libraries!

/*

 Mimics the fade example but with an extra parameter for frequency. It should dim but with a flicker 
 because the frequency has been set low enough for the human eye to detect. This flicker is easiest to see when 
 the LED is moving with respect to the eye and when it is between about 20% - 60% brighness. The library 
 allows for a frequency range from 1Hz - 2MHz on 16 bit timers and 31Hz - 2 MHz on 8 bit timers. When 
 SetPinFrequency()/SetPinFrequencySafe() is called, a bool is returned which can be tested to verify the 
 frequency was actually changed.
 
 This example runs on mega and uno.
 */

#include <PWM.h>

//use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
int led = 9;   // pin 9 out put
int led2 = 10; //pin 10 output
int32_t frequency = 10000; //frequency (in Hz)

void setup()
{
  //initialize all timers except for 0, to save time keeping functions
  InitTimersSafe(); 

  //sets the frequency for the specified pin
  bool success = SetPinFrequencySafe(led, frequency);
  
  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);    
  }
}

void loop()
{
  //use this functions instead of analogWrite on 'initialized' pins
  pwmWrite(led, 127);
  pwmWriteInv(led2, 127); 
 
}

PWM.rar (10.7 KB)

I don't know that library (where did you get it?) but it looks as if it implements a function pwmWriteInv() and comments in the header imply it does what you want, and I see your sketch uses it. Is it a standard function? What does it actually do?

I forgot where I got it from but i tested, it generates a single square wave up to 2Mhz! I believe only for pin 9 and 10. Not sure though.

Question here is though, i am trying to invert the original and have both of the square wave on pin 9 and the inverted on 10.

Use PINB to read pin 9, invert, and use PORTB to control pin 10.
Look up Arduino port manipulation.

Thanks for the response, let me see if i can figure it out!

At 2MHz, port manipulation will not work.
Add a simple transistor to invert the PWM output, or a 74CH04 gate,
or a 74F04 gate for even smaller difference in edge times - <2nS into a 50pF, 500ohm load.

At 2MHz, port manipulation will not work

Your right of course. I was thinking much lower.

dannyg21: i am trying to invert the original and have both of the square wave on pin 9 and the inverted on 10.

Are you trying to do the inverted output on a pin that the library supports for that? If so, if the library is implemented correctly it ought to be doing what you asked. Presumably it isn't doing it though, since you're here asking for help. What's it actually doing?

CrossRoads: At 2MHz, port manipulation will not work.

I don't think this code is actually using 2MHz - it appears to be using 10000 Hz.

CrossRoads:
At 2MHz, port manipulation will not work.
Add a simple transistor to invert the PWM output, or a 74CH04 gate,
or a 74F04 gate for even smaller difference in edge times - <2nS into a 50pF, 500ohm load.

I would think that you could generate a PWM signal on one pin, read it, then use two other pins to output the two desired signals.

If you can interrupt on the change of a PWM signal, it becomes almost trivial.

Set pin 9, 10 and 11 to OUTPUT
Set PB1 to 1, set PB2 to 0
Generate PWM signal on pin 11 (PB3)
attachinterrupt to pin 11, CHANGE

Interrupt Routine:
XOR PORTB with 0x06

If you can’t interrupt on a PWM signal, feed Pin 11 back to another pin and attach an interrupt to that one.

With this method, the two output signals will be slightly delayed from the original, but if that doesn’t matter, the only real cost is the use of one or two extra pins in use.

The 74CH04 rout sound the way for me at this point probably. I tried it with p-channel and N- Channel Fet configuration seem to work for lower frequencies, but as the frequency increases delay eats up the inverted square.

You guys rock!

Hardware rules!