The PWM signal is a stream of pulses whose width can be controlled through software. PWM signals can be used for controlling the speed of DC motors, the shaft angle of servo motor, brightness of an LED, etc.
The PWM module provides APIs to generate PWM signals. The PWM functions
are declared in pwm.h
. The no. of available channels is indicated by
the constant PWM_NCHANNELS
. The channels are indicated using the
following bitmasks.
Macro | Mask |
---|---|
PWM_CH0
|
1 << 0
|
PWM_CH1
|
1 << 1
|
PWM_CH2
|
1 << 2
|
PWM_CH3
|
1 << 3
|
PWM_CH4
|
1 << 4
|
PWM_CHn
|
1 << n
|
The following example program displays a heardbeat on a LED, connected to PWM channel 0.
Listing 15.1. Heartbeat LED
#include <board.h>
#include <pwm.h>
#include <delay.h>
int main()
{
int duty;
board_init();
systick_init();
delay_init();
pwm_set_frequency(PWM_CH0, 10);
pwm_set_duty(PWM_CH0, 0);
pwm_start(PWM_CH0);
while (1) {
for (duty = 0; duty < 100; duty++) {
pwm_set_duty(PWM_CH0, duty);
mdelay(10);
}
for (duty = 0; duty < 100; duty++) {
pwm_set_duty(PWM_CH0, 100 - duty);
mdelay(10);
}
}
}