Inverting Issue

I was wondering if any one can chime in regarding an inverting issue.

I have the below massacred code outputting PWM 10khz on pin 9 and I was trying to invert this on pin 10 with no success.

Any help would help :slight_smile:
Thanks.

#include <PWM.h>

//use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
int outA = 9;                // the pin that the LED is attached to
int outB = 10;
int32_t frequency = 10000; //frequency (in Hz) MAX 2MHZ @ 255/1.6 DUTY CYCLE
int dutyCycle = 255/2;      //Duty cycle (255/1.1 to 255/9) is sufficient 10%/90% **AS FREQUENCY INCREASES DUTY CYCLE DECREASES

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

  //sets the frequency for the specified pin
  bool success = SetPinFrequencySafe(outA, 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(outA, dutyCycle);
  

  pwmWriteInv(outB, dutyCycle);

    
}

Here are the attachments.

PWM.h (4.84 KB)

ATimerDefs.cpp (10.3 KB)

ATimerDefs.h (7.67 KB)

BTimerDefs.cpp (10.3 KB)

BTimerDefs.h (3.85 KB)

Hi,

analogWrite(outA,dutyCycle);
analogWrite(outB,(255-dutyCycle);

outB is the inverted copy of outA.
Tom..... :slight_smile:

Thanks for the response, but its not as simple as that :slight_smile:

Why not add an external device? 74F04 will invert it with just a new nS of propagation delay.

Or a simple transistor with pullup resistor on open collector output if you can live with a little longer propagation delay.
10KHz is 100uS from rising edge to rising edge, how close do you need the inverted signal to match?
Do you want to call these out on every pass thru loop? PWM is usually a set & forget thing, so call them out in setup instead if all they will do is free run after that.

  pwmWrite(outA, dutyCycle);
  pwmWriteInv(outB, dutyCycle);

The other way to go is blink without delay style:

unsigned long currentMicros;
unsigned long previousMicros;
unsigned long duration = 50UL; // 50uS, 1/2 of 10HKz
void setup(){
pinMode (9, OUTPUT); // PORTB-bit1
digitalWrite (9, HIGH);
pinMode (10, OUTPUT); // PORTB-bit2
digitalWrite (10, LOW);
}
void loop(){
currentTime = micros();
elapsedTime = currentTime - previousTime;
if (elapsedTime >= duration){
previousTime = previousTime + duration; // 50/50 duty cycle
PINB = PINB | 0b00000110; // toggle bits 2,1 by writing to input register
}
// do other stuff while waiting to 50uS to pass
}

I ran some tests and posted oscilloscope screen shots here

showing speeds of up to 2 MHz using this method (writing to input register).

I have the below massacred code outputting PWM 10khz on pin 9 and I was trying to invert this on pin 10 with no success.

What signal are you getting pin 10? What do you expect to see? You have the duty cycle set to 50% so you should be able to see the inversion (when comparing to the signal on pin 9) with a dual trace oscilloscope scope or logic analyzer.

Try setting the duty on pin 9 to 25% (65) ... should get 75% on pin 10.

Tinkering with the duty cycle won't invert the signal unless there's a time offset involved.

I can see 2 different functions using the same variable. If they work as intended, should get 2 unique signals.

pwmWrite(outA, dutyCycle);
pwmWriteInv(outB, dutyCycle);

Either I completely missed something or the two functions are exactly identical?!

Yep, the internal code for those functions is identical and I don't believe that's the ultimate intention. Can't help with that as this is AVR based ... too bad, as the Arduno Due has complimentary PWM embedded, does not eat up processor cycles and I have an example for that.

@dannyg21 ... What is the application and what range of PWM frequency, duty cycle resolution and timing performance do you require?

Oh, good. I was beginning to think I was suffering from code blindness there. I wonder where the library came from though.

Wow, a lot of response!! Thanks.

Externarnal device is tempting at this point, given the condition of my codeing skills, but I am trying to minimize dead times as much as possible b/n the two needed out puts.

My end goal is to get an inverted signal of pin 9 on to pin10. The initial codes attached earlier goes upto 2MHz @50% duty cycle. I am trying to figure out or if it is even possible to get the inverted version pin10!

This is for coil triggering experiment (push-pull) using a fet driver ir2113/2110.

I will post some scope shots of what I get on both OPs when I get home.

dannyg21:
but I am trying to minimize dead times as much as possible b/n the two needed out puts.

Nanoseconds, mate. You'll never know the inverter was there.

If you can use a Micro or Leonardo with the 32U4, that device has three timers and their complement brought out on pins.

Not sure if you need the MEGA for other reasons. I think they geared the 32U4 to do 3-phase PWM.