Może się komuś przydadzą gotowe opisy wyprowadzeń w kodzie no i funkcja do odczytu pięcioklawiszowej klawiatury na jednym pinie ADC
Funkcja ma wbudowane autorepeat czyli przy dłuższym wciśnięciu kliki się powtarzają same.
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- $regfile = "m328pdef.dat"
- $crystal = 16000000
- $hwstack = 64
- $swstack = 32
- $framesize = 128
- '***********************************************
- '* *
- '* LDC KEYPAD SHIELD DEMO *
- '* *
- '* RobotDyn Shield with resistive keypad *
- '* *
- '* Demo shows hardware configuration, and *
- '* function for reading switches *
- '* *
- '* by BARTek *
- '***********************************************
- Config Submode = New
- '***********************************************
- '* ALPHANUMERIC LCD *
- '***********************************************
- Config Lcdpin = Pin , Rs = Portb.0 , E = Portb.1 , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7
- Config Lcd = 16x2
- Cursor Off , Noblink : Cls
- '***********************************************
- '* CONFIG ADC FOR SWITCHES *
- '***********************************************
- Config Adc = Single , Prescaler = Auto , Reference = Avcc
- '***********************************************
- '* TIMER2 FOR TIME BASE *
- '***********************************************
- Config Timer2 = Timer , Prescale = 1024 , Compare_a = Disconnect , Clear_timer = 1
- Compare2a = 155 '10ms @16MHz
- '***********************************************
- '* VARIABLES *
- '***********************************************
- Const Sw_r = 1
- Const Sw_u = 2
- Const Sw_d = 3
- Const Sw_l = 4
- Const Sw_s = 5
- 'switches
- Dim Old_key As Byte , Key_cnt As Byte , Key As Byte
- 'demo
- Dim Lr As Integer , Up_dn As Integer , Flip As Byte
- Function Read_keypad() As Byte
- 'function with autorepeat
- Local Adc_read As Word , Read_key As Byte , Key_val As Byte
- Adc_read = Getadc(0)
- Select Case Adc_read
- Case 0 To 100 : Read_key = Sw_r 'right = 0
- Case 101 To 250 : Read_key = Sw_u 'up = 145
- Case 251 To 400 : Read_key = Sw_d 'dwn = 330
- Case 401 To 600 : Read_key = Sw_l 'left = 506
- Case 601 To 850 : Read_key = Sw_s 'select = 743
- Case Is > 851 : Read_key = 0 'no key = 1023
- End Select
- If Read_key <> 0 Then
- If Old_key = Read_key Then
- If Key_cnt < 20 Then
- Incr Key_cnt
- Key_val = 0
- Else
- Key_cnt = 0
- Key_val = Read_key
- End If
- End If
- Else
- Key_cnt = 15
- Key_val = 0
- End If
- Old_key = Read_key
- Read_keypad = Key_val
- End Function
- 'for demo
- Locate 1 , 1 : Lcd "UpDn=0"
- Locate 2 , 6 : Lcd "< 0 >"
- Locate 1 , 14 : Lcd "*"
- Do
- If Tifr2.ocf2a = 1 Then '10ms
- Tifr2.ocf2a = 1
- Key = Read_keypad()
- End If
- If Key <> 0 Then
- Select Case Key
- Case Sw_u
- If Up_dn < 9 Then Incr Up_dn
- Locate 1 , 6 : Lcd Up_dn ; " "
- Case Sw_d
- If Up_dn > -9 Then Decr Up_dn
- Locate 1 , 6 : Lcd Up_dn ; " "
- Case Sw_r
- If Lr < 9 Then Incr Lr
- Locate 2 , 7 : If Lr >= 0 Then Lcd " " : Lcd Lr
- Case Sw_l
- If Lr > -9 Then Decr Lr
- Locate 2 , 7 : If Lr >= 0 Then Lcd " " : Lcd Lr
- Case Sw_s
- Locate 1 , 14
- If Flip = 0 Then
- Lcd " *" : Flip = 1
- Else
- Lcd "* " : Flip = 0
- End If
- End Select
- Key = 0
- End If
- Loop
- End