📄 hardware.asm
字号:
; This is hardware.asm - it contains the routines that touch the hardware
; void init_hardware(void);
; BYTE scan_buttons(void)
include "m8c.inc"
include "M8Cmacros.inc"
AREA bss (RAM, REL)
RawButtonData: BLK 1 ; Temporary
ButtonStatus: BLK 4 ; Bits 7..3 = debounce counter, Bits 10 = state
ButtonsValue: BLK 1 ; Debounced buttons value
AREA UserModules (ROM, REL)
.section
_init_hardware::
MOV Reg[PRT3DR], 0xFC ; LEDs/S2/S1 OFF
; I am using the analog mux input bus for VR1 and VR2, set this up
MOV Reg[AMUXCFG], 0x40 ; Allow analog mux to drive PGA
M8C_SetBank1
MOV Reg[DAC_CR], 0 ; Disable analog mux DAC
MOV Reg[MUX_CR3], 0x80 ; Set VR1 to initially drive analog mux bus
M8C_SetBank0
RET
;------------------------
; Constant Definitions
;------------------------
ButtonStateMask: EQU %00000011
ButtonUpState: EQU 0
ButtonDownState: EQU 1
ButtonGoingDownState: EQU 2
ButtonGoingUpState: EQU 3
DebounceTime: EQU 70H
AREA UserModules (ROM, REL)
_scan_buttons::
; Buttons are active high on port 3 bits 10
MOV A, Reg[PRT3DR]
CPL A
MOV [RawButtonData], A
MOV X, 3
; Update the state machine for each button
Loop:
MOV A, [X+ButtonStatus]
AND A, ButtonStateMask
CALL DoButtonAction
DEC X
JNC Loop
; Return the debounced value
MOV A, [ButtonsValue]
RETURN: ; Label needed by M8Cmacros.inc
RET
DoButtonAction:
INDEX ButtonAction
JACC ButtonAction
ButtonAction:
DB ButtonUp-ButtonAction
DB ButtonDown-ButtonAction
DB ButtonGoingDown-ButtonAction
DB ButtonGoingUp-ButtonAction
ButtonUp:
; Button is currently up, is it being pressed
CALL GetButtonPosition
RNZ
; Button is starting down, need to debounce it
MOV A, DebounceTime | ButtonGoingDownState
MOV [X+ButtonStatus], A
RET
ButtonGoingDown:
; Check that button is still down
CALL GetButtonPosition
JNZ GoToButtonUp
CALL IncrementCounter
RNC
; Button down has been debounced
MOV A, X
INDEX IndexToBit
OR [ButtonsValue], A
GoToButtonDown:
MOV A, ButtonDownState
MOV [X+ButtonStatus], A
RET
ButtonDown:
; Button is currently down, is it being released?
CALL GetButtonPosition
RZ
; Button is starting up, need to debounce it
MOV A, DebounceTime | ButtonGoingUpState
MOV [X+ButtonStatus], A
RET
ButtonGoingUp:
; Check that button is still up
CALL GetButtonPosition
JZ GoToButtonDown
CALL IncrementCounter
RNC
; Button up has been debounced
MOV A, X
INDEX IndexToBit
CPL A
AND [ButtonsValue], A
GoToButtonUp:
MOV A, ButtonUpState
MOV [X+ButtonStatus], A
RET
GetButtonPosition:
MOV A, X
INDEX IndexToBit
AND A, [RawButtonData]
RET
IncrementCounter:
MOV A, 10H ; Put 1 in counter field
ADD A, [X+ButtonStatus] ; Sets C on done
MOV [X+ButtonStatus], A
RET
IndexToBit:
DB %00000001 ; 0
DB %00000010 ; 1
DB %00000100 ; 2
DB %00001000 ; 3
DB %00010000 ; 4
DB %00100000 ; 5
DB %01000000 ; 6
DB %10000000 ; 7
.endsection
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -