Shop OBEX P1 Docs P2 Docs Learn Events
Interrupt for reading encoder? — Parallax Forums

Interrupt for reading encoder?

ClintClint Posts: 95
edited 2007-02-08 03:17 in General Discussion
I'm new to the SX (coming from Basic Stamp) and don't understand what the interrupt is all about. Can someone explain it or direct me to some place that explains it?

I am using SX/B for something similar to the encoder example shown in the SX/B help. The example·uses an interrupt for reading·an encoder and updating a display. Why use an interrupt rather than include these functions in the main program?

Thanks for any help.

Comments

  • thoseythosey Posts: 7
    edited 2007-02-07 22:28
    Okay, since an encoder can go faster than the speed of a complete scan of a program taking into account all the long loops and so on you are able to use an interrupt so that you do not miss a pulse count.·Think of this as a subroutine that can be set up either on a time base or an event base like a pin is triggered high.

    If your encoder is very slow updating and the programm runs faster than the encoder you might not need to do this with an interrupt.· Example a 15 cpr encoder that turn over every 15seconds might not need an interrupt. Since the program should be able to handle this.· But wouldn't you like to go into an interrupt routine every time a new pulse occurs.· This way you make sure that you handle all of your counts from the encoder.
  • JonnyMacJonnyMac Posts: 8,943
    edited 2007-02-07 22:56
    Since I wrote that demo, I'll answer: the program uses a 3-digit, 7-segment display that is best handled with an interrupt (multiplexing). By putting the encoder material in the interrupt it's handled automatically and the encoder value is refreshed at a known rate.
  • ClintClint Posts: 95
    edited 2007-02-07 23:57
    thosey said...

    Okay, since an encoder can go faster than the speed of a complete scan of a program taking into account all the long loops and so on you are able to use an interrupt so that you do not miss a pulse count.·Think of this as a subroutine that can be set up either on a time base or an event base like a pin is triggered high.

    If your encoder is very slow updating and the programm runs faster than the encoder you might not need to do this with an interrupt.· Example a 15 cpr encoder that turn over every 15seconds might not need an interrupt. Since the program should be able to handle this.· But wouldn't you like to go into an interrupt routine every time a new pulse occurs.· This way you make sure that you handle all of your counts from the encoder.



    Thank you for the explanation! That helps a lot. I will read the help file carefully·and figure out how to do asynchronous interrupts to monitor my encoder.
  • ClintClint Posts: 95
    edited 2007-02-08 00:08
    Okay I am confused on how to use the wake up enable (WKEN_B) and wake up edge select (WKED_B) registers. Do I understand correcly that if a bit is set to '0' in the WKEN_B register, the interrupt routine will be executed based on the corresponding port pin and the type of edge detection specifid in WKED_B? How can I get the interrupt routine to be exectured for ANY change in on a pin (rising OR falling)? Do I need to use WKPND_B somehow?

    I am reading the HELP and I am just plain confused on how to use WKEN_B, WKED_B, and WKPND_B to trigger an interrupt. Does anyone have an example I could look at?
  • BeanBean Posts: 8,129
    edited 2007-02-08 00:53
    Clint,
    You really only need an interrupt on one of the encoder signals, and only on rising OR falling edge.
    Let's say you have it on phase-A rising. In the interrupt you can simple sample phase-B to see if it is high or low. If it's high your moving in one direction, if low your moving in the other direction.

    Now that doesn't allow you to monitor all four states, but normally you don't need to.

    If you NEED to monitor both falling and rising edge, you can use two pins. Setup one for rising edge, and the other for falling edge. Or you could switch edge detects inside the interrupt routine.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com
    Stuff I'm selling on ebay http://search.ebay.com/_W0QQsassZhittconsultingQQhtZ-1

    "USA Today has come out with a new survey - apparently, three out of every four people make up 75% of the population." - David Letterman
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-02-08 03:17
    Bean,

    Any time you use different parts of the waveform for forward and backward edge counting you risk a miscount when the direction changes. Imagine, if you will, an encoder stopped on an edge that's only "sensitive" to a low-high transition. Then imagine the consequences of vibration about that edge. You'd get continuous counts in one direction and none in the other, even though the net rotation is zero.

    For the most accurate count, you really do need to interrupt on both edges of one channel. For example:

    A channel rising, B channel high = count up
    A channel falling, B channel high = count down
    A channel rising or falling, B channel low = no change

    In the SX, this can possibly be accomplished by inverting the WKED_B register during the ISR each time an interrupt occurs. I'm not sure, though, if this would lead to spurious or missed interrupts. That would depend on how the processor responds if the complementary edge arrives during the ISR but before WKED_B gets flipped. My guess is that the interrupt is really only sensitive to one edge and that the input pin is simply XORed with the corresponding bit in the WKED_B register. In this case flipping that bit would have the desired effect, without any glitches.

    But if it is a problem, a solution would be to use the timer interrupt at a rate at least 8x the fastest encoder frequency (assuming symmetrical waveforms). This would allow the waveforms to be sampled often enough to account for all relevant edges.

    -Phil
Sign In or Register to comment.