Use of the DACPin Command to Control a DC Motor
' Program MOTOR_1.BAS
'
' Interfaces with a driver (ULN2803) to control the speed of a DC
' Motor or to vary the brightness of an LED.
'
' May be simply modified for use with L293 or L298 H Bridge
' so as to change direction.
'
' P20, and 21 are used as inputs. Depression of push buttons on
' P20 and P21 causes the motor motor to turn faster or slower.
'
' P22 is used as an output to control the speed of the motor.
'
' Illustrates the use of DACpin command. This is the equivalent of
' the Parallax PWM command.
'
' Compile with SerialPort.Bas. SerialPort.Bas includes subroutines
' OpenSerialPort, PutB, PutByte, PutI, NewLine, etc. These are important
' tool in debugging.
'
' Note that for the duty cycle, an integer DutyInt is used. This
' enables testing as to whether the quantity is >255 or less than 0.
' Neither of these tests could be made if the duty were a byte.
'
' However, the duty cycle must be passed to DACpin as a byte. Thus, note
' that DutyInt is typecast as a byte using CByte
'
' Copyright, Peter H. Anderson, Baltimore, Sept, 99
Public Const PB_21 as byte = 21 ' Faster Pushbutton
Public Const PB_22 as byte = 22 ' Slower
Public Const MOTOR as byte = 23 ' Ouput to Motor Driver
Sub Main()
Dim DutyInt as Integer
Dim DutyByte as Byte
Dim DACcounter as Byte
Dim y as Byte
Dim N as Integer
Call OpenSerialPort(2, 9600) ' For debugging
DutyInt = 0
Do ' continually loop
Call PutPin(PB_21, bxInputPullup) ' configure push buttons as inputs
Call PutPin(PB_22, bxInputPullup) ' with internal pullup resistors
' y = GetPin(PB_21) ' Used for debugging
' Call PutB(y)
' Call NewLine()
If (GetPin(PB_21)= 0) then ' read Faster pushbutton
DutyInt = DutyInt + 2 ' increase the duty
If (DutyInt > 255) then ' limit highest value to 255
DutyInt = 255
End If
ElseIf (GetPin(PB_22) = 0) then ' if Slower pusbutton
DutyInt = DutyInt -2 ' decrease the duty
If (DutyInt < 0) then ' limit lowest val to 0
DutyInt = 0
End If
End If
DutyByte = CByte(DutyInt)
Call PutI (DutyInt)
Call NewLine()
For N = 1 TO 25
Call DACpin(MOTOR, DutyByte, DACcounter)
Next
Loop
End Sub