Encoder triggered by Port B interrupt

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply
medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Encoder triggered by Port B interrupt

Post by medelec35 »

I have created a V4 Flowchart for an encoder that's triggered by port B interrupts. The advantage being, movement of encoder is detected instantly.
Target device is 16F88, configured for 8MHz internal oscillator and internal weak pull-ups on port B (so no external resistors required)
LCD is used to indicate rotation by the form of a number which increases or decreases depending on encoder rotation.
I have also included a bargraph function just like you see on audio equipment ranging from 0 = no bars to 255 = 16 bars.
Encoder and data sheet I used can can be found here:
http://uk.rs-online.com/web/p/products/692-8471/
The encoder has three connections labelled: A (connect to port B6), B (connect to port B7) & C (connect to GND),
Flowchart works by waiting for port B to be triggered. When port B is triggered it is then checking to see if port B6 has changed. If not then nothing will happen.
If port B6 has changed, then the value of port B7 is read.

Code: Select all

B6 changed from		B7 is read as	   Encoder is rotating
0 - 1			         0			        CCW
0 - 1		            1                 CW
1 - 0			         0                 CCW	
1 - 0			         1                 CW
I have tested this with no debouncing cap and I can confirm it works well.
If display is showing 0 then after one full rotation clockwise display shows 28.
If you want display to indicate a higher number after one rotation, then change the all 1's in the Count = Count+1 to a higher value.

In the simulation the bargraph shows a y with 2 dots on the top, but with hardware the correct block is displayed.


Martin
Attachments
Rotory Encoder V1.fcf
(16 KiB) Downloaded 616 times
Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: Encoder triggered by Port B interrupt

Post by fotios »

Hello Martin
I was busy 3 days ago with rotary encoders like you. The difference is that i experiment with detent equiped encoders, an ALPS a BOURNS and a Panasonic. As for the rest i also use a P16F88 and instead LCD i use the Led board. Until now, i haven't managed to use the PORT interrupt. Detent equiped encoders (used mostly in audio devices) present two different paterns. I attach a picture, the upper patern is that of ALPS and BOURNS while the lower pattern is that of Panasonic. The vertical blue lines are the detent positions, the red area is the debounce space offered by ALPS and BOURNS (bounce is reffered as "Chatter" in encoders). The difference is that both A and B outputs of ALPS in detent position are LOW, while A output of Panasonic can be either HI or LOW in each detent position and its B output is not deffined because its transition edge is exactly placed on detent position. For this reason, different code needed for ALPS-BOURNS and different for Panasonic (many other constructors also use the Panasonic pattern). I followed the same way like you in software, PIC reads the A output as a transition event while the B output as direction.
I attach the two fcf files, both are tested and working on actual hardware. Common terminal "C" of encoder must be connected at +V. You can test them and i think that your encoder can run both versions as it is not detent equiped. If you have any idea of what i can do to use the PORT interrupt i will appreciate it. I will try your program tomorrow on actual hardware, it is very interesting.
Regards
Fotis
Attachments
Encoder Patterns.jpg
Encoder Patterns.jpg (21.76 KiB) Viewed 14158 times
Panasonic_Loop.fcf
(10.5 KiB) Downloaded 517 times
ALPS_Loop.fcf
(10.5 KiB) Downloaded 496 times
Last edited by fotios on Mon Aug 15, 2011 8:17 am, edited 5 times in total.
Best Regards FOTIS ANAGNOSTOU

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Encoder triggered by Port B interrupt

Post by medelec35 »

Hi Fotis,

Problem with using port B int is you can't use the 5ms delays. If you place a delay within an interrupt, and delay has not ended before interrupt is triggered again, then corruption will occur.
So if there is a bounce issue, then you will have to use a cap across the o/p of A and B to GND

I can't think of a way of software debounce when using port interrupt, since every time bounce occurs then port will be triggered anyway.


Martin
Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: Encoder triggered by Port B interrupt

Post by fotios »

Hi Martin
Thanks for the reply
Just now i tried your project on EB006 with the ALPS encoder. In general is working as you described. There are some issues due to detents of encoder:
1) The counter increments or decrements by "2" (e.g. 2-4-6-8..... 24-22-20-18....) and that is correct for this type of encoder.
2) There is "Chattering"; if the encoder is fast turned and stopped in a detent position enough times shows odd numbers (e.g. 13, 25, 33 etc.) .
I must clear out, that the project of Martin will be working perfectly with the use of continuous turning encoders like that he used to develop his program
Regards
Fotis
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: Encoder triggered by Port B interrupt

Post by fotios »

Hi Martin
I have good news. Early the morning (when the mind is clear) i resolved the problem of PORT interrupt. The problem finaly wasn't the debounce time of 5ms, it was the lack of a variable and a decision. I attach both new fcf which i tried with success on actual hardware. The PORT interrupt is enabled, the delay of 5ms still exists, and the encoder run the LEDs in crazy speed without errors. I tried everything to cause errors, crazy rotation, steep change of turn direction, stop in the middle of detents etc. the program responds positively without mistakes in any abuse. Few words about rotary encoders for the newcomers on this thread:
In the picture you can see the patterns produced by different types. In my examples i use encoders with detents, while Martin in his example use a continuous rotation encoder. In audio and multimedia devices detent encoders (most times equiped with push-switch) are prefered because they stay immobile when are not operated. That is of big importance when they execute other operations except volume control like input selection. In other audio devices - like pro-CD players - continuous rotation encoders are used for JOG - DIAL operations; in measuring instruments and medical devices this type is also used. It is upon the demands of device. Any type of encoder produces these patterns
Image
Continuous encoders present just bounces, while detent encoders produce chatter due to detents and bounce due to switches. In addition, detent encoders offer different patterns combining detent positions and switches state. Therefore, different code is needed for each type. The 3 terminals of encoder (A, B and C=Common) may have different place from type to type. Both ALPS and Panasonic that i use in my examples have same terminal arangement. Common at the middle, B at the left and A at the right of C. Martin uses internal pull-ups of P16F88 and has the C terminal connected to Gnd node. Me i use external pull-down resistors - exactly like in the EB007 switch board - so Common terminal is tied at +V. You can run both fcf in simulator, but you could not see the game live. You have to buy Panasonic and ALPS or BOURNS detent encoders to make your experiment. I stoped purchases from Farnell UK, DigiKey, Mouser etc. as they charge an arm and a leg to ship worldwide (Greece included) and i search on e-bay for parts. Here is a good source for Panasonic: http://cgi.ebay.co.uk/ws/eBayISAPI.dll? ... K:MEWAX:IT and here for ALPS: http://cgi.ebay.co.uk/ws/eBayISAPI.dll? ... K:MEWAX:IT. Of course not for you that you live in UK.
Regards
Fotis
Attachments
Panasonic_INT.fcf
(11.5 KiB) Downloaded 531 times
ALPS_INT.fcf
(11 KiB) Downloaded 521 times
Last edited by fotios on Mon Aug 15, 2011 10:59 pm, edited 1 time in total.
Best Regards FOTIS ANAGNOSTOU

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Encoder triggered by Port B interrupt

Post by medelec35 »

Hi Fotis,
Thanks for posting flowcharts.
You have got 5 ms delays within your port interrupt macro. Would this will cause corruption issues?
The reason being if your port b interrupt has fired and during the 5ms delay contact bounce causes the interrupt to be re-triggered. I didn't think that can happen, since already still within the interrupt macro.
It could only be timer interrupts that you can't put delays in, and its OK to put them within port interrupts
If someone would knows, can confirm which is correct please I would appreciate it.


Thanks


Martin
Martin

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: Encoder triggered by Port B interrupt

Post by JohnCrow »

Hi Martin

Just tried your flowchart with an Alpha Rotary Encoder
Part No. 318-ENC130175F-12PS

This has detents and takes 16 clicks to do 1 full revolution.

Starting at zero
1)Turning CCW it counts from 1 to 255 incements of 1 with the bar graph
2) Will count back from 255 to zero turning CW
3)From zero turning CW no effect

These are reversed if I swop A & B Connetions.

The component is a budget one, (about £2 or so from proto-pic)
Will study these posts in more detail later.
1 in 10 people understand binary, the other one doesn't !

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: Encoder triggered by Port B interrupt

Post by fotios »

Hi John
Here the specs and pattern of your Alpha encoder: http://www.sparkfun.com/datasheets/Comp ... 700198.pdf. I had the same issues like you, so pins A and B are placed inverserly in relation with Martin's BOURNS encoder and in the same sequence like ALPS and Panasonic (Com in mid position, A in the right side of Com and B in the left). The given pattern of encoder, is same with this of Panasonic with the difference that Alpha detents exist only in the HI periods of A terminal (in Panasonic in both HI and LO periods). The issue of B terminal is the same like in Panasonic, its state is unpredictable at detent points i.e. can be either HI or LO. I think that the code of Panasonic can be used for Alpha with slight modifications.
Fotis
Best Regards FOTIS ANAGNOSTOU

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: Encoder triggered by Port B interrupt

Post by JohnCrow »

Thanks
I'll have to try a few experiments, never done anything with RCs so its a case of study the datasheets and the FC files posted by you and Martin
1 in 10 people understand binary, the other one doesn't !

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: Encoder triggered by Port B interrupt

Post by fotios »

medelec35 wrote:You have got 5 ms delays within your port interrupt macro. Would this will cause corruption issues?
The reason being if your port b interrupt has fired and during the 5ms delay contact bounce causes the interrupt to be re-triggered. I didn't think that can happen, since already still within the interrupt macro.
It could only be timer interrupts that you can't put delays in, and its OK to put them within port interrupts
If someone would knows, can confirm which is correct please I would appreciate it
Hi Martin
Sincerely i believe you, your thought is very logic. But the facts... I tried again and again, two times each fcf, with or without delays. I can confirm you that it hapens the inverse: if i remove the 5ms delays, then the program on actual hardware is not working, is absolutelly dead. When i place again the delays, the program runs correctly on actual hardware, without error, corruption, hang-up etc.
Some details: I haven't activated the internal weak pull-ups of P16F88, i use external pull-down resistors tied at pins RB.6-RB.7, the Com terminal of encoder is tied thru a 390Ω resistor at +V supply rail. But i think that does not matters. Also, oscillator frequency is 4MHz.
It is probably a mechanical issue, owed to the detents of encoder. I remember that i placed these delays by reading carefully datasheets. Panasonic and ALPS encoders, both have a chattering time of 5ms max. Chattering is the time interval between a detent position and a switch activation, in which is also included the bounce time of switch. In the diagram i posted above, the red areas indicate this time interval.
ALPS recomends that this interval should be masked to avoid chattering phenomena. I hope i gave you an idea.
Regards
Fotis
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: Encoder triggered by Port B interrupt

Post by fotios »

JohnCrow wrote:I'll have to try a few experiments, never done anything with RCs so its a case of study the datasheets and the FC files posted by you and Martin
I hope this can help you a little. In the attached picture, you can see both patterns of Panasonic and Alpha encoder in CW direction.
Regards
Fotis
Attachments
ALPHA encoder.jpg
ALPHA encoder.jpg (64.5 KiB) Viewed 14107 times
Best Regards FOTIS ANAGNOSTOU

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Encoder triggered by Port B interrupt

Post by medelec35 »

Thanks for that Fotis,
I have put a single 5ms delay in the interrupt to see how it goes. it could have misread about delays in port interrupts anyway.
Your more an expert with encoders than I am, since the day of my initial post was the 1st time I have played with encoders. I do find the topic interesting though.

The other puzzling thing when comparing Panasonic with alpha, is the waveforms look identical ,but DET is different.
Isn't the DET base on how you set the flowchart up, since A and B are only on/off switches.
If Panasonic is DET on leading and falling edges, then why can't same apply to alpha since has same leading an falling edges of Panasonic? Also its all the changing edges that will trigger some,and not 1/2 of them as shown on alpha.

@ John Odd its the opposite, since the waveform of all three types look identical ie B lags A when rotated clockwise. I have deliberately placed coding within interrupt so count does not roll over or under.

So long as either Fotis or my Flowcharts work for you then that's great!
Attachments
Rotory Encoder V1 with a 5ms delay .fcf
(16 KiB) Downloaded 514 times
Martin

Post Reply