Klawiatura drabinkowa

Pytania, kody i porady dotyczące nie tylko Bascom.
ODPOWIEDZ
Awatar użytkownika
Jacek
Posty: 384
Rejestracja: 25 kwie 2016, 19:14

Klawiatura drabinkowa

Post autor: Jacek » 22 paź 2021, 22:36

Witam - próbuję zaadaptować w swoim programie klawiaturę drabinkową z tego tego tematu viewtopic.php?f=13&t=138
program główny wygląda tak
  1.        '**********************************************
  2.     'STEROWNIK WYKONAWCZY OBROTU
  3.     'ANTENY TYPU M0PLK MIKROKONTROLER ATMEGA328p 16MHz
  4.     '**********************************************
  5. $regfile = "m328pdef.dat"
  6. $crystal = 16000000
  7. $hwstack = 64
  8. $swstack = 64
  9. $framesize = 128
  10. $baud = 115200
  11. Config Submode = New
  12.     'Config Watchdog = 4096
  13.     'Start Watchdog
  14.  
  15.     'Debug Off
  16. Debug On
  17. '*******************************************************************
  18. $include "subs\G_adc.inc"
  19. $include "subs\Blink_.inc"
  20. $include "subs\Display_.inc"
  21. $include "subs\Hold_.inc"
  22. $include "subs\Left_.inc"
  23. $include "subs\Right_.inc"
  24. $include "subs\Convert_adc_actuator.inc"
  25. $include "subs\RS485_read.inc"
  26. $include "subs\Convert_adc_keypad.inc"
  27. '----------------------------------------------------------------------
  28. Config Scl = Portc.5                                       ' used i2c pins
  29. Config Sda = Portc.4
  30. Config Twi = 400000                                         ' i2c speed
  31.  
  32. I2cinit
  33.  
  34. $lib "i2c_twi.lbx"                                          ' we do not use software emulated I2C but the TWI
  35. $lib "glcdSSD1306-I2C_V2.lib"                               ' override the default lib with this special one
  36. Const Ssd1306_rotate = 1                                    ' Display rotation. [connection cable is lower side = 0, upper side = 1]
  37. Config Graphlcd = Custom , Cols = 128 , Rows = 64 , Lcdname = "SSD1306"
  38. '------------------------------------------------------------------------------------------------
  39.     'KONFIGURACJA TIMER'ÓW
  40. Config Timer1 = Timer , Prescale = 8
  41. On Timer1 Przertimer1
  42. Enable Timer1
  43.     'Config Timer1 = Pwm , Pwm = 8 , Compare_a_pwm = Clear_up , Compare_b_pwm = Clear_down , Prescale = 1
  44. Config Timer2 = Pwm , Compare_a_pwm = Clear_up , Prescale = 1024
  45.     '--------------------------------------------------------------------
  46. Enable Interrupts
  47.     '--------------------------------------------------------------------
  48.     'KONFIGURACJA ADC
  49. Config Adc = Single , Prescaler = Auto   , Reference = Avcc
  50.     '--------------------------------------------------------------------
  51.     'KONFIGURACJA PORTÓW
  52. Config Portc.0 = Input : Pinc.0 = 1                         'ADC POTI ACTUATOR
  53. Config portc.1 = Input: pinc.1=0                            'ADC_KEYPAD
  54. Config Portd.2 = Output : Portd.2 = 0                       'IN_A
  55. Config Portd.3 = Output : Portd.3 = 0                       'IN_B
  56. Config Portd.4 = Output : Portd.4 = 1                       'CS_DIS
  57. Config Portd.7 = Output : Portd.7 = 1                       'ENA/DIAGA
  58. Config Portb.0 = Output : Portb.0 = 1                       'ENA/DIAGB
  59. Config Portb.5 = Output : Portb.5 = 0                       'LED_LIVE
  60. Config PORTD.5 = Output : PORTD.5=0                         'LED_HOLD
  61. Config Portb.3 = Output : Portb.3 = 1                       'PWM_OUT
  62.  
  63.     '--------------------------------------------------------------------
  64.     'ALIASY PORTÓW
  65. In_a Alias Portd.2
  66. In_b Alias Portd.3
  67. Cs_dis Alias Portb.2
  68. Ena_da Alias Portd.7
  69. Ena_db Alias Portb.0
  70. Led_hold Alias Portd.5
  71. Led_live Alias Portb.5                                      'Dioda Led sygnalizująca prace układu
  72. Pwm_ alias portb.3
  73.     '--------------------------------------------------------------------
  74.     'ZMIENNE   CZASU
  75. Dim Timer_tikx10 As Byte                                    'zmienna liczaca przepełnienie timera
  76. Dim Timertikx100 As Byte
  77. Dim Timertikx200 As Byte
  78. Dim Timer_tik_flag As Byte
  79. Flaga10ms Alias Timer_tik_flag.0
  80. Flaga100ms Alias Timer_tik_flag.1
  81. Flaga500ms Alias Timer_tik_flag.2
  82. Flaga1s Alias Timer_tik_flag.3
  83. Flaga2s Alias Timer_tik_flag.4
  84. Flaga2_5s Alias Timer_tik_flag.5
  85.     'zostaly jeszce trzy wolne flagi
  86.     ' a uzylem tylko jednego bajtu
  87.     ' Paweł - pikczu
  88. '**********************************************
  89.     'STAŁE
  90. Const Sw_r = 1
  91. Const Sw_u = 2
  92. Const Sw_d = 3
  93. Const Sw_l = 4
  94.     '--------------------------------------------------------------------
  95.     'ZMIENNE
  96. Dim _contrast As Byte                                       ' Contrast value of OLED. [0-255]
  97. Dim adc_actuator As Word ,  actuator_h as word
  98. dim ADC_keypad_ as word
  99. dim angle_actuator as word
  100. dim angle_pos_antenna as word
  101. dim pwm_h as word
  102. Dim Key As Byte
  103. Dim Read_key As Byte
  104. '---------------------------------------------------------------------
  105.     'USTAWIENIA POCZATKOWE
  106. Ena_da = 1
  107. Ena_db = 1
  108. cls
  109. Waitms 500
  110. Cls
  111. waitms 250
  112. setfont font16x16
  113. lcdat 1,24,"M0PLK"
  114. setfont font8x8tt
  115. lcdat 4,35,"STEROWNIK"
  116. lcdat 5,45,"OBROTU"
  117. setfont font16x16
  118. lcdat 7,16,"SQ2EER"
  119. wait 5
  120. cls
  121.     '--------------------------------------------------------------------
  122.     'PĘTLA GŁÓWNA
  123. Do
  124.      if Timer_tikx10 = 1 then
  125.           Timer_tikx10 = 0
  126.      end if
  127.      If Timer_tikx10 >= 10 Then                               'jesli uplynelo 100ms to
  128.           Flaga100ms = 1                                        'ustaw flage uplynelo 100ms
  129.           Incr Timertikx100
  130.           Timer_tikx10 = 0                                      ''zeruj liczenie
  131.      End If
  132.      If Timertikx100 >= 10 Then                               'jesli uplynela sekunda
  133.           Flaga1s = 1                                           'ustaw flage sekundy
  134.           Incr Timertikx200
  135.           Timertikx100 = 0                                      'zeruj liczenie
  136.      End If
  137.      If Timertikx200 >= 2 Then
  138.           Flaga2s = 1
  139.           Timertikx200 = 0
  140.      End If
  141.      If Flaga10ms = 1 Then
  142.           Call G_adc
  143.           Flaga10ms = 0
  144.      End If
  145.      If Flaga100ms = 1 Then                                   'co 100ms
  146.           call  Convert_adc_actuator
  147.           call Convert_adc_keypad
  148.           Call Display_
  149.           Flaga100ms = 0                                        'wyzeruj Flaga100ms
  150.      End If
  151.      If Flaga1s = 1 Then
  152.           Call Blink_
  153.           debug "adc_keypad " ;  adc_keypad_
  154.           debug "key " ; key
  155.           debug "Read_key " ; Read_key
  156.           Flaga1s = 0
  157.      End If
  158.      If Flaga2s = 1 Then                                      'co2s
  159.           Flaga2s = 0
  160.      End If
  161.  
  162. '**********************************************************************
  163. if    angle_actuator > angle_pos_antenna  then
  164.      call right_
  165. end if
  166. if    angle_actuator < angle_pos_antenna  then
  167.      call left_
  168. end if
  169. if    angle_actuator =  angle_pos_antenna then
  170.      call Hold_
  171. end if
  172. '***************************************************************
  173.  
  174.  
  175.      Select Case Key
  176.           Case Sw_u
  177.                If pwm_h < 255 then Incr pwm_h
  178.           Case Sw_d
  179.                If pwm_h > 0 Then decr pwm_h
  180.           Case Sw_r
  181.                If angle_pos_antenna < 90 then incr angle_pos_antenna
  182.           Case Sw_l
  183.                If angle_pos_antenna > 0 then decr angle_pos_antenna
  184.      End Select
  185.  
  186.      Key = 0
  187.  
  188.  
  189. Loop
  190. End
  191. '------------------------------------------------------------
  192. $include "font8x8TT.font"                                   ' this is a true type font with variable spacing
  193. $include "font16x16.font"
  194. '*******************************************************************
  195.     'PODPROGRAM PRZERWAŃ TIMER1
  196. Przertimer1:
  197.      Timer1 = 45536                                           'wstawienie do Timer1 wartości 45536
  198.      Incr Timer_tikx10
  199.      If Timer_tikx10 = 1 Then
  200.           Set Flaga10ms
  201.      End If
  202. Return
  203.     '----------------------------------------------------------------------


funkcję wstawiłem do suba

  1. 'KONWERSJA ADC KLAWISZY
  2. $nocompile
  3. sub Convert_adc_keypad
  4.      Local  adc_keypad As Word , Read_key As Byte , Key_val As Byte , Old_key As Byte , Key_cnt As Byte
  5.      Function Read_keypad() As Byte
  6.      If Read_key <> 0 Then
  7.           If Old_key = Read_key Then
  8.  
  9.                If Key_cnt < 20 Then
  10.                     Incr Key_cnt
  11.                     Key_val = 0
  12.                Else
  13.                     Key_cnt = 0
  14.                     Key_val = Read_key
  15.                End If
  16.  
  17.           End If
  18.  
  19.      Else
  20.           Key_cnt = 15
  21.           Key_val = 0
  22.      End If
  23.  
  24.      Old_key = Read_key
  25.      Read_keypad = Key_val
  26. End Function
  27. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  28.      key = Read_keypad()
  29. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  30.  
  31. end sub

odczyt adc mam również w osobnym sub'ie

  1.      'POBIERANIE DANYCH Z ADC
  2. $nocompile
  3. Sub G_adc
  4.      adc_actuator=getadc(0)
  5.      adc_keypad_=getadc(1)
  6.       Select Case adc_keypad_                                                                    ' Select Case Adc_read
  7.           Case 0 To 100 : Read_key = Sw_r                         'right = 0
  8.           Case 101 To 250 : Read_key = Sw_u                       'up = 145
  9.           Case 251 To 400 : Read_key = Sw_d                       'dwn = 330
  10.           Case 401 To 600 : Read_key = Sw_l                       'left = 506
  11.           Case Is > 601 : Read_key = 0                                      'Case Is > 851 : Read_key = 0                            'no key = 1023
  12.      End Select
  13.  
  14. End Sub

w terminalu widzę odczyt klawiszy i wartości odczytu, niestety
w pierwszym subie nie jest przekazywana wartość funkcji Read_keypad() do zmiennej key

  1.  key = Read_keypad()



czy może ktoś pomóc lub podpowiedzieć co mam źle napisane.
pozdrawiam Jacek.
Awatar użytkownika
niveasoft
Posty: 1216
Rejestracja: 17 sie 2015, 12:13
Kontakt:

Re: Klawiatura drabinkowa

Post autor: niveasoft » 22 paź 2021, 23:12

Drabinka to w sumie proste...zwykłe ADC i kilka odczytów interpretowanych jako stopień..napisz gdzie tu problem.
Awatar użytkownika
Jacek
Posty: 384
Rejestracja: 25 kwie 2016, 19:14

Re: Klawiatura drabinkowa

Post autor: Jacek » 23 paź 2021, 10:37

Witam - nie wiem czy dobrze rozumiem działanie Twojego programu, funkcja Read_keypad ma zwracać do zmiennej key który klawisz został naciśniety.
W oknie terminala
IMG_20211023_102826.jpg
witać że funkcja odczytuje wartość adc, który klawisz został wciśnięty, ale nie przekazuje tej wartości do zmiennej key, chociaż wstawianie tej wartości mam w tym samym subie co funkcja.
Nie wiem jak to zrobić.
pozdrawiam Jacek.
Nie masz wymaganych uprawnień, aby zobaczyć pliki załączone do tego posta.
Awatar użytkownika
Pikczu
Posty: 390
Rejestracja: 17 sie 2015, 13:46
Lokalizacja: Dublin, Ireland
Kontakt:

Re: Klawiatura drabinkowa

Post autor: Pikczu » 23 paź 2021, 20:49

Jacek przecież masz gotowca dlaczego go nie użyjesz ??
ODPOWIEDZ