I have experimented using a 16-way rotary switch hexadecimal rotary (4-bits) on an interrupt (TMR0) to flash an LED on pin-18 (RA1) either faster or slower depending on position.
Now I want to do the same kind of thing (initially) with a inc. encoder (2-bit gray - is this right?) - ideally using the encoder's push button for a zeroing operation.
I have had quite a good look over google to find something similar but must be typing in the wrong search criteria somehow.
I am using a PicKit2 with MPLab and PICC compiler.
There a a few sites on the net for this.. however the gray code for a two bit or even a four bit encoder is minimal... you can use a simple lookup table to convert..
I really meant to ask about how to start off writing code for incremental encoders.
I am genuinely confused as to why the encoder's description (admittedly it's not a full datasheet, but) actually mentions gray code, as the encoder doesn't appear to have any actual absolute encoding, only incremental - with two square waves, one lagging or leading the other to denote direction of rotation. Afterall, the encoder boasts 20-positions & so would require 5-bits not 2.
Although I have got my head around the simple TMR0 interrupt, I am aware that the interrupts I should be using would probably be looking for' a rising edge on a pin. I'm not sure if the PIC I'm using can handle two of these type interrupts that (I think) need to see both a rising edge and then later falling edge.
Of course I can just keep polling the ports on a timer event, but this seems less eloquent & also may cause problems if later on I start carrying out other functions from main().
hi,
Post the datasheet for the encoder or a link.
I would say the encoder you have that gives a quadrature signal, even though it has 20 index positions, will only be a 2 bit code.
hi rs,
With that type of quadrature switch you have to detect the switching edges in order to determine inc or dec [ CW/CCW] rotation.
You could use interrupt on change on two of the PORTB pins.
Looking at the amount of bounce on the contacts some program delays will be required.
Also I would say also the current inc/dec count will have to be saved in EEPROM at power down, recall on power up.
That's not true Eric. You can also poll for a change in level.
The nice thing about that quadrature signal is that after detecting a change, you can easily determine direction by exclusive-OR'ing the current A or B bit with the opposite bit from the previous sample. The result, 0 or 1, is the direction.
That's not true Eric. You can also poll for a change in level.
The nice thing about that quadrature signal is that, after detecting a change, you can easily determine direction by exclusive-OR'ing the current A or B bit with the opposite bit from the previous sample. The result, 0 or 1, is the direction.
just viewing the section in '628A datasheet on:
PORTB INTERRUPT
In the code I basically copied out when using TMR0 the interrupt conditions a reset in this manner
TMR0 = 0; // Timer (or counter) TMR0 returns its initial value
INTCON = 0x20; // Bit T0IE is set, bit T0IF is cleared
//T0IF = 0; // clear timer0 flag
//T0IE = 1; // re-enable timer0 interrupt
For the on-change intpt I need to set RBIE in main()
then RBIF will go high if there is any change on pins RB4:7 - rising or falling, is this right?
RBIE will need to be set again at the end of the isr routine for the next change to be caught (?).
For a CW rotation the pin for A (say RB4) will see an event before the pin for B (eg RB5), but it's this double visit that I can't get my head around - what if I miss one or the other?
Although I should probably put this together from scratch myself I could sure use an bit of example code right now.
I am having trouble with using the PortB Interrupt to pick up the pulses from the encoder.
I think this is, as was suggested as a likely problem previously, there is a debounce issue - so at the moment I am trying to allow the code to run a a timer interrupt once a pulse is seen on the RB7:4 pins to stop this.
What I want to know is: Can I not have both the OnPortChange interrupt running at the same time OnTimer interrupt? As when I switch both ON in the INTCON register the TMRO event stops getting called...?
Thanks for any advice