![]() ZBasic System Library
253
ZBasic Microcontrollers
Type
Subroutine
Invocation
PWM(channel, dutyCycle)
PWM(channel, dutyCycle, status)
Parameter
Method
Type
Description
channel
ByVal
Byte
The channel to use for PWM generation.
dutyCycle
ByVal
Single or integral
The desired duty cycle.
status
ByRef
Boolean
The variable to receive the status value.
Discussion
This subroutine begins or modifies the generation of a 16-bit PWM signal on the specified channel. The
channel must have been previously prepared for PWM generation by calling OpenPWM(). 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.
The dutyCycle parameter specifies the desired duty cycle of the generated signal, expressing the
percentage of time that the PWM signal will be at the logic 1 state. If the supplied parameter is of type
Single, the value is in percent with a resolution of 0.01%. If the supplied parameter is integral, the units
are percent, i.e., the value 100 means 100%. Specifying a Single value that is negative or any value
greater than 100 will have an undefined effect.
The status parameter, if supplied, receives a value to indicate success or failure of the call.
If this subroutine is called without a preceding call to OpenPWM() to prepare the timer, the call will have
no effect. This subroutine may be called multiple times to effect changes to the PWM signals duty cycle
while the signal is being generated. The change in duty cycle is synchronized so that it takes effect at the
beginning of the next PWM pulse.
Example
Call OpenPWM(2, 50.0, zxFastPWM)' prepare for 50Hz Fast PWM using channel 2
Call PWM(2, 7.5) ' generate PWM with 7.5% duty cycle (1.5mS)
Call Delay(1.0)
Call PWM(2, 6.25) ' generate PWM with 6.25% duty cycle (1.25mS)
Compatibility
This subroutine is not available in BasicX compatibility mode.
See Also
|