While looking through this discussion I found a simple method of reading arrow key inputs. But what is actually happening here?

Code:

getArrowKeys:
  ld a,$FE
  out (1),a
  nop
  nop
  in a,(1)
  cpl
  ret

Based on my understanding, the "ld a, $FE: out (1), a" is just resetting the keyboard, and the "in a, (1)" is actually retrieving data from it. But why are there two "nop"s? Why is the data from "a" getting complemented? How is this detecting multiple keys? Why would it only detect arrow keys?
Take a look at Day 22 in ASM in 28 Days.

Quote:
Based on my understanding, the "ld a, $FE: out (1), a" is just resetting the keyboard

Nope: This is selecting which key group we're scanning, in this case the arrow keys.

Quote:
the "in a, (1)" is actually retrieving data from it
This retrieves which keys are pressed in the key group we just selected.

Quote:
But why are there two "nop"s?
It's necessary to pause for a few microseconds to let the keypad matrix's output stabilize before reading.

Quote:
Why is the data from "a" getting complemented?
A depressed key returns a "0" in the corresponding bit; a released key returns a "1". This gives you the more easily understandable "1" for a depressed key.

Quote:
How is this detecting multiple keys?
By scanning 8 keys simultaneously, 1 bit per key, you can detect the state of 8 keys at a time. Unlike GetKey or GetCSC, you're not getting a unique code from port 1 for each unique key - depending on which group you select, in a,(1) returning 0xFE could be [down], [ENTER], [(-)], [.], [0], or [GRAPH] being pressed.

Quote:
Why would it only detect arrow keys?
Because you've only selected the arrow key group - because each bit is a key, you can only scan 8 keys per group (it's a matrix of rows and columns).
Quote:
It's necessary to pause for a few microseconds to let the keypad matrix's output stabilize before reading.

What resources are there for knowing how to time things?
Beakbonk wrote:
Quote:
It's necessary to pause for a few microseconds to let the keypad matrix's output stabilize before reading.

What resources are there for knowing how to time things?

Information is available here: https://wikiti.brandonw.net/index.php?title=83Plus:Ports:01

See the "Delay" section.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement