Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 21st March 2006, 05:47 AM   (permalink)
Default optical encoders?

Hi,

I hope someone out there has done this in assembly before.
I took apart a mouse, and removed the encoder wheels, along with the IR Led's.
I want to use this, as an encoder for an accelerator, using 2 bit gray code.
In other words, when thumb pressure is applied to a spring returned knob, it send gray code to micro. 16f628.
I can't use interupt as it is all ready used by a safety switch. My delema is knowing the direction(accel or deccelerate)the encoder is going. As the encoder is moved clockwise, the micro needs to increase speed of dc motor, and decrease when moving ccw. My guess is ,I should get 4 different speeds. Right?

I have searched this forum, and others, and there search engines sucks..with no good results.
I have googled, and everybody including there dog, has done this.
Unfortunately, I don't have time learning CHBasic or even C. I write in assembly exclusively.
I hope someone can help.
Thank you!
hjl4 is offline   Reply With Quote
Old 21st March 2006, 08:18 AM   (permalink)
Default Re: optical encoders?

Quote:
Originally Posted by hjl4
Hi,

I hope someone out there has done this in assembly before.
I took apart a mouse, and removed the encoder wheels, along with the IR Led's.
I want to use this, as an encoder for an accelerator, using 2 bit gray code.
In other words, when thumb pressure is applied to a spring returned knob, it send gray code to micro. 16f628.
I can't use interupt as it is all ready used by a safety switch. My delema is knowing the direction(accel or deccelerate)the encoder is going. As the encoder is moved clockwise, the micro needs to increase speed of dc motor, and decrease when moving ccw. My guess is ,I should get 4 different speeds. Right?

I have searched this forum, and others, and there search engines sucks..with no good results.
I have googled, and everybody including there dog, has done this.
Unfortunately, I don't have time learning CHBasic or even C. I write in assembly exclusively.
I hope someone can help.
Thank you!
There's more assembler code than anything else available, so using assembler is an advantage.

However, I don't think your scheme will work?, a mouse encoder doesn't output grey code, it's just a quadrature encoder. So you only get speed and direction, NOT absolute position.

The conventional way for throttle control is to use a pot.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline   Reply With Quote
Old 21st March 2006, 09:19 AM   (permalink)
Default

Correct me if I'm wrong, but, it seems you would like to use one of the encoders for a speed control by determining if there's been a change and by the direction of the change, clockwise or anti-clockwise...

Polling the encoder A and B lines is perfectly acceptable... You can determine if the encoder has moved simply by checking a saved ENCOLD value to the current encoder value... You can determine which direction the encoder has moved by exclusive-or'ing the A or B bit of the old encoder value with the opposite bit of the current encoder value... After determining direction, adjust your speed value or variable up or down accordingly...

Good luck with your project... Regards, Mike

Code:
;
;  check the Rotary Encoder A and B switches for a change (the
;  SWDATA variable contains debounced switch data)
;
ISR_Encoder
        movf    SWDATA,W        ; get switch data  '------BA'    |B0
        andlw   b'00000011'     ; mask for B and A switches      |B0
        xorwf   ENCOLD,W        ; same as last reading?          |B0
        bz      ISR_NextColumn  ; yes, branch (no change)        |B0
        xorwf   ENCOLD,W        ; else restore encoder bits in W |B0
        rrf     ENCOLD,f        ; prep for B-old ^ A-new         |B0
        xorwf   ENCOLD,f        ; ENCOLD bit 0 = direction       |B0
        rrf     ENCOLD,f        ; now Carry bit = direction	   |B0
        movwf   ENCOLD          ; update ENCOLD with new AB	   |B0
;
Mike, K8LH is offline   Reply With Quote
Old 21st March 2006, 11:42 AM   (permalink)
Default

Thanks Mike and Nigel.
Last night I was very tired, as I worked on this all day.
And I could'nt visualize how this could be done.
Your right Nigel when you say it would'nt be grey code. What was I thinking?
And Mike I will definately go your way. Now that I have slept, I am refreshed, and now I can see the whole picture.

What sleep can do.
If I get stuck on this, I'll make sure to follow up. And if success, I will post my code here, to see if it could be optimized in any way.

Thanks Guys.
hjl4 is offline   Reply With Quote
Old 21st March 2006, 01:18 PM   (permalink)
Default

Before we start finding solutions to your problem, I have some issues with your assumptions:

Quote:
I can't use interupt as it is all ready used by a safety switch.
By decoding which interrupt flag was set, you can use interrupts from several sources simultaneously.

Unless you need response times in order of microseconds, you don't really need interrupts to detect the safety switch closure. With a properly designed program, you can poll the safety switch once every 1-10 msec for instance. This is still faster than how most mechanical systems can respond anyway.

Quote:
I took apart a mouse, and removed the encoder wheels, along with the IR Led's.
I want to use this, as an encoder for an accelerator, using 2 bit gray code.
It might be more of a pain to remanufacture the mouse into an optical encoder. It will be easier to find a PS2 mouse program to receive the step and direction signals already encoded in the serial PS2 protocol. You will need to use interrupts here though. Hence, if you insist on using interrupts for the safety switch, you would need to decode the interrupt source to use two interrupts simultaneously. Or you could also use a serial mouse.

If you convert the mouse into an optical encoder, you need to poll the signals at high speed to detect transistions. If you miss two transistions between polls, you can no longer determine the direction last taken. Interrupts will be useful here.

BTW, the quadrature signal from the encoder is in 2-bit gray code.
http://www.nist.gov/dads/HTML/graycode.html

Lastly, there are PICs with built-in quadrature decoders.
__________________
"Having to do with Motion Control"
motion is offline   Reply With Quote
Old 22nd March 2006, 06:43 AM   (permalink)
Default

Polling could be hokey if the motor speed is significant. You need to dedicate a lot of time to polling.

Polling could be done, but from what you say, you may not need to do this. You say the ext interrupt is taken up by a safety switch, and in any case a single interrupt won't read gray code. That's what the interrupt-on-port-change RBIE/RBIF is for. This will cover any changes in portB<7:4> pins configured as inputs, so any of these 4 you don't use as encoder inputs should be used for an output to avoid useless interrupts.
__________________
I thought what I'd do was I'd pretend I was one of those deaf-mutes.
Oznog is online now   Reply With Quote
Old 22nd March 2006, 11:55 AM   (permalink)
Default

Thanks Oznog,

I thought I had read that somewhere. So if I set up Portb as interupt, lets say bit 1 and 2 are inputs and the rest outputs, I can still have bit 3 and 4 as outputs?
If so, That would save me from trying to see what i can squeeze out of the Pic. I'll have plenty of pins left over.
The Datasheets can be so overwhelming sometimes.
I'll read it again, and see if I can set it up that way. It would save precious time, instead of frequent continuous polling.



Thanks.
hjl4 is offline   Reply With Quote
Old 26th March 2006, 11:46 AM   (permalink)
Default

Quote:
Originally Posted by Oznog
Polling could be hokey if the motor speed is significant. You need to dedicate a lot of time to polling.
We're only talking about polling the rotary encoder which is being used as a control, not polling a rotary encoder attached to a motor...

Polling (and debouncing) the encoder switches, or any switches for that matter, can be automated as a background task quite easily using an ISR (interrupt service routine)... I often use TMR2 1.0-msec interrupts and ISR code for switch management to provide such niceties as switchpress audio feedback (40-msec short beeps), 'short' and 'long' switchpress detection, and rotary encoder translation to pseudo "INC" and "DEC" switches...

Kind regards, Mike
Mike, K8LH is offline   Reply With Quote
Old 27th March 2006, 08:36 AM   (permalink)
Default

I'd definitely use RBIF but timer driven sampling should work as well. You can pretty easily estimate the speed of of the quad encoder pulses. 1mS might be a bit slow as you should be sampling at least 2X the max rate.

I've played with mouse encoders and some of them can be flakey. I wound up building a state machine that factored in glitches and it worked pretty well. just consider all possible transitions and you should be fine. I used the RBIF (int on change) feature of a midrange PIC.
philba is offline   Reply With Quote
Old 27th March 2006, 09:17 AM   (permalink)
Default

hjl4 (and gang),

As you can see, there's many many different ways to accomplish the same thing... Don't be afraid to experiment and try different methods... The interrupt on change method is certainly just as viable as an interrupt driven polling method and there are subtle and not so subtle advantages and disadvantages to each method... Many times your hardware design may force you to use a particular method...

I've found rotary encoders with a built-in push button switch on the shaft (PEC11 series and NSE120 series) can provide rather simple and elegant single front panel control solutions for some projects... Rotate the encoder to scroll through LCD menus, select values from a list, or to increment or decrement values... Decode 'short' and 'long' encoder push button switch presses for <OK>, <ESC>, or <TAB> type functions...

Regards, Mike
Mike, K8LH is offline   Reply With Quote
Old 27th March 2006, 11:58 AM   (permalink)
Default

Hi Mike,

I have'nt had time to work on my encoder or program.
I 'm trying to read thru your first reply, and I'm having a bit of problem making sense of it.
If I use quadrature encoder, and turn it beyond it's 2 bits(3h), how does the Pic know if I am rotating it clockwise or ccw if I go beyond the 2 bit limit. I mean after 01,11,10,00, and then what???. Does the Pic see this as a reset back to 01?
I have an encoder with total of 25 steps.(optical that is).

Maybe I'm way over my head here.
I can't seem to grasp the concept.

Anyways,Thanks for your replies.
hjl4 is offline   Reply With Quote
Old 27th March 2006, 12:33 PM   (permalink)
Default

Quote:
Originally Posted by hjl4
Hi Mike,

I have'nt had time to work on my encoder or program.
I 'm trying to read thru your first reply, and I'm having a bit of problem making sense of it.
If I use quadrature encoder, and turn it beyond it's 2 bits(3h), how does the Pic know if I am rotating it clockwise or ccw if I go beyond the 2 bit limit. I mean after 01,11,10,00, and then what???. Does the Pic see this as a reset back to 01?
I have an encoder with total of 25 steps.(optical that is).
It's a question of which bit changes first, this tells the program which way it's turning.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline   Reply With Quote
Old 27th March 2006, 12:37 PM   (permalink)
Default

You can define a rectangular area so that you can create absolute positions with your mouse.

In your code, you need to compare whether each and every mouse move goes out of the area, and if it does, have it return to the last spot in the area.

It's just like the mouse arrow on your screen. It never falls off of the screen completely. You still see at least one pixel even if you moved it to the bottom right.
__________________
-=: The best low-priced components to troubleshoot with are the speaker and the LED :=-
mstechca is offline   Reply With Quote
Old 27th March 2006, 12:44 PM   (permalink)
Default

Ok I'll try that .

If I still can't get it, I'll be back.

Thanks.
hjl4 is offline   Reply With Quote
Old 27th March 2006, 06:52 PM   (permalink)
Default

you don't have to kill yourself with loads of code if you can live with one more chip (dual D-type flip flop). this way all you have to do is count up or down:
http://www.engr.colostate.edu/~dga/m...gures/9-16.gif
panic mode is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes




All times are GMT. The time now is 05:44 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.