Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

ENCODER

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
sielcon

Bascom Member



Joined: 16 Mar 2006
Posts: 82
Location: Argentina

argentina.gif
PostPosted: Wed Sep 18, 2019 9:30 pm    Post subject: ENCODER Reply with quote

Hello everyone. I was trying to use the Keyes encoder and it really didn't work well with the ENCODER function, I did a small routine in interruptions per level that left me very satisfied.
I leave them in case someone can be useful.
Regards

Code:

'-------------test encoder rotativo Keyes ------------------------------------------

$regfile = "m328pdef.dat"
$crystal = 16000000
$baud = 19200
$swstack = 128
$framesize = 128

Config Submode = New


Dim Actu As Byte
Portb = &B111
Rota Alias Pinb.0
Rotb Alias Pinb.1
Config Button = Input
Config Rota = Input
Config Rotb = Input

Enable Interrupts
Enable Pcint0
On Pcint0 Isr_pcint0
Pcmsk0 = &B0000011
'------------------------------------------------------------------------------------------------------------------
Actu = 0

Do
Loop

End
'----------------------------------
Isr_pcint0:
If Rota = 0 Then
   Do
   Loop Until Rotb = 0
   Incr Actu
   Print Actu
   Goto Isr1
End If

If Rotb = 0 Then
   Do
   Loop Until Rota = 0
   Decr Actu
   Print Actu
End If
Isr1:
Do
   Rotab = Pinb
   Rotab = Rotab And 3
Loop Until Rotab = 3
Waitms 5
Return

 
Back to top
View user's profile Visit poster's website
Arek2014

Bascom Member



Joined: 30 Dec 2014
Posts: 22
Location: Kielce

poland.gif
PostPosted: Wed Sep 25, 2019 8:28 pm    Post subject: Reply with quote

Code:
config pind.1 = input

Config Int1 = Falling
On Int1 enkoder

dim a as byte

enable interrupts
enable int1

Do
Loop

enkoder:
If Pind.1 = 0 Then
Incr A
End If
If Pind.1 = 1 Then
Decr A
End If
Return
 


[img]
[/img]
Back to top
View user's profile
sielcon

Bascom Member



Joined: 16 Mar 2006
Posts: 82
Location: Argentina

argentina.gif
PostPosted: Fri Sep 27, 2019 3:50 am    Post subject: Reply with quote

Yes, this is the way to do it, but I use Int0 and Int1 for another application
Back to top
View user's profile Visit poster's website
CBailey

Bascom Member



Joined: 07 May 2015
Posts: 115

blank.gif
PostPosted: Mon Jan 23, 2023 5:53 am    Post subject: Reply with quote

Sorry to dig up an old thread. I'm trying to use an encoder on a Mega328p. I have it working fine using ENCODER, but it just sits there waiting for input. I tried compiling the above code, but got a bunch of errors I couldn't figure out. Should I just configure ENCODER to not wait, and put it in my Do-Loop, or are interrupts the way to go? I'll probably be using interrupts for timers later.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6197
Location: Holland

blank.gif
PostPosted: Mon Jan 23, 2023 10:17 am    Post subject: Reply with quote

encoder() must be called in a loop, or be triggered by an interrupt, for example a timer. but when you have pcint interrupt you could use that as well.
it depends a bit on the encoder and your app.
when you have problems, you always best show a small as possible piece of code of what you tried. the code posted earlier should work for you too.

_________________
Mark
Back to top
View user's profile Visit poster's website
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 338
Location: Japan

japan.gif
PostPosted: Mon Jan 23, 2023 11:12 am    Post subject: Reply with quote

Two rotary encoders are available.
Attach a pull-up resistor and a CR noise filter to the encoder input.
If the main routine goes around fast enough, it can be used without interrupts.
Code:

$regfile = "m88pdef.dat"
$crystal = 8000000
   '
$hwstack = 64
$swstack = 32
$framesize = 24

   '
   '  * Port name definition *
   '
   Encoder_1a Alias Pind.0                                  'Connection port for [A] pin of rotary encoder [1].
   Encoder_1b Alias Pind.1                                  'Connection port for [B] pin of rotary encoder [1].
   Encoder_2a Alias Pind.2                                  'Connection port for [A] pin of rotary encoder [2].
   Encoder_2b Alias Pind.3                                  'Connection port for [B] pin of rotary encoder [2].
   '
   '  * Declaring variables *
   '
   Dim Encoder1temp As Byte                                 'Saves the state before the change of the rotary encoder [1]. (b1,b0)
   Dim Encoder2temp As Byte                                 'Saves the state before the change of the rotary encoder [2]. (b1,b0)
   Dim Encoder1sta As Byte                                  'Rotation detection flag for rotary encoder[1]. (Rotate right: [1000_0111], Rotate left: [0100_1011])
   Dim Encoder2sta As Byte                                  'Rotation detection flag for rotary encoder[2]. (Rotate right: [1000_0111], Rotate left: [0100_1011])
   Dim Testdata1 As Byte                                    'Test data for rotary encoder [1].
   Dim Testdata2 As Byte                                    'Test data for rotary encoder [2].
   Dim Temp1 As Byte
   Dim Temp2 As Byte

   '
   '  * LCD initial settings *
   '
   Config Lcdmode = Port
   Config Lcdbus = 4
   Config Lcdpin = Pin , Db4 = Portb.3 , Db5 = Portb.2
   Config Lcdpin = Pin , Db6 = Portb.1 , Db7 = Portb.0
   Config Lcdpin = Pin , E = Portb.4 , Rs = Portb.5
   Config Lcd = 16 * 2
   Cursor Off
   Cls

   '
   Testdata1 = 0                                            'Initialize test data.
   Testdata2 = 0

Main:
   Do
      Gosub Encoderread                                     'Detects the rotation of a rotary encoder.

      '
      '  * Processing Rotary Encoder [1] *
      '
      Select Case Encoder1sta
         Case &B1000_0111 :                                 'If the rotary encoder [1] is rotating clockwise.
            Incr Testdata1
            Locate 1 , 1
            Lcd Testdata1 ; "  "
            Encoder1sta = 0
            '
         Case &B0100_1011 :                                 'If the rotary encoder [1] is rotating counterclockwise.
            Decr Testdata1
            Locate 1 , 1
            Lcd Testdata1 ; "  "
            Encoder1sta = 0
      End Select

      '
      '  * Processing Rotary Encoder [2] *
      '
      Select Case Encoder2sta
         Case &B1000_0111 :                                 'If the rotary encoder [2] is rotating clockwise.
            Incr Testdata2
            Locate 2 , 1
            Lcd Testdata2 ; "  "
            Encoder2sta = 0
            '
         Case &B0100_1011 :                                 'If the rotary encoder [2] is rotating counterclockwise.
            Decr Testdata2
            Locate 2 , 1
            Lcd Testdata2 ; "  "
            Encoder2sta = 0
      End Select
   Loop

   '
   '  ********************************************
   '  * Detects the rotation of a rotary encoder *
   '  ********************************************
   '
Encoderread:
   Temp1 = Encoder_1a                                       'Read pin of rotary encoder [1].
   Temp1.1 = Encoder_1b
   Temp2 = Encoder1temp Xor Temp1                           'Compare with the previous state.
   If Temp2 <> 0 Then                                       'Has the rotary encoder [1] changed?
      Encoder1temp = Temp1                                  'Store the state of the change.
      Shift Encoder1sta , Left , 2                          'Shift the bit state of the pulse by 2 bits to the left
      Encoder1sta = Encoder1sta Or Temp1                    'Add rotation bit state.
   End If
   '
   Temp1 = Encoder_2a                                       'Read pin of rotary encoder [2].
   Temp1.1 = Encoder_2b
   Temp2 = Encoder2temp Xor Temp1                           'Compare with the previous state.
   If Temp2 <> 0 Then                                       'Has the rotary encoder [2] changed?
      Encoder2temp = Temp1                                  'Store the state of the change.
      Shift Encoder2sta , Left , 2                          'Shift the bit state of the pulse by 2 bits to the left
      Encoder2sta = Encoder2sta Or Temp1                    'Add rotation bit state.
   End If
   Return

   End
 
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum