Rotary Encoder. Need help very much! - diyAudio
Go Back   Home > Forums > Source & Line > Digital Source

Digital Source Digital Players and Recorders: CD , SACD , Tape, Memory Card, etc.

Please consider donating to help us continue to serve you.

Ads on/off / Custom Title / More PMs / More album space / Advanced printing & mass image saving
Reply
 
Thread Tools Search this Thread
Old 22nd November 2005, 10:08 AM   #1
vytas is offline vytas  Lithuania
diyAudio Member
 
Join Date: Oct 2003
Location: Vilnius
Default Rotary Encoder. Need help very much!

Hello technicians,

I worked only with analog projects for a long time and now want to change my direction and make some stuff from digital section.

Some mentions:
I have PIC/AVR programmer;
Have some experience with “C” programming and no deal with ASM;
I already made single up/down counter with buttons;

And now I want to make single up/down counter with rotary (mechanical) encoder. This will be my the first section of preamp

I use legendary AVR chip AT90S2313
If it is possible I would like to avoid using INT’errupt interface however, my rotary encoder is connected to INT pins just in case if there are no other ways

So, can you give me an idea or advices how can I do this task

Thanking you in anticipation

There it is my inoperative “C” code and schema:
Attached Files
File Type: zip rotary.zip (98.9 KB, 77 views)
  Reply With Quote
Old 22nd November 2005, 04:07 PM   #2
bocka is offline bocka  Germany
diyAudio Member
 
Join Date: Jul 2003
Location: Hannover
This can be done in a simple way:

1. You need a task/interrupt or whatever, which is called every 5ms or so (longer than the bouncing time).
2. In the task look for a 0 -> 1 transition of pin A
3. if pin B is low the rotary switch is moved clockwise, if pin B is high the rotary switch is moved C.C.W

I think your program hangs in the while(!(PIND&pin6)&&!(PIND&pin7)) {} loop. At least it's not a good program style.
  Reply With Quote
Old 23rd November 2005, 05:02 AM   #3
vytas is offline vytas  Lithuania
diyAudio Member
 
Join Date: Oct 2003
Location: Vilnius
Thanks for replay Bocka,

You are right! In my case I have 2 way of problem solution:

1. Using interrupts, and checking the state of the pins before and after rotation of encoder;
2. Sampling the pins every n microseconds and see if their state has changed.

I’ll try to do this thing, but it seems to me it will take a long time to the paradise.
And thank you for critical of my programming style. I need this.
  Reply With Quote
Old 23rd November 2005, 06:17 AM   #4
diyAudio Member
 
Francis_Vaughan's Avatar
 
Join Date: Feb 2005
Location: Adelaide
What is typically done for these sorts of problems is to code a finite state automata to do the work. Doing it in the style you have is fraught with potential problems, and gets messy beyond belief as soon as any real complexity is needed.

A quadrature encoder is trivial to express as a FSA.

You code up a general FSA, which simply wakes up, looks at the input token, looks at the current state, and then does the transition. Then goes back to sleep. The input token can be triggered by any sort of interrupt - so you could even code the delays with a timer rather than a loop. If you can get an interrrupt on each input transition it all becomes very easy.
  Reply With Quote
Old 23rd November 2005, 06:54 AM   #5
seoman is offline seoman  Netherlands
diyAudio Member
 
Join Date: Apr 2004
Location: Nijmegen
I prefer to use "interupt on pinchange"
That way i never mis a rotation.

Although extreem fast turning tends to get misread, it's no real problem because i could never know if i made 30 or 40 turns.
So the software may be inaccurate aswell.

But missing a turn cause the software wasn't focussed. Not ideal!

Regards Simon
  Reply With Quote
Old 23rd November 2005, 07:06 AM   #6
vytas is offline vytas  Lithuania
diyAudio Member
 
Join Date: Oct 2003
Location: Vilnius
the situation is getting clearer and clearer. The scanning is too difficult for me at this moment so I decided to use interrupts. But at the firs I must to learn how to handle them. It is not very easy to find out the samples and examples in C.
  Reply With Quote
Old 23rd November 2005, 07:23 AM   #7
diyAudio Member
 
Francis_Vaughan's Avatar
 
Join Date: Feb 2005
Location: Adelaide
Yup, do the lot with interrrups. When you get an interrupt you first look about to see where it came from - i.e. timer, pin change. Then you simply step the FSA. Then go back to sleep an await the next interrupt (or in reality, return from interrrupt and allow the code to do the main stuff.) Coded like this you would have to connect your rotary encoder to a power drill before you got enough latency into the interrupt service routine to drop a step.

Whose C compiler are you using?
  Reply With Quote
Old 23rd November 2005, 08:06 AM   #8
bocka is offline bocka  Germany
diyAudio Member
 
Join Date: Jul 2003
Location: Hannover
Hi vytas,

although this is the code for the Mega8 it should work with minor changes with your AVR

// interrupt initialisation
// External Interrupt 0 initialization
// INT0: On
// INT0 Mode: Falling Edge

#define fInitInt0() { GICR |= 0x40; \
MCUCR |= 0x02; \
GIFR &= ~0x40; }


/************************************************** ***
External Interrupt 0 service routine
************************************************** ***/
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// insert your FSM code here
}

/************************************************** ***
main skeleton
************************************************** ***/
void main(void)
{
fInitInt0() ;
asm("cli")
while (1)
{
// insert your main application code here, i.e. switch relays
// turn LED on/off and so on
}
}

Quote:
I prefer to use "interupt on pinchange"
This works fine for optical encoders. With mechanical rotary switches always signal bouncing occurs. Using a timer is a elegant solution to solve this problem. But this I would call step 2 for interrupt beginners
  Reply With Quote
Old 23rd November 2005, 08:14 AM   #9
vytas is offline vytas  Lithuania
diyAudio Member
 
Join Date: Oct 2003
Location: Vilnius
Handle the interrupt is not as easy as reading value on pins. But I’ll do it some how. My C compiler is ICC avr 6.31A. Maybe you have some good advices where I must pay attention.

Thank you, all for giving right direction it saves me a lot of time
  Reply With Quote
Old 23rd November 2005, 08:32 AM   #10
seoman is offline seoman  Netherlands
diyAudio Member
 
Join Date: Apr 2004
Location: Nijmegen
When using 'interupt on pinchange' (NOT the same as INT0 )
you only know when to reed the pins.
Thats all.
But you have to service the interuptflag.

External interupts a commonly only active on a 'low to high' edge or 'high to low' edge. Rearly both! So this interupt is more intended for time measument.

The interupt on pinchange or keyboard interupt is intended for userinterface handling.

I use a electrical encoder and i only have bouce problems when spinning extremly fast. Let say a step/ 2ms.
Some RC filtering will improve that.
But maybe the decoding itselve has a large invluence on that aswell.


Here my encoder routine!
Comments in dutch. Sorry
Written with mplab asm for the pic16f876
It also has boundery checks.
So the encoder value cannot exceed its min and max value.
A keypin is checked to speed up rotation.

Feel free to translate to C and other processors

Regards Simon
Attached Files
File Type: txt encoder.txt (3.2 KB, 67 views)
  Reply With Quote

Reply


Hide this!Advertise here!
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the Rotary Encoder to control the cs3310 ? cac liu Digital Source 12 25th April 2013 10:59 AM
nice rotary encoder for audio? h_a Parts 4 14th February 2009 10:43 AM
Rotary encoder instead of buttons robmil Parts 11 16th April 2008 05:30 PM
Good rotary encoder for preamp project mbcouple Digital Source 4 31st October 2006 09:39 PM
Digital rotary encoder rho Parts 17 3rd December 2003 03:39 PM


New To Site? Need Help?

All times are GMT. The time now is 11:31 PM.


vBulletin Optimisation provided by vB Optimise (Pro) - vBulletin Mods & Addons Copyright © 2014 DragonByte Technologies Ltd.
Copyright ©1999-2014 diyAudio

Content Relevant URLs by vBSEO 3.3.2