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

📄 spntestg.asm

📁 单片机快速入门原程序
💻 ASM
📖 第 1 页 / 共 2 页
字号:
	CLR	RI		;2T
	mov	a,RecWrIdx	;2T
	add	a,#RecBuff	;2T
	mov	r0,a		;1T
	MOV	@R0,SBUF      	;2T 存入接收缓冲区
	inc	RecWrIdx	;2T
	mov	a,RecWrIdx	;2T
	cjne	a,#REC_BUFF_LEN,RECIEV5;4T
	clr	a		;1T
	mov	RecWrIdx,a	;2T
RECIEV5:RET			;5T
;======================================================
	;判断是否是起始位及长度处理子程序
;======================================================
PStart:	mov	a,RecRdIdx	;2T
	add	a,#RecBuff	;2T
	mov	r0,a		;1T
	MOV	A,@R0		;2T
;	CLR	RI	
;	mov	A,SBUF
;	MOV	R2,A
;	jnb	ri,PStartA
;	LCALL	RECIEV	
	CLR	C		;1T
	SUBB	A,#0AAH		;2T 起始位判断
	JNZ	PStart9		;3T
	LCALL	RecRP		;接收缓冲区读指针调整
;	LCALL	Delay140us
;	LCALL	Delay20us

;	jnb	ri,PStartA	;接收数据长度
PStart3:jnb	ri,PStart3
	LCALL	RECIEV
	mov	a,RecRdIdx	
	add	a,#RecBuff
	mov	r0,a
	MOV	A,@R0
;	CLR	RI
;	mov	A,SBUF
;	MOV	R3,A	
	ADD	A,#6
	MOV	Len,A		;数据的长度加地址和校验码长度加长度位和类型位
	SETB	Start
PStart9:LCALL	RecRP
;	LCALL	Delay140us
;	LCALL	Delay20us


;	jnb	ri,PStartA
;PStart5:jnb	ri,PStart5
;	LCALL	RECIEV		
;	mov	a,RecRdIdx	
;	add	a,#RecBuff
;	mov	r0,a
;	MOV	A,@R0
;	CLR	RI
;	mov	A,SBUF		;类型判断
;	MOV	R6,A
;	MOV	A,R6
;	LCALL	RecRP
;	JZ	PStartA
;	CLR	C
;	SUBB	A,#01
;	JNZ	PStart9
;	SETB	DownLoadOk
;	SJMP	PStartA;4T 接收缓冲区读指针调整
PStartA:RET	   		;5T
;======================================================
		;延时140US
;======================================================
;Delay140us:
;	LCALL	Delay30us
;	LCALL	Delay20us
;	LCALL	Delay20us
;	LCALL	Delay30us
;	LCALL	Delay20us
;	LCALL	Delay20us
;	RET
;======================================================
	;接收缓冲区读指针调整子程序
;======================================================
RecRP:	INC	RecRdIdx		;2T
	MOV	A,RecRdIdx
	cjne	a,#REC_BUFF_LEN,RecRP5	;4T if the RdIdx at end of RecBuff
	mov	RecRdIdx,#0		;3T
RecRP5:	mov	a,RecRdIdx
	add	a,#RecBuff
	mov	r0,a
	RET				;5T
;======================================================
		;接收数据判断处理
;======================================================
RecDataProc:
	
Proc1:	MOV	BCC_L,#0FFH
	MOV	BCC_H,#0FFH
	MOV	CRC_L,#0A0H	;生成多项式为0000 0001(省1) 1010 000,即P=X16+X15+X2+1
	MOV	CRC_H,#01H
	MOV	A,Len		
	PUSH	ACC		;保护Len
	LCALL	CRC_16		;CRC校验
	POP	ACC
	MOV	Len,A
	MOV	A,BCC_L
	JNZ	Proc5
	MOV	A,BCC_H
	JNZ	Proc5
	PUSH	Len		;判断类型
	CLR	C
	MOV	A,Len
	SUBB	A,#2
	MOV	Len,A
	PUSH	RecRdIdx
Proc3:	DEC	RecRdIdx
	DJNZ	Len,Proc3
	mov	a,RecRdIdx
	add	a,#RecBuff
	mov	r0,a
	POP	RecRdIdx
	mov	A,@R0
	JZ	Proc4
	POP	Len
	CLR	C
	SUBB	A,#01
	JNZ	Proc10
	SETB	DownLoadOk
	SJMP	Proc10
Proc4:	POP	Len
	PUSH	Len
	LCALL	ADDRP		;存地址
	POP	Len
	PUSH	Len
	LCALL	AddrJudge	;地址判断
	POP	Len
;	MOV	Len,#0
	JNB	WriteFlash,Proc8
	MOV	RecRdIdx,RecWrIdx
	LCALL	ExacP
	sjmp	Proc10
Proc5:	MOV	RecRdIdx,RecWrIdx
	LCALL	ERROP
	sjmp	Proc10
Proc8:	MOV	RecRdIdx,RecWrIdx
	LCALL	AddrErro	;无效地址
Proc10:	RET
;====================================================
		;地址冲突判断子程序
;====================================================
AddrJudge:
	PUSH	ADDR_L
	PUSH	ADDR_H
	MOV	R6,#10
	MOV	R7,#75H
	MOV	A,R7
	CJNE	A,ADDR_H,AddrJ
	SJMP	AddrJ6
AddrJ:	INC	R7
	MOV	A,R7
AddrJ3:	CJNE	A,ADDR_H,AddrJ5
	SJMP	AddrJF
AddrJ5:	DJNZ	R6,AddrJ
	SJMP	AddrJA
AddrJ6:	MOV	R2,#0FDH
	MOV	R3,#3
AddrJ62:MOV	A,ADDR_L
	CLR	C
	SUBB	A,R2
	JZ	AddrJF
	INC	R2
	DJNZ	R3,AddrJ62
	CLR	C
	MOV	A,Len
	SUBB	A,#5
	MOV	Len,A
AddrJ7:	INC	ADDR_L
	MOV	A,ADDR_L
	CLR	C	
	SUBB	A,#0FDH
	JNZ	AddrJ9
	SJMP	AddrJF	
;	INC	ADDR_H
;	MOV	A,#76H
;	CJNE	A,ADDR_H,AddrJ9
;	SJMP	AddrJF
AddrJ9:	DJNZ	Len,AddrJ7	
AddrJA:	SETB	WriteFlash
AddrJF:	POP	ADDR_H
	POP	ADDR_L
	RET
;====================================================
		;无效地址子程序
;====================================================
AddrErro:
	MOV	SendAL,#7
	MOV	A,SendWrIdx
	add	a,#SendBuff
	mov	r1,a
	MOV	A,#0AAH
	MOV	@R1,A      ;存入发送缓冲区
	LCALL	SendWrP		;写指针调整
	MOV	@R1,#00H	;发下一祯
	LCALL	SendWrP	
	MOV	@R1,#04H
	LCALL	SendWrP		
	MOV	@R1,#00H
	LCALL	SendWrP	
	MOV	@R1,#00H
	LCALL	SendWrP	
	MOV	@R1,#041H
	LCALL	SendWrP	
	MOV	@R1,#0E5H
	LCALL	SendWrP	
	RET
;====================================================
		;存地址子程序
;====================================================
ADDRP:	
	DEC	Len
	DEC	Len
	DEC	Len
;	MOV	ADDR_H0,ADDR_H
;	MOV	ADDR_L0,ADDR_L
ADDRP0:	mov	a,RecRdIdx
	JNZ	ADDRP1
	MOV	RecRdIdx,#REC_BUFF_LEN-1
	SJMP	ADDRP3
ADDRP1:	DEC	RecRdIdx
ADDRP3:	djnz	Len,ADDRP0
	mov	a,RecRdIdx
	add	a,#RecBuff
	mov	r0,a
	mov	ADDR_L,@R0
	LCALL	RecRP	
ADDRP5:	mov	a,RecRdIdx
	add	a,#RecBuff
	mov	r0,a
	mov	ADDR_H,@R0
	RET
;=====================================================
		;正确处理子程序
;=====================================================
ExacP:	MOV	SendAL,#7
	MOV	A,SendWrIdx
	add	a,#SendBuff
	mov	r1,a
	MOV	A,#0AAH
	MOV	@R1,A      ;存入发送缓冲区
	LCALL	SendWrP		;写指针调整
	MOV	@R1,#00H	;发下一祯
	LCALL	SendWrP	
	MOV	@R1,#02H
	LCALL	SendWrP		
	MOV	@R1,#00H
	LCALL	SendWrP	
	MOV	@R1,#00H
	LCALL	SendWrP	
	MOV	@R1,#0A1H
	LCALL	SendWrP	
	MOV	@R1,#0E4H
	LCALL	SendWrP	
	RET
;====================================================
;====================================================
		;出错处理程序
;====================================================
ERROP:	MOV	SendAL,#7
	MOV	A,SendWrIdx
	add	a,#SendBuff
	mov	r1,a
	MOV	A,#0AAH
	MOV	@R1,A      ;存入发送缓冲区
	LCALL	SendWrP		;写指针调整
;	DEC	SendAL
;	JZ	ERROP2
	MOV	@R1,#00H	;重发特定数据
	LCALL	SendWrP	
;	DEC	SendAL
;	JZ	ERROP2
	MOV	@R1,#03H
	LCALL	SendWrP
;	DEC	SendAL
;	JZ	ERROP2
	MOV	@R1,#00H
	LCALL	SendWrP
	MOV	@R1,#00H
	LCALL	SendWrP
	MOV	@R1,#0F0H
	LCALL	SendWrP
	MOV	@R1,#24H
	LCALL	SendWrP
ERROP2:	RET		
;=================================================
;=================================================
  		;发送缓冲区写指针调整子程序		
;=================================================
SendWrP:INC	SendWrIdx
	mov	a,SendWrIdx
	cjne	a,#SEND_BUFF_LEN,RETT
	mov	SendWrIdx,#0
RETT:	MOV	A,SendWrIdx
	ADD	A,#SendBuff
	mov	r1,a
	ret
;================================================
;================================================
               ;CRC子程序
;=================================================
CRC_16:	

CGMb0:	mov	a,RecRdIdx	
	add	a,#RecBuff
	mov	r0,a
	MOV	A,@R0		
	XRL	A,BCC_H
	MOV	BCC_H,A
	MOV	BYT,#08H	;8位数据
CGMb1:	MOV	A,BCC_L
	CLR	C
	RRC	A	
	MOV	BCC_L,A
	MOV	A,BCC_H
	RRC	A		
	MOV	BCC_H,A
	JNC	CGMb2
	CLR	C
	XRL	A,CRC_H
	MOV	BCC_H,A
	MOV	A,BCC_L
	XRL	A,CRC_L
	MOV	BCC_L,A
CGMb2:	DEC	BYT		
	MOV	A,BYT
	JNZ	CGMb1
	DEC	Len
	MOV	A,Len
	JZ	CGMb5
	INC	RecRdIdx
	MOV	A,RecRdIdx
	cjne	a,#REC_BUFF_LEN,CGMb0	;if the RdIdx at end of RecBuff	
	mov	RecRdIdx,#0
	LJMP	CGMb0	
CGMb5:	RET
;=======================================================
;===============================================
		;发送子程序
;===============================================
SEND:	;CLR	SendRdy
	;MOV	SendAL,#4
SEND0:;	JNB	TI,SEND3 
	JNB	TI,SEND0  
	CLR	TI
	mov	a,SendRdIdx	
	add	a,#SendBuff
	MOV	R1,A
	MOV	SBUF,@R1
	INC	SendRdIdx
	MOV	A,SendRdIdx
	CJNE	A,#SEND_BUFF_LEN,SEND1
	MOV	SendRdIdx,#0
SEND1:;	LCALL	Delay140us
;	LCALL	Delay20us
	DJNZ	SendAL,SEND0
SEND3:	MOV	SendRdIdx,SendWrIdx
	ret
;=========================================================	
;=========================================================
		;写FLASH
;=========================================================
WriteCode:
	MOV	A,#0
	CJNE	A,ADDR_H,WriteCD
	CJNE	A,ADDR_L,WriteCD
	MOV	FLSCL,#88H
	MOV	PSCTL,#01H
WriteC:	CLR	C
	MOV	A,Len
	SUBB	A,#4
	MOV	Len,A
	PUSH	Len
WriteC0:mov	a,RecRdIdx
	JNZ	WriteC1
	MOV	RecRdIdx,#REC_BUFF_LEN-1
	SJMP	WriteC3
WriteC1:DEC	RecRdIdx
WriteC3:djnz	Len,WriteC0
	POP	Len
	mov	a,RecRdIdx
	add	a,#RecBuff
	mov	r0,a
	mov	A,@R0
	MOV	R2,#2
	CJNE	A,#2,WriteC8
	MOV	R2,#3
WriteC8:MOV	DPTR,#75FDH
WriteC9:mov	A,@R0
	MOVX	@DPTR,A
	LCALL	RecRP
	INC	DPTR
	DJNZ	R2,WriteC9
;	MOV	PSCTL,#00H		
;WriteCA:MOV	FLSCL,#88H
;	MOV	PSCTL,#03H
;	MOV	DPTR,#0000H
;	MOVX	@DPTR,A
;	MOV	PSCTL,#01H
;	MOV	A,#02H
;	MOVX	@DPTR,A
;	INC	DPTR
;	MOV	A,#76H
;	MOVX	@DPTR,A
;	INC	DPTR
;	MOV	A,#00H
;	MOVX	@DPTR,A
	MOV	DPH,#00H
	MOV	DPL,#03H
	DEC	Len
	DEC	Len
	PUSH	Len
	CLR	C
	MOV	A,Len
	SUBB	A,#3
	MOV	Len,A
	JNZ	WriteCB
	MOV	RecRdIdx,RecWrIdx
	SJMP	WriteCC
WriteCB:LCALL	WRIT4
WriteCC:POP	Len
	MOV	PSCTL,#00H
	AJMP	WriteCF
WriteCD:MOV	FLSCL,#88H
	MOV	PSCTL,#01H
	LCALL	WRIT
	MOV	PSCTL,#00H
WriteCF:;MOV	Len0,Len		;纯数据长度				
	RET	
;===========================================================
		;具体的写FLASH子程序
;===========================================================
WRIT:	
	CLR	C
	MOV	A,Len
	SUBB	A,#4
	MOV	Len,A
	push	Len
WRIT0:	mov	a,RecRdIdx
	JNZ	WRIT1
	MOV	RecRdIdx,#REC_BUFF_LEN-1
	SJMP	WRIT3
WRIT1:	DEC	RecRdIdx
WRIT3:	djnz	Len,WRIT0
	pop	Len
	DEC	Len
	DEC	Len
	MOV	DPL,ADDR_L
	MOV	DPH,ADDR_H
	mov	a,RecRdIdx
	add	a,#RecBuff
	mov	r0,a
WRIT4:	push	Len
WRIT8:	mov	A,@R0
	MOVX	@DPTR,A
	LCALL	RecRP
	INC	DPTR
	DJNZ	Len,WRIT8
	pop	Len
	MOV	RecRdIdx,RecWrIdx	
	RET
;==========================================================
		;DPTR调整
;==========================================================
;DPTRAdjust:
	
;DPTRAd1:INC	ADDR_L0		;DPTR往后调整
;	MOV	A,ADDR_L0
;	JZ	DPTRAd3
;DPTRAd2:DJNZ	Len0,DPTRAd1
;	sjmp	DPTRAd5
;DPTRAd3:INC	ADDR_H0
;	SJMP	DPTRAd2		
;DPTRAd5:CLR	C
;	MOV	A,ADDR_H
;	SUBB	A,ADDR_H0
;	JNZ	DPTRAd9
;	CLR	C
;	MOV	A,ADDR_L
;	SUBB	A,ADDR_L0
;	JZ	DPTRAdF
;	MOV	R4,A
;DPTRAd8:INC	DPTR
;	DJNZ	R4,DPTRAd8
;	SJMP	DPTRAdF
;DPTRAd9:MOV	R5,A
;DPTRAdA:INC	DPTR
;	INC	ADDR_L0
;	MOV	A,ADDR_L0
;	JNZ	DPTRAdA
;	DJNZ	R5,DPTRAdA
;	MOV	R4,ADDR_L
;	SJMP	DPTRAd8	
	
;DPTRAdF:RET
;==========================================================
		;应用程序
;==========================================================
ORG	75FDH
AppProg:LJMP	0003H
ORG	0003H
AppPro1:cpl	p1.6
	MOV	R4,#50
AppPro2:LCALL	Delay20Ms       
	DJNZ	R4,AppPro2
	SJMP	AppPro1 
	
;==================================================

;CH0001:
;	ljmp	 CH0020

;----wait for 1S
;
;==================================
$if ProgProtect=1
	org	07DFEh
	db	0	;disable All Flash Erase/Write
	db	0	;disable All Flash Read
$endif
;==================================
end
  	 

⌨️ 快捷键说明

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