📄 main.asm
字号:
;****************************************************************
;* remote mouse & keyboard
;* ---------------------------------------
;* Mcu: sonix 2612
;* Voltage: 3v,4Mhz crystal
;* Author: winter zhao
;* Date : 2006-4-26 21:49
;* Other:
;*
;* (c) Copyright 2006, KeP TECHNOLOGY CO., LTD.
;****************************************************************
CHIP SN8P2612 ; Select the CHIP
//{{SONIX_CODE_OPTION
.Code_Option LVD LVD_H ; 2.4V Reset Enable LVD36 bit of PFLAG for 3.6V Low Voltage Indicator
.Code_Option Reset_Pin P15
.Code_Option Watch_Dog Disable
.Code_Option High_Clk 4M_X'tal ; Crystal/Resonator: 2Mhz~10Mhz
.Code_Option Fcpu #0 ; Fcpu = Fosc/1
.Code_Option Security Disable
.Code_Option Noise_Filter Disable
//}}SONIX_CODE_OPTION'
;-------------------------------------------------------------------------------
; Include Files
;-------------------------------------------------------------------------------
.nolist ; do not list the macro file
;INCLUDE macro.h
INCLUDESTD MACRO1.H
INCLUDESTD MACRO2.H
INCLUDESTD MACRO3.H
.list ; Enable the listing function
;-------------------------------------------------------------------------------
; Constants Definition
;-------------------------------------------------------------------------------
B_KEY1O = P5.4
B_KEY2O = P5.5
B_KEY3O = P5.6
B_KEY4O = P1.0
B_KEY5O = P1.1
B_KEY1I = P1.2
B_KEY2I = P1.3
B_KEY3I = P1.5
B_SCK = P0.0
B_SDO = P5.2
B_SDI = P5.1
B_TP_POWER = P5.0
B_SS = P5.3
B_IR = P5.7
READ_ID = 01010011111100000000000000000001b
READ_XY = 01010011001000000000000000000001b
;-------------------------------------------------------------------------------
; Variables Definition
;-------------------------------------------------------------------------------
.DATA
;Bank 0 data section start from RAM address 0x000 00~3F
ORG 000H
Temp0 ds 1
Temp1 ds 1
TpCount ds 1
TP_Data0 ds 1
TP_Data1 ds 1
TP_Data2 ds 1
TP_Data3 ds 1
TP_Data4 ds 1
TPCom0 ds 1 ;command and record return state
TpCom1 ds 1
TpCom2 ds 1
TpCom3 ds 1
IrData0 ds 1
IntFlag ds 1
Read40State = IntFlag.0
HighCount ds 1
KeyCode ds 1
C_MouseL = 15
C_MouseR = 3
KeyBuf ds 1
KeyBuf2 ds 1
BitFlag ds 1
B_32msFlag = BitFlag.0
B_KeyState = BitFlag.1
B_FingerState = BitFlag.2 ;1=pressing
;B_LeKeTmrEn = BitFlag.3 ;left key timer enable control
AutoOffTimer ds 2
LeftKeyTimer ds 1
C_UpTime = 200/20 ;in 200ms
;BANK0 80~FFH
;-------------------------------------------------------------------------------
; Program code start
;-------------------------------------------------------------------------------
.CODE
ORG 0 ;Code section start
jmp Reset ;Reset vector
;Address 4 to 7 are reserved
ORG 8
jmp Isr ;Interrupt vector
ORG 10H
;===============================================
Reset:
mov A,#00000111B ;Initial stack pointer and
b0mov STKP,A ;disable global interrupt
clr PFLAG ;pflag = x,x,x,x,x,c,dc,z
mov A,#00h ;Initial system mode
b0mov OSCM,A
mov A, #0x5A
b0mov WDTR, A ;Clear watchdog timer
mov a,#00100110b ;set p1.2,p1.3,p1.5 wake up
b0mov P1W,a
mov A,#00000000B ;all interrupt disable
b0mov INTEN,A
mov A,#00000000B ;clear all interrupt request
b0mov INTRQ,A
mov a,#00000000B
b0mov TC0R,A
mov a,#00000000B
b0mov T0C,A
mov A,#00000000B ;
b0mov T0M,A ; To set T0 clock = Fcpu / 256
mov a,#00000000B
b0mov TC0M,A
mov a,#00000000B
b0mov TC0C,A
mov a,#00000000B
b0mov P1OC,A
mov a,#00010000B ;p0 fall edge trigger
b0mov PEDGE,a
;---------------
call InitialPort ;close key output,close Tp
call Delay50ms
call OpenTpPower
call Delay10ms
call ReadSpi32
DINT
;---------------
call ClrRAM ;Clear RAM
mov a,#11000000b
mov IrData0,a
clr INTRQ
EINT P00 ;enable int0 interrupt,clr irq flag
EINT ;enable global interrupt
;-------------------------------------------------------------------------------
; Main application
;-------------------------------------------------------------------------------
Main:
; Set T0 timer wakeup function.
b0bclr FT0ENB ; To disable T0 timer
mov A,#00H
b0mov T0C,A ; To set T0C initial value = 00(To set T0 interval = 16 ms
b0bclr FT0IRQ ; To clear T0 interrupt request
b0bset FT0ENB ; To enable T0 timer
;---------------
b0bts0 Read40State
jmp TouchIrMode
;---------------Go into green mode
IntoGreenMode:
b0bclr FCPUM0 ;To set CPUMx = 10
b0bset FCPUM1
nop
nop ;green mode
nop
nop
;================================================
b0bts1 Read40State
jmp NormalIrMode
TouchIrMode:
b0bclr Read40State
call KeyScan
;---------------
mov a,TP_Data1
cmprs a,#0ffh
jmp FingerPressing
bclr B_FingerState ;finger put up
mov a,LeftKeyTimer
sub a,#C_UpTime
b0bts0 FC
jmp LeftKeyEnd
mov a,#C_MouseL ;if finger up<uptime,equ leftkey
mov KeyCode,a
jmp LeftKeyEnd
FingerPressing:
b0bts0 B_FingerState
jmp AlreadyPress
clr LeftKeyTimer ;for the first time press finger
bset B_FingerState
jmp LeftKeyEnd
AlreadyPress:
incs LeftKeyTimer
mov LeftKeyTimer,a ;equal 255 stop
LeftKeyEnd:
;---------------
bset B_32msFlag ;set 32ms
clr AutoOffTimer
clr AutoOfftimer+1
;---------------send IR
SendIR:
mov a,IrData0
call IrSend8b ;head,id
mov a,TP_Data1
call IrSend8b ;x location
mov a,TP_Data2
call IrSend8b ;y location
mov a,KeyCode
call IrSend8b ;key
clr KeyCode
jmp Main
;================================================
NormalIrMode:
incs LeftKeyTimer
mov LeftKeyTimer,a ;equal 255 stop
;---------------
mov a,@INT(B_32msFlag)
xor a,#@FIELD(B_32msFlag)
mov @INT(B_32msFlag),a
b0bts1 B_32msFlag
jmp NormalEnd
;---------------32ms interval
call KeyScan
mov a,KeyCode
b0bts0 FZ
jmp AutoOffControl
;---------------
clr AutoOffTimer
clr AutoOfftimer+1
;---------------
mov a,IrData0
call IrSend8b ;head,id
mov a,TP_Data1
call IrSend8b ;x location
mov a,TP_Data2
call IrSend8b ;y location
mov a,KeyCode
call IrSend8b
clr KeyCode ;key
jmp NormalEnd
;---------------
AutoOffControl:
incms AutoOffTimer
jmp NormalEnd
incs AutoOffTimer+1
mov AutoOffTimer+1,a
cmprs a,#15 ;after 2minute,sleep
jmp NormalEnd
;---------------into sleep
bclr B_FingerState
clr INTRQ
b0bclr FGIE
call InitialPort ;close key output(open out pullup),close Tp
mov a,#@FIELD(B_KEY1I)|@FIELD(B_KEY2I)|@FIELD(B_KEY3I)
mov P1W,a
nop
nop
b0bset FCPUM0 ;To set CPUMx = 01 system sleep
b0bclr FCPUM1
nop
nop
jmp Reset ;sleep to wakeup
NormalEnd:
jmp Main
;================================================
;-------------------------------------------------------------------------------
; Isr (Interrupt Service Routine)
; Arguments :
; Returns :
; Reg Change:
;-------------------------------------------------------------------------------
Isr:
;-----------------------------------
; Save ACC and system registers
;-----------------------------------
push ;Save A and Pflag
;-----------------------------------
; Check which interrupt happen
;-----------------------------------
;
;INTP00CHK: ; Check INT0 interrupt request
; B0BTS1 FP00IEN ; Check P00IEN
; JMP INTP01CHK ; Jump check to next interrupt
; B0BTS0 FP00IRQ ; Check P00IRQ
JMP INTP00 ; Jump to INT0 interrupt service routine
;INTP01CHK: ; Check INT1 interrupt request
; B0BTS1 FP01IEN ; Check P01IEN
; JMP INTT0CHK ; Jump check to next interrupt
; B0BTS0 FP01IRQ ; Check P01IRQ
; JMP INTP01 ; Jump to INT1 interrupt service routine
;INTT0CHK: ; Check T0 interrupt request
; B0BTS1 FT0IEN ; Check T0IEN
; JMP INTTC1CHK ; Jump check to next interrupt
; B0BTS0 FT0IRQ ; Check T0IRQ
; JMP INTT0 ; Jump to T0 interrupt service routine
;INTTC1CHK: ; Check TC1 interrupt request
; B0BTS1 FTC1IEN ; Check TC1IEN
; JMP INT_EXIT ; Jump to exit of IRQ
; B0BTS0 FTC1IRQ ; Check TC1IRQ
; JMP INTTC1 ; Jump to TC1 interrupt service routine
;-------------------------------------------------------------------------------
; INT0 interrupt service routine
;-------------------------------------------------------------------------------
INTP00:
b0bclr FP00IRQ
;Process P0.0 external interrupt here
b0bts0 B_SCK
jmp INT_EXIT
;Is low ,start read
clr TpCount
clr HighCount
Read40Loop:
b0bts1 B_SCK
jmp WaitLtoH ;high->low
incms HighCount
jmp $-3
jmp INT_EXIT ;overflow exit
WaitLtoH:
clr HighCount
b0bts1 B_SCK
jmp $-1
B2B B_SDO,FC ;low->high
rlcm TP_Data4
rlcm TP_Data3
rlcm TP_Data2
rlcm TP_Data1
rlcm TP_Data0
incms TpCount
mov a,TpCount
cmprs a,#40
jmp Read40Loop
;Read40bitOk,check right
b0bts0 TP_Data0.7
jmp $+3
b0bts0 TP_Data4.0
jmp @f
jmp Reset
@@: bset Read40State
jmp INT_EXIT
;-------------------------------------------------------------------------------
; INT1 interrupt service routine
;-------------------------------------------------------------------------------
INTP01:
b0bclr FP01IRQ
;Process P0.1 external interrupt here
jmp INT_EXIT
;-------------------------------------------------------------------------------
; T0 interrupt service routine
;-------------------------------------------------------------------------------
INTT0:
b0bclr FT0IRQ
;Process T0 internal interrupt here
jmp INT_EXIT
;-------------------------------------------------------------------------------
; TC1 interrupt service routine
;-------------------------------------------------------------------------------
INTTC1:
; b0bclr FTC1IRQ
;Process TC1 timer interrupt here
;-----------------------------------
; Exit interrupt service routine
;-----------------------------------
INT_EXIT:
pop ;Restore A and Pflag
reti ;Exit the interrupt routine
;-------------------------------------------------------------------------------
; ClrRAM
; Use index @YZ to clear RAM (00h~7Fh)
;-------------------------------------------------------------------------------
ClrRAM:
; RAM Bank 0
clr Y ;Select bank 0
mov A, #0x7F
b0mov Z, A ;Set @YZ address from 7fh
ClrRAM10:
clr @YZ ;Clear @YZ content
decms Z ;z = z - 1 , skip next if z=0
jmp ClrRAM10
clr @YZ ;Clear address 0x00
ret
;===============SUBROUTINE AREA=========================
;=======================================================
;**********************************************
; Key Scan
;**********************************************
KeyScan:
clr KeyBuf
;---------------whole scan
b0bset P5M.@BIT(B_KEY1O)
b0bset P5M.@BIT(B_KEY2O)
b0bset P5M.@BIT(B_KEY3O)
b0bset P1M.@BIT(B_KEY4O)
b0bset P1M.@BIT(B_KEY5O)
b0bclr B_KEY1O
b0bclr B_KEY2O
b0bclr B_KEY3O
b0bclr B_KEY4O
b0bclr B_KEY5O
call Delay10us
b0bts1 B_KEY1I
jmp @f
b0bts1 B_KEY2I
jmp @f
b0bts0 B_KEY3I
jmp Nokey
@@:
mov a,#00101111B
b0mov P1UR,a
mov a,#01110100B
b0mov P5UR,a
b0bclr P5M.@BIT(B_KEY2O)
b0bclr P5M.@BIT(B_KEY3O)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -