(C) SKL-KEEPER aka Kolesnikov
Leading the section "Coding for Dummies
__________________________________________
For Dummies. " Detail about one of the
ways to survey the keyboard.
For Beginners "kodit" their programs
inevitably raises the question of whether and how
to organize a poll the keyboard
algebra? For without this, no
program will not do, ka
Coy would be easy, it was not!
There are several ways
Poll the keyboard, for example,
using the system
variables KSTATE and LASTK.
I think that these methods are unlikely
whether the cause difficulties in
understanding why they are not
should stop. But
way to poll the keyboard as
external port, though exposition
wives in a number of computer publications, but
presented as something not very intelligible, and may not be
very clear. Here at there we stop.
This method, unlike the above, you can apply for the
organization moving an object on the screen simultaneously two
directions, or, for example, to monitor keystrokes
simultaneously with another key (arrow keys, etc.).
Poll the keyboard in this way is
the only acceptable if
interruption is prohibited. Incidentally, this fact can be used
to accelerate of your program. Put a DI before
poll the keyboard and the processor will not be 50
times a second call a procedure KEYSCAN.
A small, but time saving!
Just do not forget after you exit the procedure include
interrupt command EI ...
Let me remind you that the poll the keyboard in this way is
the external port 254 (# FE HEX). Since the address of the
keyboard port is a two-byte number, then # FE is the low byte
address port, and the senior must be pre-installed in the
battery.
Analyzes five bits. When
Moreover, if a key is pressed, the bit is turned off
(Conversely, in comparison with the Kempston joystick). In this
method checks the status of one half of a number keypad.
Consider Figure keyboard:
N: BITA 0 1 2 3 4 4 3 2 1 0
N_
N_ 3 1 2 3 4 5 6 7 8 9 0 4 P
P O
O 2 Q W E R T Y U I O P A 5
A Y
At 1 A S D F G H J K L ENT6 P
P I
I 0 CS Z X C V B N M SS SPC7 D
DA
A
3 - # F7 4 - # EF
2 - # FB 5 - # DF
1 - # FD 6 - # BF
0 - # FE 7 - # 7F
A small explanation of the figure. Above the keyboard,
horizontal, I put down number of bits that are active after
pressing a certain klavishi.Po vertical numbered rooms
poluryadov. By the way, poluryady numbered conventionally.
Now, for example, if we press the button
"1", then the third will be used poluryad and active will be
zero bits.
Now let us consider a concrete example:
We must organize a poll on the keyboard
keystrokes: "1" and "2". In accordance
so what key is pressed, will change
Border color (so clearly). So ...
KEY HALT; wait for the next interrupt
XOR A; zero out the battery.
LD A, # F7; record in the battery
; Poluryad our (third).
IN A, (# FE); low bit port address.
BIT 0, A; define consist. bits
; Sootvetstvuyusch.klavishe "1"
, If the bit is active, the flag
; Z = 1, if not, then Z = 0.
JR Z, BLUE; if Z = 1, then the operation
; JR Z. .. will be executed
And if Z = 0, we have it "Prosky
; Chili.
LD A, # F7; again our poluryad.
IN A, (# FE); poll clave.
BIT 1, A; determine the status of the first
; Vågå bit which corresponds to
, Corresponds to key "2".
JR Z, RED; here is similar to previous
; Schemu.
JR KEY; more personal interview
; Do not, "loops".
BLUE LD A, 1; to accumulate. - Color blue
OUT (254), A; BORDER 1.
RET
RED LD A, 2, deuce, respectively. red
OUT (254), A; BORBER 2.
RET
We hope you all became clear. Note that the bits of the
right poluryada go from right to left. To really quite and for
all deal with this issue, give an example, the same program, but
for another poll poluryada.Organizuem keys: "9" and "0":
KEY HALT; waiting for the next interrupt
XOR A; zero out the battery.
LD A, # EF; record in the battery
; Our poluryad (fourth).
IN A, (# FE); low bit port address.
BIT 0, A; define consist. bits
; Sootvetstvuyusch.klavishe "0"
, If the bit is active, the flag
; Z = 1, if not, then Z = 0.
JR Z, BLUE; if Z = 1, then the operation
; JR Z, BLUE will be satisfied
; Nyatsya
LD A, # EF; again our poluryad.
IN A, (# FE); poll clave.
BIT 1, A; determine the status of the first
; Vågå bit which corresponds to
, Corresponds to the key "9".
JR Z, RED; here is similar to previous
; Schemu.
JR KEY; "loops."
BLUE LD A, 1
OUT (254), A
RET
RED LD A, 2
OUT (254), A
RET
It's over. And if we need to identify pressing together
with CAPS SHIFT? Here is program for you where we do a poll
pressed "6" and 7 "together with the CAPS SHIFT:
KEY HALT; expect to interrupt.
XOR A; resets the battery.
LD A, # FE; because CAPS SHIFT is
; In zero poluryadu.
IN A, (# FE); poll clave.
BIT 0, A; define the state
; Zero bits.
JR NZ, PROG; if the fold. "CSh" not clicking
; That, we'll go to the label
; PROG, where we have some
; Own program, but if
; Pressed, Z = 1 and JR Z, PROG
And we "go clear."
LD A, # EF; here we take 4 poluryad.
IN A, (# FE); poll clave.
BIT 3, A; third bit-key "7".
CALL Z, BLUE; when pressed-> tag BLUE.
LD A, # EF; fourth poluryad.
IN A, (# FE); poll clave.
BIT 4, A; fourth bit - champ. "6."
CALL Z, RED; when pressed -> Tag RED.
PROG .......... And here is your
.......... ; Program, for example,
.......... ; Survey of other keys.
JR KEY; loops.
BLUE LD A, 1; here is all clear ...
OUT (254), A
RET
RED LD A, 2
OUT (254), A
RET
Now, hopefully, the issues on this topic will not arise.
It should be noted that there is another
almost exactly the same way to survey the keyboard, which
differs from the above that the larger number of verifiable is
in the register pair BC, and a code key that was pressed is
transferred into register A. It is:
LD BC, nnnn
IN A, (C); input data from port
; In the battery.
Here nnnn - address the external port to be installed in BC.
We need only remember that case C would be # FE (constantly),
and B - value corresponding to the number poluryada. For
example, for our first applets instead:
LD A, # F7
IN A, (# FE)
should dial:
LD BC, # F7FE
IN A, (C)
Another small subtlety.
Check bit 0 can not produce
team BIT 0, A, and a team of RRCA, if
verifiable key extreme on the keyboard
as, for example, the keys Q, A and P.
As is known, RRCA means ROTATE ACCUMULATOR RIGHT WITHOUT
CARRY (rotate accumulator right without carry flag). Flag
transfer even though changes in the light of the operation, but
in the rotation is not involved.
Using the command RRCA saves memory, because a single-byte
command. When We use it to produce rotation of the contents of
the battery to the right, when you audited key flag off
(C = 0). Thus, instead of:
BIT 0, A
JR Z, BLUE
in our first program can be put
Design:
RRCA
JR C, BLUE; if a key is pressed,
, As indicated by the established
; Lenny bit, the team of "pro
; Skakivaetsya.
And here is a piece of software that lets you check your
keystrokes and O P:
........
KEY1 LD A, # DF; cells. O and P - 5 poluryad
IN A, (# FE)
RRCA; key P
JR C, LABEL1; conditional tag.
, Because press O and P are in one of; luryadu, run the command
twice IN No ; Necessary. Just another shift bi, you're right
battery.
RRCA; key ON
JR C, LABEL2
......... ; Well, and so on.
I think this is possible and finish.
In conclusion, I want to thank Andrew Rila (ICB) for
assistance in preparing article.
----------------------------------------- P. S. And we, in
turn, would like to thank Sergei Kolesnikov for his active work
in preparing articles for the magazine, and congratulate him on
his appointment as the lead section. We're really pleased to be
dealing with here is people!
-----------------------------------------