inverting a pwm signal

Hello,

I want to drive a dc motor with motor driver's two input pins (enable pins will be always +5V). So i will need a pwm signal and it's reverse. How can i get a reverse of a pwm signal? So what i mean is for example if normal pwm signal is 1010 it's reverse should be 0101.

Thanks

Use an inverter, like a 7400 (NAND) with its inputs tied together.

Can't we do it software based?

Well, you could, (e.g. analogWrite (pin1, speed); analogWrite (pin2, 255 - speed):wink: though I'm sure you'd maintain correct phase relationships.

The external inverter also means you only use one PWM pin.

though I'm sure you'd maintain correct phase relationships.

I think that should read " though I'm not sure you'd maintain correct phase relationships."

If you look at the timer/counter sections of the atmega data sheet, the pwm operation can be inverted, and the frequency changed if needed, but it is beyond a simple explanation here, you need to read & understand what the datasheet says.

jabber you're right. i found a document explaining how to set timers of the microcontroller and it can be done. I will read the datasheet and try to apply to my motor driver. I will write forum if i succeed. I hope i will :slight_smile:

Thank you guys!!

So what i mean is for example if normal pwm signal is 1010 it's reverse should be 0101.

That's an odd way to describe a PWM signal, it is simply an on / off time ratio not a bit pattern.
The trick is to use PWM pins off the same timer, then they will be in phase.

If I understand the topic right it is about controlling an H-bridge (two halves) with individual PWM outputs and making sure they're not switched on at the same time.

The trick is to use PWM pins off the same timer...

This may be possible, but will require low level programming to configure the direction of set/clear to up/down respectively for the two output pins. A further complication is that enabling the two channels (and whenever dudty cycle is changed) need to be synchronized (e.g. stop timer, set logic level on both outputs to zero and then restart).

All in all - a logical NAND gate as suggested by Groove may be the safer option.

Go with the inverter - this isn't going to fail and cause complete shoot-through.

OK i guess you are right. I will go with the inverter. I am sure it will save me from a lot of complex coding. Thanks

As the PMW is managed by the built in timers it sould simply be a matter of changeing the output mode.
Lets say you are using PWM on pin 10. This is controlled by the B output of timer 1. So after you set 'analogWrite(10, 100)' you need to reverse the output like this:

TCCR1A = TCCR1A & ~B00110000; //switch off output B
TCCR1A |= B00110000;  //switch on the B output with inverted output

you could combine this into a function like:

void invertAnalogWrite(int pin, int value)
{
    analogWrite(pin, value);
   TCCR1A = TCCR1A & ~B00110000; //switch off output B
   TCCR1A |= B00110000;  //switch on the B output with inverted output
}

This would do the job.

I have scoped the output and the above does work.

Great, thank you!

Is it possible to invert one pin's pwm to another with this code? If i didn't understand wrong, your code creates a pwm signal with analogwrite function and then invert it and assign to same pin so it overwrites the non inverted pwm signal..

What i want is to create a pwm signal on pin 10 and it's reverse on pin 11. If you can modify your code to do this i would really appreciate it.

Thanks so much!

What i want is to create a pwm signal on pin 10 and it's reverse on pin 11. If you can modify your code to do this i would really appreciate it.

Can I suggest using pins 9 and 10, rather than 10 and 11, because 9 and 10 both run off the same timer, so they should be syncronised.
Simply do a normal analogWrite to pin 9, then run my code for pin 10.

Like so:

void invertAnalogWrite(int pin, int value)
{
    analogWrite(pin, value);
   TCCR1A = TCCR1A & ~B00110000; //switch off output B
   TCCR1A |= B00110000;  //switch on the B output with inverted output
}

void loop()
{
    analogWrite(9, value);
    invertAnalogWrite(10, value);
}

This should do the trick. There is an outside chance that there might be one pluse that is out of sync if the timer ticks between the analogWrite() command and the invert commands. If this would cause a problem, then I would be quite happy to write you some code to get round this. Basically it would require a custom analogWrite function.
If that is what you want then tell me and I would happily knock it up.

Chris

1 Like

I believe this would work perfect for me but im not sure if one pulse would make any difference while driving a dc motor. i will let you know if something goes wrong. Thanks again for the enlightening information.

No problem. Glad I could help.

Chris

I was wondering, I haven't had much experience driving DC motors. I gather that you are using PWM, but how are you controlling speed? is it done by simply increasing the duty cycle, or can it be done by changing the frequency of the pulse, like you would with a stepper motor?
Sorry if these are daft questions, but I really don't know anything about DC motors!

Chris

Yes, speed control is provided by changing duty cycle. It is proportional to voltage.

In order to understand effects of frequency over pwm here is a quote from the http://homepages.which.net/~paul.hills/SpeedControl/SpeedControllersBody.html

The frequency of the resulting PWM signal is dependant on the frequency of the ramp waveform. What frequency do we want? This is not a simple question. Some pros and cons are:

  • Frequencies between 20Hz and 18kHz may produce audible screaming from the speed controller and motors - this may be an added attraction for your robot!

  • RF interference emitted by the circuit will be worse the higher the switching frequency is.

  • Each switching on and off of the speed controller MOSFETs results in a little power loss. Therefore the greater the time spent switching compared with the static on and off times, the greater will be the resulting 'switching loss' in the MOSFETs.

  • The higher the switching frequency, the more stable is the current waveform in the motors. This waveform will be a spiky switching waveform at low frequencies, but at high frequencies the inductance of the motor will smooth this out to an average DC current level proportional to the PWM demand. This spikyness will cause greater power loss in the resistances of the wires, MOSFETs, and motor windings than a steady DC current waveform.

If using a software solution you should be aware that the two phases should not make the two halves of your bridge conducting at the same time.Otherwise you will blow up you motor drivers. The motor drivers usually have some security that avoid cross conduction of the 2 transistors of the same halve. Choose your bridge carefully.