PWM Motor Control - PICAXE-18X

copyright, Peter H Anderson, Baltimore, MD, Mar, '04


Introduction

This was prepared for a Pre College Initiative sponsored by the National Society of Black Engineers (NSBE) at Morgan State University involving three groups of 25 high school students for an hour. I briefly spoke of the use of microcontrollers, generally described the following program and the students then entered the program using 12 setups in my lab. Although they did no design, they were impressed, and particularly impressed when the PICAXE was disconnected from the PC and they could see it was truly a standalone processor.

Over the course of the exercise, I lost two PICAXE-18X due to little fingers pulling components out of the board. However, the cost ($15.00) was far less than loosing two Parallax Basic Stamps.

The setup consisted of two pushbuttons on Pin0 and Pin1 and a TIP41 transistor driving a DC motor on Out3 of the PICAXE-18X. There was also an LED on Out0 which was periodically flashed.

Details

The PICAXE-18X provides a single PWM output and the -28X and -40X each provide two outputs. The PWM differs from that used by Parallax Basic Stamp. The Stamp uses an algorithm which causes the output to on average be high for duty / 256 of the time. The result is a very high frequency pulse train which may not propagate through an optocoupler. In addition, the Parallax PWM must be continually refreshed. The PICAXE uses true PWM, bringing the output high for duty amount of time and then low for the balance of the period which permits the user to use an optocoupler. It also operates continuously in the background and need not be refreshed.

In the following, I opted for a period of 250 (250 usec or 4.0 kHz). Thus, the duty may be set over the range of 0 - 999.



' PWM_1.Bas
'
' Momentary pushbuttons on Pin0 and Pin1.  LED on Out0.  TIP41 NPN transistor to DC Motor
' on Out3.
'
' Motor starts at medium speed.
'
' If one pushbutton is depressed the Duty is decreased, slowing the motor.  If the other
' is depressed, the Duty is increased, speeding the motor.  If both are depressed, the motor
' is stopped.
'
' LED on Out0 flashed each time through the loop.
'
' Peter H Anderson, Baltimore, Mar, '04


Symbol Duty = W0

   Duty = 400		' start with medium duty
   PWMOut 3, 249, Duty


TOP:

   GoSub Flash		' flash the LED on Out0

   SerTxD (#Duty, 13, 10)	' used for debugging student programs

   If Pin1 = 0 and Pin0 = 0 Then Stop	' if both pushbuttons depressed

   If Pin1 = 0 and Pin0 = 1 Then Slower	' if one depressed

   If Pin1 = 1 and Pin0 = 0 Then Faster	' if other depressed

							' no change if neither depressed.

   GoTo TOP						' continually loop


Stop:

   PWMOut 3, 0, Duty				' zero the period

   GoTo TOP

Slower:
   If Duty < 300 Then Top	' minimum floor

   Duty = Duty - 15		' decrease duty
   PWMOut 3, 249, Duty
   Pause 100

   GoTo TOP

Faster:
   If Duty > 984 Then Top

   Duty = Duty + 15		' increase duty
   PWMOut 3, 249, Duty
   Pause 100

   GoTo TOP


Flash:				' flash LED on Out0

   High 0
   Pause 100
   Low 0
   Pause 100
   Return


Modifying the Period.

As noted in the above, the period is 250 us or 4.0 kHz which leads to an annoying whine. One may easily double or quadruple this by adjusting the period to 125 or 63 resulting in 8.0 and 16.0 kHz.

After the exercise, I used the following to poke the T2CON register so as to provide a clock pre-scale of either 4 or 16.

	PWMOut 3, 249, Duty
	Poke $12, $05		' provides a pre-scale of 1:4


	PWMOut 3, 249, Duty
	Poke $12, $06		' provides a pre-scale of 1:16
The result is frequencies of 1.0 kHz and 250 Hz,