Jump to content

Rotary encoder issue, help needed!

Recommended Posts

Hey all, I'm completely stumped on a bug for my design day project.  I've got a rotary encoder that is used to change a value on an LCD screen.  The code works perfectly when the encoders state at power up is 00.  However if either pin is high, it doesn't work at all.  Some transitions will cause the value on the screen to move in the wrong direct, and some transitions don't seem to trigger an interrupt at all.


This is the code I'm using and would LOVE some help!  Is it possible that the MSP430 is recording the initial values as reference voltages causing subsequent interrupts to not work?



#pragma vector=PORT2_VECTOR //Port 2 interrupt triggered by either rotary encoder pin
      __interrupt void Port_2(void) {
     //int gray0=P2IN&GRAY0;
     //int gray1=P2IN&GRAY1;
      if(P2IFG&GRAY0) {
      (P2IN&GRAY1)? parameter++ : parameter-- ;
      if(parameter>999) parameter=0;
      else if(P2IFG&GRAY1) {
      (P2IN&GRAY0)? parameter++ : parameter-- ;
      if(parameter<0) parameter=999;
      inactivity=0;  //interaction has been seen, so reset the inactivity counter


Link to post
Share on other sites

The CPU definitely references it correctly, although IIRC it only fires on transitions not levels.  So if GRAY0 is already set when the MCU starts, the next event will look like GRAY1 was the one hitting its interrupt.  I think the issue is in your code, it might be a bit too simplistic for the task, I've done this before but it was on AVR with the PinChangeInterrupt which fires on any transition up or down... Take a look at RobG's link.

Link to post
Share on other sites

Ok so I'm actually using RobGs code now plugged into my interrupt.  P2IES=0, and the pullup resistors are turned on.  Turning the switch either direction it only counts down, and seems to fire multiple times per transition.  (I'm about to try debouncing this)

Link to post
Share on other sites

Definitely recommend debouncing.  Also I've seen a confusing one before, a rotary encoder I bought off mouser used gray code but it had "detents" which resulted in strange behavior--the two pulses were far apart initially (as the dial moved between detents), but both fired almost simultaneously (or well within the scope of interrupt service routine latency) when it reached the next detent ("click" in the rotation).  Required significant rework of the code and I only caught it with my Logic Analyzer running at high speed.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...