Rotory Encoder and PIC
Moderator: Benj
Rotory Encoder and PIC
Hi every one / Martin,
I am trying to use Enocoder (Incremental), farell order code: 1520813, to input numbers on the LCD.
I am new to encoders, these are similar to car radio rational switches, where you can rotate and select the options on the menu. As there is no component in flowcode, I have used two switches to mimic the two input pins from the enocoder.
Please help, as the attached programme doesn't work.
regards
Indana
What I know:
I am told incremental encoders have two outputs which are out phase with each other at 90 degrees.
In order to determine direction of rotation. Gray coding is used.
Two useful links:
http://www.tufts.edu/programs/mma/emid/ ... ncoder.pdf
http://www.parallax.com/dl/docs/cols/nv ... ol/nv8.pdf
I am trying to use Enocoder (Incremental), farell order code: 1520813, to input numbers on the LCD.
I am new to encoders, these are similar to car radio rational switches, where you can rotate and select the options on the menu. As there is no component in flowcode, I have used two switches to mimic the two input pins from the enocoder.
Please help, as the attached programme doesn't work.
regards
Indana
What I know:
I am told incremental encoders have two outputs which are out phase with each other at 90 degrees.
In order to determine direction of rotation. Gray coding is used.
Two useful links:
http://www.tufts.edu/programs/mma/emid/ ... ncoder.pdf
http://www.parallax.com/dl/docs/cols/nv ... ol/nv8.pdf
- Attachments
-
- rotory enocder and numbers.fcf
- (10.5 KiB) Downloaded 472 times
- JohnCrow
- Valued Contributor
- Posts: 1367
- Joined: Wed Sep 19, 2007 1:21 pm
- Location: Lincolnshire
- Has thanked: 364 times
- Been thanked: 716 times
Re: Rotory Encoder and PIC
Hi
Rotary encoders is something I want to have a look at myself in the near future,
Sorry but I don't know anymore than you at the moment.
Have you looked at this post maybe something useful to you here
http://www.matrixmultimedia.com/mmforum ... der#p21037
Rotary encoders is something I want to have a look at myself in the near future,
Sorry but I don't know anymore than you at the moment.
Have you looked at this post maybe something useful to you here
http://www.matrixmultimedia.com/mmforum ... der#p21037
1 in 10 people understand binary, the other one doesn't !
- Steve
- Matrix Staff
- Posts: 3431
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
Re: Rotory Encoder and PIC
Strangely enough, we have just signed-off work on a rotational encoder E-block. We hope to have this product for sale in the next 2 months.
We'll also work on a Flowcode component, but this is likely to be limited to Flowcode V5. However, I'm sure that functions in C code will be made available.
We'll also work on a Flowcode component, but this is likely to be limited to Flowcode V5. However, I'm sure that functions in C code will be made available.
- Dan81
- Valued Contributor
- Posts: 268
- Joined: Sun Jan 15, 2006 4:07 pm
- Location: Albi France
- Been thanked: 60 times
- Contact:
Re: Rotory Encoder and PIC
Hello Indana
You can do something like this.
An other way is to use interrupt (RB0 or PortChange)
Daniel
You can do something like this.
An other way is to use interrupt (RB0 or PortChange)
Daniel
- Attachments
-
- rotory encoder v2.fcf
- OK in simulation
Not hard tested - (9 KiB) Downloaded 576 times
Re: Rotory Encoder and PIC
Thanks Daniel,
Will try it out and let you know if it worked, but the Rotory encoder would need two input pins as they are at 90 degrees with
each other at any one time. RB0 is a single input, PORTB change is a good one.
Thanks for your help.
Regards
Indana
Will try it out and let you know if it worked, but the Rotory encoder would need two input pins as they are at 90 degrees with
each other at any one time. RB0 is a single input, PORTB change is a good one.

Thanks for your help.
Regards
Indana
- Dan81
- Valued Contributor
- Posts: 268
- Joined: Sun Jan 15, 2006 4:07 pm
- Location: Albi France
- Been thanked: 60 times
- Contact:
Re: Rotory Encoder and PIC
Hello Indana
If you use RB0 int , just connect one of the output of the encoder to RB0 and the other one anywhere.
If you use PortB Change , you will have to search which pin have changed and the sign of the edge .
With a 16F887 you can reduce the number of pins of the interrupt.
Daniel
If you use RB0 int , just connect one of the output of the encoder to RB0 and the other one anywhere.
If you use PortB Change , you will have to search which pin have changed and the sign of the edge .
With a 16F887 you can reduce the number of pins of the interrupt.
Daniel
Re: Rotory Encoder and PIC
Hi Daniel,
Your prog althoug working on simulation, is not working well with the hardware, you are right, may be it will work with an interrupt as you have suggested!.
thanks any way
Regards
Indana
Your prog althoug working on simulation, is not working well with the hardware, you are right, may be it will work with an interrupt as you have suggested!.
thanks any way

Regards
Indana
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Rotory Encoder and PIC


I know it wont work in flowcode simulator because the simulator can't simulate T0CKI pin. The simulator behaves as though clock source is selected.
All you need to do is connect RA4 to 1 I/P of encoder, and the other 90 deg I/P connect to RA0 .Its the RA0 pin that's determining direction.
Count is increased or decreased on every rising edge. To change count on falling edge, then alter within timer0 properties to: High-to-low transition.
I decided to try using T0CKI (a part of timer 0 interrupt) instead of port B for the interrupt.
Only because the LCD is connected to port B.
If the count direction is opposite to what your after, just double click Rotation_Direction and select 'Swap Yes and No'.
There is no switch de-bounce within Flowchart posted. If not using a LPF then you can decrease value of tmr0 (within Encoder_Detect macro) to increase de-bounce. If set too low the de-bounce will be too high, so will not detect properly.
E.g currently tmr0=+255
This means a count of 1 every time pin A4 goes from low to high.
If you change 255 to 252 then there will be a count of 1 when A4 goes from low to high for 3rd time. (255-253 = 3)
Regards
Martin
- Attachments
-
- rotory encoder v2 Modified.fcf
- (9 KiB) Downloaded 457 times
Martin
- Dan81
- Valued Contributor
- Posts: 268
- Joined: Sun Jan 15, 2006 4:07 pm
- Location: Albi France
- Been thanked: 60 times
- Contact:
Re: Rotory Encoder and PIC
Hello Indana
Can you tell what my program is doing or not doing.
Which hardware are you using ?
Daniel
Can you tell what my program is doing or not doing.
Which hardware are you using ?
Daniel
Re: Rotory Encoder and PIC
Hi Daniel,
I am 16f88, with the encoder used RB6/RB7 pins.
Both clockwise and AC produce the same result, ie, incrementing!. Bizzare?.
Let me know if I am doing something wrong?.
Thanks
regards
Indana
I am 16f88, with the encoder used RB6/RB7 pins.
Both clockwise and AC produce the same result, ie, incrementing!. Bizzare?.

Let me know if I am doing something wrong?.
Thanks
regards
Indana
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Rotory Encoder and PIC
With the one I posted you need to use RA4. But the RA0 can be changed.nmindana wrote: I am 16f88, with the encoder used RB6/RB7 pins.
Both clockwise and AC produce the same result, ie, incrementing!. Bizzare?.![]()
Let me know if I am doing something wrong?.
Thanks
regards
Indana
I could change to the B6 and B7 pins if you like?
Martin
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Rotory Encoder and PIC
Here is a slightly different version using ports RB6 and RB7 as promised.
I have not used an encoder before, so can't guarantee it will work, but you never know
This version is not using timer0 T0CKI but instead using portB interrupt, so should simulate in Flowcode as well.
Just for testing purposes I have changed Days into and integer. Since starting from 0 if counting down you will see a negative number.
Regards
Martin
I have not used an encoder before, so can't guarantee it will work, but you never know

This version is not using timer0 T0CKI but instead using portB interrupt, so should simulate in Flowcode as well.
Just for testing purposes I have changed Days into and integer. Since starting from 0 if counting down you will see a negative number.
Regards
Martin
- Attachments
-
- rotory encoder v2 Modified1.fcf
- (9 KiB) Downloaded 422 times
Martin
-
- Posts: 1333
- Joined: Tue Oct 06, 2009 9:39 am
- Has thanked: 1135 times
- Been thanked: 299 times
Re: Rotory Encoder and PIC
Hi!
I made a small project for a Ham radio with a rotary encoder. Maybe you can use something from here. It works in real world.
Regard
jgu1
I made a small project for a Ham radio with a rotary encoder. Maybe you can use something from here. It works in real world.
Regard
jgu1
- Attachments
-
- Storno11.fcf
- (22.7 KiB) Downloaded 446 times
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Rotory Encoder and PIC
After looking at the datasheet for the encoder (again
)
I have altered Flowchart so this version should work, whereas I don’t think the one on my post above post will work that well.

I have altered Flowchart so this version should work, whereas I don’t think the one on my post above post will work that well.
- Attachments
-
- rotory encoder v2 Modified2.fcf
- (10 KiB) Downloaded 386 times
Martin
- Dan81
- Valued Contributor
- Posts: 268
- Joined: Sun Jan 15, 2006 4:07 pm
- Location: Albi France
- Been thanked: 60 times
- Contact:
Re: Rotory Encoder and PIC
Hello Indana
Which hardware are you using : Matrix board HP488 , EB006 ?
Daniel
Which hardware are you using : Matrix board HP488 , EB006 ?
Daniel
Re: Rotory Encoder and PIC
Hi everyone,
I am pleased to say I found the cure. I worked it out based on the Grey coding. . This is a system of binary counting in which adjacent codes differ in only one position.
The encoder I am using is a quadrate mechanical type, it has 24 indents, going through 360 degree rotation.
Gray Coding
Sector Contact 1 Contact 2 Contact 3 Angle
1 off off off 0° to 45°
2 off off ON 45° to 90°
3 off ON ON 90° to 135°
4 off ON off 135° to 180°
5 ON ON off 180° to 225°
6 ON ON ON 225° to 270°
7 ON off ON 270° to 315°
8 ON off off 315° to 360°
The attached works well, it is set to reset to "0" after 180 days.
"WoW", Can't believe it, I surprised myself. Thanks for all your support, especially Martin.
Now I can input numbers, how to I input alphabets ?.
Regards
Indana
I am pleased to say I found the cure. I worked it out based on the Grey coding. . This is a system of binary counting in which adjacent codes differ in only one position.
The encoder I am using is a quadrate mechanical type, it has 24 indents, going through 360 degree rotation.
Gray Coding
Sector Contact 1 Contact 2 Contact 3 Angle
1 off off off 0° to 45°
2 off off ON 45° to 90°
3 off ON ON 90° to 135°
4 off ON off 135° to 180°
5 ON ON off 180° to 225°
6 ON ON ON 225° to 270°
7 ON off ON 270° to 315°
8 ON off off 315° to 360°
The attached works well, it is set to reset to "0" after 180 days.
"WoW", Can't believe it, I surprised myself. Thanks for all your support, especially Martin.
Now I can input numbers, how to I input alphabets ?.
Regards
Indana
- Attachments
-
- rotory using nterrupts working.fcf
- (11 KiB) Downloaded 436 times
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Rotory Encoder and PIC
Well done Indana!
Glad you found your own solution. It just proves how well your getting to know Flowcode.
Regards
Martin
Glad you found your own solution. It just proves how well your getting to know Flowcode.
I have modified your Flowchart to count up in the alphabet instead of numbers.nmindana wrote:how to I input alphabets ?.
Regards
Indana
Regards
Martin
- Attachments
-
- rotory using nterrupts working V2.fcf
- (11 KiB) Downloaded 413 times
Martin
-
- Posts: 155
- Joined: Sat Mar 28, 2009 5:44 pm
- Location: LYON FRANCE
- Has thanked: 3 times
- Been thanked: 41 times
Re: Rotory Encoder and PIC
I did that encoder reading 2 years ago:
Maybe it is possible to do shorter with RotaryEncoder.pdf XOR method (link in the first post).
It works in real world on a manual encoder. If this can help.
For simulation, press 1 and 0 on the computer keyboard.
To increase the counter:
press 0, 0 and 1,1,nothing and repeat...
I don't know if it can work on a motor encoder because it must be high speed.
I want to perform test in the future.
Steve, will your component count with hardware or software?
That one is very simple: only 6 flowcode instructions in interrupt. 7 instructions in main for display.Maybe it is possible to do shorter with RotaryEncoder.pdf XOR method (link in the first post).
It works in real world on a manual encoder. If this can help.
For simulation, press 1 and 0 on the computer keyboard.
To increase the counter:
press 0, 0 and 1,1,nothing and repeat...
I don't know if it can work on a motor encoder because it must be high speed.
I want to perform test in the future.
Steve, will your component count with hardware or software?
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Rotory Encoder and PIC
Hi Indana,
Have you seen this thread:
http://www.matrixmultimedia.com/mmforum ... =26&t=8773
It may be of interest since It has flowcharts of encoders that have been tested with hardware before being posted.
Martin.
Have you seen this thread:
http://www.matrixmultimedia.com/mmforum ... =26&t=8773
It may be of interest since It has flowcharts of encoders that have been tested with hardware before being posted.
Martin.
Martin
-
- Posts: 155
- Joined: Sat Mar 28, 2009 5:44 pm
- Location: LYON FRANCE
- Has thanked: 3 times
- Been thanked: 41 times
Re: Rotory Encoder and PIC
The last program what tested on hardware with an optical encoder. It works at 500 pulses/s.
There is no need to debounce because it is an optical encoder.
On the thread:
http://www.matrixmultimedia.com/mmforum ... =26&t=8773
It is a mechanical encoder so you need a debounce.
Here is a new program with the xor method: Only 4 instruction in the interrupt.
Not tested yet. I want to test 30000 pulse/s.
Indiana, that one won't work on your encoder because it is mechanical. You need to add a 5ms delay in the interrupt so it should work.
There is no rollover protection. Add one if necessary.
Medelec, your "Rotory Encoder V1 with a 5ms delay .fcf" is alpha style (1/2) in http://www.matrixmultimedia.com/mmforum ... =26&t=8773?
The "test codeur-XOR.fcf" is panasonic style (1)
The "test codeur.fcf" is 1/4
There is no need to debounce because it is an optical encoder.
On the thread:
http://www.matrixmultimedia.com/mmforum ... =26&t=8773
It is a mechanical encoder so you need a debounce.
Here is a new program with the xor method: Only 4 instruction in the interrupt.
Not tested yet. I want to test 30000 pulse/s.
Indiana, that one won't work on your encoder because it is mechanical. You need to add a 5ms delay in the interrupt so it should work.
There is no rollover protection. Add one if necessary.
Medelec, your "Rotory Encoder V1 with a 5ms delay .fcf" is alpha style (1/2) in http://www.matrixmultimedia.com/mmforum ... =26&t=8773?
The "test codeur-XOR.fcf" is panasonic style (1)
The "test codeur.fcf" is 1/4