Use of a PICAXE-08M as an Accessory to a Basic Stamp, PICAXE-18 or Similar

copyright, Peter H Anderson, Baltimore, MD, Nov 10, '05


Introduction

This discussion deals with the PICAXE-08M as an accessory to a Basic Stamp or similar to control the speed of a motor or the intensity of a lamp using the background PWM capability of the PICAXE-08M. The interface is relatively simple, consisting of two leads, STOP and PULSE from the Stamp or similar to the PICAXE-08M.

In the following, the PICAXE code is offered which the user may easily use to program a PICAXE-08M. You can do this yourself, or I will supply them for $5.00 each.

Note that multiple slaves may be accommodated on a single Basic Stamp, each slave requiring a STOP and PULSE output from the BS2.

(Note that although a Parallax Basic Stamp is considerd in this discussion, the master processor might well be a PICAXE-18, -18A or 18X, particularly in applications where multiple PWM channels are required).

Background

The PWM commands associated with the Basic Stamp is not what one would think of as PWM, but rather a series of logic ones and zeros over a cycle of 256 slots such that the output is high an average is num / 256. For example;

	1 0 0 0 0 0 0 0 ...  ' 1/256
	1 1 0 0 1 1 0 0 ...  ' 64/256
	1 0 1 0 1 0 1 0 ...  ' 128/256
Thus, the period changes, depending on the duty cycle and I have found the transitions so fast that I was uncertain whether the signal was propagating through an optocoupler. True PWM is high for num / 256 ths of the time, followed by low for the balance of the time. For example;
	1 0 0 0 0 0 0 0 ...  ' 1/256
	1 1 1 1 ... 0 0 ...  ' 64/256, 64 logic one states followed by 192 logic zero states.
	1 1 1 1 ... 0 0 ...  ' 128/256, 128 logic one states followed by 128 logic zero states.

In addition, the PWM command associated with the Stamp requires the full resources of the processor. That is, the PWM output only occurs while executing the PWM command. There is little to no time for the Stamp to do anything else and at $49.00, the Stamp is relatively expensive to dedicate to simply performing the PWM task.

Using the PCAXE-08M

The PICAXE-08M provides background true PWM capability on Pin 2 (term 5).

   PWMOut 2, 255, PWMDuty4
The period is 255 + 1 clock ticks or 256 us (3.9 kHz) for 4.0 MHz operation. When operated at 8 MHz, the period is 256 * 0,5 us or 128 us (7.6 kHz).

In the following program, the PWMDuty is initialized to zero. When the STOP input (Pin3) is at a logic one, the PWM output (Pin2) is low (motor or lamp is off). When the STOP input is brought low, the PWMOut command is executed using the current value of PWMDuty.

Normally, the PULSE input on Pin4 (terminal 3) is at a logic one. When brought low, the PICAXE is interrupted and the number of positive transitions of the Pulse input are counted over a 100 ms window. At the conclusion of the 100 ms window, the PICAXE waits for the PULSE input to again go high. The number of counts received during this window then becomes the new PWMDuty.

' PWMSlave.Bas  (PICAXE-08M)
'
' Basic Stamp          		               PICAXE-08M
'
'
'  P7 (term 12)  --------------- STOP ----- Pin3 (Term 4)
'
'  P6 (term 11)  --------------- PULSE ---- Pin4 (Term 3)
'
'                                   Pin2 / PWM (Term 5) ------- To TIP41, TIP122, IRLZ44
'                                                               ULN2803, L293DNE or similar
' 10K to +5 VDC pull up resistors on STOP and PULSE inputs
'
' Stamp to PWM PICAXE-08M
'
' copyright, P H Anderson, Baltimore, MD, Nov 8, '05

   Symbol PWMDuty = B0
   Symbol Counts = B1

   Symbol PWMDuty4 = W1 ' uses B2 & B3

'''''''''''''''''''

   Poke $8f, $71		   ' set OSCON register to 8 MHz

   Dirs = %00000100 ' Pin2 is output

   PWMDuty = 0				' initialize to zero

Main:
   SetInt %00000000, %00010000 ' interrupt on low on Pin4

   If Pin3 = 1 Then StopPWM

   ' otherwise

   PWMDuty4 = 4 * PWMDuty
   PWMOut 2, 255, PWMDuty4

Main2:
   Goto Main

StopPWM:
   PWMOut 2, 255, 0
   Goto Main2

Interrupt:
    Count 4, 200, Counts ' 100 ms window when running at 8 MHz
    PWMDuty = Counts
    SetInt %00000000, %00010000
WaitForOne:
    Pause 10
    If Pin4=0 then WaitForOne
' wait for Pin4 to go back to logic 1
    Return

Basic Stamp 2

The following program illustrates a test program which causes the PWM output of the PICAXE-08M to completely off, to 128/256 duty to 254/256 duty.

' Program PWM_STAMP.BS2
'
' Illustrates a Basic Stamp interface with a PICAXE-08M programmed with
' PWM firmware discussed above.
'
' The program begins with a high state on Pulse (P6).  The motor is held off
' for nominally two seconds with a high on the Stop (P7).
'
' The Duty cycle is then set to 128 by bringing Pulse (P6) low and then sending 128
' pulses within a 100 ms window.  This is followed by a delay to assure the 100 ms
' window has elapsed prior to returning the Pulse output to its normal high state.
'
' As the Stop output is low, the PWM Duty  at term 5 of the PICAXE is now 128 / 256.
' One can view this using and LED (with series 330 Ohm resistor) from the PICAXE PWM
' output to ground.  This is followed by a two second delay.
'
' The PWM Duty is then set to 254 / 256 causing a test LED to appear near full
' brilliance.
'
' This sequence of LED off, followed by 128/256 duty followed by 254/256 duty
' is continually repeated.
'
'  Basic Stamp          		               PICAXE-08M
'
'
'  P7 (term 12)  --------------- STOP ----- Pin3 (Term 4)
'
'  P6 (term 11)  --------------- PULSE ---- Pin4 (Term 3)
'
'                                           Pin2 / PWM (Term 5) ------- To TIP41, TIP122, IRLZ44
'                                                               ULN2803, L293DNE or similar
'
' 10K to +5 VDC pull up resistors on STOP and PULSE inputs
'
'
' copyright, Peter H Anderson, Baltimore, MD, Nov, '05

    N Var Byte

    High 6  ' Pulse output is high when idle

Top:

    High 7 ' Stop Output

    Pause 2000

    Low 6  ' low on Pulse to interrupt the PICAXE-08M
    For N = 1 to 128
       High 6  ' then send the number of pulses within 100 ms
       Low 6
    Next
    Pause 100  ' be sure the 100 ms window has elapsed
    High 6     ' done writing the PWMDuty to the PICAXE-08M

    Low 7	   ' low on Stop causes PWMOut at the specified duty


    Pause 2000 ' pause to admire


    Low 6      ' now adjust PWM Duty while motor is turning.
    For N = 1 to 254
       High 6   ' set to Duty of 254
       Low 6
    Next
    Pause 100   ' be sure the 100 ms window has elapsed
    High 6      ' done writing to the PICAXE

    Pause 2000  ' pause to admire


    Goto Top    ' repeat