Encodery jeszcze raz

Pytania, kody i porady dotyczące nie tylko Bascom.
ODPOWIEDZ
Awatar użytkownika
Henryk
Posty: 348
Rejestracja: 22 sty 2018, 17:20

Encodery jeszcze raz

Post autor: Henryk » 22 paź 2020, 13:35

Witam :D
Panowie , a możecie podpowiedzieć jak taki enkoder zmajstrować w oparciu o atmega 8 ?
Próbowałem zgłębiać przykłady Bartka ze strony domowej , tylko nie wiem jak wysłać te impulsy na port :oops: .
Moje nisko budżetowe cnc powoli powstaje ;)
IMG_20201021_144523.jpg
IMG_20201021_144557.jpg
Na początek chciałbym sterować tym ustrojstwem ręcznie nim zacznę z Linux cnc ( jest już zainstalowany na starym laptopie ) .
Nie masz wymaganych uprawnień, aby zobaczyć pliki załączone do tego posta.
Awatar użytkownika
niveasoft
Posty: 1216
Rejestracja: 17 sie 2015, 12:13
Kontakt:

Re: Xmega sprzętowa obsługa enkodera -to takie proste

Post autor: niveasoft » 22 paź 2020, 14:09

Tak dopiero teraz zauważyłem, że na stronie obiecałem zamieścić mój uniwersalny kod do enkoderów (taki kuloodporny), ale na Forum chyba go nie ma :D
Co ciekawe widziałem ten kod już na Elektrodzie - jakiś klient go tam sobie beztrosko umieścił.

Kod najlepiej się sprawdza na przerwaniach PCINT więc Mega8 odpada. Chcac mieć trzy enkodery najlepiej każdy oprogramować na oddzielnym porcie PORTB, PORTC i PORTD. Tak więc najmniejsza to chyba Mega88 a potem najtańsza Mega328P...

Kod zamieszczę później.
Awatar użytkownika
Henryk
Posty: 348
Rejestracja: 22 sty 2018, 17:20

Re: Xmega sprzętowa obsługa enkodera -to takie proste

Post autor: Henryk » 22 paź 2020, 14:23

Mega 328P mam ;)
Na chwilę obecną obszedłem temat podczerwienią , prostym " baranim " sposobem .
  1. $regfile = "m8def.dat"
  2. $crystal = 8000000
  3. $hwstack = 64
  4. $swstack = 36
  5. $framesize = 64
  6. 'Config Lcd = 20 * 4
  7. Config Portb = &B11111111 : Portb = &B11111111
  8. Config Portd = &B11110011 : Portd = &B11111111
  9. 'Config Lcdpin = Pin , Db4 = Portc.2 , Db5 = Portc.3 , Db6 = Portc.4 , Db7 = Portc.5 , E = Portc.1 , Rs = Portc.0
  10. 'Cursor Off
  11. 'Cls
  12.  
  13. Config Timer1 = Timer , Prescale = 1024 , Clear_timer = 1
  14.  Compare1a = 77       '10ms @8MHz/1024
  15.  
  16.  
  17. Config Timer0 = Timer , Prescale = 256       '8000000/256=31250 Hz
  18. Config Int1 = Falling       'Interruption on Falling
  19. Stop Timer0
  20. Enable Timer0
  21. Enable Int1
  22. Enable Interrupts
  23. On Timer0 Tikers       'work on timer
  24. On Int1 Infrared
  25.  
  26. Led_0 Alias Portb.0
  27. Led_1 Alias Portb.1
  28. Led_2 Alias Portb.2
  29. Led_3 Alias Portb.3
  30. Led_4 Alias Portb.4
  31. Led_5 Alias Portb.5
  32. Led_6 Alias Portb.6
  33. Led_7 Alias Portb.7
  34.                                             'work on interruption
  35.  
  36. 'Dim Got As Byte
  37. dim spid as word
  38. Dim Tik As Word       'counter of teaks of timer
  39. Dim Byt As Byte       'counter accepted bit
  40. Dim Repeat_flag As Byte       'flag of repetition
  41. Dim Start_flag As Byte       'flag of start condition
  42. Dim Address_1 As Byte       'direct byte of address
  43. Dim Command_1 As Byte       'direct byte of command
  44. Dim Address_0 As Byte       'indirect byte of address
  45. Dim Command_0 As Byte       'indirect byte of command
  46. Dim Summa As Word
  47. Dim Address_nec As Byte , Command_nec As Byte
  48. spid = 50
  49.  
  50. '################################################################################################################
  51.  
  52. Do       'Main cycle
  53.  'If Got = 1 Then
  54.    'Cls
  55.    'Locate 1 , 1
  56.   ' Lcd "Adr:" ; Address_nec
  57.   ' Locate 2 , 1
  58.    'Lcd "Com:" ; Command_nec       'Lcd ADDRESS and COMMAND
  59.   ' Reset Got
  60. ' End If
  61.  
  62.  If Address_nec <> 1 Then       'jeśli Wpisać "If Address=0 Then" to zawęzimy tylko do telewizorów
  63.  
  64.  Select Case Command_nec
  65.  
  66.    Case 32:       'przycisk X w LEWO
  67.        Led_0 =0
  68.        Pulseout Portb , 1 , spid
  69.       Waitms 5
  70.    Case 96:       'przycisk X w PRAWO
  71.       set Led_0
  72.       Pulseout Portb , 1 , spid
  73.        Waitms 5
  74.    Case 216:       'przycisk Y w TYL
  75.        Led_2=0
  76.        Pulseout Portb , 3 , spid
  77.        Waitms 5
  78.    Case 88:       'przycisk Y w PRZOD
  79.       set Led_2
  80.       Pulseout Portb , 3 , spid
  81.       Waitms 10
  82.    Case 16:       'przycisk CH1 - Z W DOL
  83.        Led_4=0
  84.       Pulseout Portb , 5 , spid
  85.       Waitms 5
  86.    Case 40:       'przycisk CH3 - Z W GORE
  87.        SET Led_4
  88.       Pulseout Portb , 5 , spid
  89.       Waitms 5
  90.    Case 80:       'przycisk pilota - 7
  91.        Led_6=0
  92.       Pulseout Portb , 7 , spid
  93.       Waitms 10
  94.    Case 104:       'przycisk pilota - 8
  95.       SET Led_6
  96.          Waitms 5
  97.    CASE 8:       'mode - wolny
  98.        waitms 5
  99.    case 160:     'menu -wolny
  100.       waitms 5
  101.    Case 72:
  102.       Set Led_0
  103.       Set Led_1
  104.       Set Led_2
  105.       Set Led_3
  106.       Set Led_4
  107.       Set Led_5
  108.       Set Led_6
  109.       Set Led_7
  110.   End Select
  111.  
  112. End If
  113.  
  114.  Loop
  115. End       'End of main cycle
  116.  
  117. '################################################################################################################
  118.  
  119.  Tikers:       'work on timer
  120.    Timer0 = 253       '31250/(256-253)=10416,66 Hz (96 een)
  121.    Incr Tik
  122.    If Tik >= 1200 Then       'if 1200 teaks, have thrown all in source condition
  123.      Tik = 0
  124.      Repeat_flag = 0
  125.      Start_flag = 0
  126.      Address_1 = 255
  127.      Command_1 = 255
  128.      Address_0 = 0
  129.      Command_0 = 0
  130.      Address_nec = 255
  131.      Command_nec = 255
  132.      Stop Timer0
  133.    End If
  134.  Return
  135. '################################################################################################################
  136.  
  137.  Infrared:       'work on interruption
  138.  Start Timer0
  139.  
  140.  If Tik >= 139 And Tik < 150 Then       'if has happenned from 139 before 150 teaks - "START"
  141.   Address_nec = 1
  142.   Repeat_flag = 0
  143.   Start_flag = 1
  144.   Address_1 = 255
  145.   Command_1 = 255
  146.   Address_0 = 0
  147.   Command_0 = 0
  148.  End If
  149.  
  150.  If Tik >= 116 And Tik < 139 Then       'if has happenned from 116 before 138 teaks - "REPETITION"
  151.   Address_nec = 0
  152.   Repeat_flag = 1
  153.   Start_flag = 0
  154.  End If
  155.  
  156.  If Tik >= 22 And Tik < 116 And Start_flag = 1 Then       'if has happenned from 22 before 115 teaks - have taken "1"
  157.    Incr Byt
  158.  
  159.    If Byt < 9 Then
  160.       Shift Address_1 , Left
  161.       Address_1 = Address_1 + 1
  162.    End If
  163.  
  164.    If Byt >= 9 And Byt < 17 Then
  165.       Shift Address_0 , Left
  166.       Address_0 = Address_0 + 1
  167.    End If
  168.  
  169.    If Byt >= 17 And Byt < 25 Then
  170.       Shift Command_1 , Left
  171.       Command_1 = Command_1 + 1
  172.    End If
  173.  
  174.    If Byt >= 25 Then
  175.       Shift Command_0 , Left
  176.       Command_0 = Command_0 + 1
  177.    End If
  178.  End If
  179.  
  180.  If Tik >= 10 And Tik < 22 And Start_flag = 1 Then       'if has happenned from 10 before 21 teaks - have taken "0"
  181.     Incr Byt
  182.     If Byt < 9 Then
  183.       Shift Address_1 , Left
  184.     End If
  185.  
  186.     If Byt >= 9 And Byt < 17 Then
  187.        Shift Address_0 , Left
  188.     End If
  189.  
  190.     If Byt >= 17 And Byt < 25 Then
  191.        Shift Command_1 , Left
  192.     End If
  193.  
  194.     If Byt >= 25 Then
  195.        Shift Command_0 , Left
  196.     End If
  197.  End If
  198.  
  199.  Tik = 0
  200.  
  201.  If Byt = 32 Then
  202.    Address_nec = Address_1
  203.    Command_nec = Command_1
  204.    'Got = 1
  205.    Address_1 = 255
  206.    Command_1 = 255
  207.    Byt = 0
  208.    Repeat_flag = 0
  209.    Start_flag = 0
  210.    Stop Timer0
  211.  End If
  212.  
  213.  Return
Działa , tylko silnik kreci powoli :D
Awatar użytkownika
Jacek
Posty: 384
Rejestracja: 25 kwie 2016, 19:14

Enkodery jeszcze raz

Post autor: Jacek » 22 paź 2020, 17:54

Witam napisałem taki program do testu obrotomierza tokarki
  1. $regfile = "m32def.dat"
  2. $crystal = 16000000
  3. $hwstack = 255
  4. $swstack = 128
  5. $framesize = 128
  6. Config Submode = New
  7.  
  8. '--------------------------------------------------------------------
  9. $projecttime = 2
  10. $version 0 , 0 , 218
  11. Dim Firmware As String * 32
  12. Firmware = "Obrotomierz TYTAN 750"
  13. 'Debug Off
  14. Debug On
  15. '-------------------------------------------------------------------
  16. 'KONFIGURACJA TIMERA
  17. Config Timer1 = Timer , Prescale = 8
  18. On Timer1 Przertimer1
  19. Enable Timer1
  20. '--------------------------------------------------------------------
  21. 'KONFIGURACFJA ENKODERA
  22. 'Config Int0 = Falling
  23. 'On Int0 Enkoder
  24. 'Enable Int0
  25. '--------------------------------------------------------------------
  26. 'Enable Interrupts
  27. '--------------------------------------------------------------------
  28. 'KONFIGURACJA LCD
  29. Config Lcdpin = Pin , Db4 = Porta.0 , Db5 = Porta.1 , Db6 = Porta.2 , Db7 = Porta.3 , E = Porta.4 , Rs = Porta.5       'mirley UPT M16
  30. Config Lcd = 16 * 4
  31. '--------------------------------------------------------------------
  32.  
  33. 'KONFIGURACJA PORTÓW
  34. Config Portd.2 = Input : Portd.2 = 1                        'enkoder kanał A
  35. Config Portd.3 = Input : Portd.3 = 1                        'enkoder kanał B
  36. Config Portd.6 = Output : Portd.6 = 1                       'LED Live
  37. Config Portb.0 = Input                                      ': Pinb.0 = 1                         'sw_menu
  38. Config Portb.1 = Input :                                    'sw_zero_kąt
  39.  
  40. Portb = &B11111111
  41. '--------------------------------------------------------------------
  42. 'ALIASY PORTÓW
  43. Kanal_a Alias Pind.2
  44. Kanal_b Alias Pind.3
  45. Led_live Alias Portd.6
  46. Sw_menu Alias Pinb.0
  47. Sw_zero_kat Alias Pinb.1
  48. '--------------------------------------------------------------------
  49. 'ZMIENNE ENKODERA
  50. Dim En_impuls As Long                                       'zmienna przechowywujaca impulsy z enkodera
  51. '--------------------------------------------------------------------
  52. 'ZMIENNE CZASU
  53. Dim Timer_tikx10 As Byte                                    'zmienna liczaca przepełnienie timera
  54. Dim Timertikx100 As Byte                                    'zmienna liczaca przepełnienie timera x10
  55. Dim Timertikx200 As Byte                                    'zmienna liczaca przepełnienie timera x 20
  56. Dim Timertikx500 As Byte                                    'zmienna liczaca przepełnienie timera x 50
  57. Dim Timer_tik_flag As Byte
  58. Flaga10ms Alias Timer_tik_flag.0
  59. Flaga100ms Alias Timer_tik_flag.1
  60. Flaga500ms Alias Timer_tik_flag.2
  61. Flaga1s Alias Timer_tik_flag.3
  62. Flaga2s Alias Timer_tik_flag.4
  63. Flaga2_5s Alias Timer_tik_flag.5
  64.  'zostaly jeszce trzy wolne flagi
  65.  ' a uzylem tylko jednego bajtu
  66. '####################################################################
  67. 'ZMIENNE POMIARU OBROTÓW
  68. Dim Pomiar1 As Single                                       'zmienna pomocnicza1
  69. Dim Pomiar2 As Single                                       'zmienna pomocnicza2
  70. Dim Pomiar3 As Single                                       'zmienna pomocnicza3
  71. Dim Wynik As String * 5                                     'zmienna pomocnicza4
  72. Dim Wynik1 As Long
  73. '-----------------------------------------------------------------------
  74. 'ZMIENNE POMIARU KĄTA
  75. Dim Kat_ As Word
  76. Dim Kat_1 As Single
  77. '-----------------------------------------------------------------------
  78. 'ZMIENNE MENU
  79. Dim Poz_menu As Byte                                        'zmienna menu
  80. Dim Wyswietl_ As Byte                                       'zmiennawyboru wyświetlania
  81. '----------------------------------------------------------------------------
  82. 'ZMIENNE PRZYCISKU ENKODERA
  83. Dim Sw_cnt As Word
  84. Dim Lock As Byte
  85. '----------------------------------------------------------------------------
  86. $include "subs\mruganie.inc"
  87. $include "subs\wyswietl.inc"
  88. '$include "subs\pomiar.inc"
  89. '-------------------------------------------------------------------
  90. 'Enable Interrupts
  91. '----------------------------------------------------------------------------
  92. 'KONFIGURACFJA ENKODERA
  93. Config Int0 = Falling
  94. On Int0 Enkoder
  95. Enable Interrupts
  96. Enable Int0
  97. Waitms 500
  98. Cursor Off
  99. Cursor Noblink
  100. Cls
  101. Poz_menu = 0
  102. '----------------------------------------------------------------------------
  103. Do
  104. '====================================================================
  105.    If Timer_tikx10 >= 10 Then                               'jeśli uplyneło 100ms to
  106.       Flaga100ms = 1                                        'ustaw flage upłyneło 100ms
  107.       Incr Timertikx100                                     'zwiększaj Timertikx100
  108.       Timer_tikx10 = 0                                      'zeruj liczenie
  109.    End If                                                   'koniec warunku
  110.    If Timertikx100 >= 10 Then                               'jeśli uplynela sekunda
  111.       Flaga1s = 1                                           'ustaw flage upłyneła sekunda
  112.       Incr Timertikx200                                     'zwiększaj Timertikx200
  113.       Incr Timertikx500
  114.       Timertikx100 = 0                                      'zeruj liczenie
  115.    End If                                                   'koniec warunku
  116.    If Timertikx200 >= 2 Then                                'jeśli upłyneły dwie sekundy
  117.       Flaga2s = 1                                           'ustaw flagę upłyneły dwie sekundy
  118.       Timertikx200 = 0                                      'zeruj liczenie
  119.    End If                                                   'koniec warunku
  120.    If Timertikx500 >= 5 Then
  121.       Flaga500ms = 1
  122.       Timertikx500 = 0
  123.    End If
  124.    '=====================================================================
  125.    If Flaga10ms = 1 Then                                    'jeśli Flaga10ms jest równa 1
  126.       Flaga10ms = 0                                         'wyzeruj Flaga10ms                                                            'dopisano 19.06.2018
  127.    End If                                                   'koniec warunku
  128. '---------------------------------------------------------------------------------------------------
  129.    If Flaga100ms = 1 Then                                   'jeśli Flaga100ms jest równa 1
  130.       Flaga100ms = 0                                        'wyzeruj Flaga100ms
  131.    End If                                                   'koniec warunku
  132.   '--------------------------------------------------------------------------------------------------
  133.    If Flaga500ms = 1 Then                                   'jeśli Flaga500ms jest równa 1
  134.       Flaga500ms = 0                                        'wyzeruj Flaga500ms
  135.    End If                                                   'koniec warunku
  136. '---------------------------------------------------------------------------------------------------
  137.    If Flaga1s = 1 Then                                      'jeśli Flaga1s jest równa 1
  138.       Mruganie
  139.       Wyswietl
  140.       'Pomiar
  141.       Pomiar1 = En_impuls / 1000                            '720
  142.       Pomiar2 = Pomiar1 * 60
  143.       Pomiar3 = Pomiar2
  144.       En_impuls = 0
  145.       Flaga1s = 0                                           'wyzeruj Flaga1s
  146.    End If                                                   'koniec warunku
  147.  
  148. '---------------------------------------------------------------------------------------------------
  149.    If Flaga2s = 1 Then                                      'jeśli Flaga2s jest równa 1
  150.       Flaga2s = 0                                           'wyzeruj Flaga
  151.    End If
  152.                                                       'koniec warunku
  153.    If Sw_menu = 0 Then
  154.       Poz_menu = Poz_menu + 1
  155.    End If
  156.  
  157.    If Poz_menu = 0 Then
  158.       Wyswietl_ = 0
  159.    End If
  160.    If Poz_menu = 1 Then
  161.       Wyswietl_ = 1
  162.    End If
  163.    If Poz_menu = 2 Then
  164.       Poz_menu = 0
  165.    End If
  166.  
  167.    If Sw_zero_kat = 0 Then
  168.       Kat_1 = 0
  169.    End If
  170.  
  171.  
  172.    If Kat_ > 720 Then
  173.       Kat_ = 0
  174.    End If
  175.  
  176.    Kat_1 = Kat_ / 2
  177.  
  178. Loop
  179. End
  180. '-----------------------------------------------------------------------------------------------------
  181. '            podprogram przerwań Timer1
  182. Przertimer1:
  183.    Timer1 = 45536                                           'wstawienie do Timer1 wartości 45536
  184.  
  185.    Incr Timer_tikx10
  186.  
  187.    If Timer_tikx10 = 1 Then
  188.       Set Flaga10ms
  189.    End If
  190.  
  191. Return                                                      'powrót
  192. '-----------------------------------------------------------------------------------------------------
  193. Enkoder:
  194.    Disable Int0
  195. 'Waitms 1
  196.    If Kanal_a = 0 Then
  197.       If Kanal_b = 0 Then
  198.          Incr En_impuls
  199.          Incr Kat_
  200.       Else
  201.          Decr Kat_
  202.       End If
  203.    End If
  204.    Enable Int0
  205. Return
  206. '------------------------------
podprogramy
  1. $nocompile
  2. 'mruganie Led_live
  3. Sub Mruganie
  4.  
  5.    Toggle Led_live
  6.  
  7. End Sub
  1. $nocompile
  2. 'wyświetlanie na LCD
  3. Sub Wyswietl
  4.    Wynik = Str(pomiar3)
  5.    Wynik = Fusing(pomiar3 , "###.#")
  6.    Wynik1 = Val(wynik)
  7.  
  8.  
  9.                                                       'koniec warunku
  10.    'If Sw_menu = 0 Then
  11.    '   Poz_menu = Poz_menu + 1
  12.    'End If
  13.  
  14.    'If Poz_menu = 0 Then
  15.    '   Wyswietl_ = 0
  16.    'End If
  17.    'If Poz_menu = 1 Then
  18.    '   Wyswietl_ = 1
  19.    'End If
  20.    'If Poz_menu = 2 Then
  21.    '   Poz_menu = 0
  22.    'End If
  23.  
  24.    Select Case Wyswietl_
  25.       Case 0
  26.  
  27.          Locate 4 , 1 : Lcd "poz_menu " ; Poz_menu : :
  28.          'Poz_menu = 0
  29.          'Cls
  30.          Locate 1 , 1 : Lcd "OBROTY WRZECIONA"
  31.          If Wynik1 > 999 Then
  32.             Locate 2 , 3 : Lcd ; Wynik1                     '; Wynik1
  33.             Locate 2 , 7 : Lcd " OBR/MIN "
  34.          End If
  35.  
  36.          If Wynik1 < 1000 Then
  37.             Locate 2 , 3 : Lcd "0" ; Wynik1
  38.             Locate 2 , 7 : Lcd " OBR/MIN "
  39.          End If
  40.          If Wynik1 < 100 Then
  41.             Locate 2 , 3 : Lcd "00" ; Wynik1                '; Wynik1
  42.             Locate 2 , 7 : Lcd " OBR/MIN "
  43.          End If
  44.          If Wynik1 < 10 Then
  45.             Locate 2 , 3 : Lcd "000" ; Wynik1               '; Wynik1
  46.             Locate 2 , 7 : Lcd " OBR/MIN "
  47.          End If
  48.          Locate 3 , 1 : Lcd "               "
  49.       Case 1:
  50.  
  51.          Locate 4 , 1 : Lcd "poz_menu " ; Poz_menu
  52.          'Poz_menu = 1
  53.          'Cls
  54.          'Kat_1 = Kat_ / 2
  55.  
  56.          If Kat_1 > 360 Then
  57.             Kat_1 = 0
  58.          End If
  59.  
  60.  
  61.          Locate 1 , 1 : Lcd "  POMIAR  KATA  "
  62.          Locate 2 , 1 : Lcd "   WRZECIONA  "
  63.          Locate 3 , 1 : Lcd "KAT " ; Kat_1 ; "   "
  64.  
  65.    End Select
  66.  
  67. End Sub
  1. $nocompile
  2. 'przeliczanie obrotów
  3. Sub Pomiar
  4.  
  5.    Pomiar1 = En_impuls / 1000                               ' 720
  6.    En_impuls = 0
  7.    Pomiar2 = Pomiar1 * 60
  8.  
  9. End Sub
nie pamiętam z skąd mam obsługę enkodera ale to działa https://www.youtube.com/watch?v=qehq8E07sno
chciałem zrobić jeszcze pomiar kata obrotu - no ale przeszedłem na xmegę - ale będę chciał to zaincludować w projekcie sterownika tokarki ELS
pozdrawiam Jacek.
Awatar użytkownika
niveasoft
Posty: 1216
Rejestracja: 17 sie 2015, 12:13
Kontakt:

Re: Encodery jeszcze raz

Post autor: niveasoft » 22 paź 2020, 18:41

Okay. Wydzieliłem temat bo to nie Xmega i tego się trzymajmy :idea:

Zobacz jak można mieć trzy enkodery na raz. Na pewno da się to uruchomić na Mega328P i lepiej nawet przenieść piny z PD0 i PD1 na jakies inne bo tam jest akurat USART który zawsze się przydaje ;)

LINK :arrow: viewtopic.php?f=13&t=360
Awatar użytkownika
Henryk
Posty: 348
Rejestracja: 22 sty 2018, 17:20

Re: Encodery jeszcze raz

Post autor: Henryk » 22 paź 2020, 19:39

Dziękuje za ten przykład z linku :D
Przyznam się bez nacisków , nie łapię jak połączyć te enkodery , ale to już nie problem .
Sprawę rozwiązał zwykły regulowany generator na 555 i joystik do wyboru osi którą che sterować ;)
ODPOWIEDZ