⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqcomm51.msa

📁 51串行令牌格式 a51编译 Read data from serial port and write into buffer DPTR pointed in XRAM if a data
💻 MSA
📖 第 1 页 / 共 3 页
字号:


;=============================================================================
T2CON		=	0C8H
TH2		=	0CDH
TL2		=	0CCH
PT2		=	IP.5
ET2		=	IE.5
RCAP2H		=	0CBH
RCAP2L		=	0CAH
PCON		=	87H			; Power control

TF2		=	T2CON+7
EXF2		=	T2CON+6
RCLK		=	T2CON+5
TCLK		=	T2CON+4
EXEN2		=	T2CON+3
TR2		=	T2CON+2
C_T2		=	T2CON+1
CP_RL2		=	T2CON+0

;=============================================================================

ST_NO0		=	00H
ST_NO1		=	11H
ST_NO2		=	22H
ST_NO3		=	33H
ST_NO4		=	44H
ST_NO5		=	55H
ST_NO6		=	66H
ST_NO7		=	77H
ST_NO8		=	88H
ST_NO9		=	99H
ST_NOA		=	0AAH
ST_NOB		=	0BBH
ST_NOC		=	0CCH
ST_NOD		=	0DDH
ST_NOE		=	0EEH
ST_NOF		=	0FFH

ACTIVE		=	80H			; station active mark

GLOB_ADDR	=	0FFH
SUM_CODE	=	5AH
SOURCE		=	0FH			; Lower nibble source address
DEST		=	0F0H			; higher nibble dest. address
BI_LENGTH	=	256			; Size of one frame (bytes)
BO_LENGTH	=	256			; Size of one frame (bytes)
BIF_NUM 	=	32			; Numbers of input buffer fram in MAX. length
BOF_NUM 	=	32			; Numbers of output buffer fram in MAX. length

EN_I		=	P1.4			; Enable receiving bus
EN_O		=	P1.5			; Enable sending bus
MAX_RETRY_T	=	16+ 1			; retry 8 times for token passing
MAX_RETRY_D	=	4 + 1			; retry 4 times for data transmite
MAX_SEND	=	4 + 1			; sending 4 frames for one token period
BROD_ADDR	=	0FFH			; only one station
TOKEN_DELAY	=	-10000			; token passing delay including
						; data transmit time of first
						; token station (10ms)

;======================== System command defination ==========================
NO_DATA 	=	00H
; Receive station acknowledges
RRDY		=	11H			; Receive ready
REJECT		=	22H			; Request retransmitting
; Transmission station command
INSERT		=	33H			; insert a station
REMOVE		=	44H			; remove a station
TOKEN_PASSING	=	55H			; Token passing
DATA		=	66H			; Data frame
RESTART 	=	77H			; CPU restart
HELLO		=	88H			; first call in network
SYNCHRONIZE	=	99H			; synchronize system
TOKEN_RRDY	=	0AAH			; token passing acknowledge
TEST_BB_OUT	=	0BBH			; test bit bus for output
NO_COMMAND2	=	0CCH			; no command defination
NO_COMMAND3	=	0DDH			; no command defination
NO_COMMAND4	=	0EEH			; no command defination
NO_COMMAND5	=	0FFH			; no command defination

		RSEG	BIT(0)
TOKEN		DS	1			; TOKEN=1 Enable transmition
FIRST_TOKEN	DS	1			; FIRST_TOKEN=0 station get first token
DATA_ACK	DS	1			; 0:means no transmiting data ACK
TOKEN_ACK	DS	1			; 0:means no transmiting token ACK
IN_BUF_FULL	DS	1			; 1:means buffer full
IN_BUF_EMPT	DS	1			; 1:means buffer empty
OUT_BUF_FULL	DS	1			; 1:means buffer full
OUT_BUF_EMPT	DS	1			; 1:means buffer empty
R_OUT_TIME	DS	1			; 1:means receive out of time
NO_DEST_ST	DS	1			; 1:means no destination station
NET_LINE_ERR	DS	1			; 1:means net line error
NET_GOOD	DS	1			; 1:means net initialization completed
REMOVE_MK	DS	1			; 1:means a station to be removed
INSERT_MK	DS	1			; 1:means a ststion to be inserted
RESET_MK	DS	1			; 1:means all station to be reset
SYNCHRO_MK	DS	1			; 1:manes system to be synchronized by next sys. syn.
TOKEN_RETRY_MK	DS	1			; 1:means token retry
						; out of time retry
USER_SYN	DS	1			; 1:means sequence nnumber synchronize
						; set by SYNCHRONIZE command
						; cleared by system synchronize interrupt
SOFT_RESET	DS	1			; 1:means reset by software
						; 0:means reset by hardware

		RSEG	IDATA(0)

SEND_TIMES	DS	1			; for counte sending frames in a token period
ST_ADDR 	DS	1			; Hold station address
RETRY_T 	DS	1			; Increaced one per 1/16 second for token passing
RETRY_D 	DS	1			; Increaced one per 1/16 second for data tranmite
NEXT_ADDR	DS	1			; Next station addr. for TOKEN passing
						; [ssss0000]
STATION_ADDR	DS	1			; Local station address

INS_BI		DS	2			; Pointer for insert a byte to input buffer
REM_BI		DS	2			; Pointer for remove a byte form input buffer

INS_BO		DS	2			; Pointer for insert a byte to output buffer
REM_BO		DS	2			; Pointer for remove a byte from output buffer
SRC_POINT	DS	2			; save source pointer
DEST_POINT	DS	2			; save destination pointer
MODIFY_ADDR	DS	1			; station address to be insert/remove
TOKEN_GENERATOR DS	1			; for timer2 soft counter
						; inserted or removed
		RSEG	INDEX(0)		; I_RAM index addressing
ALL_STATION	DS	16			; all ststions address table in network
						; [E X X X A3 A2 A1 A0]
						; E:1 means active
						; A3A2A1A0: station address
						; X: not defined
						; last location is null
STK_BOTTOM	=	0E0H

		RSEG	ISTACK
Istack_begin:
		DS	100H-STK_BOTTOM

		RSEG	UDATA(0)
IN_BUF		DS	BI_LENGTH * BIF_NUM	; Buffer for input
OUT_BUF 	DS	BO_LENGTH * BOF_NUM	; Buffer for output
						; Use XRAM (256+1)*16*2=8224
						; about 8K-byte
FRAME_BUFFER	DS	BO_LENGTH		; for serial output a frame
;{
FRAME_TOKEN	DS	2			; for passing token
FRAME_REJECT	DS	2			; for acknowledge rejection
FRAME_RRDY	DS	2			; for acknowledge receive ready
FRAME_T_ACK	DS	2			; for token passing ACK
;}
COMMAND_BUF	DS	2			; for broadcasting command to network
IN_BUF_START	=	IN_BUF
OUT_BUF_START	=	OUT_BUF
;============================== Macro defination ============================
;Macro %INC_DPTR increace pointer in circul buffer
		MACRO	%INC_DPTR
		PUSH	ACC
		INC	DPTR
		MOV	A,DPH
		XRL	A,#HIGH(\0+\1_LENGTH*\1F_NUM)
		JNZ	NO_EQU\@
		MOV	A,DPL
		XRL	A,#LOW(\0+\1_LENGTH*\1F_NUM)
		JNZ	NO_EQU\.
		MOV	DPTR,#\0		; Modify pointer to start addr.
NO_EQU\<:	POP	ACC
		ENDMAC

;============================== Macro defination ============================

;Macro %DELAY	Delay
		MACRO	%DELAY

		NOP
		NOP
		NOP
		NOP

		NOP
		NOP
		NOP
		NOP
		NOP

		NOP
		NOP
		NOP

		ENDMAC

;============================== Macro defination ============================

;Macro %ENABLE_RXD  enable bit bus input
		MACRO	%ENABLE_RXD
		CLR	EN_I
		ENDMAC

;============================== Macro defination ============================
;Macro %ENABLE_TXD  enable bit bus output
		MACRO	%ENABLE_TXD
		SETB	EN_O
		ENDMAC

;============================== Macro defination ============================
;Macro %DISABLE_RXD  disable bit bus input
		MACRO	%DISABLE_RXD
		SETB	EN_I
		ENDMAC

;============================== Macro defination ============================
;Macro %DISABLE_TXD  disable bit bus output
		MACRO	%DISABLE_TXD
		CLR	EN_O
		ENDMAC

;============================== Macro defination ============================
;Macro %TXD sending data to serial port
		MACRO	%TXD
		MOV	SBUF,A
		JNB	TI,$			; wait for sending
;;		CJNE	A,SBUF,NET_L_ERR\@
		CLR	NET_LINE_ERR
		SJMP	NET_L_ERR1\.
NET_L_ERR\.:	SETB	NET_LINE_ERR
NET_L_ERR1\<:	CLR	TI
		CLR	TB8
		ENDMAC

;============================== Macro defination ============================
;Macro %RXD receiving data to serial port
		MACRO	%RXD
		MOV	R1,#0
WAIT_RXD\@:	CLR	R_OUT_TIME
		JBC	RI,RXD_OK\.
		DJNZ	R1,WAIT_RXD\.
		SETB	R_OUT_TIME
RXD_OK\<:	MOV	A,SBUF
		ENDMAC

;============================================================================

PUBLIC		START,TIMER_2,SERIAL,INC_P2_R0
PUBLIC		NET_INIT,FRAME_READ,FRAME_WRITE,GET_ADDR,NET_OUT_TEXT,USER_SYN
PUBLIC		ST_INSERT,ST_REMOVE,ST_RESET,GET_STATUS,ALL_STATION,SYS_SYNCHRO
PUBLIC          SRC_POINT,DEST_POINT,SOFT_RESET,COMM
PUBLIC		ADD16,SUB16,ADD16C,SUB16C,COMP16,CPL16
EXTERN		MAIN,STATION_ADDRESS

		RSEG	CODE(0)
 ;====================主机通讯子程序=======================================================
 ;R0- 主机接收的数据地址
 ;R1- 主机发送的数据地址
 ;R2- 从机地址
 ;R3- 主机发送的命令
 ;R4- 主机发送的数据长度

 COMM:          MOV SCON,#0D8H
 MSIO1:         MOV A,R2
                MOV SBUF,A
                JNB TI,$
                CLR TI
                CLR P1.0
                JNB RI,$
                CLR RI
                MOV A,SBUF
                XRL A,R2
                JZ MSIO3

 MSIO2:         SETB TB8
                SJMP MSIO1

 MSIO3:         CLR TB8
                SETB P1.0
                MOV SBUF,R3
                JNB TI,$
                CLR TI
                CLR P1.0
                JNB RI,$
                CLR RI
                MOV A,SBUF
                JNB ACC.7,MSIO4
                SJMP MSIO2

 MSIO4:         CJNE R3,#00H,MSIO5
                JNB ACC.0,MSIO2

 STX:           SETB P1.0
                MOV SBUF,@R1
                JNB TI,$
                CLR TI
                INC R1
                DJNZ R4,STX
                CLR C
                RET

 MSIO5:         JNB ACC.1,COMM_END

 SRX:           CLR P1.0
                JNB RI,$
                CLR RI
                MOV A,SBUF
                MOV @R0,A
                INC R0
                DJNZ R4,SRX
                CLR C
                RET

COMM_END:       SETB C
                RET


;============================== Initialize serial port ======================

SERIAL_INIT:	MOV	SCON,#10110000B 	; MODE=2 SM2=1 REN=1 RI and TI=0
		ORL	PCON,#10000000B 	; SMOD=1 baud rate =376K
		SETB	ES			; Enable serial port interrupt
		SETB	PS			; Serial interrupt in High level
		%ENABLE_RXD			; Enable bit bus input
		%DISABLE_TXD			; Disable bit bus output
		RET

;============================== Subrouting defination ============================

SERIAL_READ1:	MOV	A,SBUF			; Get address
		MOV	R4,#NO_DATA
		CLR	RI			; for next char.
		MOV	R5,#SUM_CODE
		MOV	R6,A			; R6 <= (FOF) for output
		CLR	SM2			; Clear multi_proccessor mode
		XRL	A,R5
		MOV	R5,A			; Calculate the check sum

		%RXD				; Get control byte
		JB	RB8,SERR_RD1_RET	; Error ret
		MOV	R4,A			; Save control byte
		XRL	A,#TOKEN_RRDY
		JNZ	SERR_RD1_RET		; not token RRDY frame
		MOV	A,R4			; restore control byte
		XRL	A,R5
		MOV	R5,A			; Calculate the check sum
		%RXD				; Get check sum
		JB	RB8,SERR_RD1_RET	; Error ret
		XRL	A,R5
		CLR	C

SERIAL_RD1_RET: SETB	SM2			; Set multi_proccessor mode
		RET
SERR_RD1_RET	SETB	C
		JMP	SERIAL_RD1_RET

;============================== Subrouting defination ============================

;SERIAL_READ:	Read data from serial port
;		and write into buffer DPTR pointed in XRAM if a data frame is
;		received and calculate the check sum
;		if a information frame is received return control byte only
;Input: 	DPTR=buffer pointer
;Output:	C=0 station address match, C=1 station address is not match
;		ACC=0 check sum code right , ACC<>0 check sum code error
;		R4= received control byte
;		R6= [dst_addr.src_addr]
;		DPTR= next buffer location
;Modify 	increace pointer INS_BI if a data frame
;USE REG.:	R7,R6,R5,R4,R3,R2,R0,DPTR
;In data frame:
;Data receiving from serial port format as follow :
;[dest.src] [control] [length(0 to 253)] [data1] [data2] ... [data253] [ check sum]
;Data in serial receiving buffer format as follow (if a data frmae):
;[dest.src] [length(0 to 253)] [data1] [data2] ... [data253]

;In information frame :
;Data receiving from serial port format as follow :
;[dest.src] [control] [ check sum]
;When return
;R6=[dest.src] ,R4=[control]

SERIAL_READ:	MOV	A,SBUF			; Get address
		CLR	RI			; for next char.
		MOV	R4,A			; Save address (FOF)
		XRL	A,#GLOB_ADDR		; global address
		ANL	A,#DEST 		; Get destination address
		CLR	C
		JZ	ADDR_MATCH		; jump if match
		MOV	A,R4
		XRL	A,ST_ADDR		; compare with my address
		ANL	A,#DEST
		JZ	ADDR_MATCH
		SETB	C
		JNB	NET_GOOD,ADDR_MATCH
		JMP	ERR_R_RET

ADDR_MATCH:	MOV	R2,#0			; Clear counted length
		MOV	R7,#0			; Clear receive length
		MOV	R5,#SUM_CODE
		MOV	A,R4
		MOV	R6,A			; R6 <= (FOF) for output
		CLR	SM2			; Clear multi_proccessor mode

		MOV	DPL,INS_BI+0
		MOV	DPH,INS_BI+1
		XRL	A,R5
		MOV	R5,A			; Calculate the check sum
		INC	R2			; Increace receive data length

		%RXD				; Get control byte
		JB	RB8,ERR_R_RET		; Error ret
		MOV	R4,A			; Save control byte
		INC	R2			; Increace receive data length
		XRL	A,#DATA
		JNZ	CTRL_FRAM_R		; control frame jump
		JNB	NET_GOOD,ERR_R_RET
		JB	IN_BUF_FULL,NOT_SAVE1	; if input buffer is full jump
		MOV	A,R4			; restore control byte
		XRL	A,R5
		MOV	R5,A			; Calculate the check sum
		MOV	A,R6			; restore (FOF)
		MOVX	@DPTR,A 		; Write address (FOF) to buffer
		%INC_DPTR IN_BUF_START,BI	; Increace buffer pointer
						; Ignore the control byte
						; in buffer for data frame
NOT_SAVE1:	%RXD				; Get length byte
		JB	RB8,ERR_R_RET		; Error ret
		INC	R2			; Increace receive data length
		MOV	R7,A			; Save data length
		INC	R7			; for check sum byte
		MOV	R3,A			; Save data length
		INC	R3			; for check sum byte

WAIT_DATA:	JB	IN_BUF_FULL,NOT_SAVE2	; if input buffer is full jump
		MOVX	@DPTR,A 		; Write to buffer
		%INC_DPTR IN_BUF_START,BI	; Increace buffer pointer
		XRL	A,R5
		MOV	R5,A			; Calculate the check sum
NOT_SAVE2:	INC	R2			; increace data length counter
		%RXD				; Get data
		JB	RB8,ERR_R_RET		; Error ret
		DJNZ	R3,WAIT_DATA		; Waitting for next data
						; ACC=check sum for last char.
SERIAL_R_RET:	XRL	A,R5
		MOV	R5,A			; Calculate the check sum

		MOV	A,R7			; ACC <= data length received
		INC	A			; increace FOF byte
		INC	A			; increace dat control byte
		INC	A			; increace dat length byte
						; increace dat check sum
		XRL	A,R2			; data length received and
						; counted match if zero in ACC
		ORL	A,R5			; check sum right if R5=0
		JNB	IN_BUF_FULL,ERR_R_RET
		CLR	C
		CLR	A

ERR_R_RET:	SETB	SM2			; Set multi_proccessor mode
		JC	SERIAL_RD_RET		; address not match return
		JNZ	SERIAL_RD_RET		; check code bad return
		MOV	INS_BI+0,DPL
		MOV	INS_BI+1,DPH		; INS_BI pointe to next location
SERIAL_RD_RET:	RET

CTRL_FRAM_R:	MOV	A,R4			; restore control byte
		XRL	A,R5
		MOV	R5,A			; Calculate the check sum
		%RXD				; Get check sum
		JB	RB8,ERR_R_RET		; Error ret
		INC	R2			; Increace receive data length
		SJMP	SERIAL_R_RET

;============================== Subrouting defination ============================

;SERIAL_WRITE:	Get data from buffer DPTR pointed in XRAM
;		and write into serial port
;		and calculate the check sum sending by next to last data
;Input: 	DPTR=buffer pointer
;Output:	C=0 successful, C=1 failure
;		DPTR= pointer to next buffer location
;USE REG.:	R7,R6,R5,R4,DPTR
;Data in serial transmitting buffer format as follow (if a data frmae):
;[dest.src] [control] [length(1 to 253)] [data1] [data2] ... [data253]
;Data transmitting to serial port format as follow :
;[dest.src] [control] [length(1 to 253)] [data1] [data2] ... [data253] [ check sum]
;[dest.src] [control] [ check sum] (if a information frame)


SERIAL_WRITE:	CLR	C
		CLR	ES			; Disable serial interrupt
		%ENABLE_TXD
		MOV	R5,#SUM_CODE
		MOVX	A,@DPTR 		; ACC <= address (FOF)
		SETB	TB8			; Send FOF
		%TXD				; Send first char. (FOF)
		XRL	A,R5
		MOV	R5,A			; Calculate check sum
		INC	DPTR			; Increace buffer pointer
		MOVX	A,@DPTR 		; ACC <= control byte
		%DELAY				; delay for address decode
		%TXD				; Send control byte
		CJNE	A,#DATA,CTRL_FRAM_W	; control frame jump
		XRL	A,R5
		MOV	R5,A			; Calculate check sum
		INC	DPTR			; Increace buffer pointer
		MOVX	A,@DPTR 		; ACC <= length

		MOV	R3,A			; save data length
		INC	R3			; increace data length byte

WAIT_WRITE:	%TXD				; wait for sending
		XRL	A,R5
		MOV	R5,A			; Calculate check sum
		INC	DPTR			; Increace buffer pointer
		MOVX	A,@DPTR
		DJNZ	R3,WAIT_WRITE
		MOV	A,R5			; ACC <= check sum
SERIAL_W_RET:	%TXD				; send check sum
		SETB	C			; send OK
ERR_W_RET:	CPL	C
		%DISABLE_TXD
		SETB	ES			; enable serial interrupt
		CLR	RI
		RET

CTRL_FRAM_W:	XRL	A,R5
		MOV	R5,A			; Calculate check sum
		SJMP	SERIAL_W_RET

;============================== Subrouting defination =====================

;TEST_BIT_BUS:	Get station addr.
;Input: 	none
;Output:	C=0 if ok , C=1 if bad

TEST_BIT_BUS:	MOV	A,#TEST_BB_OUT
		MOV	SBUF,A
		CLR	SM2
		%RXD
		CLR	TI
		SETB	C
		XRL	A,SBUF
		JNZ	TEST_BB_RET
		CLR	C
TEST_BB_RET:	SETB	SM2
		RET

;============================== Subrouting defination =====================

;GET_ADDR: Get station addr.
;Input:    none
;Output:   ACC=station addr.

GET_ADDR:	PUSH	DPL
		PUSH	DPH
		MOV	DPTR,#STATION_ADDRESS	; Get station address
		CLR	A
		MOVC	A,@A+DPTR
		POP	DPH
		POP	DPL
		RET

;============================== Subrouting defination =====================

;INC_P2_R0:	P2.R0 <= P2.R0 + 1
;Input: 	P2.R0
;Output:	P2.R0

INC_P2_R0:	PUSH	ACC
		MOV	A,R0
		ADD	A,#1
		MOV	R0,A
		JNC	INC_P2_RET
		INC	P2
INC_P2_RET:	POP	ACC
		RET

;============================== Subrouting defination =====================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -