View previous topic :: View next topic |
Author |
Message |
karlos
Joined: 03 Apr 2015 Posts: 107
|
Posted: Wed Apr 13, 2016 12:41 pm Post subject: detection rotation direction using Encoder function |
|
|
Hi
I am using int0 and int1 in pin change mode in combination with encoder function.
for detection of direction i should add 2 sub as mentioned in help:
Var = ENCODER( pin1, pin2, LeftLabel, RightLabel , wait)
i use LeftLabel and RightLabel just for assign a bit to 0 or 1 for it.
if there is any other way to detect direction without going to sub, it would decrease time and code consuming and also may improve some issue related to stack in my program in very fast pulses come from encoder.
i think Mark use some variable to detect direction, if i can use this variable,i dont need to use this subs.
does any one use this way?
thanks.
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
karlos
Joined: 03 Apr 2015 Posts: 107
|
Posted: Thu Apr 14, 2016 7:00 am Post subject: |
|
|
an another question:
then type of variable that can be assigned by result of encoder function is not specified.
thanks |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Thu Apr 14, 2016 9:21 am Post subject: |
|
|
karlos wrote: | an another question: |
Where is the question?
Quote: | then type of variable that can be assigned by result of encoder function is not specified. |
|
|
Back to top |
|
|
karlos
Joined: 03 Apr 2015 Posts: 107
|
Posted: Thu Apr 14, 2016 10:15 am Post subject: |
|
|
Quote: | Where is the question? |
an another question:
the type of variable that can be assigned by result of encoder function is not specified.which types can by assigned by result of encoder function?
thanks |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Thu Apr 14, 2016 10:16 am Post subject: |
|
|
hi,
Ijust post an example of rotary encoder home made function see forum bascom-arduino
jp |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
|
Back to top |
|
|
karlos
Joined: 03 Apr 2015 Posts: 107
|
Posted: Thu Apr 14, 2016 2:39 pm Post subject: |
|
|
Duval JP wrote: | hi,
Ijust post an example of rotary encoder home made function see forum bascom-arduino
jp |
thanks Duval |
|
Back to top |
|
|
karlos
Joined: 03 Apr 2015 Posts: 107
|
Posted: Thu Apr 14, 2016 2:46 pm Post subject: |
|
|
MWS wrote: | karlos wrote: | which types can by assigned by result of encoder function? |
There's encoder.bas in the samples folder, excerpt:
Code: | Dim B As Byte 'we have dimmed a byte because we need to maintain the state of the encoder
B = Encoder(pinb.0 , Pinb.1 , Links , Rechts , 1) |
Better open your eyes, before you roll 'em. |
Dear MWS
thanks
if you test the function, you should see it work with other types of variable,such as word,integer...with completely open eyes!
but i want to use single or long and i want to sure that it work corectly with this type of variables.
and also i did not get my answer about detecting direction without going to sub!
thanks |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Thu Apr 14, 2016 4:08 pm Post subject: |
|
|
karlos wrote: | single or long |
I have no way to test - you have.
From my logic understanding I'd say any integer variable is ok, while floats, aka single and double are not useful for this purpose, because of their inherent rounding errors. |
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Thu Apr 14, 2016 4:32 pm Post subject: |
|
|
Maybe you missunderstand the role of the variable. the var holds only the previous state of the two encoder signals, so the stored information is 2 Bit long. So a byte variable does fit.
The ENCODE() function does not count, this has to be done in the two subs, there you can use any variable as you like, even SINGLE if it helps you.
Helpfile explains, how the routine works.
If you have trouble with very fast signals you have to write your own assembler routine and call it in a pin change interrupt with nosave. |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Thu Apr 14, 2016 6:35 pm Post subject: |
|
|
laborratte wrote: | Maybe you missunderstand the role of the variable. |
He wants to try, why not, maybe he achieves something new.
I've already posted that:
Quote: | 'we have dimmed a byte because we need to maintain the state of the encoder |
And he did read and quoted it, so it must be his desire to try it out |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Thu Apr 14, 2016 6:40 pm Post subject: |
|
|
The question is what do you want to do with an rotary encoder ?
it is something like that ?
if yes, don't look about speed with this kind of material, it just make to use as a digital poti.
JP |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Apr 14, 2016 8:10 pm Post subject: |
|
|
I think by now it is clear?
the variable you assign is used to maintain the state. it should be a byte, but feel free to use something else.
the encoder just detects left or right direction. you add a variable yourself for example :
goleft:
volume=volume+1
return
goright:
voulme=volume -1
return
where volume can be any numeric variable you like/need. _________________ Mark |
|
Back to top |
|
|
karlos
Joined: 03 Apr 2015 Posts: 107
|
Posted: Thu Apr 14, 2016 9:16 pm Post subject: |
|
|
thanks a lot
yes, i misunderstand that.
i have a encoder produce about 400 pulse per second.i know that 400 pulse is not a bad and fast condition! but consider some other thing related to my project:
i should save encoder position in eeprom pulse by pulse,because in any position if power disconnected,we should never miss absolute position.
the controller should work without any battery.
i also use an graphical lcd to display rotation value in a big digit that take a big time to refresh.
i use timer0 for timing and save the time events.
i use timer1 for capturing pulses come from another device and i should save about 600 pulses in microsecond periods
i should poll inputs and check for many changes in input commands.
i have about 20 sub in my code.
my most problem is related to stack.most times the microcontroller hangs and i think it related to stack.i try to increase soft and hard stack and frame up to 512 and problem does not solve.
so i want to decrease subs and executing interrupt run time as possible.
i use my own function for read encoder before,but it was not code efficient.
so i want to try using encoder function,but i think this way by using 2 more sub,my problem will increase!
if you have any suggestion,it would help me.
and about encoder function:
i read whole forum and i know that many user want to use quadrature encoder and they use their functions instead of bascom encoder function because of nature of time critical of fast encoder pulses.
i think it was possible for MARK to change the encoder routine so that the assigned value hold the rotation value, and also a bit save direction. left and right sub may be optional.i think it would be more powerful this way. it is just my idea, and i know that Mark consider so many other things before writing that code. |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Apr 14, 2016 9:43 pm Post subject: |
|
|
I probably do not understand your project but i think the way these encoders differ from normal volume knobs, is that you can keep them turning, so everything is relative and not absolute.
when storing to eeprom, i would advise to either battery backup, and save only when your micro does not do something else because writing to eeprom is limited ans time consuming.
you could use FRAM which is very quick.
the encoder is just a basic block and flexible. when you do not want to miss a pulse, just call the function on the PCINT interrupt. Using PCINT is the best way.
a stack of 512 makes no sense at all. it is way too much for a normal non recursive program. so the problem is likely somewhere else.
but take in mind : when servicing an interrupt, other interrupts are blocked.
before you write your code it is good to make a flow chart.
it would be nice to post a link to the encoder you use. _________________ Mark |
|
Back to top |
|
|
|