Author Topic: PWM software  (Read 2783 times)

0 Members and 1 Guest are viewing this topic.

Offline vipulan12Topic starter

  • Robot Overlord
  • ****
  • Posts: 181
  • Helpful? 0
PWM software
« on: November 15, 2013, 09:14:57 PM »
can a pwm signal be created from any pin on any avr microcontroller using high's, low's and delays( bit banging)?
Also what is the difference doing this with software vs hardware? what's the trade off?

thanks

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: PWM software
« Reply #1 on: November 15, 2013, 09:40:23 PM »
Yes, any digital output pin can be used (not just on avr's) using software.

The difference between doing this in software verse in hardware (PWM module) is:
1- the PWM frequency and/or resolution is lower with software
2- the software must execute instructions thus using a portion of the total instruction time available.
3- If the software is doing all the delays to time to high and low PWM cycles then the processor can not do any other tasks.
4- a hardware PWM module will generate the PWM signal with using any instruction time. The software then only needs to change a timer/register to change the PWM output frequency and/or duty cycle.
5- a mix of hardware and software is done using a hardware timer and an ISR. This is a very common method that allows the software to do other tasks while the hardware is counting (delay) and can allow for quite a few PWM outputs without a lot of software overhead.

Offline vipulan12Topic starter

  • Robot Overlord
  • ****
  • Posts: 181
  • Helpful? 0
Re: PWM software
« Reply #2 on: November 16, 2013, 11:24:58 AM »
 what do you mean by lower resolution?
also what kind of hardware is used to create pwn other than microcontrollers?
 could you use a 555 timer?

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: PWM software
« Reply #3 on: November 16, 2013, 12:52:18 PM »
Quote
what do you mean by lower resolution?
How many duty cycle widths can be achieved. Or how many bits of resolution.
IF you only have 4 bits then you can have 16 different widths or duty cycles.
If you have 8 bits then there are 256 widths.

555's can be used as well as discrete digital logic and analog circuits.

Offline vipulan12Topic starter

  • Robot Overlord
  • ****
  • Posts: 181
  • Helpful? 0
Re: PWM software
« Reply #4 on: November 16, 2013, 02:16:12 PM »
so with more possible duty cycles you have a wider range of voltages right?
also could you explain the 4 bits and 8 bits (is it similar to how PCs have 64 and 32 bit or is that different?)
« Last Edit: November 16, 2013, 02:17:48 PM by vipulan12 »

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,345
  • Helpful? 82
Re: PWM software
« Reply #5 on: November 16, 2013, 04:03:09 PM »
Quote
1- the PWM frequency and/or resolution is lower with software

This is not true. Software is more flexible, and in most cases, can get to single-cycle precision in the PWM width. With a 16 MHz CPU (such as the Arduino / Atmegas) you should be able to get within 1/16th of a microsecond of resolution, which is at least as good as the highest precision hardware timer.

The main problem is that, to get the best resolution, you need to turn off interrupts, which means you can't also do something else while generating that PWM.

The bits of resolution for a timer determines how many discrete steps of different duty cycle you can have on a PWM driven by that timer. This is exactly analogous to the bits of resolution in a ADC or sound file. AVR chips typically have 8 and 16 bit timers, and may have 10 bit timers, too.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: PWM software
« Reply #6 on: November 16, 2013, 10:51:42 PM »
So to try to summarize for the OP.
Quote
can a pwm signal be created from any pin on any avr microcontroller

Hardware PWM:- this uses the avr timers to generate the PWM. See http://www.societyofrobots.com/member_tutorials/node/228. The benefit of hardware PWM is that it is the ONLY way to always get 100% correct PWM (without extra hardware), even with interrupts enabled, and irrespective of whatever else your chip is trying to do. The drawback is that it only works with the specific hardware PWM pins associated with the timer.

Software PWM:- most libraries provide software PWM. WebbotLib, Arduino etc. The benefit of software PWM is that the software allows the PWM to be output on ANY output pin. However: because it IS generated via software then its not 100% reliable. Yes - you can make it 100% by disabling all interrupts and not running any software - but then your board just becomes a servo controller. And thats how many servo boards work. Assuming you are also using the same board for other stuff then here is why it goes a bit off: the software tries to measure the length of the pulse but it is normally oblivious to other interrupts. So the pulse length may be the correct length + the time taken to process all other interrupts that have happened. A 9600 baud serial port will generate less interrupts than say motor encoders. The side effect of all this is that your servos will 'judder'. As you add more and more servos then the refresh rate (every 20ms) comes into play. If your servos don't get a pulse every 20ms then they will become ' floppy' - ie loose their torque. The Arduino default servo code is a good example of this problem whereby the refresh rate is 20ms + sum(all servo pulse lenghts). So the more servos then the lower the refresh rate.

But you can of course compromise. EG a walking robot probably needs exact PWM for the legs (use hardware PWM) - whereas the arm and head movement may only be for effect and so could use software PWM.

Word of caution: you may be tempted to use software PWM for everything since it can use any pin. If you go mad then the cpu could spend 100% of its time generating PWM with no time left to run your program. If thats the case then think about off-loading servo control to a child board (bought or hand made).
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here