CON bits = 14 'try different values from table here attenuation = 0 'try 0-4 averaging = 13 '2-power-n samples to compute average with VAR long mic PUB start return cognew(@asm_entry, @mic) PUB input(level) : result result := (mic - level) #> 0 DAT org asm_entry mov dira,asm_dira 'make pin 16 (ADC) output movs ctra,#16 'POS W/FEEDBACK mode for CTRA movd ctra,#17 movi ctra,#%01001_000 mov frqa,#1 mov asm_cnt,cnt 'prepare for WAITCNT loop add asm_cnt,asm_cycles :loop waitcnt asm_cnt,asm_cycles 'wait for next CNT value (timing is determinant after WAITCNT) mov asm_sample,phsa 'capture PHSA and get difference sub asm_sample,asm_old add asm_old,asm_sample add average,asm_sample 'compute average periodically so that djnz average_cnt,#:avgsame 'we can 0-justify samples mov average_cnt,average_load shr average,#averaging mov asm_justify,average mov average,#0 'reset average for next averaging :avgsame max peak_min,asm_sample 'track min and max peaks for triggering min peak_max,asm_sample djnz peak_cnt,#:pksame mov peak_cnt,peak_load mov x,peak_max 'compute min+12.5% and max-12.5% sub x,peak_min shr x,#3 mov trig_min,peak_min add trig_min,x mov trig_max,peak_max sub trig_max,x mov peak_min,bignum 'reset peak detectors mov peak_max,#0 :pksame cmp mode,#0 wz 'wait for negative trigger threshold if_z cmp asm_sample,trig_min wc if_z_and_c mov mode,#1 if_z jmp #:loop cmp mode,#1 wz 'wait for positive trigger threshold if_z cmp asm_sample,trig_max wc if_z_and_nc mov mode,#2 if_z jmp #:loop sub asm_sample,asm_justify 'justify sample to bitmap center y sar asm_sample,#attenuation 'this # controls attenuation (0=none) add asm_sample,#384 / 2 mins asm_sample,#0 maxs asm_sample,#384 - 1 wrlong asm_sample,par jmp #:loop asm_cycles long |< bits - 1 'sample time asm_dira long $00020000 'output mask average_cnt long 1 peak_cnt long 1 peak_load long 512 mode long 0 bignum long $FFFFFFFF average_load long |< averaging asm_justify res 1 trig_min res 1 trig_max res 1 average res 1 asm_cnt res 1 asm_old res 1 asm_sample res 1 asm_mask res 1 asm_data res 1 x res 1 y res 1 peak_min res 1 peak_max res 1