The new expiremental version of AtomPro basic is available. Also here is a modified IRPD/Brat code that shows how to take advantage of the TimerW tick.
www.basicmicro.com/downloads/software/B … 017Exp.exe
This example code also shows how to use ONASMINTERRUPT
[code];USEASM con 0 ;uncomment to use ONASMINTERRUPT version of WKP handler
;System variables
righthip con p15
rightknee con p14
rightankle con p13
lefthip con p12
leftknee con p11
leftankle con p10
;calibrate steps per degree.
stepsperdegree fcon 166.6
;calibrate servos to “zero”.
;When properly homed in the servos are at 0 degrees
;the robot should be standing strait with
;irpd pointing up and AtomPro chip pointing forward.
righthip_start con -1000
rightknee_start con 0
rightankle_start con 0
lefthip_start con -1500
leftknee_start con -1500
leftankle_start con 0
;Interrupt init
ENABLEHSERVO
#ifndef USEASM
ONINTERRUPT WKPINT_3,handle_irpd
PMR5.bit3 = 1
ENABLE WKPINT_3
#else
ONASMINTERRUPT WKPINT,handle_irpd
PMR5.bit3 = 1
ENABLE WKPINT
#endif
command var byte
;End of System setup. Add user variables below this line.
;Init positions
;Note, movement subroutine arguments are Rightankle,Rightknee,Righthip,Leftankle,Leftknee,Lefthip,speed
gosub movement 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
low 1
low 2
pause 1000
lasttime var long
currenttime var long
diff var long
datacount var byte
irpd_data var word
command=0xFF
main
if(command<>0xFF)then
if(command = 1) then ;(Button 2) Walk Forwards
gosub movement -7.0, 20.0, 20.0, 7.0,-20.0,-20.0, 500.0]
gosub movement 7.0, 20.0, 20.0, -7.0,-20.0,-20.0, 500.0]
gosub movement 7.0,-20.0,-20.0, -7.0, 20.0, 20.0, 500.0]
gosub movement -7.0,-20.0,-20.0, 7.0, 20.0, 20.0, 500.0]
elseif(command = 4) ;(Button 5) Walk Backwards
gosub movement -7.0,-20.0,-20.0, 7.0, 20.0, 20.0, 500.0]
gosub movement 7.0,-20.0,-20.0, -7.0, 20.0, 20.0, 500.0]
gosub movement 7.0, 20.0, 20.0, -7.0,-20.0,-20.0, 500.0]
gosub movement -7.0, 20.0, 20.0, 7.0,-20.0,-20.0, 500.0]
elseif(command = 18) ;(Up Vol) long stride forward
gosub movement -12.0, 45.0, 45.0, 12.0,-45.0,-45.0, 750.0]
gosub movement 12.0, 45.0, 45.0,-12.0,-45.0,-45.0, 750.0]
gosub movement 12.0,-45.0,-45.0,-12.0, 45.0, 45.0, 750.0]
gosub movement -12.0,-45.0,-45.0, 12.0, 45.0, 45.0, 750.0]
elseif(command = 19) ;(Dn Vol) long stride backward
gosub movement -12.0,-45.0,-45.0, 12.0, 45.0, 45.0, 750.0]
gosub movement 12.0,-45.0,-45.0,-12.0, 45.0, 45.0, 750.0]
gosub movement 12.0, 45.0, 45.0,-12.0,-45.0,-45.0, 750.0]
gosub movement -12.0, 45.0, 45.0, 12.0,-45.0,-45.0, 750.0]
elseif(command = 2) ;(Button 3) Kick
gosub movement 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 500.0]
gosub movement 42.0, 0.0, 0.0,-14.0, 0.0, 0.0, 500.0]
gosub movement 0.0,-32.0, 41.0,-23.0, 0.0, 0.0, 500.0]
gosub movement 0.0, 24.0,-20.0,-23.0, 0.0, 0.0, 250.0]
gosub movement 0.0, 0.0, 0.0,-18.0, 0.0, 0.0, 500.0]
gosub movement 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 500.0]
sound 9,[50\3960]
pause 50
sound 9,[50\4400]
pause 50
sound 9,[50\3960]
elseif(command = 16) ;(Channel Up) Get up from front
gosub movement 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 500.0]
gosub movement 0.0, 90.0, 45.0, 0.0, 90.0, 45.0, 500.0]
gosub movement 40.0, 90.0,-37.0, 0.0, 90.0, 45.0, 500.0]
gosub movement 0.0, 90.0,-65.0, 0.0, 90.0,-65.0, 500.0]
pause 500
gosub movement 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 750.0]
sound 9,[50\4400]
pause 50
sound 9,[50\4400]
pause 50
sound 9,[50\3960]
elseif(command = 17) ;(Channel Dn) Get up from back
gosub movement 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,1000.0]
gosub movement 0.0,-90.0, 5.0, 0.0,-90.0, 5.0,1000.0]
gosub movement 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,1000.0]
pause 1000
gosub movement 0.0, 22.0,-80.0, 0.0, 22.0,-80.0,1000.0]
gosub movement 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,1000.0]
sound 9,[50\3960]
pause 50
sound 9,[50\3960]
pause 50
sound 9,[50\4400]
elseif(command = 0) ;(Button 1) Home Position
gosub movement 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 500.0]
sound 9,[50\4400]
pause 50
sound 9,[50\4400]
pause 50
sound 9,[50\4400]
elseif(command = 5) ;(Button 6) Headbutt
gosub movement 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 500.0]
gosub movement 0.0,-50.0,-90.0, 0.0,-50.0,-90.0, 500.0]
gosub movement 0.0, 32.0,-58.0, 0.0, 32.0,-58.0, 400.0]
pause 200
;gosub movement 0.0,-11.0, 7.0, 0.0,-11.0, 7.0, 500.0]
gosub movement 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 500.0]
sound 9,[50\4400]
pause 50
sound 9,[50\3960]
pause 50
sound 9,[50\3960]
elseif(command = 6) ;(Button 7) Turn
gosub movement 0.0,-35.0,-40.0, 0.0, 35.0, 37.0,500.0]
gosub movement 0.0, 35.0, 37.0, 0.0,-35.0,-40.0,500.0]
gosub movement -14.0, 35.0, 37.0, 20.0,-35.0,-40.0,500.0]
gosub movement -14.0, 35.0, 37.0, 20.0, 35.0, 37.0,500.0]
gosub movement 20.0, 35.0, 37.0,-14.0, 35.0, 37.0,500.0]
gosub movement 20.0,-35.0,-40.0,-14.0, 35.0, 37.0,500.0]
;gosub movement 0.0,-35.0,-70.0, 0.0, 35.0, 7.0,500.0]
;gosub movement 0.0, 35.0, 7.0, 0.0,-35.0,-70.0,500.0]
;gosub movement -14.0, 35.0, 7.0, 20.0,-35.0,-70.0,500.0]
;gosub movement -14.0, 35.0, 7.0, 20.0, 35.0, 7.0,500.0]
;gosub movement 20.0, 35.0, 7.0,-14.0, 35.0, 7.0,500.0]
;gosub movement 20.0,-35.0,-70.0,-14.0, 35.0, 7.0,500.0]
elseif(command = 3) ;(Button 4) Rest Position
gosub movement 0.0, 45.0, 45.0, 0.0, 45.0, 45.0, 500.0]
endif
Command=0xFF
endif
goto main
;Should never need to edit anything below this line. Add user subroutines above this and below main.
lefthippos var float
leftkneepos var float
leftanklepos var float
righthippos var float
rightkneepos var float
rightanklepos var float
last_lefthippos var float
last_leftkneepos var float
last_leftanklepos var float
last_righthippos var float
last_rightkneepos var float
last_rightanklepos var float
lhspeed var float
lkspeed var float
laspeed var float
rhspeed var float
rkspeed var float
raspeed var float
speed var float
longestmove var float
;movement [lefthippos,leftkneepos,leftanklepos,righthippos,rightkneepos,rightanklepos,speed]
movement [rightanklepos,rightkneepos,righthippos,leftanklepos,leftkneepos,lefthippos,speed]
if(speed<>0.0)then
gosub getlongest[lefthippos-last_lefthippos, |
leftkneepos-last_leftkneepos, |
leftanklepos-last_leftanklepos, |
righthippos-last_righthippos, |
rightkneepos-last_rightkneepos, |
rightanklepos-last_rightanklepos],longestmove
speed = ((longestmovestepsperdegree)/(speed/20.0))
gosub getspeed[lefthippos,last_lefthippos,longestmove,speed],lhspeed
gosub getspeed[leftkneepos,last_leftkneepos,longestmove,speed],lkspeed
gosub getspeed[leftanklepos,last_leftanklepos,longestmove,speed],laspeed
gosub getspeed[righthippos,last_righthippos,longestmove,speed],rhspeed
gosub getspeed[rightkneepos,last_rightkneepos,longestmove,speed],rkspeed
gosub getspeed[rightanklepos,last_rightanklepos,longestmove,speed],raspeed
else
lhspeed=0.0;
lkspeed=0.0;
laspeed=0.0;
rhspeed=0.0;
rkspeed=0.0;
raspeed=0.0;
endif
hservo [lefthip\TOINT (-lefthipposstepsperdegree) + lefthip_start\TOINT lhspeed, |
righthip\TOINT (righthipposstepsperdegree) + righthip_start\TOINT rhspeed, |
leftknee\TOINT (-leftkneeposstepsperdegree) + leftknee_start\TOINT lkspeed, |
rightknee\TOINT (rightkneeposstepsperdegree) + rightknee_start\TOINT rkspeed, |
leftankle\TOINT (-leftankleposstepsperdegree) + leftankle_start\TOINT laspeed, |
rightankle\TOINT (rightanklepos*stepsperdegree) + rightankle_start\TOINT raspeed]
hservowait [lefthip,righthip,leftknee,rightknee,leftankle,rightankle]
last_lefthippos = lefthippos
last_leftkneepos = leftkneepos
last_leftanklepos = leftanklepos
last_righthippos = righthippos
last_rightkneepos = rightkneepos
last_rightanklepos = rightanklepos
return
one var float
two var float
three var float
four var float
five var float
six var float
getlongest[one,two,three,four,five,six]
if(one<0.0)then
one=-1.0one
endif
if(two<0.0)then
two=-1.0two
endif
if(three<0.0)then
three=-1.0three
endif
if(four<0.0)then
four=-1.0four
endif
if(five<0.0)then
five=-1.0five
endif
if(six<0.0)then
six=-1.0six
endif
if(one<two)then
one=two
endif
if(one<three)then
one=three
endif
if(one<four)then
one=four
endif
if(one<five)then
one=five
endif
if(one<six)then
one=six
endif
return one
newpos var float
oldpos var float
longest var float
maxval var float
getspeed[newpos,oldpos,longest,maxval]
if(newpos>oldpos)then
return ((newpos-oldpos)/longest)*maxval
endif
return ((oldpos-newpos)/longest)*maxval
#ifndef USEASM
handle_irpd
enable
;Get current timer tick
mov.w @_TIMERWTICK:16,e0
mov.w @TCNT,r0
mov.l er0,@CURRENTTIME:16
;increment data bit count
datacount=datacount+1
;calc pulse width
diff=currenttime-lasttime
;save current time so we can calculate the pulse width of the next pulse
lasttime = currenttime
if(diff>44800 AND diff<49920)then ;If we get a start pulse reset datacount and irpd_data
datacount=0
irpd_data=0
elseif(diff>25600 and diff<30720) ;we got a "1" pulse
irpd_data.bit10=1
elseif(diff>16640 and diff<21760) ;we got a "0" pulse
irpd_data.bit10=0
else ;we got an invalid pulse so reset
datacount=0
endif
if(datacount=11)then
datacount=0
command = irpd_data&0x7F
endif
;get ready for next data bit
irpd_data=irpd_data>>1
resume
#else
BEGINASMSUB
handle_irpd
;save workspace registers
push.l er0
push.l er1
;Clear WKP int flag
mov.b @IWPR:8,r0l
and.b #0xC0,r0l
mov.b r0l,@IWPR:8
;re-enable ints to reduce possible glitching of HSERVO or other interrupts that may be enabled
andc #0x7F,ccr
;Get current timer tick
mov.w @_TIMERWTICK:16,e0
mov.w @TCNT,r0
mov.l er0,@CURRENTTIME:16
;increment data bit count
;datacount=datacount+1
mov.b @DATACOUNT:16,r0l
inc.b r0l
mov.b r0l,@DATACOUNT:16
;calc pulse width
;diff=currenttime-lasttime
mov.l @LASTTIME:16,er1
mov.l @CURRENTTIME:16,er0
;save current time so we can calculate the pulse width of the next pulse
;lasttime = currenttime
mov.l er0,@LASTTIME:16
sub.l er1,er0 ;er0 holds diff
;if(diff>44800 AND diff<49920)then ;If we get a start pulse reset datacount and irpd_data
; datacount=0
; irpd_data=0
;elseif(diff>25600 and diff<30720) ;we got a "1" pulse
; irpd_data.bit10=1
;elseif(diff>16640 and diff<21760) ;we got a "0" pulse
; irpd_data.bit10=0
;else ;we got an invalid pulse so reset
; datacount=0
;endif
cmp.l #44800,er0
ble NOT_START:8
cmp.l #49920,er0
bge NOT_START:8
xor.w r0,r0
mov.b r0l,@DATACOUNT:16
mov.w r0,@IRPD_DATA:16
bra FINISH_PULSE:8
NOT_START:
cmp.l #25600,er0
ble NOT_ONE:8
cmp.l #30720,er0
bge NOT_ONE:8
mov.w @IRPD_DATA:16,r0
bset #2,r0h
mov.w r0,@IRPD_DATA:16
bra FINISH_PULSE:8
NOT_ONE:
cmp.l #16640,er0
ble NOT_ZERO:8
cmp.l #21760,er0
bge NOT_ZERO:8
mov.w @IRPD_DATA:16,r0
bclr #2,r0h
mov.w r0,@IRPD_DATA:16
bra FINISH_PULSE:8
NOT_ZERO:
xor.b r0l,r0l
mov.b r0l,@DATACOUNT:16
FINISH_PULSE:
;if(datacount=11)then
; datacount=0
; command = irpd_data&0x7F
;endif
mov.b @DATACOUNT:16,r0l
cmp.b #11,r0l
bne FINISH_DATA:8
xor.b r0l,r0l
mov.b r0l,@DATACOUNT:16
mov.w @IRPD_DATA:16,r0
and.b #0x7F,r0l
mov.b r0l,@COMMAND:16
FINISH_DATA:
;get ready for next data bit
;irpd_data=irpd_data>>1
mov.w @IRPD_DATA:16,r0
shlr.w r0
mov.w r0,@IRPD_DATA:16
;restore workspace registers
pop.l er1
pop.l er0
rte
ENDASMSUB
#endif[/code]