It may seem a lot harder than it actually is.
Try this. We're using Timer0 configured as a counter. When your encoder
makes a transition on RA4/TOCKI (Timer0 clock input pin), it increments
the Timer0 count.
Real simple. Runs in the background all on its own, etc,,.
Code:
TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
TRISB.0 = 0 ' RB0 = output for LED
CMCON = 7 ' All digital
' Assign prescaler to WDT for 1:1 prescale on TMR0
' TMR0 clock will be from your external input on RA4/T0CKI
' Increment on high-to-low transitions
OPTION_REG = %00111000
' If you prefer, then increment on low-to-high transitions
' OPTION_REG = %00101000
Main:
TMR0 = 0 ' Clear TMR0 count before start
Loop:
WHILE TMR0 = 0 ' Wait for high-to-low transition
WEND ' on RA4/T0CKI
PORTB.0 = 1 ' LED on to indicate transition seen
PAUSE 500
PORTB.0 = 0 ' LED off
GOTO Main ' Start over
END
Timer0 configured as a counter can count your encoder transitions in the
background, and your main code can do whatever else it needs to in the
mean time.
You can set it to increment on high-to-low or low-to-high transitions by
flipping a single bit in the OPTION register.
Just look in your datasheet at the OPTION register & Timr0 sections for
how/why it works. It's really simple once you tinkered with it a bit.
Bookmarks