599 Menlo Drive, Suite 100
Rocklin, California 95765, USA
Office: (916) 624-8333
Fax: (916) 624-8003
General: info@parallax.com
Technical: support@parallax.com
Web Site: www.parallax.com
Educational: www.stampsinclass.com
PWMPAL (#28020)
PWM Generation, Control, and Background Counting
Introduction
The PWMPAL is an intelligent peripheral that adds up to four PWM output channels and up to four
control/counter input channels to the 24-pin versions of the BASIC Stamp® microcontroller. PWM
channels can be configured to operate under software control, or under hardware control through the
corresponding counter input channel. In addition to PWM waveform generation, the PWMPAL has four
16-bit counters that operate at all times, even when the counter pin is used for hardware PWM control.
Communication with the PWMPAL is handled through a bi-directional serial connection on pin P0 of the
microntroller module. The Parallax AppMod communications protocol is used, allowing baud rates of
9600, 19,200 and 38,400 baud.
Examples of PWMPAL Uses:
•
•
•
Servo or DC motor control for robotics and animatronics
AC waveform (square wave) generation for bi-color LEDs and sensors
Background counting for process control, robotic motion monitoring, etc.
Packing List
Verify that your PWMPAL kit is complete in accordance with the list below:
•
•
PWMPAL "Smart Socket" module
Documentation
Note: PWMPAL demonstration software files may be downloaded from www.parallax.com.
Features
•
•
•
•
•
•
•
Up to four simultaneous PWM outputs
Independent control of each PWM output high-time, low-time, and phase
Generate frequencies from 0.3 Hz to 20 kHz; duty cycle independent *
Outputs may operate under software or hardware control
Up to four 16-bit counters
Auto-baud detection (9600, 19.2K, 38.4K) for Host-to-PWMPAL communications
Requires no PCB "real estate" – mounts underneath microcontroller module
* Duty cycle independence is not available for the entire range of output frequencies
© Parallax, Inc. • PWMPAL (#28020) • 02/2005
Page 1
Connections
Since the PWMPAL mounts directly beneath a 24-pin microcontroller module, the connections are
automatic. Before installing the PWMPAL, you should perform the following steps:
1. Clear the current BASIC Stamp microcontroller program to ensure that all pins are set to inputs.
The simplest program to do this contains just one statement:
END
2.
3.
4.
5.
6.
Disconnect power from the BASIC Stamp microcontroller circuit.
Remove the BASIC Stamp microcontroller from its socket.
Install the PWMPAL in the 24-pin socket, noting the position of pin 1 (marked with dot).
Install the BASIC Stamp microcontroller in the PWMPAL socket.
Reconnect power to the circuit and program the BASIC Stamp microcontroller as desired.
The table below lists the PWMPAL / BASIC Stamp pin sharing connections:
Pin
P0
P8
P9
P10
P11
P12
P13
P14
P15
PWMPAL Function
Serial link between BASIC Stamp / PWMPAL
Counter 1 input / Motor 1 HW control
Counter 2 input / Motor 2 HW control
Counter 3 input / Motor 3 HW control
Counter 4 input / Motor 4 HW control
Motor 1 output
Motor 2 output
Motor 3 output
Motor 4 output
Dedicated
Yes
No
No
No
No
No
No
No
No
As noted in the table above, the only pin dedicated to the PWMPAL is P0. This pin serves as the serial
link between the PWMPAL and host microcontroller. The PWMPAL pins connected to pins P8 – P11 are
always configured as inputs, so there is no conflict and the host may use P8 – P11 as inputs or outputs
as the program requires. For pins P12 – P15 the programmer must use caution to prevent conflict
between the host and the PWMPAL.
Note: The PWMPAL module has current-limiting resistors on its motor output lines to prevent a conflict
from damaging either the PWMPAL or the host microcontroller. Still, the programmer should exercise
caution. When any of the PWMPAL motor control outputs are enabled the associated host pin(s) should
be set to input mode.
How It Works
Using a small coprocessor, the PWMPAL receives serial commands from the host microcontroller, then
configures and controls the PWMPAL I/O pins as directed. The PWMPAL "motor" control outputs will be a
TTL level square wave. The frequency and period of this waveform is determined by control values sent
to the PWMPAL from the host. Figure 1 shows the PWM output waveform.
© Parallax, Inc. • PWMPAL (#28020) • 02/2005
Page 2
Figure 1 – PWM Output
The PWMPAL gives the program control of the on- and off-time values, hence giving control of the output
frequency and duty cycle. In the PWMPAL, the on and off times are specified with 16-bit values in units
of 25 microseconds (0.000025 seconds). Using the formula:
Frequency = 1 / Period
the minimum and maximum output frequencies of the PWMPAL can be calculated. Given the minimum
and maximum values for on- and off-time:
Frequency (max) = 1 / ((1 x 0.000025) + (1 x 0.000025)) 1 / 0.00005 = 20,000 Hz
Frequency (min) = 1 / ((65535 x 0.000025) + (65535 x 0.000025)) 1 / 3.27675 = 0.30518 Hz
Duty Cycle describes the relationship between the on-time and the total period in terms of percent. With
full control of the on- and off-time values, the programmer can set the frequency and duty cycle as
required. If, for example, the desired PWM output was 1000 Hz with duty cycle of 40%, the following
steps would be used to calculate the on-time and off-time values:
1. Determine the waveform period (1 / Frequency):
1 / 1000 = 0.001
2. Determine the PWMPAL units in the period:
0.001 / 0.000025 = 40
3. For the on-time, multiply the total period units by the desired duty cycle:
40 x 0.4 = 16
4. For the off-time, subtract the on-time units from the total period units:
40 – 16 = 24
See the following sections for specifics on sending PWM on- and off-time values to the PWMPAL.
© Parallax, Inc. • PWMPAL (#28020) • 02/2005
Page 3
PWMPAL Commands
All PWMPAL commands (except ID, see below) follow the Parallax AppMod serial protocol and will begin
with the string:
"!PWM"
The "!" is used for synchronization and baud rate detection; the "PWM" identifies the device type. What
follows will generally be a command letter and unit identifier.
"ID"
Request PWMPAL ID String
Use: SEROUT 0, baud, ["!ID]
SERIN 0, baud, [STR version\3]
baud
version
variable or constant value for 9600, 19.2K, or 38.4K baud (host-dependent value )
3-byte array to hold version string
The "!ID" command requests the PWMPAL ID string (three ASCII characters). Note that this command
does not follow the standard AppMod protocol by specifying the device type in the command string, so it
should NOT be used on the same serial connection as other AppMod devices.
"Mn"
Set PWM Motor Timing Control Values
Use: SEROUT 0, baud, ["!PWMMx", tOn.BYTE0, tOn.BYTE1, tOff.BYTE0, tOff.BYTE1]
baud
n
tOn
tOff
variable or constant value for 9600, 19.2K, or 38.4K baud
"1" to "4" – specifying the PWMPAL output channel (P12 – P15)
variable or constant for on-time; in 25 microsecond units
variable or constant for off-time; in 25 microsecond units
The "Mn" command sets the on- and off-time values for a specified PWM channel. Note that the 16-bit
on- and off-time values must be transmitted as bytes, low-byte first.
Example (Baudmode value for BS2 microcontroller ):
SEROUT 0, 6, ["!PWMM1", 2, 0, 6, 0]
The example above configures PWM channel 1 (P12) to have an output frequency of 5000 Hz with a duty
cycle of 25%.
It is important to note that setting the on- and off-time values does not enable the PWM output channel
if it was previously disabled. This is handled separately through the Set Status command (see below).
Finally, there is a special-case use of the "Mn" command: "M0". For example:
SEROUT 0, 6, ["!PWMM0"]
The purpose of this command is to disable all PWM output channels; returning all to a Hi-Z state.
© Parallax, Inc. • PWMPAL (#28020) • 02/2005
Page 4
"SS"
Set Motor Control/Status Byte
Use: SEROUT 0, baud, ["!PWMSS", status]
baud
status
variable or constant value for 9600, 19.2K, or 38.4K baud
variable or constant value; PWMPAL status bits (see table below)
The "SS" command sets the control/status byte to the PWMPAL. The purpose of this byte is to enable or
disable PWMPAL channel outputs and to set the type of control (software or hardware). The low nibble
of the control/status byte holds the control bits; the upper nibble holds the output PWM channel enable
bits.
Control/Status Byte Definitions:
Bit
0
1
2
3
4
5
6
7
Purpose
M1 Control
M2 Control
M3 Control
M4 Control
M1 Status
M2 Status
M3 Status
M4 Status
When 0
Software
Software
Software
Software
Disabled
Disabled
Disabled
Disabled
control
control
control
control
When 1
Hardware control
Hardware control
Hardware control
Hardware control
Enabled *
Enabled *
Enabled *
Enabled *
(P8)
(P9)
(P10)
(P11)
* When a PWM channel is enabled and set for hardware control, the associated hardware control input
pin must be high before the PWM output will be active.
Example:
SEROUT 0, 6, ["!PWMSS", %00010000]
The example above enables PWM output channel 1 (P12). The PWM output will begin immediately with
the frequency and duty cycle as set with the "Mn" command. To change the output to hardware control,
send the following:
SEROUT 0, 6, ["!PWMSS", %00010001]
Now the PWM output channel 1 (P12) will only be active when control channel 1 (P8) is high.
"GS"
Get Motor Control/Status Byte
Use: SEROUT 0, baud, ["!PWMGS"]
baud
variable or constant value for 9600, 19.2K, or 38.4K baud
The "GS" command will cause the PWMPAL to return the current control/status byte. After sending this
command the BASIC Stamp must use SERIN to retrieve the control/status byte.
Example:
SEROUT 0, 6, ["!PWMGS"]
SERIN 0, 6, [status]
© Parallax, Inc. • PWMPAL (#28020) • 02/2005
Page 5
"SP"
Set Counter Enable/Motor Phase Byte
Use: SEROUT 0, baud, ["!PWMSP", phase]
baud
phase
variable or constant value for 9600, 19.2K, or 38.4K baud
variable or constant value; PWMPAL counter/phase bits
The "SP" command transmits the counter/phase byte to the PWMPAL. The purpose of this byte is to
enable or disable PWMPAL counter channels and to set the phase of the PWM channels. (start low or
start high). The lower nibble the counter/phase byte holds the counter enable bits; the upper nibble
holds the output PWM channel phase bits.
Counter/Phase Byte Bit Definitions:
Bit
0
1
2
3
4
5
6
7
*
Purpose
C1 Control
C2 Control
C3 Control
C4 Control
M1 Phase
M2 Phase
M3 Phase
M4 Phase
When 0
Disabled
Disabled
Disabled
Disabled
Output starts
Output starts
Output starts
Output starts
When 1
Enabled *
Enabled *
Enabled *
Enabled *
Output starts
Output starts
Output starts
Output starts
low
low
low
low
high
high
high
high
Counters inputs may also be used as motor control inputs. In this application, the PWMPAL will count
the number of times the PWM output was enabled (input = High).
Example:
SEROUT
SEROUT
SEROUT
SEROUT
0,
0,
0,
0,
6,
6,
6,
6,
["!PWMM3",
["!PWMM4",
["!PWMSP",
["!PWMSS",
$4D, $01, $4D, $01]
$4D, $01, $4D, $01]
%10000001]
%11000000]
'
'
'
'
60 Hz, 50%
60 Hz, 50%
set phase,
enable PWM
DC
DC
counter 1 enabled
outputs
This example sets PWM channels 3 (P14) and 4 (P15) to a 60 Hz, 50% duty-cycle output of opposite
phase (P14 is high when P15 is low and vice-versa). Counter channel 1 is also enabled (line 3 of the
code). Finally, the outputs are enabled under software control.
"GP"
Get Counter Enable/Motor Phase Byte
Use: SEROUT 0, baud, ["!PWMGP"]
baud
variable or constant value for 9600, 19.2K or 38.4K baud
The "GP" command will cause the PWMPAL to return the current counter enable/motor phase byte. After
sending this command the BASIC Stamp must use SERIN to retrieve the counter/phase byte.
Example:
SEROUT 0, 6, ["!PWMGP"]
SERIN 0, 6, [phase]
© Parallax, Inc. • PWMPAL (#28020) • 02/2005
Page 6
"Cn"
Get Counter Value
Use: SEROUT 0, baud, ["!PWMCn"]
baud
n
variable or constant value for 9600, 19.2K, or 38.4K baud
"1" to "4" – specifying the PWMPAL counter channel (P8 – P11)
The "Cn" command will cause the PWMPAL to return the current value of the specified counter channel.
After sending this command the BASIC Stamp must use SERIN to retrieve the counter data.
Example:
SEROUT 0, 6, ["!PWMC1"]
SERIN 0, 6, [cntr1.LOWBYTE, cntr1.HIGHBYTE]
' get counter 1
' receive value
By using "C0" the value of all counters will be returned. The SERIN function must be setup to receive
eight bytes when "C0" is used.
"Xn"
Clear Counter Value
Use: SEROUT 0, baud, ["!PWMXn"]
baud
n
variable or constant value for 9600, 19.2K, or 38.4K baud
"1" to "4" – specifying the PWMPAL counter channel (P8 – P11)
The "Xn" command will cause the PWMPAL to clear (reset to zero) the specified counter channel value.
Example:
SEROUT 0, 6, ["!PWMX1"]
' clear counter 1
By using "X0" the value of all counters will be reset to zero.
Programming the PWMPAL
The examples that follow will demonstrate how easy the PWMPAL is to program. The programmer must
keep in mind, however, that using BASIC Stamp microcontroller pins P12 – P15 as outputs should be
done with extreme caution as there could be a conflict with the PWMPAL that leads to unpredictable
results. Design your code carefully so that PWMPAL outputs do not conflict with host outputs on these
pins.
© Parallax, Inc. • PWMPAL (#28020) • 02/2005
Page 7
Example 1 – PWMPAL Programming Template
For advanced programs a template is often useful to keep organized and prevent errors. This template
file contains useful constant and variable values for programs that utilize the PWMPAL. Conditional
compilation defintions allow this program to work properly with all BS2-family modules.
' =========================================================================
'
'
File....... PWMPAL_Template.BS2
'
Purpose.... Template for PWMPAL Programs
'
Author..... Parallax
'
E-mail..... support@parallax.com
'
Started....
'
Updated.... 09 FEB 2005
'
'
{$STAMP BS2}
'
{$PBASIC 2.5}
'
' =========================================================================
' -----[ Program Description ]--------------------------------------------' -----[ Revision History ]-----------------------------------------------' -----[ I/O Definitions ]------------------------------------------------PpPin
PIN
0
' PWMPAL Serial I/O
' -----[ Constants ]------------------------------------------------------#SELECT $STAMP
#CASE BS2, BS2E, BS2PE
T9600
CON
84
T19K2
CON
32
T38K4
CON
6
#CASE BS2SX, BS2P
T9600
CON
240
T19K2
CON
110
T38K4
CON
45
#ENDSELECT
PpBaud
CON
T38K
' -----[ Variables ]------------------------------------------------------chan
status
phase
VAR
VAR
VAR
Nib
Byte
Byte
' channel number
' control/status
' counters/phase
onTime
offTime
counter
VAR
VAR
VAR
Word
Word
Word
' work variable
' work variable
' work variable
© Parallax, Inc. • PWMPAL (#28020) • 02/2005
Page 8
m1Ctrl
m2Ctrl
m3Ctrl
m4Ctrl
m1Enable
m2Enable
m3Enable
m4Enable
VAR
VAR
VAR
VAR
VAR
VAR
VAR
VAR
status.BIT0
status.BIT1
status.BIT2
status.BIT3
status.BIT4
status.BIT5
status.BIT6
status.BIT7
' status bits
c1Enable
c2Enable
c3Enable
c4Enable
m1Phase
m2Phase
m3Phase
m4Phase
VAR
VAR
VAR
VAR
VAR
VAR
VAR
VAR
phase.BIT0
phase.BIT1
phase.BIT2
phase.BIT3
phase.BIT4
phase.BIT5
phase.BIT6
phase.BIT7
' phase bits
' -----[ EEPROM Data ]----------------------------------------------------' -----[ Initialization ]-------------------------------------------------Setup:
' -----[ Program Code ]---------------------------------------------------Main:
END
' -----[ Subroutines ]-----------------------------------------------------
© Parallax, Inc. • PWMPAL (#28020) • 02/2005
Page 9
Example 2 – PWM Output with Hardware Control
This example uses an active-high pushbutton circuit to control a DC motor connected to the PWM
channel specified by the MotorNum constant. When the switch is pressed, the motor speed will ramp
from 25% (minimum speed to get test motor moving) to 100% (full speed). When the switch is released
the motor will stop – without having to send a new speed value to the PWM channel. This program
shows how the BASIC Stamp microcontroller can monitor and use the control inputs.
' =========================================================================
'
'
File....... PWMPAL_Simple_Motor.BS2
'
Purpose.... Simple DC motor control – with hardware input control
'
Author..... Parallax
'
E-mail..... support@parallax.com
'
Started....
'
Updated.... 09 FEB 2005
'
'
{$STAMP BS2}
'
{$PBASIC 2.5}
'
' =========================================================================
' -----[ Program Description ]--------------------------------------------' Demonstrates simple DC motor control with the PWMPAL.
' NOTE: Do NOT connect a DC motor directly to the PWMPAL / micro. You
' must use a buffer (transistor, MOSFET, etc.) to switch the current
' required by the motor.
' -----[ I/O Definitions ]------------------------------------------------PpPin
SpdCtrl
PIN
PIN
0
8
© Parallax, Inc. • PWMPAL (#28020) • 02/2005
' PWMPAL Serial I/O
' speed button
Page 10
' -----[ Constants ]------------------------------------------------------MotorNum
CON
1
' PWMPAL motor output
#SELECT $STAMP
#CASE BS2, BS2E, BS2PE
T9600
CON
84
T19K2
CON
32
T38K4
CON
6
#CASE BS2SX, BS2P
T9600
CON
240
T19K2
CON
110
T38K4
CON
45
#ENDSELECT
PpBaud
CON
T38K
Yes
MinSpeed
CON
CON
1
25
' minimum DC to spin motor
' -----[ Variables ]------------------------------------------------------speed
status
VAR
VAR
Byte
Byte
' speed, 0% to 100%
' motor control status
onTime
offTime
VAR
VAR
Word
Word
' PWM timing
' -----[ Program Code ]---------------------------------------------------Main:
DO
IF (SpdCtrl = Yes) THEN
IF (speed < 100) THEN
speed = speed + 1 MIN MinSpeed
GOSUB Set_Speed
ENDIF
ELSE
speed = 0
ENDIF
DEBUG HOME, "Speed = ", DEC speed, CLREOL
PAUSE 100
LOOP
'
'
'
'
button pressed
can we speed up?
increase speed
update PWMPAL
' show current speed
' speed ramp delay
' -----[ Subroutines ]----------------------------------------------------Set_Speed:
IF (speed = 100) THEN
onTime = $FFFF
offTime = $0001
ELSE
onTime = speed
offTime = 100 - speed
ENDIF
© Parallax, Inc. • PWMPAL (#28020) • 02/2005
' full on for 100%
' set duty cycle
Page 11
SEROUT PpPin, PpBaud, ["!PWMM", (48 + MotorNum),
onTime.BYTE0, onTime.BYTE1,
offTime.BYTE0, offTime.BYTE1]
status.HIGHNIB = %0001