Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

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.
 

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
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;
}
}
}



 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
47,844
Helped
14,117
Reputation
28,491
Reaction score
12,799
Trophy points
1,393
Location
Bochum, Germany
Activity points
277,710
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.
 

alexxx

Advanced Member level 4
Joined
Apr 17, 2011
Messages
1,013
Helped
273
Reputation
552
Reaction score
270
Trophy points
1,383
Location
Greece
Activity points
7,938
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.
http://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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top