📄 sqcomm51.msa
字号:
;FRAME_READ: read a frame from input buffer into user buffer
;Input: DPTR point to user buffer ( MAX. length is 256 )
;Output: C=0 successfull , C=1 failure( no data in buffer )
; ACC= FOF [dest.src] ,R7= length (1 to 253)
; Modify pointer REM_BI
;Data in receiving buffer format as follow (if a data frmae):
;[dest.src] [length(1 to 253)] [data1] [data2] ... [data253]
;Data in user buffer format as follow (if a data frmae):
;[data1] [data2] ... [data253]
FRAME_READ: JNB IN_BUF_EMPT,FRAME_READ1
JMP READ_F_RET
FRAME_READ1: MOV DEST_POINT+1,DPH
CLR ES
MOV DEST_POINT+0,DPL ; save destination pointer
MOV R3,INS_BI+1
MOV R2,INS_BI+0
SETB ES
MOV R5,REM_BI+1
MOV R4,REM_BI+0
CALL COMP16
JNZ FRAME_READ2
SETB IN_BUF_EMPT
JMP READ_F_RET ; no data in buffer jump
FRAME_READ2: MOV DPH,R5 ; Get pointer
MOV DPL,R4
MOVX A,@DPTR ; get FOF
PUSH ACC ; save FOF
%INC_DPTR IN_BUF_START,BI ; Increace buffer pointer
MOVX A,@DPTR ; get length
%INC_DPTR IN_BUF_START,BI ; Increace buffer pointer
PUSH ACC ; save length
MOV R7,A ; counter
READ_F_LOP: MOVX A,@DPTR
MOV SRC_POINT+1,DPH
MOV SRC_POINT+0,DPL ; save source pointer
MOV DPH,DEST_POINT+1
MOV DPL,DEST_POINT+0 ; restore destination pointer
MOVX @DPTR,A ; Move data
INC DPTR
MOV DEST_POINT+1,DPH
MOV DEST_POINT+0,DPL ; save destination pointer
MOV DPH,SRC_POINT+1
MOV DPL,SRC_POINT+0 ; restore source pointer
%INC_DPTR IN_BUF_START,BI ; Increace buffer pointer
DJNZ R7,READ_F_LOP
CLR ES
NOP
MOV REM_BI+1,DPH ; modify pointer
MOV REM_BI+0,DPL
SETB ES
POP ACC
MOV R7,A ; return length
POP ACC ; return FOF
READ_F_RET: MOV C,IN_BUF_EMPT
CLR IN_BUF_FULL ; buffer not full
RET
;============================== Subrouting defination =====================
;TEST_STATION testing station if active
;Input: ACC=[dest.XXXX]
;Output: CY=0 station active , CY=1 station disactive
TEST_STATION: PUSH ACC
ANL A,#DEST
SWAP A
ADD A,#ALL_STATION
MOV R0,A
MOV A,@R0
CPL A
MOV C,ACC.7
MOV NO_DEST_ST,C
POP ACC
RET
;============================== Subrouting defination =====================
;FRAME_WRITE,FRAME_WRITE1 write a data frame into output buffer
;Input: DPTR point to user buffer ( MAX. length is 253 )
;(FRAME_WRITE) ACC=dest. address [dest 0000] or [dest XXXX] or [dest dest]
;(FRAME_WRITE1) ACC=FOF [dest.src]
; R7=length [length(1 to 253)]
;Output: C=0 successfull , C=1 failure
; Modify pointer INS_BO
;Data in user buffer format as follow
;[data1] [data2] ... [data253]
;Data in output buffer format as follow (if a data frmae):
;[dest.src] [control(data)] [length(1 to 253)] [data1] [data2] ... [data253]
FRAME_WRITE: MOV R2,A
ANL A,#DEST
MOV R3,A
MOV A,R2
SWAP A
ANL A,#DEST
ORL A,R3
MOV R3,A ; R2=[dest 0000]
MOV A,ST_ADDR
ANL A,#SOURCE
ORL A,R3 ; A=[dest.src]
FRAME_WRITE1: PUSH ACC ; save FOF
CALL TEST_STATION ; test station , if active CY=0
; if disactive CY=1
JNC FRAME_ACTIVE
CLR C
JMP OUT_BUF_ERR ; if destination station is
; disactive return
FRAME_ACTIVE: MOV A,R7 ; A <= length
DEC A
CLR C
SUBB A,#253-1 ; check length
JC FRAME_LEN_OK
CPL C
JMP OUTBUF_FULL ; length error return
FRAME_LEN_OK: JNB OUT_BUF_FULL,FRAME_W1
JMP OUTBUF_FULL
FRAME_W1: MOV SRC_POINT+0,DPL
MOV SRC_POINT+1,DPH ; save source pointer
MOV DPH,INS_BO+1
MOV DPL,INS_BO+0
CLR ES
MOV R5,REM_BO+1
MOV R4,REM_BO+0
SETB ES
MOV R3,DPH
MOV R2,DPL
CALL SUB16
JNC NO_O_ROLL
CALL CPL16
JMP BUF_ROLL1
NO_O_ROLL: MOV A,R3
MOV R5,A
MOV A,R2
MOV R4,A
MOV R3,#HIGH(BO_LENGTH*BOF_NUM)
MOV R2,#LOW(BO_LENGTH*BOF_NUM)
CALL SUB16
BUF_ROLL1: MOV A,R3
JNZ OUTBUF_USED
SETB OUT_BUF_FULL
JMP OUT_BUF_ERR ; error return
OUTBUF_USED: POP ACC ; restore FOF
PUSH ACC ; save FOF
MOVX @DPTR,A ; write FOF into output buffer
%INC_DPTR OUT_BUF_START,BO ; Increace buffer pointer
MOV A,#DATA ; control byte (=data)
MOVX @DPTR,A ; write length into output buffer
%INC_DPTR OUT_BUF_START,BO ; Increace buffer pointer
MOV A,R7 ; restore length
MOVX @DPTR,A ; write length into output buffer
%INC_DPTR OUT_BUF_START,BO ; Increace buffer pointer
F_WRITE_LOP: MOV DEST_POINT+0,DPL
MOV DEST_POINT+1,DPH ; save destination pointer
MOV DPL,SRC_POINT+0
MOV DPH,SRC_POINT+1 ; restore source pointer
MOVX A,@DPTR
INC DPTR
MOV SRC_POINT+0,DPL
MOV SRC_POINT+1,DPH ; save source pointer
MOV DPL,DEST_POINT+0
MOV DPH,DEST_POINT+1 ; restore destination pointer
MOVX @DPTR,A
%INC_DPTR OUT_BUF_START,BO ; Increace buffer pointer
DJNZ R7,F_WRITE_LOP
CLR ES
MOV INS_BO+1,DPH
MOV INS_BO+0,DPL
MOV DPL,SRC_POINT+0
MOV DPH,SRC_POINT+1 ; restore source pointer
SETB ES
CLR C
OUTBUF_FULL: ORL C,OUT_BUF_FULL
CLR OUT_BUF_EMPT
OUT_BUF_ERR: POP ACC ; restore FOF
RET
;============================== Subrouting defination ============================
;MAKE_TOKEN_F: move token frame from XROM to XRAM
;Input: none
;Output: none
MAKE_TOKEN_F: MOV DPTR,#FRAME_TOKEN
MOV A,ST_ADDR
ANL A,#SOURCE
ORL A,NEXT_ADDR
MOVX @DPTR,A
INC DPTR
MOV A,#TOKEN_PASSING
MOVX @DPTR,A
RET
;============================== Subrouting defination ============================
;MAKE_ACK_FRAME:move token frame from XROM to XRAM
;Input: none
;Output: none
MAKE_ACK_FRAME: MOV R7,#2+2+2
MOV DPTR,#FRAME_REJECT_K
MOV DEST_POINT+1,#HIGH(FRAME_REJECT)
MOV DEST_POINT+0,#LOW(FRAME_REJECT)
MAKE_ACK_LOP: CLR A
MOVC A,@A+DPTR
INC DPTR
MOV SRC_POINT+1,DPH
MOV SRC_POINT+0,DPL
MOV DPH,DEST_POINT+1
MOV DPL,DEST_POINT+0
MOVX @DPTR,A
INC DPTR
MOV DEST_POINT+1,DPH
MOV DEST_POINT+0,DPL
MOV DPH,SRC_POINT+1
MOV DPL,SRC_POINT+0
DJNZ R7,MAKE_ACK_LOP
MOV A,ST_ADDR
ANL A,#SOURCE
ORL A,#GLOB_ADDR AND DEST
MOV DPTR,#FRAME_REJECT
MOVX @DPTR,A
MOV DPTR,#FRAME_RRDY
MOVX @DPTR,A
MOV DPTR,#FRAME_T_ACK
MOVX @DPTR,A
RET
;============================== Subrouting defination ============================
;FRAME_OUT: move data from output buffer into frame output buffer
;Input: none
;Output: C=0 successful , C=1 failure (output buffer empty)
FRAME_OUT: JNB OUT_BUF_EMPT,FRAME_OUT1
JMP FRAME_OUT_RET
FRAME_OUT1: MOV R3,REM_BO+1
MOV R2,REM_BO+0
MOV R5,INS_BO+1
MOV R4,INS_BO+0
CALL COMP16
JNZ FRAME_OUT2 ; output buffer not empty
SETB OUT_BUF_EMPT
JMP FRAME_OUT_RET ; output buffer empty
FRAME_OUT2: MOV DPH,R3
MOV DPL,R2
MOV P2,#HIGH(FRAME_BUFFER)
MOV R0,#LOW(FRAME_BUFFER)
MOVX A,@DPTR
MOVX @R0,A ; address byte (FOF)
%INC_DPTR OUT_BUF_START,BO
CALL INC_P2_R0
MOVX A,@DPTR
MOVX @R0,A ; control byte
%INC_DPTR OUT_BUF_START,BO
CALL INC_P2_R0
MOVX A,@DPTR
MOVX @R0,A ; length byte
%INC_DPTR OUT_BUF_START,BO
CALL INC_P2_R0
MOV R7,A
FRAME_OUT_LOP: MOVX A,@DPTR
MOVX @R0,A ; data byte
%INC_DPTR OUT_BUF_START,BO
CALL INC_P2_R0
DJNZ R7,FRAME_OUT_LOP
MOV REM_BO+1,DPH
MOV REM_BO+0,DPL ; modify the pointer
CLR C
FRAME_OUT_RET: MOV C,OUT_BUF_EMPT
CLR OUT_BUF_FULL
RET
;============================== Subrouting defination ============================
;MY_DELAY: Delay on station addr.
;Input: none
;Output: none
MY_DELAY: PUSH ACC
PUSH B
MOV A,R6
PUSH ACC
MOV A,R7
PUSH ACC
MOV A,ST_ADDR ; Get station addr.
ANL A,#0F0H ; Use higher nibble
INC A ; Station addr.=1 to F
MOV R7,A ; R7=ADDR.*16
S_DELAY: MOV R6,#0
S_DELAY1: %DELAY ; 16 cycles
DJNZ R6,S_DELAY1 ; 16* 256 = 8196 cycles (8ms on 12MHz)
DJNZ R7,S_DELAY ; About 128ms delay
; bettwen two stations
POP ACC
MOV R7,A
POP ACC
MOV R6,A
POP B
POP ACC
RET
;============================== Subrouting defination ============================
;MY_DELAY1: Delay on station addr.
;Input: none
;Output: none
MY_DELAY1: PUSH ACC
PUSH B
MOV A,R6
PUSH ACC
MOV A,R7
PUSH ACC
MOV A,ST_ADDR ; Get station addr.
ANL A,#0F0H ; Use higher nibble
INC A ; Station addr.=1 to F
XRL A,#0F0H
MOV R7,A ; R7=ADDR.*16
S_DELAY2: MOV R6,#0
S_DELAY3: %DELAY ; 16 cycles
DJNZ R6,S_DELAY3 ; 16* 256 = 8196 cycles (8ms on 12MHz)
DJNZ R7,S_DELAY2 ; About 128ms delay
; bettwen two stations
POP ACC
MOV R7,A
POP ACC
MOV R6,A
POP B
POP ACC
RET
;============================== Subrouting defination ============================
;CLR_IRAM: function:clear the on chip ram from 00h to r0 pointed
;Input: R0 the top address to be cleared
; R7 = reset status ( 0:means hardware reset )
;Output: none
;Use reg. ACC,R0
CLR_IRAM: CLR A
CLRLOP: MOV @R0,A
MOV A,R0
CJNE A,#20H,CLR_ON
MOV A,R7
JZ CLRAM_RET
SETB SOFT_RESET
CLRAM_RET: RET
CLR_ON: CLR A
DEC R0
JMP CLRLOP
;============================== Subrouting defination ============================
;COMP16
; (R3.R2)>=(R5.R4):CY=0;ACC<>0
; (R3.R2)<(R5.R4) :CY=1;ACC<>0
; (R3.R2)=(R5.R4) :ACC=0
COMP16: MOV A,R3
CLR C
SUBB A,R5
JNZ COMPBK
MOV A,R2
SUBB A,R4
COMPBK: RET
;============================== Subrouting defination ============================
;ADD16: R3.R2<=R3.R2+R5.R4
ADD16: CLR C
ADD16C: MOV A,R2
ADDC A,R4
MOV R2,A
MOV A,R3
ADDC A,R5
MOV R3,A
ORL A,R2
RET
;============================== Subrouting defination ============================
;SUB16: R3.R2<=R3.R2-R5.R4
SUB16: CLR C
SUB16C: MOV A,R2
SUBB A,R4
MOV R2,A
MOV A,R3
SUBB A,R5
MOV R3,A
ORL A,R2
RET
;============================== Subrouting defination ============================
;CPL16: (R3.R2)<=0-(R3.R21)
CPL16: MOV A,R2
CPL A
ADD A,#1
MOV R2,A
MOV A,R3
CPL A
ADDC A,#0
MOV R3,A
RET
;============================== Subrouting defination ============================
;SYS_INIT: Initialize MCS_51 for system
;Input: none
;Output: none
SYS_INIT: CLR RS0
CLR RS1 ; Use reg. bank 0
MOV T2CON,#0 ; 16-bit auto-reload
MOV RCAP2H,#HIGH(TOKEN_DELAY)
MOV RCAP2L,#LOW(TOKEN_DELAY); Interrupt per 1/16 second
SETB ET2 ; timer2 interrupt enable
;; CALL GET_ADDR
;; XRL A,#ST_NO1
;; JZ IS_ST_NO1
IS_ST_NO1: CLR PT2 ; TIMER2 interrupt in low level
CALL GET_ADDR
MOV ST_ADDR,A ; Save station address
MOV INS_BI+0,#LOW(IN_BUF)
MOV INS_BI+1,#HIGH(IN_BUF)
MOV REM_BI+0,#LOW(IN_BUF)
MOV REM_BI+1,#HIGH(IN_BUF)
MOV INS_BO+0,#LOW(OUT_BUF)
MOV INS_BO+1,#HIGH(OUT_BUF)
MOV REM_BO+0,#LOW(OUT_BUF)
MOV REM_BO+1,#HIGH(OUT_BUF) ; Initialize pointers
CALL SERIAL_INIT ; initialize serial port
SETB EA ; Enable interrupt
RET
;============================== Subrouting defination ============================
;RESET_TIMER2: reset timer2
;Input none
;Output none
RESET_TIMER2: MOV TL2,#LOW(TOKEN_DELAY)
MOV TH2,#HIGH(TOKEN_DELAY) ; interput per 1/40 second
CLR TF2 ; clear timer2 interrupt
RET
;============================== Subrouting defination ============================
;RESET_T2: reset timer2 for last power on station
;Input none
;Output none
RESET_T2: MOV TL2,#LOW(TOKEN_DELAY/8)
MOV TH2,#HIGH(TOKEN_DELAY/8)
CLR TF2 ; clear timer2 interrupt
RET
;============================== Subrouting defination ============================
;NET_INSERT: insert a station to network
;Input ACC= [X X X X S3 S2 S1 S0]
; S3S2S1S0= station address to be inserted
;Output C=0 successful , C=1 failure
NET_INSERT: ANL A,#SOURCE
PUSH ACC
ADD A,#ALL_STATION
MOV R0,A
POP ACC ; restore source address
PUSH ACC
ORL A,#ACTIVE
MOV @R0,A ; save disactive station in table
CALL NEXT_TOKEN ; modify next station addres
POP ACC ; restore source address
ORL A,#(GLOB_ADDR AND DEST)
MOV DPTR,#COMMAND_BUF
MOVX @DPTR,A
INC DPTR
MOV A,#INSERT
MOVX @DPTR,A
CALL SEND_COMMAND
RET
;============================== Subrouting defination ============================
;NET_REMOVE: remove a station from network
;Input ACC= [X X X X S3 S2 S1 S0]
; S3S2S1S0= station address to be removed
;Output C=0 successful , C=1 failure
NET_REMOVE: ANL A,#SOURCE
PUSH ACC
ADD A,#ALL_STATION
MOV R0,A
POP ACC ; restore source address
PUSH ACC
ANL A,#SOURCE
MOV @R0,A ; save disactive station in table
CALL NEXT_TOKEN ; modify next station addres
POP ACC ; restore source address
ORL A,#(GLOB_ADDR AND DEST)
MOV DPTR,#COMMAND_BUF
MOVX @DPTR,A
INC DPTR
MOV A,#REMOVE
MOVX @DPTR,A
CALL SEND_COMMAND ; modify the net token table
RET
;============================== Subrouting defination ============================
;SYS_SYN: system synchronize
;Input none
;Output none
SYS_SYN: MOV A,ST_ADDR
ANL A,#SOURCE
ORL A,#(GLOB_ADDR AND DEST) ; station address that send SYNCHRO. command
MOV DPTR,#COMMAND_BUF
MOVX @DPTR,A
INC DPTR
MOV A,#SYNCHRONIZE
MOVX @DPTR,A
CALL SEND_COMMAND ; modify the net token table
RET
;============================== Subrouting defination ============================
;NET_COMMAND: send broadcasting network command
;Input ACC= net command
;Output C=0 successful , C=1 failure
NET_COMMAND: PUSH ACC ; save command
MOV A,ST_ADDR ; get my address
ANL A,#SOURCE
ORL A,#(GLOB_ADDR AND DEST)
MOV DPTR,#COMMAND_BUF
MOVX @DPTR,A ; FOF
INC DPTR
POP ACC
MOVX @DPTR,A ; command (control byte)
SEND_COMMAND: MOV DPTR,#COMMAND_BUF
CALL SERIAL_WRITE
RET
;============================== Subrouting defination ============================
;ST_REMOVE: remove a station from net.
;Input ACC [XXXXssss] ssss is the address of station to be removed
;Output none
ST_REMOVE: MOV MODIFY_ADDR,A
ANL A,#SOURCE
MOV R0,#ALL_STATION
ADD A,R0
MOV R0,A
MOV A,@R0
ANL A,#7FH ; disactive soource station
MOV @R0,A
SETB REMOVE_MK
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -