Go Down

Topic: inverting a pwm signal (Read 24613 times) previous topic - next topic

ozanenginoglu

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

GrooveFlotilla

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

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

ozanenginoglu

Can't we do it software based?

GrooveFlotilla

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

The external inverter also means you only use one PWM pin.
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

AWOL

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


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

jabber

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.

ozanenginoglu

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 :)

Thank you guys!!

Grumpy_Mike

Quote
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.

borref

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.

Quote
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.

MarkT

Go with the inverter - this isn't going to fail and cause complete shoot-through.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

ozanenginoglu

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

cjparish

#11
Mar 30, 2010, 04:33 pm Last Edit: Mar 30, 2010, 04:34 pm by cjparish Reason: 1
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:

Code: [Select]

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:

Code: [Select]

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.

ozanenginoglu

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!

cjparish

Quote

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:

Code: [Select]

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

ozanenginoglu

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.

Go Up