Lights, ASM, Beethoven
I added three LED's to my Stingray to indicate ping sensor action. I also read about using the counters, and I added a piezo speaker, so I figured I'd apply the knowledge with a taste of Beethoven. I also rolled the ping code. This is turning out to be a good learning experience for me.
DAT
org
sensor_loop
:sound mov items,#24 ' # items in notes array
or ctra,_nco ' set nco mode
mov index,#notes ' address first item
or ctra,#6 ' set output pin
:load movs :get_freq,index ' move index address to :get_freq label
or dira,s_pin ' set pin to output
:get_freq mov frqa,0-0 ' move notes[noparse][[/noparse]index] into frqa
mov s_delay,#10 ' plays slower if pings don't detect obstacle
:delay_note call #three_pings ' play note while checking pings
' <- add more sensors here
djnz s_delay,#:delay_note ' lower s_delay if adding more sensors
andn dira,s_pin ' set pin to input
add index,#1 ' increment index to next item
djnz items,#:load ' decrement items, jump if not zero
jmp #sensor_loop
' end sensor_loop =============================
three_pings mov pin,#1 ' first ping pin
mov addr,par ' get distance variable address
:get_ping or dira,pin ' set pin to output
or outa,pin ' set high
mov time,cnt ' need 2-5 us delay
add time,delay
waitcnt time,delay
andn outa,pin ' clear pin
andn dira,pin ' set pin to input
waitpne null,pin ' wait for pin to go high
mov cnt1,cnt ' store count
waitpeq null,pin ' wait for pin to go low
mov cnt2,cnt ' store count2
sub cnt2,cnt1 ' calculate ping time
shr cnt2,#7
wrlong cnt2,addr ' write ping time
shl pin,#1 ' next ping sensor pin
add addr,#4 ' move to next long
cmp pin,#5 wc ' inner loop if carry
if_c jmp #:get_ping ' if carry, pin < 5
three_pings_ret ret ' all pings returned distance
' Notes to frequencies
' C6 C#6 D6 Eb6 E6 F6 F#6 G6 Ab6 A6 Bb6 B6
'56_184, 59_524, 63_066, 66_814, 70_786, 74_995, 79_456, 84_181, 89_186, 94_489, 100_108, 105_629
' C7 C#7 D7 Eb7 E7
'112_528, 119_049, 126_128, 133_628, 141_574
notes long 141_574, 133_628, 141_574, 133_628, 141_574, 105_629, 63_066, 56_184, 94_489
long 70_786, 94_489, 112_528, 141_574, 94_489, 105_629, 70_786, 89_186, 70_786
long 89_186, 105_629, 112_528, 70_786, 94_489, 141_574
s_pin long 1 << 6 ' sound pin
_nco long %00100 << 26 ' nco counter
index long 0 ' index for notes array
null long 0
delay long 160 ' ping trigger delay
s_delay res 1 ' sound delay
items res 1 ' number of items in notes array
pin res 1 ' ping pin
cnt1 res 1 ' timing counters
cnt2 res 1
addr res 1
time res 1
fit

Comments
' stairway to heaven - led zeppelin notes long 094_489, 112_367, 141_574, 168_360, 089_186, 141_574, 112_367, 212_121 long 084_180, 141_574, 112_367, 112_367, 063_064, 126_128, 188_978, 149_992 long 074_996, 112_367, 094_489, 112_367, 141_574, 112_367, 094_489, 112_367 long 106_060, 094_489, 094_489, 094_489, 094_489, 094_489, 149_992, 141_574 { little wing - jimi hendrix notes long 094_489, 063_064, 070_787, 070_787, 070_787, 084_180, 212_121, 141_574 long 070_787, 141_574, 212_121, 126_128, 126_128, 188_978, 212_121, 188_978 long 084_180, 212_121, 126_128, 084_180, 084_180, 168_360, 084_180, 070_787 long 168_360, 094_489, 126_128, 094_489, 094_489, 094_489, 188_978, 168_360 long 188_978, 070_787 }Needless to say, they still aren't happy with my choices.
sensor_loop or ctra,_nco ' set nco mode :sound mov index,#notes ' address first item mov items,#32 ' # items in notes array :load movs :get_freq,index ' move index address to :get_freq label or dira,s_pin ' set pin to output :get_freq mov frqa,0-0 ' move notes[noparse][[/noparse]index] into frqa mov s_delay,#10 ' plays slower if pings don't detect obstacle :delay_note call #three_pings ' play note while checking pings ' <- add more sensors here djnz s_delay,#:delay_note ' lower s_delay if adding more sensors andn dira,s_pin ' set pin to input add index,#1 ' increment index to next item djnz items,#:load ' decrement items, jump if not zero jmp #:sound ' sensor_loop_ret ============================= three_pings mov pin,#1 ' first ping pin mov addr,par ' get distance variable address :get_ping or dira,pin ' set pin to output or outa,pin ' set high mov time,cnt ' 2-5 uS trigger add time,delay waitcnt time,delay andn outa,pin ' clear pin andn dira,pin ' set pin to input waitpne null,pin ' wait for pin to go high mov cnt1,cnt ' store count waitpeq null,pin ' wait for pin to go low mov cnt2,cnt ' store count2 sub cnt2,cnt1 ' calculate ping time shr cnt2,#7 wrlong cnt2,addr ' write ping time shl pin,#1 ' next ping sensor pin add addr,#4 ' move to next long cmp pin,#5 wc ' inner loop if carry if_c jmp #:get_ping ' if carry, pin < 5 three_pings_ret ret ' all pings returned distance ' stairway to heaven - led zeppelin notes long 094_489, 112_367, 141_574, 168_360, 089_186, 141_574, 112_367, 212_121 long 084_180, 141_574, 112_367, 112_367, 063_064, 126_128, 188_978, 149_992 long 074_996, 112_367, 094_489, 112_367, 141_574, 112_367, 094_489, 112_367 long 106_060, 094_489, 094_489, 094_489, 094_489, 094_489, 149_992, 141_574 s_pin long 1 << 6 ' sound pin _nco long %00100 << 26 | 6 ' nco counter + sound pin index long 0 ' index for notes array null long 0 delay long 160 ' ping trigger delay s_delay res 1 ' sound delay items res 1 ' number of items in notes array pin res 1 ' ping pin cnt1 res 1 ' timing counters cnt2 res 1 addr res 1 time res 1 fit