;******************************************************************************** ; @module keypad4x4 ; @type driver ; @device 4x4 Matrix Keypad ; @details ; The LCD must be connected to a W65C22N Interface Chip: ; - IO.RB0-7 -> ; @requires KP_IO: Base Address of IO Chip ; @depends IO-W65C22N ;******************************************************************************** .ifndef INCLUDE_KEYPAD INCLUDE_KEYPAD = 1 .ifndef KP_IO .fatal "KP_IO is not defined: set it to the base address of the IO chip of the Keypad" .endif RBUF_MEM_START = $200 RBUF_MEM_END = $2ff .define RBUF_NAME "keypad" .include "buffer.s65" KB_VAR = $05 ; any free zp address .proc kp_init ; todo remove later and test lda #$ff sta KP_IO+IO::DDRB stz KP_IO+IO::RB ; INIT KEYPAD ; todo: use masks lda #%00001111; KP_IO+IO::RB 0-3 output sta KP_IO+IO::DDRB stz KP_IO+IO::RB ; KP_IO+IO::RB 4-7 1 so keypad press can be detected stz KP_IO+IO::ACR ; lda #%00010000 ; set CB1 to interrupt on pos. edge ; todo: use masks lda #IO::PCR::CB1_IP_AE sta KP_IO+IO::PCR jsr rb_keypad_init ; init keybuffer lda #(IO::IRQ::IRQ | IO::IRQ::CB1) ; enable interrupt for CB1 on KP_IO sta KP_IO+IO::IER rts .endproc ;******************************************************************************** ; @function Read which key is pressed on the keypad ; @details ; Checks which key is pressed and stores it in the keybuffer ; The value stored in the keybuffer is the offset which must be added to ; kp_VALUES to retrieve the key that was pressed ;******************************************************************************** .proc kp_read_on_irq ; test each "row" and check which column is 1 ; todo dont check every column if the value has been found lda #%00001110 ldx #$00 jsr @kp_read_column lda #%00001101 ldx #$04 jsr @kp_read_column lda #%00001011 ldx #$08 jsr @kp_read_column lda #%00000111 ldx #$0c jsr @kp_read_column stz KP_IO+IO::RB ; todo why all zero? ; lda KP_IO+IO::RB ; read to definetly clear the interrupt flag rts @kp_read_column: sta KP_IO+IO::RB lda KP_IO+IO::RB sta KB_VAR ; store result in zeropage so that bbr can be used bbr4 KB_VAR,@kp_write inx bbr5 KB_VAR,@kp_write inx bbr6 KB_VAR,@kp_write inx bbr7 KB_VAR,@kp_write rts @kp_write: lda kp_VALUES,x jsr rb_keypad_write rts .endproc .segment "RODATA" kp_VALUES: ; TODO change to literal .byte "123A", "456B", "789C", "*0#D" .endif