![]() ZBasic System Library
201
ZBasic Microcontrollers
Type
Subroutine
Invocation
OpenPWM(channel, frequency, mode)
OpenPWM(channel, frequency, mode, stat)
Parameter
Method
Type
Description
channel
ByVal
Byte
The channel to use for PWM generation.
frequency
ByVal
Single
The desired PWM frequency.
mode
ByVal
Byte
The desired PWM mode (see discussion below).
stat
ByRef
Boolean
The variable to receive the status code.
Discussion
This subroutine prepares a PWM channel for generating a pulse width modulated (PWM) signal. PWM
generation is performed using one of the CPUs 16-bit timers, the number of which varies depending on
the ZBasic device. See the Resource Usage sub-section 16-Bit PWM Timers for details of the available
channels and the corresponding timer and output pin used. See the description of PWM() for additional
details on the PWM channels.
The frequency parameter specifies the PWM base frequency in Hertz. Since the same frequency and
generation mode will be used for all PWM channels based on the same timer, it is only necessary to call
OpenPWM() once to prepare the timer for all of the PWM channels that are based on a given timer.
The mode parameter specifies the PWM generation mode. Two modes are supported: Fast PWM mode
and Phase/Frequency Correct mode. The constants zxFastPWM and zxCorrectPWM, having the values
0 and 1 respectively, may be used to specify the mode. The Fast PWM mode has a maximum frequency
of one-half of the CPU clock frequency and is intended for fixed-frequency applications. The
Phase/Frequency Correct PWM mode has a maximum frequency of one-quarter of the CPU clock
frequency and may be used when the PWM frequency will be changed in the midst of PWM signal
generation. Frequency changes are effected by making additional calls to OpenPWM() and the change is
synchronized so that it takes effect at the beginning of a cycle.
The status parameter, if supplied, receives a value to indicate success or failure of the call.
A side effect of calling OpenPWM() is that the timer busy flag for the underlying timer (e.g.
Register.Timer1Busy) will be set to True irrespective of its prior state. It is recommended that the
initial call to OpenPWM() be preceded by a call to acquire the semaphore for the timer. This will ensure
that an existing timer operation will not be disturbed.
It is important to note that the call to OpenPWM() doesn't affect the configuration the PWM output pin.
When a call is eventually made to PWM(), the PWM pin will be made an output and be actively driven. If
your application needs to have the PWM pin in a particular state prior to PWM beginning you must
configure the pin in your code.
Example
Call OpenPWM(1, 50.0, zxFastPWM) 'prepare for 50Hz Fast PWM using channel 1
Compatibility
This subroutine is not available in BasicX compatibility mode.
See Also
|