Hardware Reference
In-Depth Information
while ( !pwm->stopf ) {
fperiod = 1.0 / pwm->freq;
percent = (double) pwm->n / (double)pwm->m;
ontime = fperiod * percent;
for ( pwm->chgf=0; !pwm->chgf && !pwm->stopf; ) {
gpio_write(pwm->gpio,1);
float_wait(ontime);
gpio_write(pwm->gpio,0);
float_wait(fperiod-ontime);
}
}
return 0;
}
One final note about the PWM structure members concerns the use of the C
keyword volatile . Both chgf and stopf structure members are declared volatile so
that the compiler will generate code that will access these values every time they are
required. Otherwise, compiler optimization may cause the generated code to reuse
values held in registers. This would cause the thread to not notice a change in these
values, which are critical.
volatile char chgf; /* True when N/M changed */
volatile char stopf; /* True when thread to stop */
How Many PWMs?
The design of the preceding PWM software routines is such that you can open as many
PWM instances as you require. The limiting factors are as follows:
Number of free GPIO output lines
CPU resource utilization
On a nonturbo mode Raspberry Pi, the code shown seems to require approximately
6% CPU for each soft PWM created. (The CPU utilization rises with frequency, however.)
This leaves you with a certain latitude in the number of PWM signals you generate.
Running the Software PWM Command
To generate a fixed software PWM signal on GPIO 22 (GEN3), run the command like this:
$ ./ softpwm 60 100 2000
PWM set for 60 / 100 , frequency 2000.0 (for 60 seconds )
Obviously, the PWM signal is present for only as long as the softpwm program
continues to run.
 
Search WWH ::




Custom Search