Hi,
Here is the code that I’m using. I’m also running it on an old machine (Windows 98 2nd edn) as it doesn’t work on the brand new Dell (XP) that we bought for it.
Regards,
Kris.
'This unit contains procedures and functions for use with the 'VELLEMAN Stepping Motor Card K8005'
'I2C BUS CONDITIONS
DECLARE SUB SelectI2cPrinterPort (PrinterNo)
DECLARE SUB I2CBusNotBusy ()
DECLARE FUNCTION BINNOT (Dec)
DECLARE SUB I2CInit ()
DECLARE SUB I2Cmasterclockpulse ()
DECLARE FUNCTION I2Cinput ()
DECLARE SUB I2Cstart ()
DECLARE SUB I2Coutput (SerData)
DECLARE SUB I2Cclockpulse ()
DECLARE SUB I2Cstop ()
DECLARE FUNCTION SHL (Dec, Positions)
DECLARE SUB SpeedTestText ()
DECLARE SUB SpeedTestNText ()
'RADIX CONVERSION
DECLARE FUNCTION BinToDec (BinNumber$)
DECLARE FUNCTION DecToBin$ (DecNumber)
'OUTPUT SUBROUTINES FOR ONE CARD
DECLARE SUB SendMotor (MotorNo, Commando, time, steps)
'STEPPING SUBROUTINES FOR ONE CARD
DECLARE SUB SendStepCCWFull (MotorNo, time, steps)
DECLARE SUB SendStepCCWHalf (MotorNo, time, steps)
DECLARE SUB SendStepCWFull (MotorNo, time, steps)
DECLARE SUB SendStepCWHalf (MotorNo, time, steps)
DECLARE SUB SendstepCW (MotorNo, time, steps)
DECLARE SUB SendstepCCW (MotorNo, time, steps)
DECLARE SUB SendStepHalf (MotorNo, time, steps)
DECLARE SUB SendStepFull (MotorNo, time, steps)
DECLARE SUB SendstepPING (MotorNo, time, steps)
' SETTING SUBROUTINES FOR ONE CARD
DECLARE SUB SendSetFree (MotorNo)
DECLARE SUB SendSetTake (MotorNo)
DECLARE SUB Sendstop (MotorNo)
DECLARE SUB SendSetCW (MotorNo)
DECLARE SUB SendSetCCW (MotorNo)
DECLARE SUB SendSetHalf (MotorNo)
DECLARE SUB SendSetFull (MotorNo)
DECLARE SUB SendSetCWFull (MotorNo)
DECLARE SUB SendSetCWHalf (MotorNo)
DECLARE SUB SendSetCCWFull (MotorNo)
DECLARE SUB SendSetCCWHalf (MotorNo)
' OUTPUT SUBROUTINES FOR ALL CARDS
DECLARE SUB SendAllMotors (Commando, time, steps)
' STEPPING SUBROUTINES FOR ALL CARDS
DECLARE SUB SendAllStepCCWFull (time, steps)
DECLARE SUB SendAllStepCCWHalf (time, steps)
DECLARE SUB SendAllStepCWFull (time, steps)
DECLARE SUB SendAllStepCWHalf (time, steps)
DECLARE SUB SendAllstepCW (time, steps)
DECLARE SUB SendAllstepCCW (time, steps)
DECLARE SUB SendAllStepHalf (time, steps)
DECLARE SUB SendAllStepFull (time, steps)
DECLARE SUB SendaLLstepPING (time, steps)
' SETTING SUBROUTINES FOR ALL CARDS
DECLARE SUB SendAllSetFree ()
DECLARE SUB SendAllSetTake ()
DECLARE SUB SendAllStop ()
DECLARE SUB SendAllSetCW ()
DECLARE SUB SendAllSetCCW ()
DECLARE SUB SendAllSetHalf ()
DECLARE SUB SendAllSetFull ()
DECLARE SUB SendAllSetCWFull ()
DECLARE SUB SendAllSetCWHalf ()
DECLARE SUB SendAllSetCCWFull ()
DECLARE SUB SendAllSetCCWHalf ()
'INPUT SUBROUTINES
DECLARE SUB Readmotor (MotorNo)
DECLARE SUB ReadAllMotors ()
'COMMON USED CONSTANTS AND VARIABLES
CONST MaxMotorCards = 15
COMMON SHARED Statusport, ControlPort, I2cBusDelay, Overwrite, WaitTime
DIM SHARED motorcardcode(0 TO MaxMotorCards)
FOR CardNo = 0 TO MaxMotorCards
motorcardcode(CardNo) = 192 + (2 * CardNo)
NEXT
DIM SHARED MotorCardData(0 TO MaxMotorCards)
I2CInit
'Example 6
SpeedTestText
'#################### DEBUG ##################################
PRINT ""; CardNo
Readmotor 0
IF MotorCardData(0) = 1 THEN PRINT "MotorCard 0 is ready to receive data"
IF MotorCardData(0) = 127 THEN PRINT "The memory of the stepper motor card 0 is full"
IF MotorCardData(0) = 255 THEN PRINT "The motor card 0 is not connected or powered on"
Readmotor 15
IF MotorCardData(15) = 1 THEN PRINT "MotorCard 15 is ready to receive data"
IF MotorCardData(15) = 127 THEN PRINT "The memory of the stepper motor card 15 is full"
IF MotorCardData(15) = 255 THEN PRINT "The motor card 15 is not connected or powered on"
PRINT "Press enter to continue"
INPUT a$
'#################### DEBUG ##################################
time = 10
steps = 1000
motor = 0
SendStepCWFull motor, time, steps
END
'#############################
FUNCTION BINNOT (Dec)
Temp$ = DecToBin(Dec)
Complement$ = ""
FOR i = 1 TO LEN(Temp$)
IF MID$(Temp$, i, 1) = "1" THEN
Complement$ = Complement$ + "0"
ELSE
Complement$ = Complement$ + "1"
END IF
NEXT
BINNOT = BinToDec(Complement$)
END FUNCTION
FUNCTION BinToDec (BinNumber$)
Weight = 1
Dec = 0 'Reset decimal number
IF BinNumber$ <> "00000000" THEN
FOR i = LEN(BinNumber$) TO 1 STEP -1
IF MID$(BinNumber$, i, 1) = "1" THEN
Dec = Dec + Weight 'If bit=1 then add weigth factor
END IF
Weight = Weight * 2 'Multiply weight factor by 2
NEXT
BinToDec = Dec 'Store result
ELSE
BinToDec = 0
END IF
END FUNCTION
'RADIX CONVERSION
FUNCTION DecToBin$ (DecNumber)
'Conversion of decimal number (0...255) to 8 bit binary string.
'--------------------------------------------------------------
Bin$ = ""
faktor = 128
IF DecNumber <> 0 THEN
FOR i = 1 TO 8
IF faktor > DecNumber THEN
Bin$ = Bin$ + "0"
ELSE
Bin$ = Bin$ + "1"
DecNumber = DecNumber - faktor
END IF
faktor = faktor \ 2
NEXT
DecToBin$ = Bin$
ELSE
DecToBin$ = "00000000"
END IF
END FUNCTION
SUB I2CBusNotBusy
OUT ControlPort, 4
END SUB
SUB I2Cclockpulse
OUT ControlPort, 12
FOR i = 0 TO I2cBusDelay
NEXT
OUT ControlPort, 4
FOR i = 0 TO I2cBusDelay
NEXT
OUT ControlPort, 12
FOR i = 0 TO I2cBusDelay
NEXT
END SUB
SUB I2CInit
SelectI2cPrinterPort 1
I2cBusDelay = 20
I2CBusNotBusy
Overwrite = 0
WaitTime = 300
ReadAllMotors
END SUB
FUNCTION I2Cinput
SerData = 0
FOR j = 1 TO 8
SerData = SHL(SerData, 1)
OUT ControlPort, 4
FOR i = 0 TO I2cBusDelay
NEXT
Inputdata = INP(Statusport) AND 16
IF Inputdata <> 0 THEN
SerData = SerData OR 1
END IF
OUT ControlPort, 12
FOR i = 0 TO I2cBusDelay
NEXT
NEXT
I2Cinput = SerData
END FUNCTION
SUB I2Cmasterclockpulse
OUT ControlPort, 14
FOR i = 0 TO I2cBusDelay
NEXT
OUT ControlPort, 6
FOR i = 0 TO I2cBusDelay
NEXT
OUT ControlPort, 14
FOR i = 0 TO I2cBusDelay
NEXT
OUT ControlPort, 12
FOR i = 0 TO I2cBusDelay
NEXT
END SUB
SUB I2Coutput (SerData)
Temp = SerData
Serdat$ = DecToBin(Temp)
FOR j = 1 TO 8
IF MID$(Serdat$, j, 1) = "1" THEN
DataOut = 12
ELSE
DataOut = 14
END IF
OUT ControlPort, DataOut
FOR i = 0 TO I2cBusDelay
NEXT
DataOut = INP(ControlPort) AND 7
OUT ControlPort, DataOut
FOR i = 0 TO I2cBusDelay
NEXT
DataOut = INP(ControlPort) OR 8
OUT ControlPort, DataOut
FOR i = 0 TO I2cBusDelay
NEXT
NEXT
END SUB
SUB I2Cstart
FOR i = 0 TO I2cBusDelay
NEXT
OUT ControlPort, 6
FOR i = 0 TO I2cBusDelay
NEXT
OUT ControlPort, 14
FOR i = 0 TO I2cBusDelay
NEXT
END SUB
SUB I2Cstop
REM OUT ControlPort, 14
FOR i = 0 TO I2cBusDelay
NEXT
REM OUT ControlPort, 6
FOR i = 0 TO I2cBusDelay
NEXT
OUT ControlPort, 4
FOR i = 0 TO I2cBusDelay
NEXT
END SUB
SUB ReadAllMotors
FOR counter = 0 TO MaxMotorCards
Readmotor counter
NEXT
END SUB
' INPUT SUBROUTINES
SUB Readmotor (CardNo)
FOR time = 1 TO I2CWaitTime: NEXT time
I2Cstart
DataVar = motorcardcode(CardNo) OR 1
I2Coutput DataVar
I2Cclockpulse
MotorCardData(CardNo) = I2Cinput
I2Cclockpulse
I2Cstop
END SUB
' I2C COMMUNICATION SUBROUTINES
SUB SelectI2cPrinterPort (PrinterNo)
SELECT CASE PrinterNo
CASE 0
Statusport = 957
ControlPort = 958
CASE 1
Statusport = 889
ControlPort = 890
CASE 2
StatusPost = 633
ControlPort = 634
END SELECT
END SUB
'OUTPUT SUBROUTINES FOR ALL CARDS
SUB SendAllMotors (Commando, time, steps)
Temp1 = Commando
Temp2 = time
Temp3 = steps
FOR counter = 0 TO MaxMotorCards
SendMotor counter, Temp1, Temp2, Temp3
NEXT
END SUB
SUB SendAllSetCCW
FOR counter = 0 TO MaxMotorCards
SendSetCCW counter
NEXT
END SUB
SUB SendAllSetCCWFull
FOR counter = 0 TO MaxMotorCards
SendSetCCWFull counter
NEXT
END SUB
SUB SendAllSetCCWHalf
FOR counter = 0 TO MaxMotorCards
SendSetCCWHalf counter
NEXT
END SUB
SUB SendAllSetCW
FOR counter = 0 TO MaxMotorCards
SendSetCW counter
NEXT
END SUB
SUB SendAllSetCWFull
FOR counter = 0 TO MaxMotorCards
SendSetCWFull counter
NEXT
END SUB
SUB SendAllSetCWHalf
FOR counter = 0 TO MaxMotorCards
SendSetCWHalf counter
NEXT
END SUB
'SETTING SUBROUTINES FOR ALL CARDS
SUB SendAllSetFree
FOR counter = 0 TO MaxMotorCards
SendSetFree counter
NEXT
END SUB
SUB SendAllSetFull
FOR counter = 0 TO MaxMotorCards
SendSetFull counter
NEXT
END SUB
SUB SendAllSetHalf
FOR counter = 0 TO MaxMotorCards
SendSetHalf counter
NEXT
END SUB
SUB SendAllSetTake
FOR counter = 0 TO MaxMotorCards
SendSetTake counter
NEXT
END SUB
SUB SendAllstepCCW (time, steps)
Temp1 = time
Temp2 = steps
FOR counter = 0 TO MaxMotorCards
SendstepCCW counter, Temp1, Temp2
NEXT
END SUB
'STEPPING SUBROUTINES FOR ALL CARDS
SUB SendAllStepCCWFull (time, steps)
Temp1 = time
Temp2 = steps
FOR counter = 0 TO MaxMotorCards
SendStepCCWFull counter, Temp1, Temp2
NEXT
END SUB
SUB SendAllStepCCWHalf (time, steps)
Temp1 = time
Temp2 = steps
FOR counter = 0 TO MaxMotorCards
SendStepCCWHalf counter, Temp1, Temp2
NEXT
END SUB
SUB SendAllstepCW (time, steps)
Temp1 = time
Temp2 = steps
FOR counter = 0 TO MaxMotorCards
SendstepCW counter, Temp1, Temp2
NEXT
END SUB
SUB SendAllStepCWFull (time, steps)
Temp1 = time
Temp2 = steps
FOR counter = 0 TO MaxMotorCards
SendStepCWFull counter, Temp1, Temp2
NEXT
END SUB
SUB SendAllStepCWHalf (time, steps)
Temp1 = time
Temp2 = steps
FOR counter = 0 TO MaxMotorCards
SendStepCWHalf counter, Temp1, Temp2
NEXT
END SUB
SUB SendAllStepFull (time, steps)
Temp1 = time
Temp2 = steps
FOR counter = 0 TO MaxMotorCards
SendStepFull counter, Temp1, Temp2
NEXT
END SUB
SUB SendAllStepHalf (time, steps)
Temp1 = time
Temp2 = steps
FOR counter = 0 TO MaxMotorCards
SendStepHalf counter, Temp1, Temp2
NEXT
END SUB
SUB SendaLLstepPING (time, steps)
Temp1 = time
Temp2 = steps
FOR counter = 0 TO MaxMotorCards
SendstepPING counter, Temp1, Temp2
NEXT
END SUB
SUB SendAllStop
FOR counter = 0 TO MaxMotorCards
Sendstop counter
NEXT
END SUB
' OUTPUT SUBROUTINES FOR ONE CARD
SUB SendMotor (MotorNo, Commando, time, steps)
IF Overwrite = 0 THEN
MotorCardData(MotorNo) = 55
WHILE (MotorCardData(MotorNo) <> 1) AND (MotorCardData(MotorNo) <> 255)
Readmotor (MotorNo)
WEND
END IF
FOR T = 1 TO I2CWaitTime: NEXT T
I2Cstart
Temp = motorcardcode(MotorNo)
I2Coutput Temp
I2Cclockpulse
Temp = Commando
I2Coutput Temp
I2Cclockpulse
Temp = time
I2Coutput Temp
I2Cclockpulse
Temp = (steps - (steps MOD 255)) / 255
I2Coutput Temp
I2Cclockpulse
Temp = steps MOD 255
I2Coutput Temp
I2Cclockpulse
I2Cstop
END SUB
SUB SendSetCCW (MotorNo)
Temp = MotorNo
SendMotor Temp, 96, 0, 0
END SUB
SUB SendSetCCWFull (MotorNo)
Temp = MotorNo
SendMotor Temp, 112, 0, 0
END SUB
SUB SendSetCCWHalf (MotorNo)
Temp = MotorNo
SendMotor Temp, 120, 0, 0
END SUB
SUB SendSetCW (MotorNo)
Temp = MotorNo
SendMotor Temp, 64, 0, 0
END SUB
SUB SendSetCWFull (MotorNo)
Temp = MotorNo
SendMotor Temp, 80, 0, 0
END SUB
SUB SendSetCWHalf (MotorNo)
Temp = MotorNo
SendMotor Temp, 88, 0, 0
END SUB
' SETTING SUBROUTINES FOR ONE CARD
SUB SendSetFree (MotorNo)
Temp = MotorNo
SendMotor Temp, 4, 0, 0
END SUB
SUB SendSetFull (MotorNo)
Temp = MotorNo
SendMotor Temp, 16, 0, 0
END SUB
SUB SendSetHalf (MotorNo)
Temp = MotorNo
SendMotor Temp, 24, 0, 0
END SUB
SUB SendSetTake (MotorNo)
Temp = MotorNo
SendMotor Temp, 6, 0, 0
END SUB
' STEPPING PROCEDURES ONE CARD
SUB SendstepCCW (MotorNo, time, steps)
Temp = MotorNo
Temp1 = time
Temp2 = steps
IF steps = 0 THEN SendMotor Temp, 231, Temp1, Temp2 ELSE SendMotor Temp, 230, Temp1, Temp2
END SUB
SUB SendStepCCWFull (MotorNo, time, steps)
Temp = MotorNo
Temp1 = time
Temp2 = steps
IF steps = 0 THEN SendMotor Temp, 247, Temp1, Temp2 ELSE SendMotor Temp, 246, Temp1, Temp2
END SUB
SUB SendStepCCWHalf (MotorNo, time, steps)
Temp = MotorNo
Temp1 = time
Temp2 = steps
IF steps = 0 THEN SendMotor Temp, 255, Temp1, Temp2 ELSE SendMotor Temp, 254, Temp1, Temp2
END SUB
SUB SendstepCW (MotorNo, time, steps)
Temp = MotorNo
Temp1 = time
Temp2 = steps
IF steps = 0 THEN SendMotor Temp, 199, Temp1, Temp2 ELSE SendMotor Temp, 198, Temp1, Temp2
END SUB
SUB SendStepCWFull (MotorNo, time, steps)
Temp = MotorNo
Temp1 = time
Temp2 = steps
IF steps = 0 THEN SendMotor Temp, 215, Temp1, Temp2 ELSE SendMotor Temp, 214, Temp1, Temp2
END SUB
SUB SendStepCWHalf (MotorNo, time, steps)
Temp = MotorNo
Temp1 = time
Temp2 = steps
IF steps = 0 THEN SendMotor Temp, 223, Temp1, Temp2 ELSE SendMotor Temp, 222, Temp1, Temp2
END SUB
SUB SendStepFull (MotorNo, time, steps)
Temp = MotorNo
Temp1 = time
Temp2 = steps
IF steps = 0 THEN SendMotor Temp, 151, Temp1, Temp2 ELSE SendMotor Temp, 150, Temp1, Temp2
END SUB
SUB SendStepHalf (MotorNo, time, steps)
Temp = MotorNo
Temp1 = time
Temp2 = steps
IF steps = 0 THEN SendMotor Temp, 159, Temp1, Temp2 ELSE SendMotor Temp, 158, Temp1, Temp2
END SUB
SUB SendstepPING (MotorNo, time, steps)
Temp = MotorNo
Temp1 = time
Temp2 = steps
IF steps = 0 THEN SendMotor Temp, 135, Temp1, Temp2 ELSE SendMotor Temp, 134, Temp1, Temp2
END SUB
SUB Sendstop (MotorNo)
Temp = MotorNo
Temp1 = Overwrite
Overwrite = 1
SendMotor Temp, 0, 0, 0
Overwrite = Temp1
END SUB
FUNCTION SHL (Dec, Positions)
Temp$ = RIGHT$(DecToBin(Dec) + STRING$(Positions, "0"), 8)
SHL = BinToDec(Temp$)
END FUNCTION
SUB SpeedTestNText
CLS
I2CWaitTime = 2000
MotorCardData(0) = 2
I2cBusDelay = 0
counter = 0
DO
counter = counter + 1
I2cBusDelay = counter
Readmotor (0)
LOOP UNTIL ((MotorCardData(0) = 1) OR (counter = 32000))
DO
reliable = 1
I2cBusDelay = counter
FOR counter2 = 1 TO 30
Readmotor (0)
IF MotorCardData(0) <> 1 THEN reliable = 0
NEXT counter2
IF reliable <> 1 THEN counter = counter + 1
LOOP UNTIL reliable = 1
END SUB
SUB SpeedTestText
CLS
I2CWaitTime = 2000
PRINT "TEST PROGRAM K8005 FOR QuickBasic"
PRINT "***********************************"
PRINT "I2C bus is free, set the K8005 to work as steppermotorcard zero and switch the unit on"
PRINT "Press enter when ready"
INPUT a$
MotorCardData(0) = 2
PRINT "Speed testing"
I2cBusDelay = 0
counter = 0
DO
counter = counter + 1
I2cBusDelay = counter
Readmotor (0)
LOOP UNTIL ((MotorCardData(0) = 1) OR (counter = 32000))
PRINT "Value found for the I2CBusDelay", counter
DO
reliable = 1
PRINT "Testing reliability of value", counter
I2cBusDelay = counter
FOR counter2 = 1 TO 30
Readmotor (0)
IF MotorCardData(0) <> 1 THEN reliable = 0
NEXT counter2
IF reliable = 1 THEN PRINT "Value ", counter, " is OK as value for I2CBusDelay"; ELSE counter = counter + 1
LOOP UNTIL reliable = 1
PRINT "Press enter to continue"
INPUT a$
END SUB