Août 26

Robotics 101 : PWM (1)

You said PWM ?

A Pulse Width Modulation signal is a binary (that’s to say that there are only two different output voltages: HIGH or LOW) signal of a given fixed period. The output is HIGH for the first part of the period, then stays LOW untill the next period. The interesting point about this type of signal is that you are able to control how long the output stays high in a given period (you can change it at each period). This way you are able to simulate an analog output very easily if the pwm frequency is high enough (your eyes and mechanical devices are low-pass filter, they will average the output value). It is also used by some sensors, usually by those measuring a frequency (the width of the high part is proportional to the measured frequency).


ChibiOS provides an « easy » way, that we will see, to generate this kind of signal. First you will have to learn how to configure a PWM, then you will see how to use it.

PWM Configuration

Sample code :

In order to configure a PWM with chibiOS you have to create a chibiOS structure called a PWMConfig like this one:

Code overview :

A PWMConfig structure takes at least 4 arguments:

type name what it is
uint32_t frequency Timer clock in Hz
pwmcnt_t period PWM period in ticks
pwmcallback_t callback Periodic callback pointer
PWMChannelConfig channels [PWM_CHANNELS] Channels configurations

Quick overview :

Timer frequency : is used to choose the timer clock frequency in Hertz, that is to say the number of ‘ticks’ you have in a second.
Period : is used to specify the number of ticks in each period of the signal.
Callback : is a function that will be launched at the end of each period of the signal.
Channels : is used to specify which channels of the timer are used and which ones are not.

Timer frequency :

You simply specify the frequency, technically it has to be a divisor of the timer frequency but in facts as long as you select “regular” frequency you don’t have to worry about it (just don’t try stuff like 63691 or 98979 and you’ll be fine).

Period :

You simply specify the period of your signal in ticks. The frequency of your signal is then (Timer Frequency)/Period. The higher the period, the more pwm values you will be able to chose.

Callback :

This function will be launched at the end of each period. You have to pass a pointer to the function. Usually you set it to NULL so that nothing happens (it is very rarely needed).

Channels configuration :

As a PWMConfig structure is used to specify the use of a timer (a simple electronic device that simply « counts » at a given frequency and has 4 outputs called channels) you have to say which channels (= timer outputs) are going to be active and which are not. The main reason for this is to lower your energy consumption. After the channel mode (PWM_OUTPUT_DISABLED or PWM_OUTPUT_ACTIVE_HIGH are enough for most usages) you have to specify a callback, once again simply write down the NULL pointer.
A simple example would be:
{PWM_OUTPUT_ACTIVE_HIGH, NULL}, // channel 1 active
{PWM_OUTPUT_DISABLED, NULL}, // channel 2 disabled
{PWM_OUTPUT_DISABLED, NULL}, // channel 3 disabled
{PWM_OUTPUT_ACTIVE_HIGH, NULL} // channel 4 active

Ok now you know how to create a PWMConfig. In the next lesson you will learn how to use the PWM you just configured

Next part here.