| Posted: 08:10am 22 May 2025 |
|
|
|
A couple of other encoder decoding options from Peter and TassyJim & Volhout.
'Here is MatherP's patented switch-bounce immune rotary encoder I/F code
option default integer const false=0 const true=1 const in0=3 'encoder pins pulled high so should be reading high in detent positions, centre pin connected to ground const in1=2 'swap the pin definitions as required to get the correct sense of rotation ' init: setpin in0,intB,rint 'enable interrupts on both edges of both pins setpin in1,intB,rint rotatedright=false rotatedleft=false resetenc=true value=0 lastvalue=0 print value ' MAIN: Do if value <> lastvalue then print value lastvalue=value endif Loop end ' sub rint: pin0=NOT pin(in0) 'reverse sense to get positive logic pin1=NOT pin(in1) if pin0 then if pin1 and not resetenc then 'in0 and in1 both active if rotatedleft and not rotatedright then value=value-1 rotatedleft=false endif if rotatedright and not rotatedleft then value=value+1 rotatedright=false endif else 'only in0 active if resetenc and not rotatedleft then rotatedright=true resetenc=false endif endif else if pin1 then 'only in1 active if resetenc and not rotatedright then rotatedleft=true resetenc=false endif else 'both off so reset rotatedleft=false rotatedright=false resetenc=true endif endif if pin(in0)=pin0 or pin(in1)=pin1 then goto rint 're-enter if another change has happened almost immediately ireturn
' simple encoder reading TassyJim Nov 2022, updated Volhout SetPin 2, DIN, PULLUP' setup RB as an input SetPin 1, INTB, RInt, PULLUP' setup an interrupt when RA goes high Do ' < main body of the program > Loop
Sub RInt ' Interrupt to decode the encoder output ' If Pin(2) = 1 Then If Pin(2) = Pin(1) Then Value = Value + 1 ' clockwise rotation Else Value = Value - 1 ' anti clockwise rotation EndIf Print Value End Sub
|