Quadrature encoder module

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!

QuadEncoder.zip1.5 KB



Hey Tim,

Looks like your files aren’t being served up. If you click on the attatchments, it says they are not found.




Looks like they migrated south for the winter and never returned… I’ve reposted them. Thanks for the note! —tjw

got them, but a ?

Hey Tim,

Got them,thanks much. I’m starting to mess with them, but of course leaves another question.

I’ve not seen the EXTERN GLOBAL UDATA and CODE directives in my adventures yet, and MPLAB is choking on them when I compile, with an error:

Directive only allowed when generating an object file

Is this a workspace option I’m missing? thoughts?

Check out the MPASM

Check out the MPASM Assembler help file, the chapter on “Relocatable Objects”.

There’s a lot of code on the Web that uses so-called “Absolute” style, that specifies where everything needs to go in memory. This is generally considered obsolete, except for very small programs. “Relocatable” style allows you to split your project into multiple files and have the linker put them all together. It does use slightly different keywords and requires a “linker file.” This is all explained in the help file.

If you’re not familiar with it, it may be worth learning; it makes it much easier to reuse code and to separate different kinds of functionality into different files.


Ok, rtfm'd, and....

now I get it, but the helpfile says to choose the check box labeled “Object File” in the MPASM, which just plain doesn’t exist.

After a half hour of pullin the hair I don’t have out =), I find the option is actually under Build Options, MPASM/C17/C18 Suite (which I ignored many times, as there is also an MPASM tab, and I thought the prior didn’t apply to this mere mortal who’s only doin ASM), choose the radio button for build library target.

And happy day, it compiles without error =) (posting this in hopes that it might save some other poor soul some hair)



Sorry for the hair-pulling!

And I hope it was worth it!