User Tools

Site Tools




void pwm(u8 pwmChannel, u16 pwmDuty, u16 pwmWidth)

pwmChannel : PWM channel
pwmDuty: Duration for which the pulse is ON
pwmWidth: Pulse period

Sets the duty cycle and period of the pulse width modulator (PWM) on channel pwmChannel. pwmWidth specifies the width of an entire pulse (i.e. the pulse period) and pwmDuty specifies the duration for which the pulse is ON. This, therefore, implies that pwmDuty should always be less than pwmWidth.

The pulse is started once pwm is called. pwmOff is used to turn off the pulse.

The High-Speed Counter Module has a total of 8 PWM channels. The channels, however, are organized into two groups. Each group's period (pwmWidth) must be the same.

The PWM's frequency can be determined by the following formula:

For a pwmWidth of 65,535, the frequency would be 1099Hz.

pwm(0, 32768, 65535 );     // A 1099Hz pulse

Please note that this formula is just an approximation, so when precision is needed, the actual frequency should be measured and adjusted as necessary.

Using the PWM Output

The 5V PWM output does not provide much current to drive certain loads. An NPN transistor can be used as shown below to provide more current to loads such as a DC fan motor.

The transistor will have to be one that can bear the load of the motor. A heat sink may be needed to keep the transistor from overheating.

go MOACON home

moacon/pwm/index.txt · Last modified: 2016/04/14 11:07 (external edit)