- animation (3)
- art (11)
- computers (13)
- cryptophones (9)
- electronics (11)
- ithaca (8)
- kids (61)
- music (14)
- musings (12)
- rpg (2)
Here’s some PIC assembly source code (MPASM) for reading a quadrature encoder. It’s fairly raw and simple, but it works.
It supports both the Bourns PEC11 and Piher CI-11 encoders; to choose, uncomment one of the ENCODER_BRAND defines in the .asm file. Also set encPin and encPort in the .inc file as needed.
To use it, call InitQuadEncoder, then call PollQuadEncoder in a loop, fairly often. When the encoder is turned forward and back, the two routines DoQuadInc and DoQuadDec will be called, respectively. You must define those routines elsewhere.
You may also wish to debounce the output by waiting a little while before reacting to the input; I wait for about 0.25 ms and it works well for my application. That’s not shown in this code.
I was using it in an environment where paging wasn’t an issue, so you may need to add BANKSEL etc. to suit your particular target chip. At least it’s relocatable.
I think I based some of the logic on some code from the PIClist web site, but I can’t find the reference anymore. If it was yours, let me know so I can credit you!