fl hex { PropForth4.5 Rotary Encoder driver 16/07/2011 12:29:42 PullUpresistor 10kohm between A_phase and 3.3V ----------------------- | | A_phase |------------- P12 |Rotary Encoder Common |------------- Gnd | B_phase |------------- P13 ----------------------- | PullUpresistor 10kohm between B_phase and 3.3V ----------> CW 20132013201320 <-- 2bit binary A_phase 00110011001100 B_phase 10011001100110 CCW <-------- prev current status 0 0 stop 0 1 CW 0 2 CCW 0 3 invalid 1 0 CCW 1 1 stop 1 2 invalid 1 3 CW 2 0 CW 2 1 invalid 2 2 stop 2 3 CCW 3 0 invalid 3 1 CCW 3 2 CW 3 3 stop } c wconstant A_phase d wconstant B_phase wvariable encoder_tbl 0 c, 1 c, 2 c, f c, 2 c, 0 c, f c, 1 c, 1 c, f c, 0 c, 2 c, f c, 2 c, 1 c, 0 c, wvariable prev 0 prev W! wvariable current variable counter 0 counter L! \ read rotary_encoder ( -- n1 ) n1:read value : read_encoder ina COG@ A_phase rshift 3 and ; \ update counter for rotary_encoder ( -- ) : encoder read_encoder dup \ get current encoder_data prev W@ 4 u* encoder_tbl 2+ + + C@ \ get CW/CCW/stop from table 1 over = if counter L@ 1+ counter L! else 2 over = if counter L@ 1- counter L! else f over = if ." sampling for encoder-read is too slow or output for encoder is too fast." cr thens drop prev W! ; : test1 0 counter L! read_encoder prev W! \ read initial value 2000 0 do encoder counter L@ . cr loop ; : test2 0 counter L! read_encoder prev W! \ read initial value 64 0 do 64 delms encoder counter L@ . cr loop ; decimal