PWM Overview

Introduction:

The methods described in this section allow the generation of Pulse Width Modulation (PWM) signals. PWM signals enables the microcontroller to control items like the speed of a motor, or the brightness of a LED or lamp. The methods can also be used to generate the appropriate frequency signal to drive an infrared LED for remote control applications.

The PWMOn, PWMOff and HPWM methods use the hardware PWM generation module on the microcontroller. They will only work on some microcontrollers - see the section on each command for details. PWMOn and HPWM will cause the hardware PWM module on the microcontroller to start generating a PWM signal, which will then continue to be generated until the PWMOff instruction is run.

The method PWMOut does not make use of any special hardware within the microcontroller. The PWM signal is generated only while the PWMOut command is executing - therefore, when the PWMOut is not executing by moving onto the next command, the PWM signal will stop.

Relevant Constants:

A number of constants are used to control settings for the PWM hardware module of the microcontroller. To set them, place a line in the main program file that uses #define to assign a value to the particular constant.

There are two sets of constants: one set for Hardware PWM, and one set for Software PWM.

Hardware PWM

Hardware PWM requires a CCP, CCP1, CCP2, CCP3 or CCP4 module on the microcontroller and Software PWM has no requirements regarding a supporting hardware modle within the microcontroller. Hardware PWM is only available through the "CCP" or "CCPx" pin. This is a hardware limitation of Microchip PIC microcontrollers.

These constants are required for PWMOn.

Constant Name Controls Default Value

PWM_Freq

Specifies the output frequency of the PWM module in KHz.

38

PWM_Duty

Sets the duty cycle of the PWM module output. Given as percentage.

50

HPWM and PWMOff do not require any constants to operate.

Optimisation of the hardware PWM code can be implemented within Great Cow BASIC, see Hardware PWM Code Optimisation

Software PWM

Constant Name Controls Default Value

PWM_Delay

The PWM Period. The length of any delay used will be multiplied by 255. If no value is specified, no delays will be inserted into the PWM routine.

Not defined - no delay

PWM_Out n

The port physical port on the Microchip PIC microcontroller that corresponds to channel n.
n can represent 1, 2, 3 or 4.

Not Defined