mainTitle = "Rotary Encoder"; 
$this->asideSection = "Analyse et exploitation des signaux en provenance de l'encodeur rotatif"; 
?> 
\ ****************************************************************************** 
\ Rotary encoder 
\ 
\ Forth version: Flash Forth (http://flashforth.com/) 
\ author: M PETREMANN 
\ Creation:     23 oct. 2019 
\ Modification: 31 oct. 2019 
\ ****************************************************************************** 
 
-rotEnc 
marker -rotEnc 
 
34 constant PORTA	\ Port A Data Register 
33 constant DDRA	\ Port A Data Direction Register 
32 constant PINA	\ Port A Input Pins 
 
%00000001 constant pin78 \ pin 78 connector 22 PA0 
%00000010 constant pin77 \ pin 77 connector 23 PA1 
 
: init-ddra-input ( mask ---) 
    DDRA mclr    \ init PORT A to input for PA0 and PA1 
  ; 
 
: pin@ ( pin PINx --- fl) 
    c@   swap and 
    if    true 
    else  false  then ; 
 
: PA0@ ( --- fl) 
    pin78 PINA pin@ ; 
 
: PA1@ ( --- c) 
    pin77 PINA pin@ ; 
 
variable oldPA0 
variable oldPA1 
 
variable counter 
 
: getRotation ( PA0 PA1 --- direction ) 
    over oldPA1 @ <>  over oldPA0 @ =  and 
    if   -1 counter +!  then 
    over oldPA1 @ =   over oldPA0 @ <> and 
    if    1 counter +!  then 
    oldPA1 !   oldPA0 ! 
  ; 
 
: inits ( ---) 
    pin78 pin77 or  init-ddra-input 
    PA0@ oldPA0 ! 
    PA1@ oldPA1 ! 
  ; 
 
: changedPA01? ( PA0 PA1 --- fl) 
    oldPA1 @ <> swap oldPA0 @ <> or 
  ; 
 
: getR ( ---) 
    inits 
    begin 
        PA0@ PA1@ 
        2dup changedPA01? 
        if 
    \        2dup swap . . cr \ pour traçage visuel 
            getRotation 
            counter @ . cr \  
        else 
            2drop 
        then 
    key? until ;