athornsb 0 Posted April 24, 2013 Share Posted April 24, 2013 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; P2IFG&=~GRAY0; } else if(P2IFG&GRAY1) { (P2IN&GRAY0)? parameter++ : parameter-- ; P2IFG&=~GRAY1; if(parameter<0) parameter=999; } inactivity=0; //interaction has been seen, so reset the inactivity counter } Quote Link to post Share on other sites
RobG 1,892 Posted April 24, 2013 Share Posted April 24, 2013 Search the forum for "encoder," you will find some code examples, like this one. Quote Link to post Share on other sites
spirilis 1,265 Posted April 24, 2013 Share Posted April 24, 2013 How is your P2IES register configured? Quote Link to post Share on other sites
athornsb 0 Posted April 24, 2013 Author Share Posted April 24, 2013 It is configured for 0 to 1 detection (P2IES=0x00). It seems like maybe the processor isn't referencing ground/0 correctly. Quote Link to post Share on other sites
spirilis 1,265 Posted April 24, 2013 Share Posted April 24, 2013 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. Quote Link to post Share on other sites
athornsb 0 Posted April 24, 2013 Author Share Posted April 24, 2013 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) Quote Link to post Share on other sites
spirilis 1,265 Posted April 24, 2013 Share Posted April 24, 2013 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. athornsb 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.