📄 hardware.asm
字号:
; This is hardware.asm - it contains the routines that touch the hardware
; void init_hardware(void);
; void update_LEDs(BYTE Value)
; BYTE scan_buttons(void)
include "m8c.inc"
include "M8Cmacros.inc"
AREA bss (RAM, REL)
RawButtonData: BLK 1
ButtonStatus: BLK 4 ; Top 5 bits = debounce counter, Bits 10 are state
ButtonsValue: BLK 1 ; Debounced buttons value
AREA UserModules (ROM, REL)
.section
_init_hardware::
MOV Reg[PRT2DR], 0xFF ; Clear buttons
MOV Reg[PRT3DR], 0xFF ; LEDs OFF
RET
_update_LEDs::
; Value is passed in A, lower 4 bits are valid
; LEDs are active low on Port 3 bits 2..5
ASL A
ASL A ; Get bits in correct position
CPL A ; Get bits in correct polarity
OR A, %11000011 ; Mask off other bits
MOV Reg[PRT3DR], A ; Update the hardware
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 low on port 2 bits 2..5
MOV A, Reg[PRT2DR]
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
; Create a composite value for the return 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 ButtonPosition
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
ButtonPosition:
DB %00000100 ; 2
DB %00001000 ; 3
DB %00010000 ; 4
DB %00100000 ; 5
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 + -