Quadrature Encoder

Forum for problems or queries regarding other Flowcode Components. Eg LEDs, Switches, LCD, Gfx LCD etc

Moderators: Benj, Mods

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Quadrature Encoder

Postby Bobw » Sun Jan 30, 2011 1:38 am

I have read just about every post on here dealing with Quadrature Encoders. I do not see such a device in the flowcode arsenal.
Is there a way to add one and use it in the simulator?
Right now all I want to do is Count (increase) or decrement the pulse count and display it on the LCD. I will manually control the pulse disk for now.
Bob

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8285
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Re: Quadrature Encoder

Postby medelec35 » Sun Jan 30, 2011 9:22 am

Have you thought about using a frequency counter application? Since a frequency counter will count pulses for 1 second, reset count to 0 then display the number of pulses counted.
What you can do is remove the time part. Count the pulses, store in a temp int variable. Reset count to 0. In the background allow pulses to be read and placed in count variable. use the temp int variable for a loop that keeps counting down until temp int variable = 0
Then retrieve the new count, place that in temp int variable and so keep looping.
If encoder can go in either direction You will need to set another variable, which then can be set to 1 if clockwise or 2 if counter clockwise for example. That way you can have a decision branch, if value set to 1 run the loop that counts temp int variable down and controls motor clockwise etc.

Note: I believe the best way for detecting the pulses will be using an interrupt. Either IOC (interrupt on change) (you can use weak pull-ups to minimise hardware count) or int function for counting pulses.
I don't believe to will be able to achieve this with the demo version. As it will probably take more than the allocated number of 16 icons.
Hope I have made this clear enough.
If I get a bit of time free I could put something together later, that may help. Just wanted to give you some food for thought.
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
Dan81
Valued Contributor
Valued Contributor
Posts: 265
Joined: Sun Jan 15, 2006 4:07 pm
Location: Albi France
Been thanked: 60 times
Contact:

Re: Quadrature Encoder

Postby Dan81 » Sun Jan 30, 2011 9:53 am

Hello Bob

As Medelec wrote the best way is interrupt (RB0 or Port Change).

One of the 2 signals is connected to RB0 (obligatory) the other one to RB1 (for ex) .

In the RB0_interrupt program :
Read RB1 ,
If RB1 = 1
. .then count = count + 1
. .else count = count - 1 .

count must be an integer.

RB0 interrupt is simulable within Flowcode.


Daniel

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Quadrature Encoder

Postby Bobw » Sun Jan 30, 2011 2:13 pm

I thought about some of those options. Problem comes from determining the direction (left or right).
I am looking at purchasing flowcode 4 soon as I find the cheapest supplier. Would be nice if they add a optical encoder in future releases. The project I am working on requires 2 of them.

User avatar
Dan81
Valued Contributor
Valued Contributor
Posts: 265
Joined: Sun Jan 15, 2006 4:07 pm
Location: Albi France
Been thanked: 60 times
Contact:

Re: Quadrature Encoder

Postby Dan81 » Sun Jan 30, 2011 2:44 pm

Hello Bob

Problem comes from determining the direction :
RB0 interrupt is lauched when there is a edge on RBO if you read RB1 you know the direction.

The project I am working on requires 2 of them.
You will need to use PortB change and make some logical calculations.
see : viewtopic.php?f=29&t=7721

Daniel

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Quadrature Encoder

Postby Bobw » Sun Feb 06, 2011 2:25 pm

After learning more, and now having a registered version of Flowcode, it came to mind that I do NOT have to look at direction.
I am already controlling direction, so I just need to count the number of pulses. In my case when "Pulse_Cnt =320 then Ant_Pos = Ant_Pos +1" "Pulse_Cnt=0"
My original thoughts were to use RA2 and RA3 <PIC16F1827> for the encoder input. I have to check the data sheet, but I think this PIC has 1 interrupt on RB0 which right now has the LCD on it.
I wish I knew C code better, but Flowcode is really helping to learn some of it.

Bob

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Quadrature Encoder

Postby Bobw » Sun Feb 06, 2011 2:29 pm

Should have looked first. <PIC16F1827> HAS interrupt on change on both RA2 and RA3 so guess I am in luck. now to figure out how to use it.

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 43 times
Contact:

Re: Quadrature Encoder

Postby Sean » Mon Feb 07, 2011 3:49 pm

Here is a program that reads a quadrature encoder.

The program uses a timer interrupt to poll the selected inputs at regular intervals, so any available inputs can be used.
Using a PIC16F877a, clocked at 19.6608MHz, the 19200Hz interrupt frequency allows for individual encoder channel frequencies of up to 4800Hz (detecting the 4 possible states [quads] in each cycle).

The inputs to be used are defined in the Read_encoder macro. Port C, 0 & 1 are used in the example to allow it to run on a HP488 development board. Port B is used to display the encoder count on the LCD.

EncoderTimerInt1.fcf
(10.5 KiB) Downloaded 718 times

The target chip, clock frequency, and I/O configuration can be changed as required - noting the maximum frequency of operation.

We are planning to write a more comprehensive article on this topic in the near future.

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Quadrature Encoder

Postby Bobw » Mon Feb 07, 2011 9:21 pm

Sean,
Thanks for the file. It got me thinking (dangerous thing).
Since The program is energizing the motor either CW or CCW, I do not need to count pulses until that time.
Why couldn't I just read the highs from one of the 2 outputs of the encoder?
Here is a quick run down of how things are suppose to work.

Current Ant_Pos = 180°
User sets it to move to Set+Pos =360°
"GO" switch is pressed.
Program sees that in this case the CW motor needs to be energized.
Program starts counting pulses. (high from encoder) <--- thinking like using as a switch input.
every 320 pulses the Ant_Pos is incremented by 1 and the pulse counter is reset to zero.
When Set_Pos = Ant_Pos the motor is turned off.
Current Ant_Pos is stored in EPROM for next power cycle.
Jump back to start.

Think I will try a simple program and see if I can read the pulses from one side of the encoder and display them. I do not need 100% accuracy. If I was off .5° one way or the other not that big of a deal. I do have a zero (Home) sensor in the motor that just lights an LED on the control panel (No PIC), and I have a routine to zero the Ant_Pos when a button is pressed. Trying to keep it dumb and simple here.

Bob

Bobw
Posts: 157
Joined: Sat Jan 22, 2011 10:39 pm
Location: Michigan
Has thanked: 6 times
Been thanked: 27 times
Contact:

Re: Quadrature Encoder

Postby Bobw » Tue Feb 08, 2011 2:15 am

Well I tired it in real life. It does work, but I am using a small toothed encoder wheel. It may over count if the opening is in the right spot. Think I will try a hall sensor and see what results I get with that. Found my notes from when I was calculating the motor revolutions that equal one degree of rotation, 9 if I was counting correctly.
There has to be a simple way. Just wish there was more room inside the gear box.

Bob

The simulator uses a push button, I replaced it with the encoder on the actual circuit.
Attachments
TestSWT.fcf
(10 KiB) Downloaded 579 times