The code up to "ENDSWITCH" can be used on its own to create a momentary and latching switch from a tap input.
Hopefully the comments explain how it works, if not just ask. If anyone's got any ideas for improvements, or alternative ways of doing it feel free to post them.
Code: Select all
;tap tempo
;pot 2 tap input
;output is a value between 0 and 1 written to taptempo register,
;corresponding to a delay of 0 to 1000ms if using the full delay memory
equ db reg0 ;debounce
equ mom reg1 ;momentary output of switch +1 high, -1 Low
equ latch reg2 ;latched output of switch +1 high, -1 low
equ ramp reg3 ;current value of rmpo, scaled to 0 to 1
equ taptempo reg4 ;taptempo value, 0 to 1
equ count 0.01 ;debounce counter
skp run,START
wldr rmp0,0.064,4096 ;set up rmp0
sof 0,0.999
wrax latch,0 ;set latch = 1 high
START:
;Switch Debouncing and pot filtering work around
ldax pot2 ;read pot2
sof 1,-0.5 ;level shift to -0.5 to 0.5
skp neg,DOWN ;if negative jump to DOWN
ldax db ;else high, read db
sof 1,count ;add count
wrax db,0 ;write new value to db
skp zro,ENDDB ;jump to ENDDB
DOWN:
ldax db ;read db
sof 1,-count ;deduct count
wrax db,0 ;write new value to db
ENDDB:
;latching switch, falling edge triggered flipflop
;Output of debounce routine of < -0.9 is low, > 0.9 is high, values in between
;are ignored and the switch does nothing, Schmitt trigger action.
ldax db ;read db
absa ;get absolute value
sof 1,-0.9 ;deduct 0.9 so only values < -0.9 or > 0.9 give a positive result
skp neg,ENDSWITCH ;if negative then jump to ENDSWITCH
ldax db ;read db
sof 1,-0.9 ;deduct 0.9
skp neg,LO ;if negative jump to LO, output of debounce is low
sof 0,0.999 ;else output of debounce is high
wrax mom,0 ;set mom to 1 (high)
skp zro,ENDSWITCH ;jump to ENDSWITCH
LO:
ldax mom ;read mom
skp neg,ENDSWITCH ;if it's negative then debounce was already low last time so do nothing, jump to ENDSWITCH
sof 0,-0.999 ;else mom was high last time so switch has only just been pressed (falling edge)
wrax mom,0 ;set mom to -1 (low)
ldax latch ;read latch
sof -1,0 ;invert, high becomes low, low becomes high
wrax latch,0 ;write to value to latch
ENDSWITCH:
;tap tempo, uses rmp0 as a 1 Hz rising ramp, runs whilst latch is low and is sampled and held when latch is high
ldax latch ;read latch
skp neg,LOW ;if negative jump to LOW
jam rmp0 ;else latch is high, jam rmp0 (reset to 0)
ldax ramp ;read ramp, will contain last value of rmp0 before latch went high
wrax taptempo,0 ;write to taptempo
skp zro,ENDTT ;jump to ENDTT
LOW:
sof 0,0.064
wrax rmp0_rate,1 ;set rmp0 rate to 1Hz
cho rdal,rmp0 ;read value of rmp0
sof -2,0.999 ;level shift to 0 to 1 rising ramp
wrax ramp,0 ;write to ramp
ENDTT:
ldax taptempo
wrax dacl,0