0 Members and 3 Guests are viewing this topic.
LD b, 4 ; whatever many times the loop is executedloop: LD A, %11111111 ; group OUT (1), A IN A, (1) CP %11101101 ; whatever key(s) you wanna check for JR Z, Label ; if the keys you were checking for were pressed, JP to label djnz loop
LD b, 4 ; whatever many times the loop is executedloop: b_call _GetCSC CP sk2nd ; whatever key you wanna check for JR Z, Label ; if the keys you were checking for were pressed, JP to label djnz loop
KeyCheck: ld a,(MyKeyVariable) or a jr nz,CheckForRelease ld a, %11111111 out (1), a ld b,%11101101 ;This is a delay before reading the key groups. Does something useful. in a, (1) cp b jp z, Label jr KeysDoneCheckForRelease: xor a out ($01),a ld a,(de) ;Another delay in a,($01) inc a ld (MyKeyVariable),aKeysDone: ...Label: (MyKeyVariable),a ...
Also, you need at least a 7 clock cycle delay between setting the key groups and reading their values because the hardware is slow and otherwise it will read values of the previous key group and can cause weird things to happen.
Code: [Select] ld b,%11101101 ;This is a delay before reading the key groups. Does something useful. ld a,(de) ;Another delay
ld b,%11101101 ;This is a delay before reading the key groups. Does something useful. ld a,(de) ;Another delay
Normally, you don't notice the lack of delay because the first time it passes that part in the program, it has the wrong key group when it tries to read the key, but its set correctly afterward once it finally goes through. The time you will notice it is if you check a bunch of keys one after another from different key groups. Then, each check you do is reading from the previous keygroup, which is wrong and you end up with mismatched keys.
Its possible, but its only a byte more with negligible speed difference, I would go for the extra safety because the downside is so small and if it spares you from a gruesome hour long debugging session in the future it will have all been worth it.
ld a,(MyKeyVariable) or a jr nz,CheckForRelease LD A, %10111111 OUT (1), A ld b,%11101101 IN A, (1) CP %11101111 JP Z, presyequ CP %11110111 JP Z, preswindow CP %11111011 JP Z, preszoom CP %11111101 JP Z, prestrace CP %11111110 JP Z, presgraph; The above all works perfect with your routine, but the below doesn't: CP %11100111 JP Z, presyequ_window CP %11101011 JP Z, presyequ_zoom CP %11101101 JP Z, presyequ_trace CP %11101110 JP Z, presyequ_graph CP %11110011 JP Z, preswindow_zoom CP %11110101 JP Z, preswindow_trace CP %11110110 JP Z, preswindow_graph CP %11111001 JP Z, preszoom_trace CP %11111010 JP Z, preszoom_graph CP %11111100 JP Z, prestrace_graph jr LabelRestoftheScriptCheckForRelease: xor a out ($01),a ld a,(de) in a,($01) inc a ld (MyKeyVariable),aLabelRestoftheScript: stuf.....
;pressed is my counter and my pressed flag;saves space that way ld a, $BF out (01), a ld a, (de) ;this is 7 t-states, but 1 byte in a, (01) ld b, a ld a, (pressed) or a jr z, initialScan jp m, countDown inc b ;if no keys are pressed, b = $FF jr nz, buttonDone xor a ld (pressed), a jr buttonDoneinitialScan: inc b jr z, buttonDone ld a, -15 ;adjust this delay for responsiveness / window for two presses ld (pressed), a jr buttonDonecountDown: inc a ld (pressed), a jr nz, buttonDone inc a ld (pressed), a ;putting a one to show its pressed ;do all your two button cp's here;always use jr instead of jp, jr is 3 t-states faster on fail and 1 byte smaller ;for your singles, I have some optimization rrca jr nc, presGraph rrca jr nc, presTrace rrca jr nc, presZoom rrca jr nc, presWindow rrca jr nc, presYEqu;cp is two bytes and 7 t-states;rrca is 1 bytes and 4 t-statesbuttonDone:
;this time pressed holds the last pressed keyscan ld a, $BF out (01), a ld a, (de) in a, (01) ld b, a ld a, (pressed) or a jr nz, checkForRelease ld a, b inc b jr z, buttonDone ld (pressed), a jr buttonDonecheckForRelease: inc b jr nz, buttonDone ld b, a xor a ld (pressed), a ld a, b;do your checks here;don't forget what I said in the first onebuttonDone:
keyloop: ld a,(OldKey) ;get the stored previous keypress ($ff means none) xor 255 ;set up a mask ld c,a ;store the mask of the keypress that happened earlier ld a,$fe ;get the arrow keys out ($01),a nop ;usually i dont wait, or debounce in a,($01) or $f0 ;mask to only check the arrow keys ld (OldKey),a ;store the new keypress or c ;dont allow the old keypress to get through cp %11110101 jp z,up_left cp %11110011 jp z,up_right cp %11111100 jp z,down_left cp %11111010 jp z,down_right cp %11111110 jp z,down cp %11111101 jp z,left cp %11111011 jp z,right cp %11110111 jp z,up ... ;do stuff or use a timer here ... ;if you read the port too fast ld bc,$ffffDelay: push bc pop bc dec bc ld a,b or c jp nz,Delay ... ;button presses will be too ... ;sensitive. jp keyloopOldKey:.db $ff