Include "modedefs.bas" Define OSC 20 datap var portd.7 ' Data pin (I/O) datapin var portd.7 clk var portd.6 ' Clk pin (output) PakReset var portd.5 output clk output datap output PakReset HIGH PakReset DEFINE HSER_RCSTA 90h DEFINE HSER_TXSTA 20h DEFINE HSER_BAUD 19200 '------------- fpx var word ' Integer used by some routines fpb var byte chan var byte register var byte TimeOn var byte '--Register names DURLOW con %000 DURHIGH con %001 HIGHDUR con %010 LOWDUR con %011 PSCALE con %100 PSBASE con %101 N con %110 CTRL con %111 clearreg var bit clearchan var bit i var byte iobyte var byte '-------------- 'Configure Ports TRISD = %00000000 ' Set port B as output (for h bridge) PortD = %00000000 TRISC = %11111111 'set port C as input for switches OnLED var PortA.0 Debug1 var PortA.1 Debug2 var PortA.2 Debug3 var PortA.4 M_Back var byte M_Front var byte pwmL var byte pwmR var byte pwmStop var byte UpReachTime var byte DownReachTime var byte BetweenStepsTime var byte cycles var byte PTime var byte TUp var byte TDown var byte TBetween var byte '################ ButtonBackL var PortC.0 ButtonBackR var PortC.2 ButtonFrontL var PortC.1 ButtonFrontR var PortC.3 PrevBackButton var byte PrevFrontButton var byte PrevBackButton = 99 PrevFrontButton = 99 pwmL = 200 pwmR = 100 pwmStop = 150 M_Back = 0 M_Front = 1 PTime = 10 TUp = 200 TDown = 200 TBetween = 500 '###################### '###################### gosub LR gosub freset ' always reset! gosub freset ' always reset! '####--flash LED--#### GOSUB FlashLED '########################################################################## '###########################--start walking--############################## '########################################################################## hi: if ButtonBackL = 1 AND ButtonBackR = 0 AND PrevBackButton != 1 then ' GOSUB LR 'reset pause 10 TimeOn = 200 chan=M_Back GOSUB Execute PrevBackButton = 1 endif if ButtonBackR = 1 AND ButtonBackL = 0 AND PrevBackButton != 0 then ' GOSUB LR 'reset pause 10 TimeOn = 100 chan=M_Back GOSUB Execute PrevBackButton = 0 endif if ButtonBackL = 0 AND ButtonBackR = 0 AND PrevBackButton != 3 then ' GOSUB LR 'reset pause 10 TimeOn = 150 chan=M_Back GOSUB Execute PrevBackButton = 3 endif '########################################################## '########################################################## if ButtonFrontL = 1 AND ButtonFrontR = 0 AND PrevFrontButton != 1 then ' GOSUB LR 'reset pause 10 TimeOn = 200 chan=M_Front GOSUB Execute PrevFrontButton = 1 endif if ButtonFrontR = 1 AND ButtonFrontL = 0 AND PrevFrontButton != 0 then ' GOSUB LR 'reset pause 10 TimeOn = 100 chan=M_Front GOSUB Execute PrevFrontButton = 0 endif if ButtonFrontL = 0 AND ButtonFrontR = 0 AND PrevFrontButton != 3 then ' GOSUB LR 'reset pause 10 TimeOn = 150 chan=M_Front GOSUB Execute PrevFrontButton = 3 endif '########################################################## '########################################################## goto hi 'restart walking loop '################################################################################### '################################################################################### Execute: '--------- 'clearreg=0 'clearchan=0 register=1 fpx=TimeOn 'for instance, if TimeOn=50000 then 500 ms on and off = 500ms period = 1Hz gosub FCommand pause 10 '--------- register=2 fpx= 2600 gosub FCommand pause 10 '--------- fpb=3 gosub FJam 'turn on pulse operation pause 10 '---------- return LR: 'stands for Lee Reset Low PakReset PAUSE 10 HIGH PakReset Pause 25 return '##################################################################### '##--End of your code -- everything from here down is library code--## '##################################################################### ' Reset the Pak8 I/O FReset: LOW DATAP LOW CLK pauseus 50 ' might be able to reduce these pauses if necessary HIGH CLK pauseus 50 HIGH DATAP pauseus 50 LOW CLK pauseus 50 return ' 0 is 0, 1 is 1, 2 is hi-z, 3 is normal ' note: 3 enables channel to run! FJam: fpb=chan + (fpb<<3)+%01000000 goto FSendByte FCommand: ' inputs chan = channel, register = register, clearreg and clearchan fpb=(register<<3) + chan gosub FSendByte fpb=fpx.lowbyte gosub FSendByte fpb=fpx.highbyte goto FSendByte FReadWord: 'pauseus 8 ' wait a bit for the result to be ready 'gosub shiftinput 'fpx.lowbyte=iobyte 'gosub shiftinput 'fpx.highbyte=iobyte return FTotalReset: fpb=$FF iobyte=fpb gosub shiftoutput return FSendByte: iobyte=fpb gosub shiftoutput return FReadByte: pauseus 8 gosub shiftinput fpb=iobyte return FPrescale: ' set fpb to the prescale constant fpb = fpb + $C0 goto FSendByte '################################################### '#########--homemade shiftin/out commands--######### '################################################### pakclock con 3 ' 1*10 = 10us, this is > the 6us minimum needed for Pak. @ 4MHz, pulsout unit is 10us (adjust for your clock) pakclocklow con 3 ' 3uS low 6uS + 3uS + 1uS hold = 10uS = 100kbps shiftoutput: ' sends iobyte output datapin for i=0 to 7 ' Set data pin to 0 or 1 low datapin if (iobyte & $80) <> $80 then goto so0 high datapin so0: iobyte=iobyte<<1 ' shift byte left pauseus pakclocklow pulsout clk,pakclock pauseus 1 next pauseus 50 ' give it some time to process return shiftinput: input datapin iobyte=0 for i=0 to 7 iobyte=iobyte<<1 ' shift left iobyte=iobyte | datap pauseus pakclocklow pulsout clk,pakclock pauseus 1 ' hold data a tick next return LeesReset: ' disable PAK channels 0,1, and 2 chan = 1 register = 6 fpx = 1 gosub FCommand pause 100 chan=2 register=6 fpx=1 gosub FCommand pause 100 ' enable PAK channels 0,1, and 2 chan=0 fpb=3 gosub FJam pause 100 chan=1 fpb=3 gosub FJam 'chan=2 'fpb=3 'gosub fjam pause 3000 return FlashLED: HIGH OnLED PAUSE 100 LOW OnLED PAUSE 100 HIGH OnLED PAUSE 100 LOW OnLED PAUSE 100 HIGH OnLED PAUSE 100 LOW OnLED return