Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

problem with rotary encoder

Status
Not open for further replies.

mohamed.elsabagh

Full Member level 2
Joined
Oct 27, 2010
Messages
145
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,298
Activity points
2,320
I am using a rotary encoder with a switch button, I am using c8051f340 microcontroller to detect the encoder movement on external interrupt, but the problem is that sometimes the code detect the clock wise movement as anticlock wise and vice versa i don't know what is the wrong, is there a problem with the rotary encoder itself.
 

void External_Interrupt1() interrupt INTERRUPT_INT1
{
// the interrupt is on negative edge trigger of B

if(encoder_state != encoder_unknown) ///// The last state of the encoder is not yet read by other layers
{
return;
}
else
{
//// This code applies only when B is on interrupt
if((ENCODER_PORT & ENCODER_A_MASK) == 0x00) /// check wether A is 0 or 1
{
encoder_state = encoder_CW;
}
else
{
encoder_state = encoder_CCW;
}
}
}



 

To get full encoder resolution, you'll want to process all edges of A and B output. PIC port change interrupt is a good way to implement it. For moderate encoder speeds, a fast timer interrupt can do as well.

I wonder about the purpose of huge 100 nF capacitors parallel to the encoder switches. They are causing unwanted delays and large discharge currents that stress the switches.
 

You must implement a state machine to get direction and speed. Read wikipedia's article especially the "Incremental rotary encoder" part. Both state machines for the two directions are shown in the two tables there.
https://en.wikipedia.org/wiki/Rotary_encoder

Make sure that the pin change interrupt has a high priority, otherwise you will lose counts and this will drive your algorithm to fault conclusions.

Also it is common practice to use schmitt trigger gates in series with the A, B signals.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top