Here is the code after EddieB fine tuned it. It now runs the loop in a respectable 18mS. 8)
EddieB also told me about a really sick idea in a PM. Sorry Eddie this is too cool to keep to myself!
The long range PS2 controller is only a prototype still in development at this stage. There is no guarantee it will make it to a real product. But this is something anyone can do right now. Effectively the PS2 replaces the joysticks and or the keypad. Just need to mary the PS2 code with the PPM generating code in this program to get 3000’ range robot control.
[code]; code written by Jim Frye, modified by EddieB <- genius
; declare variables here
cha1 var word
cha2 var word
cha3 var word
cha4 var word
cha5 var word
cha6 var word
cha7 var word
a_key var bit
b_key var bit
c_key var bit
; create variables for averaging code
index var byte
buffer1 var word(8)
buffer2 var word(8)
buffer3 var word(8)
buffer4 var word(8)
sum1 var word
sum2 var word
sum3 var word
sum4 var word
; initialize each buffer element, each sum, and then index to 0
for index = 0 to 7
buffer1(index) = 542
buffer2(index) = 542
buffer3(index) = 542
buffer4(index) = 542
next
sum1 = 4336
sum2 = 4336
sum3 = 4336
sum4 = 4336
index = 0
; initialize unused channels outside the main loop
cha5=1500
cha6=1500
cha7=1500
; chirpy squeak kinda thing
sound 9, [100\880, 100\988, 100\1046, 100\1175]
; wake up the display module
serout 8,N9600,[12, 14]
; initialize the ppm output signal
low 15
; — top of main loop —
start:
; averaging expects that the a/d values are < 4096
; for each channel
; read the a/d
; subtract the previous value from 8 samples ago from the sum
; store the new value in the circular buffer
; add the new value to the sum
; divide the sum by 8 to get the average value
; convert joystick values 392 - 692 to servo values 1000uS - 2000uS
adin 16, cha1 ; right vertical
sum1 = sum1 - buffer1(index)
buffer1(index) = cha1
sum1 = sum1 + cha1
cha1 = sum1 / 8
cha1 = (((cha1*42)-6500)/10)
adin 17, cha2 ; right horizontal
sum2 = sum2 - buffer2(index)
buffer2(index) = cha2
sum2 = sum2 + cha2
cha2 = sum2 / 8
cha2 = (((cha2*42)-6500)/10)
adin 18, cha3 ; left vertical
sum3 = sum3 - buffer3(index)
buffer3(index) = cha3
sum3 = sum3 + cha3
cha3 = sum3 / 8
cha3 = (((cha3*42)-6500)/10)
adin 19, cha4 ; left horizontal
sum4 = sum4 - buffer4(index)
buffer4(index) = cha4
sum4 = sum4 + cha4
cha4 = sum4 / 8
cha4 = (((cha4*42)-6500)/10)
; finally increment the index and limit its range to 0 to 7.
index = (index + 1) & 7
; update the display module
branch (index & 3), [update1,update2,update3,update4]
update4:
serout 8, N9600, [16, 80, dec cha4]
goto makepulses
update3:
serout 8, N9600, [16, 64, dec cha3]
goto makepulses
update2:
serout 8, N9600, [16, 92, dec cha2]
goto makepulses
update1:
serout 8, N9600, [16, 76, dec cha1]
; goto makepulses
; build and send the ppm output
makepulses:
pulsout 15,800
pauseus ((cha12)-800)
pulsout 15,800
pauseus ((cha22)-800)
pulsout 15,800
pauseus ((cha32)-800)
pulsout 15,800
pauseus ((cha42)-800)
pulsout 15,800
pauseus ((cha52)-800)
pulsout 15,800
pauseus ((cha62)-800)
pulsout 15,800
pauseus ((cha7*2)-800)
pulsout 15,800
; do it again ad infinitum
goto start [/code]