Software PWM?

124 views
Skip to first unread message

John in WI

unread,
Jun 25, 2012, 12:01:56 PM6/25/12
to jal...@googlegroups.com
Hi all,
This group has been a great source for me. Learned a great deal in the last couple years.
Is there a library or routine for outputting PWM on various pins without using the CCP modules?

I want to control 2 H bridges (that means 4 different PWM signals) with an 18f14k50 which appears to only have 1 CCP pin.

Just pulsing a pin would work in this case... not sure about timing, etc, though, and how it would affect the rest of what I'm trying to accomplish.

Ideally, something like the servo_rc_master that pulses any pin you choose would work great. Might there be a way to modify that library to provide a more traditional PWM that would provide different frequencies and duty cycles?

Thanks again for all the great work here.....

-John in WI

Joep Suijs

unread,
Jun 25, 2012, 4:56:36 PM6/25/12
to jal...@googlegroups.com
Hi John,

As far as I know there are no libraries in jal for 'software pwm'.
There are however libraries up to CCP9, which suggests there are PICs
with a lot of PWM pins. But that doesn't help if you already selected
your target pic...

At one time, I experimented with software pwm, called from a fixed
ISR. The basic approach is to have a timer incremented at a fixed
interval, say 2 kHz, from 0 to 15. This will give you a pwm frequency
of 2kHz/16= 125 Hz and a resolution of 16 steps (4 bits). This
basically points out the limitation of this approach: low resolution
and low frequency. If this is a problem depend on the application.
For my application (robot motor control) it was, so I abandoned it.

It is possible to increase the frequency for many of the values using
the Bresenham algorithm. On average, this will be much better.
However, on the extreme, the performance will be similar to the
previous method.

An approach similar to the servo library is more complex but provides
higher frequency and resolution at lower cpu load. What you basically
do is set the timer to interrupt you when the next event (line toggle)
is due. The servo lib has the advantage that a servo pulse has only up
to 12.5% duty cycle, so 8 pulses can put consecutive in one cycle. If
all 4 signals can have the full duty cycle range, you have to consider
them simultaneously.

Hope this helps a bit. If not, please share some details about your
application and it's requirements in terms of resolution and
frequency.

Regards,
Joep

vasile surducan

unread,
Jun 26, 2012, 2:23:41 AM6/26/12
to jal...@googlegroups.com
On Mon, Jun 25, 2012 at 11:56 PM, Joep Suijs <jsu...@gmail.com> wrote:
Hi John,

As far as I know there are no libraries in jal for 'software pwm'.
There are however libraries up to CCP9, which suggests there are PICs
with a lot of PWM pins. But that doesn't help if you already selected
your target pic...

At one time, I experimented with software pwm, called from a fixed
ISR.  The basic approach is to have a timer incremented at a fixed
interval, say 2 kHz, from 0 to 15.  This will give you a pwm frequency
of 2kHz/16= 125 Hz and a resolution of 16 steps (4 bits). This
basically points out the limitation of this approach: low resolution
and low frequency.  If this is a problem depend on the application.
For my application (robot motor control) it was, so I abandoned it.

It is possible to increase the frequency for many of the values using
the Bresenham algorithm. On average, this will be much better.
However, on the extreme, the performance will be similar to the
previous method.

Agree with Joep,

Depends on PWM frequency. With Bresenham you can go as high as you want with resolution (I'm using usually 100% with 1% step).
I have used two PWM ( lower software + higher hardware generated), see here in fig8:
in which the lowest frequency is generated  by Bresenham algorithm with 1% step ( 100 steps available for my application)

I've also experimented software generation of two PWM by Bresenham, around 60Hz for higher PWM and 1Hz for lower PWM , where the 1Hz PWM is modulating the 60Hz PWM.
For complex ISRs I found it's necessary to use high priority interrupts, else the Jal code written into ISR is adding an offset delay to the higher frequency PWM pulse generation.

Vasile

 
 

An approach similar to the servo library is more complex but provides
higher frequency and resolution at lower cpu load. What you basically
do is set the timer to interrupt you when the next event (line toggle)
is due. The servo lib has the advantage that a servo pulse has only up
to 12.5% duty cycle, so 8 pulses can put consecutive in one cycle.  If
all 4 signals can have the full duty cycle range, you have to consider
them simultaneously.

Hope this helps a bit. If not, please share some details about your
application and it's requirements in terms of resolution and
frequency.

Regards,
Joep

--
You received this message because you are subscribed to the Google Groups "jallib" group.
To post to this group, send email to jal...@googlegroups.com.
To unsubscribe from this group, send email to jallib+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jallib?hl=en.


John in WI

unread,
Jun 28, 2012, 7:22:49 AM6/28/12
to jal...@googlegroups.com
I've decided to take the path of least resistance and ordered a few PICs that have 7 CCP modules. Looks like the easiest solution. Thanks for your replies!
Reply all
Reply to author
Forward
0 new messages