View previous topic :: View next topic |
Author |
Message |
Frankeman
Joined: 11 Aug 2004 Posts: 948 Location: the Netherlands
|
Posted: Thu Oct 18, 2018 9:38 am Post subject: Encoder debounce |
|
|
Hi,
For the correct working of the Encoder statement is it possible to connect the encoder directly to the AtMega or must there some kind of debounce filter be integrated?
Frank.
(BASCOM-AVR version : 2.0.8.1 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Oct 18, 2018 10:03 am Post subject: |
|
|
the idea is to connect it directly. I never used a filter, but maybe other users do? _________________ Mark |
|
Back to top |
|
|
Frankeman
Joined: 11 Aug 2004 Posts: 948 Location: the Netherlands
|
Posted: Thu Oct 18, 2018 12:33 pm Post subject: |
|
|
At this moment i can't get the encoder working, i test the encoder with the program below:
Code: |
$regfile = "m1281def.dat"
$crystal = 14745600
$baud = 19200
$hwstack = 64
$swstack = 64
$framesize = 64
dim EncVar as byte
config Com1 = 19200, Synchrone = 0, Parity = None, Stopbits = 1, Databits = 8, Clockpol = 0
open "COM1:" for binary as #1
config PINE.5 = input
config PINE.6 = input
set PORTE.5
set PORTE.6
config INT5 = Falling
on INT5 GetEncoderIsr
enable INT5
enable interrupts
do
loop
end
GetEncoderIsr:
EncVar = encoder(pine.5, pine.6, TurnRight, TurnLeft, 1)
return
TurnLeft:
print "Anti clockwise"
return
TurnRight:
print "Clockwise"
return
|
When i turn the encoder clockwise (or Anticlockwise) i see the following on the terminal:
Clockwise
Anti clockwise
Clockwise
Anti clockwise
Clockwise
Anti clockwise
Clockwise
Anti clockwise
Clockwise
Anti clockwise
etc....
I would expect to see:
Clockwise
Clockwise
Clockwise
Clockwise
Clockwise
etc...
When i turn the encoder clockwise.
Wat do i wrong? |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Oct 18, 2018 1:37 pm Post subject: |
|
|
there are many different encodes. include the datasheet. and also, check the output signals with a scope. _________________ Mark |
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Thu Oct 18, 2018 3:33 pm Post subject: |
|
|
just some ideas:
- don't look for falling edge only for your int
- don't let the ENCODE statement wait
- pin change INT are very senstive to bouncing, it is possible that a bounce sets the INT flag again during the execution of isr (especial through the relative long print statement) and the INT is called a second time. Because you then wait for an encoder change the routine is going nuts.
- check your timing (duration of isr vs. frequency of your encoder) |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Thu Oct 18, 2018 5:06 pm Post subject: |
|
|
Frankeman wrote: |
Code: | config INT5 = Falling
on INT5 GetEncoderIsr
enable INT5 |
|
You can not detect every encoder condition by the falling edge on one encoder pin.
Set up a timer and put encode() there, it is safer in regards of constant cpu load, bouncing encoder contacts, etc. |
|
Back to top |
|
|
Frankeman
Joined: 11 Aug 2004 Posts: 948 Location: the Netherlands
|
Posted: Tue Oct 23, 2018 9:51 am Post subject: |
|
|
The encoder that is used is a Bourns PEC16.
This is a normal encoder that works the same as the encoder used to expain the working of the encoder statement in the help.
I am pretty sure it is a debouncing issue.
Can someone confirm that he (or she) is using an encoder directly connected to the Atmega and that it works fine with the encoder statement.
In the mean time i use the following code as a work around:
Code: |
do
EncVar = encoder(pine.6, pine.5, TurnRight, TurnLeft, 0)
loop
end
TurnLeft:
N1 = N1 + 1
N2 = 0
if N1 > 3 then
N1 = 0
DraaiRichting = Linksom 'Help var used by the menu routine
EncoderBediend = True 'Help var used by the menu routine
MenuTimeOutTeller = 0 'Reset display timeout
end if
return
TurnRight:
N2 = N2 + 1
N1 = 0
if N2 > 3 then
N2 = 0
DraaiRichting = Rechtsom 'Help var used by the menu routine
EncoderBediend = True 'Help var used by the menu routine
MenuTimeOutTeller = 0 'Reset Display timeout
end if
return
|
|
|
Back to top |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
Posted: Tue Oct 23, 2018 3:43 pm Post subject: |
|
|
Frankeman wrote: |
Can someone confirm that he (or she) is using an encoder directly connected to the Atmega and that it works fine with the encoder statement. |
I can confirm (Mark confirmed also above) that it works fine direct connected to an avr.
The decoder function works fine and isn't influence by the bouncing.
However in your code you also use an interrupt and that's (very) sensitive to bouncing as various people above try to explain to you.
If you turn the encoder the contact will close and fire the interrupt and starts the encoder function. However due the bounce the contact is opened again and closed again (this repeats several times) and the interrupt is fired again and therefore the encoder function is called again while the previous call to the encoder function is not finished yet, this will give unwanted behavior and unreliable code.
Most of the time it's enough to poll the encoder in the main loop. With the clock you use thus will be done 10000 times as second, else the option from MWS working with a timer to poll the encoder is also a good option. _________________ www.evertdekker.com Bascom code vault |
|
Back to top |
|
|
Frankeman
Joined: 11 Aug 2004 Posts: 948 Location: the Netherlands
|
Posted: Tue Oct 23, 2018 6:14 pm Post subject: |
|
|
Hi Evert,
In the code from my last post the Encode statement is within the main loop between Do and Loop.
So there is no interrupt involved any more.
For each click the routine Turnleft or Turnright is called 4 times.
I can imagine that this sample program is fast enough the "see" the debounce effect.
In the real program there is lots of code in de mainloop and from there i get also 4 call's the Turnleft or Turnright.
Next i will try the advise from MWS to see what Encode does when i put the Encode statement in the interrupt routine of a timer. |
|
Back to top |
|
|
|