Hardware Reference
In-Depth Information
PWM Parameters
PWM involves modulating the width of the pulse. But the pulse's width is one aspect
relative to a whole cycle. Defining a PWM signal requires three parameters:
Frequency (or period) of the cycle
The time period that the signal is on
The time period that the signal is off
It is tempting to think that the cycle time is unimportant. But consider a cycle
lasting 10 seconds, where the signal is on for 1 second and off for the remaining 9 (10%).
Apply that signal to a meter, and the needle will show 100% for 1 second and zero for the
remaining time. Clearly the cycle is too long for the meter's movement to average out.
If you produce a software-derived PWM signal, a high-frequency rate will average well
on the meter movement. But the amount of CPU effort expended is also needlessly high,
wasting computing power. Planning the operating frequency is an important aspect of PWM.
Hardware PWM peripherals also have design frequency limits that must be considered.
The remaining two parameters form the duty in duty cycle , and are often expressed
as a fraction:
N
M
The denominator M = 100 when we talk of percentages. However, M may be any
integer that divides the complete cycle into equal units of time. The value N defines the
number of units that the PWM device is to be on . The remaining M - N step represents
the off time.
PWM Hardware Peripheral
The Raspberry Pi makes one hardware PWM peripheral available to the user. It is
available on GPIO 18 (GEN1), but you must give up one of the audio channels to use it
(or both, if you consider that the clock is also reconfigured for PWM clock rates). If your
application does not use audio, the peripheral makes a great resource for effortlessly
delivering fast and relatively clean pulse waveforms. And all this without having your
software even “think” about it. If you don't need to change the duty cycle, you can set up
the peripheral and let it run free on its own.
PWM Software
The servo folks would be wringing their hands at the thought of only one PWM signal.
Fortunately, the Pi is quite capable of generating more PWM signals if you can accept a
little jitter in the signal output and a little CPU overhead (about 6% of the CPU for each
thread-driven PWM signal, in nonturbo mode). While separate processes could be used
to generate multiple PWM signals, this is best accomplished in one process using threads.
The softpwm program at the end of this chapter demonstrates this.
 
Search WWH ::




Custom Search