SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on how to get your MSP JTAG programmer up and running.
By scoot
#23957
Hi,

I've got a shaft encoder interfaced to the msp430, and I'm using interrupts to determine the direction of turning. The shaft encoder has phase A and phase B. The interrupts are set up as following: (Shaft encoder inputs P1.1,P1.2)
Code: Select all
//Port1:	
		//		Port External Functions:
					P1SEL=0x00;
		//		Port Setup outputs:
					P1DIR=BIT3+BIT5;
		//		Port Interrupts:
					P1IE=BIT1+BIT2;
					P1IES=0x00;			// select triggering from low to high
					P1IFG=0x00;         // clear all interrupts
The interrupt routine:
Code: Select all
        if(P1IFG&BIT1){
		if(SHAFT_A)	shaft_direction=1;  // if phase A is high, we are going CW
		P1IFG&=~BIT1; // clear interrupt
	}
	if(P1IFG&BIT2){
		if(SHAFT_B)	shaft_direction=2; // if phase B is high, we are going CCW
		P1IFG&=~BIT2; // clear interrupt
	}
Now, the problem is:
Interrupts are normally generated when a transition happens from low to high. However, I've noticed that every now and then, my ISR exectues when one of the phases goes from high to low <- this shouldnt be happening.

(I am using IAR v3.21A) to debug, and monitor the interrupts

Has anyone encountered anything similar? Know a solution? Or could it be that the MSP430 has a problem.

BTW, i am using MSP430F149

Thanks,
Sam
By scoot
#24018
I think I know what the problem is:

The shaft encoder is a contact type shaft encoder. As such, you will get some bouncing when the contact is made. When I designed the board, I didn't think about putting any filtering on the board. So in the end, it is probably not a firmware issue, its a hardware issue.
By Rochey
#53045
What happens when one of the outputs goes low? Your interrupts are only triggering when something goes high, not when something goes low.

Looking at:
http://upload.wikimedia.org/wikipedia/e ... am.svg.png
(source: wikipedia)

It strikes me that driving interrupts would only react on stages 1->2 and 2->3

maybe i'm being a little slow on a saturday morning, but aren't you missing half of the "ticks"? (3->4 and 4->1)
By BillS
#53114
If you only trigger on the rising edges, it is called quadrature 2x. It is perfectly acceptable, depending on your needs. Quadrature 4x triggers on all edges and has twice the resolution of 2x. Quad 4x is more common because of the extra resolution.
By john methew
#60131
No You can also use Quad 4x but for this you may fit more transistors and their section will be extra. When interruption will starting it automatically add these extra transistors.
By Philba
#60163
I don't understand the last post.

If the switches are mechanical, you are definitely seeing bounce. (this is why the good encoders are optical). You may or may not be able to solve it with a low pass RC filter. Worth a try. I'd calculate the bounce frequency and construct a low pass filter to cut it off. Schmitt triggers may help in conjunction with the LPF.

Depending on the shaft speed (i.e. interrupt rate), you may be able to do SW debounce. It depends on how much time you have to debounce a given switch.

You could also take advantage of the bounce. If you get an int but the input pin reads wrong (i.e. it's low when it should be high), just ignore it. eventually, it will interrupt and read correctly. This presumes that the other pin isn't changing.

BTW - My favorite approach to quadrature is to use a simple state machine. a 4x4 array encodes all possible current and previous inputs. It also determines error states.

edit: thinking about it, Bounce really isn't a problem for direction. Since you will see the int from the positive edge, why do you care about the value of the pin causing the interrupt? What you care about is the value of the other pin. Given that the switches are in quadrature, the other pin should be stable (or you've got a really cr*ppy encoder). So, even if the interrupting pin is bouncing around, you know it will settle to high. This is an issue for determining shaft speed, of course, but it will work for direction.