# The BEST way to use 2 interrupts on a PIC?

Status
Not open for further replies.

#### Mike - K8LH

##### Well-Known Member
The encoders with detent that I have used (ITT NEC12 series and Bourns PEC11 series) sequence through all four states between the detent position... Or, I suppose that's really three states between the detents...

Please forgive me for repeating myself...

Mike

Last edited:

#### ericgibbs

##### Well-Known Member
hi Al,
From what you are seeing on the scope I'm coming to the same conclusion as Mike.
ie: grey pattern between detents, this would make sense.

The waveform on the screen, does it look like this image.?

Rather than video the traces, a chinagraph wax pencil can be used to over write the traces on the scope face.
It easily wipes off with a soft cloth, you will find it useful for other traces.

#### Attachments

• esp01 Oct. 11.gif
7 KB · Views: 93
Last edited:

#### bigal_scorpio

##### Active Member
Hi Eric,

Yes I would say that the pattern is like your drawing, well it is if I can turn the encoder with perfect precision, wish I had a very low geared motor, I would motorise one of the little buggers and take human error out of the equation!

So if I am getting the trace you drew what is it telling you?

I', beginning to wonder if these encoders are all they're cracked up to be.

Regards..............Al

#### ericgibbs

##### Well-Known Member
Hi Eric,

Yes I would say that the pattern is like your drawing, well it is if I can turn the encoder with perfect precision, wish I had a very low geared motor, I would motorise one of the little buggers and take human error out of the equation!

So if I am getting the trace you drew what is it telling you?

I', beginning to wonder if these encoders are all they're cracked up to be.

Regards..............Al

hi,
Its all this modern stuff, your'e not used to it.

The problem is you cant make use of the detents of the encoder when its driven by a motor, as there is no electrical connection/feedback . You will have to detect the code and determine counts and rotation.

Q: would 20 detents give enough resolution on positioning the seat.?
or do you want to stop the seat between the detent positions.?

#### bigal_scorpio

##### Active Member
Hi Eric,

I was thinking that I could gear up the motion of the seat so as to for example give maybe 2 rotations of the encoder from end to end, I will also try and build a little mechanical "slack" into the gearing so that the encoder won't stop between detents hopefully!

Do the readings tell you anything specific about the encoders? I was thinking that the mentioned version that was not absolute was more in keeping with the results but I can't get my head round the reason for this type as opposed to the absolute ones?

Regards.............Al

BTW for Mikebits, the data I have on the encoders is in the ebay link I posted, and I cannot find the actual makers sheet as there ane no markings on them.

Al

#### ericgibbs

##### Well-Known Member
Hi Eric,

I was thinking that I could gear up the motion of the seat so as to for example give maybe 2 rotations of the encoder from end to end, I will also try and build a little mechanical "slack" into the gearing so that the encoder won't stop between detents hopefully!

Do the readings tell you anything specific about the encoders? I was thinking that the mentioned version that was not absolute was more in keeping with the results but I can't get my head round the reason for this type as opposed to the absolute ones?

Regards.............Al

BTW for Mikebits, the data I have on the encoders is in the ebay link I posted, and I cannot find the actual makers sheet as there ane no markings on them.

Al
hi Al,
It looks as though the grey pattern [4 states] is repeated between detents.

This means you will have to increment/decrement a counter within the program as the grey codes are sampled by the PIC input pins.
The sense in which they change thru the 4 state will tell you the direction of rotation.

As the count up/down reaches a certain value you stop the drive motor.

Last edited:

#### Mike - K8LH

##### Well-Known Member
I', beginning to wonder if these encoders are all they're cracked up to be.
The ones with detents and built-in push-button switch on the shaft are intended for use as data entry or data input devices. For example, you could use the encoder to increase or decrease a value displayed on an LCD or 7-segment display or to move through an LCD menu of options. You might also detect "short" or "long" push-button switch presses. So you see you might be able to perform all user input on a particular product using a rotary encoder and 3 PIC pins...

Mike

#### bigal_scorpio

##### Active Member
Thanks for all the input so far Guys,

I am at the moment tending to agree with some ot the posts about these encoders, that they are not absolute and in fact have no grey code as such, just a simple A - AB - B or in reverse B - BA - A sequence between Every detent!

This may sound like a simple thing to some of you but since I'm fairly (read very) inexperienced with PICs I have to ask. How do I determine whether AB or BA has occurred? I cannot get my head round the way to do it at all, specially since there is no actual timing as such that I can rely on, the motor speed will vary according to load and other circumstances, even the weather! So I can't poll the bits at a particular interval as I don't know what it will be!

Sorry if I'm asking too many questions but I can't take in the info like I used to, it takes me a long time to take in info nowadays that would have sunk in immediately 30 years ago, they always say older and dafter! True in my case I'm afraid!

In the early 80s I was very proficient with programming the Commodore 64 and wrote many complex programs and was quite at home with Peeks and Pokes, but I must admit that the PIC programming is much more difficult even though the instruction set is smaller DOH!

Al

#### ericgibbs

##### Well-Known Member
This may sound like a simple thing to some of you but since I'm fairly (read very) inexperienced with PICs I have to ask. How do I determine whether AB or BA has occurred? I cannot get my head round the way to do it at all, specially since there is no actual timing as such that I can rely on, the motor speed will vary according to load and other circumstances, even the weather! So I can't poll the bits at a particular interval as I don't know what it will be!
Al

hi Al,
You can Poll the two signals using 2 PIC port pins, if thats the way you want to go.

Consider a PIC with a 4MHz xtal, cycle rate 1MHz.
A simple Polling loop, say that has 10 instructions, thats a 10uSec loop time.

I dont think the car seat will have moved very far in 10uSec.

You say that you are gearing the encoder by 2, thats 720 degrees of shaft rotation, so if the code changes 4 times/detent and there are 20 detents, that works out to 2 * 20 * 4 = 160 changes from end to end.

So total Polling time, 10uSec * 160 = 1.6mSec, I dont think your car seat is gong to reposition itself in about 2mSec.!!!

Work on the premise that the seat is going to move from 'A' to 'B' in say 2Secs.
I would gear the encoder so that the maximum rotation codes occur from 'A to B'.

That, if my sums are correct, gives a 'resting' code time of 2/0.002 = 1mSec, which is plenty of time to sample the code.

Last edited:

#### Mickster

##### Well-Known Member
Thanks for all the input so far Guys,

I am at the moment tending to agree with some ot the posts about these encoders, that they are not absolute and in fact have no grey code as such, just a simple A - AB - B or in reverse B - BA - A sequence between Every detent!

This may sound like a simple thing to some of you but since I'm fairly (read very) inexperienced with PICs I have to ask. How do I determine whether AB or BA has occurred? I cannot get my head round the way to do it at all, specially since there is no actual timing as such that I can rely on, the motor speed will vary according to load and other circumstances, even the weather! So I can't poll the bits at a particular interval as I don't know what it will be!

Al,

https://en.wikipedia.org/wiki/Rotary_encoder

#### bigal_scorpio

##### Active Member
Hi Guys,

No matter how much I read and even the fact that my encoders use a less complicated version of grey code, I just can't get my head round how to poll the bits so I can tell if Its turning clockwise or not!

I understand what you are saying Eric about the speed of the reading easily coping but it is the actual method of reading whichever bit comes in first and then checking the other on, especially as you pointed out that there is time to read it a thousand times probably with the speed the encoder will turn, you're right Eric, my seats don't move at warp speed, hehehe, but I won't know how long the A pulse and B pulse will be apart.

As to the link Mickster sent, thanks mate, but after reading it I'm still no wiser, my type of Encoder doesn't seem to fit any of them.

Has anyone got a "Janet and John use a rotary Encoder" book?

#### ericgibbs

##### Well-Known Member
hi Al,

Assuming PORTB.1 is 'Ain' and PORTB.0 is 'Bin'
A simple algorithm would be.
loop:
is it the same as the last reading,
if Yes, do nothing, just keep reading PORTB
if No, compare with the 'old' reading to get direction, save this reading as 'old'
inc/dec a program count depending upon 'A/B' direction.

If count greater/less than limit/position then stop motor.
else loop.

I would fit some form of limit switch on the seats, just in case.

You could build a A/B quadrature test sim, using a 4093 and a 4013 so that you debug on the bench.

Last edited:

#### ericgibbs

##### Well-Known Member
hi Al,

A scheme for a bench test sim, for the encoder, so that you can debug the program.

EDIT: added some decoupling on the +5V line.

I havn't shown the timing values for the 4093 pulse gen part as I dont know the speed of the encoder. I would start at about 1kHz.

#### Attachments

21.1 KB · Views: 83
Last edited:

#### bigal_scorpio

##### Active Member
Hi Eric,

Sorry for being such a pain mate!

I will get building the test rig now and let you know, thanks mate.

Al

#### philba

##### New Member
Detents are meant as mechanical feedback to a user. You can just ignore them since there is no one turning a knob. In CNC systems, rotary encoders are used to determine positioning. When you know the amount of rotation and the turns per inch of the lead screw, you can compute a fairly precise position. You have 20 full grey code cycles per turn (or 80 states per turn). If you have a 10 TPI (turns per inch) screw then one turn advances (or retreats) the nut by 1/10th of an inch. With your encoder directly connected to the screw, one state change is 1/80th of a rotation. This moves the nut by 1/800th of an inch ((1/10)*(1/80)). All you have to do in you PIC is add or subtract the right amount per state change.

#### Pommie

##### Well-Known Member
A simple way to read these types of devices is to detect one signal (A) going from zero to one and, as this happens, read the other signal (B). If the second signal (B) is high then it's clockwise, low is anticlockwise.

Doing this in asm is really simple.
Code:
WaitLow		btfss	PORTA,0		;A
goto	WaitLow		;wait for A = 0
WaitHigh	btfsc	PORTA,0		;A
goto	WaitHigh	;wait for A = 1
btfss	PORTA,1		;B = 0
incf	Count,F		;clockwise
btfsc	PORTA,1		;B = 1
decf	Count,F		;anticlockwise
goto	WaitLow
If you are comfortable with interrupts, this could be done more efficiently using the int pin (B0) interrupt.

This does only give 1 inc/dec per detent. If you want better resolution then you can add a check for A going low and reverse the inc/dec for B. Adding two further test for edges generated by B will give you full resolution.

<edit>
As it's so simple, here is the double resolution version,
Code:
WaitLow		btfss	PORTA,0		;A
goto	WaitLow		;wait for A = 0
btfsc	PORTA,1		;B = 1
incf	Count,F		;clockwise
btfss	PORTA,1		;B = 0
decf	Count,F		;anticlockwise
WaitHigh	btfsc	PORTA,0		;A
goto	WaitHigh	;wait for A = 1
btfss	PORTA,1		;B = 0
incf	Count,F		;clockwise
btfsc	PORTA,1		;B = 1
decf	Count,F		;anticlockwise
goto	WaitLow

</edit>

Mike.

Last edited:

#### philba

##### New Member
Well, that will certainly work but tosses away the other 3 states. So you get 1/4 of the positioning accuracy you would if using all 4 states. (or 1/2 in the second case).

What level of accuracy do you need for this project?

Last edited:

#### bigal_scorpio

##### Active Member
Hi Guys,

Keep up the good work, I'm not saying I understand everything but when I get the merest glimmer of the meanings I am very happy.

Pommie, I just don't see how the program would react if A or B was the first pulse? I have only a small experience of basic and none of ASM so I just can't figure how the program actually sorts out the pulses.

Philby as to the accuracy I was thinking that I could gear the encoder so that it turns twice (40 clicks) for the full motion of the seat. It would also be helpful if it would only stop in a position where the encoder was at a detent, but as I said earlier it may be possible to build in a little mechanical slack.

Thanks.........Al

#### ericgibbs

##### Well-Known Member
hi Al,
Looking thru some PIC application notes I found this pdf which will help regarding the encoder... Pages 23+

Also other useful information on PIC usage/applications.

#### Attachments

• pic-applications.pdf
251.9 KB · Views: 112

#### bigal_scorpio

##### Active Member
hi Al,
Looking thru some PIC application notes I found this pdf which will help regarding the encoder... Pages 23+

Also other useful information on PIC usage/applications.

Hi Eric,

Thanks for that, I am reading it and trying to understand it, and will let you know when I get further (or not!) hehehe

BTW I just finished a more urgent project (for the wife)

Running 6x 1w white LEDs for stairs lighting from a PIC using PWM, looks great and she can get just the amount of mood lighting she wants now.

Al

Last edited:
Status
Not open for further replies.

Replies
8
Views
1K
Replies
3
Views
1K
Replies
29
Views
1K
Replies
7
Views
2K
Replies
1
Views
377