Categories
Statistics
Flag Counter
Since 08.08.2014
Counts only, if "DNT = disabled".

Your IP is 54.172.169.199
ec2-54-172-169-199.compute-1
Info
Valid HTML 4.01 Transitional Creative Commons Lizenzvertrag
rss
เราจะทำแบบวิศวกรผู้ยิ่งใหญ่
We love the King
19. March 2024
Your valuable opinion :
5 stars

Avg. 6.00 from 10 votes.



ROTARY-ENCODER.php    10992 Bytes    16-05-2020 14:37:16


Micro-Projects • Tools : The Rotary Encoder


Functional Description and Code





Micro-Projects • Tools : Rotary Encoder



A Rotary Encoder is a handy part to enhance any microprocessor based project. You may drill the prontpanel and add functionality later in using an 'updated' user menue. This comfort of course has its price. You need (at least) two interrupt routines to handle this thing. Here, we show you how it can be done. A short glimpse on that debounceing stuff as well as the sourcecode and a lot of measurements make this almost a mini-tutorial.




What is inside ?




The rotary encoder consists of a stator and a rotor. The rotor consists of a disc which is mounted on an axe. It has 30 notches at its perimeter, where a bolt with a spring locks. It also has 15 conducting and 15 non-conducting sectors. The stator consists of two times two spring contacts, which are closed (connected), when they both touch a conductive sector. As there is always a non-conductive area opposide to a conductive area, there is always one switch closed (in rest position).






How does it work ?




In order to generate useful signals with this thing, two pull-up resistors and two debouncing capacitors are added. Drawing here. Depending on the direction of rotation, two rectangular waveforms are generated. The left picture shows a clockwise rotation (rising slope coincide), on the right side (counter clockwise rotation) the falling slope coincides.




The debouncing capacitors suppress the bouncing (noise), which is added by evil rotary encoder manufacturers. This mayst also be done in software, but we like to counteract the problem at the source. On the left picture, you see, how it looks. This may be missunderstood by your microprocessor. On the right side we have unambiguous states. The pull-up resistor was R = 1 kΩ, the capacitor C = 100 nF. Other combinations are of course also possible. This may even depend on your hardware structure.






Software




As we are dealing with two signals (three, if you also use the push-switch), we need two interrupt routines to handle that. The plan is, that one is triggered on the falling edge, whilst the other triggers on the rising edge. Then the interrupt service routine has to read immediately the other pin. This will result in four possibilities, leading to two results : clockwise or counter-clockwise. It is recommnended, that you do not pack too much evaluation in the interrupt routines in order to garantee, that they may finish before the next interrupt occurs. We use the following code (Codevision, PORT E.6 and PORT E.7) to do this :

// External Interrupt 6 service routine, rising edge
interrupt [EXT_INT6] void ext_int6_isr(void)
{
bit AUTRE;
AUTRE=PINE.7;
if ( AUTRE == 1 ) DO SOMETHING ; // CLOCKWISE
if ( AUTRE == 0 ) DO SOMETHING ; // COUNTER CLOCKWISE
USER_ACTION=1;
}

// External Interrupt 7 service routine, falling edge
interrupt [EXT_INT7] void ext_int7_isr(void)
{
bit AUTRE;
AUTRE=PINE.6;
if ( AUTRE == 1 ) DO SOMETHING ; // CLOCKWISE
if ( AUTRE == 0 ) DO SOMETHING ; // COUNTER CLOCKWISE
USER_ACTION=1;
}




Downloads








✈ Share your thoughts



The webmaster does not read these comments regularely. Urgent questions should be send via email. Ads or links to completely uncorrelated things will be removed.


Your Browser says that you allow tracking. Mayst we suggest that you check that DNT thing ?

 
t1 = 6459 d

t2 = 281 ms

★ ★ ★  Copyright © 2006 - 2024 by changpuak.ch  ★ ★ ★

PRchecker.info Impressum