4X4 Keypad - I2C interface using I/O expansions PCF8574 (from old forum)

Place code snippets and demo code here
Post Reply
MarioL
Posts: 20
Joined: Sun Mar 21, 2021 8:38 am
Has thanked: 250 times
Been thanked: 33 times

4X4 Keypad - I2C interface using I/O expansions PCF8574 (from old forum)

Post by MarioL »

Hi, people of annex

To insert small amounts of data into the microcontroller, a hexadecimal keyboard is sufficient, the solution is already covered in the post
https://groups.google.com/forum/#!categ ... Vai6dXh8kk
If you need to save GPIO you need to use I/O expansions.
The program that I share below uses PCF8574. The program does not use interrupt signals but scans the keyboard every half second.
The typed string is displayed on a simple web page and transmitted via serial terminal.


Saluti, poppolo di annex

Per inserire piccole quantità di dati nel microcontrollore basta una tastiera esadecimale come quella già tratttata nel post
https://groups.google.com/forum/#!categ ... Vai6dXh8kk
Nel caso occorra risparmiare GPIO bisogna usare le espansioni di I/O.
Il programma che di seguito condivido usa il PCF8574. Il programma non usa segnali di interrupt ma scansiona la tastiera ogni mezzo secondo.
La stringa digitata è visualizzata in una semplice pagina web e trasmessa tramite terminale seriale.

Code: [Local Link Removed for Guests]

'' *************************************************************************************
' *****                                 File Info                                 *****
' *************************************************************************************
'    Filename: PCF8574_KEYB
'    Date    : 08/01/2020
'    Version : 0_b3
'    Edit. by: MarioL.
'    Function: test membrane 4X4 keiyboard on I2C bus using PCF8574 interface
'    Firmware: ANNEX WiFi 1.39b9 public
'    Hardware: D1 mini, PCF8574 module, 4X4 membrane keyboad.
'    Doc.Ref.: Annex WiFi RDS Help Version 1.39, 4X4 membrane keyboad spec., PCF8574 datasheet
'    Note    : timered scan for key pressed, no interrupt used!
' ***************************************************************************************
'see also https://groups.google.com/forum/#!category-topic/annex_wifi_rds/projects/nVai6dXh8kk
  
'variables
PCFaddKB = &H20 'PCF8574's address for Key Board interface
n = 0  'number for tempor. use
R = 0  'value from row
C = 0  'value from coloumn
CpR = 0 'summ column + row, mean keyboard's selection
KeyCar$ = "" ' keyboard's charactern pressed
CmpStrng$ = "" 'composed sting, (sequence of characters pressed)
'hardware setup
I2C.setup 4, 5  'set bus I2C: pin GPIO4 (D2) for SDA (Serial DAta) and GPIO5 (D1) for SCL (Serial CLock) 
onhtmlreload  lblDrawPage    'draw simple web 
gosub lblDrawPage            'draw simple web page
timer0 500, lblKeyBrdRead
wait

lblDrawPage:  'simple web page for variable monitoring
cls
a$ = ""
a$ = a$ + |<h1><strong> Membrane Keyboard  4X4 Evaluation page</h1></strong>| 'un titolo per la  pagina
a$ = a$ + |Press D for delete last caracter,  press # and * at same time  for clear composite string,|
a$ = a$ + |<br><br><br>| 
A$ = A$ + |Variable CpR = | + textbox$(CpR,"IDtxtCrt")
A$ = A$ + |<br>| 
A$ = A$ + |Variable KeyCar$ = | + textbox$(KeyCar$,"IDtxtCrt")
A$ = A$ + |<br>| 
A$ = A$ + |Variable CmpStrng$ = | + textbox$(CmpStrng$,"IDtxtLng")
A$ = A$ + CSSID$("IDtxtCrt","width: 50px;")
A$ = A$ + CSSID$("IDtxtLng","width: 75%;") 
HTML A$ 
return


lblKeyBrdRead:
  KeyCar$ = ""   'reset variable
' test coloumn
subPCF8574_write 15    'set row=L col=H for next input
subPCF8574_read C      'if key pressed C<15
'if C = 15 return  'unrem to save 5 milliseconds if key unpressed
'test row
subPCF8574_write  240    'set row=H col=L
subPCF8574_read R        'if key pressed R<240
'ASSOCIATE KeyCar$ TO CpR VALUE
 CpR = C+R
 select case CpR
  case 119: KeyCar$ = "1"
  case 123: KeyCar$ = "2"
  case 125: KeyCar$ = "3"
  case 126: KeyCar$ = "A"
  
  case 183: KeyCar$ = "4"
  case 187: KeyCar$ = "5"
  case 189: KeyCar$ = "6"
  case 190: KeyCar$ = "B"
  
  case 215: KeyCar$ = "7"
  case 219: KeyCar$ = "8"
  case 221: KeyCar$ = "9"
  case 222: KeyCar$ = "C"
  
  case 231: KeyCar$ = "*"
  case 235: KeyCar$ = "0"
  case 237: KeyCar$ = "#"
  case 238   'Delete last character or ' KeyCar$ = "D"
            if LEN(CmpStrng$) > 0 then 
              CmpStrng$ = left$(CmpStrng$, LEN(CmpStrng$)-1)
            end if ' KeyCar$ = "D"
  
  case 229: CmpStrng$ = "" 'combination Key # + * pressed
 
 end select
 
CmpStrng$ = CmpStrng$  + KeyCar$ 'composed sting
 print "CpR= ";CpR, "KeyCar$= "; KeyCar$, "CmpStrng$= "; CmpStrng$
refresh
return



sub subPCF8574_write(n) 'write data to PCF8574
  i2c.begin PCFaddKB
  i2c.write n
  i2c.end
end sub

sub subPCF8574_read(n)  'read data from PCF8574
  i2c.begin PCFaddKB
  i2c.reqfrom PCFaddKB, 1
  n = i2c.read
  i2c.end
end sub


immagine.png
You do not have the required permissions to view the files attached to this post.
Post Reply