Start Back Next End
  
ZBasic System Library
202
ZBasic Microcontrollers
OpenPWM8
Type
Subroutine
Invocation
OpenPWM8(channel, frequency, mode)
OpenPWM8(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 using
one of the CPU’s 8-bit timers.  The table below indicates the available channels and the corresponding
timer used.  See the Resource Usage sub-section 8-Bit PWM Timers for details of the available channels
and the corresponding timer and output pin used.  See the description of PWM8() for additional details on
the PWM channels.  Note that ZBasic devices based on ATxmega processors don’t have any 8-bit timers
so 8-bit PWM is not supported on those devices.
It is important to note that the timer used for 8-bit PWM generation is the same one used for generating
the timing for the software UARTs (Com3-Com6).  Consequently, these two features cannot be used at
the same time.
The frequency parameter specifies the desired 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 OpenPWM8() once to prepare the timer for all of the PWM channels that are based on a
that 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 status parameter, if supplied, receives a value to indicate success or failure of the call.
A side effect of calling OpenPWM8() is that the timer busy flag for the underlying timer (e.g.
Register.Timer2Busy) 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 OpenPWM8() doesn't affect the configuration the PWM output pin. 
When a call is eventually made to PWM8(), 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.
The actual PWM frequency used will be the closest of the available frequencies as shown in the table
below for ZBasic devices operating at 14.7MHz.  For ZBasic devices operating at a different frequency,
the available PWM frequencies will be proportionally higher or lower and can be computed by the
formulae given in the table headings
Previous page Top Next page