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

📄 rfdriver.asm

📁 FM1702SL射频模块底层驱动源程序
💻 ASM
📖 第 1 页 / 共 3 页
字号:
		movwf	comm_set
		call	command_send

		bsf	status,rp0
		movlw	SecondaryStatus_Reg		;读取SecondaryStatus寄存器
		movwf	SpiAddress
		movlw	0x01
		movwf	Loop_Count
		movlw	TxRx_Buf
		movwf	fsr
		call	SPIRead
		btfsc	status,c
		return

		bsf	status,rp0
		movf	TxRx_Buf,w			;判断LoadKey执行是否正确
		andlw	0x40
		btfsc	status,z
		goto	Load_Key_Err

		movlw	Command_Reg			;向command寄存器写控制命令码
		movwf	SpiAddress
		movlw	0x01
		movwf	Loop_Count
		movlw	TxRx_Buf
		movwf	fsr
		movlw	0x00
		movwf	indf
		call	SPIWrite
		bcf	status,c
		return
Load_Key_Err:
		movlw	Command_Reg			;向command寄存器写控制命令码
		movwf	SpiAddress
		movlw	0x01
		movwf	Loop_Count
		movlw	TxRx_Buf
		movwf	fsr
		movlw	0x00
		movwf	indf
		call	SPIWrite
		bsf	status,c
		return
;***********************************************************************
;FM1702SL将EE中的密码调入密码缓冲区中
;Load_Key_EE	发送Load_Key_EE信号
;	入口:
;		Send_Buf	待装载密码的EE地址(2bytes),EE地址低字节在前
;	出口:
;		c = 1:		密码装载执行失败
;		c = 0:		密码装载执行成功
;影响的寄存器:
;		w,status,fsr,Temp5,Temp4,Temp3,Temp2,Loop_Count,SpiAddress
;		Send_Length,comm_set,Src,Dst,count
;***********************************************************************
Load_Key_EE:
		bsf	status,rp0
		movlw	0x02				;将待发送数据写入FIFO中
		movwf	Send_Length
		movlw	LoadKeyE2
		movwf	comm_set
		call	command_send
		btfsc	status,c
		return

		bsf	status,rp0
		movlw	ErrorFlag_Reg			;读取ErrorFlag寄存器
		movwf	SpiAddress
		movlw	0x01
		movwf	Loop_Count
		movlw	TxRx_Buf
		movwf	fsr
		call	SPIRead
		btfsc	status,c
		return

		bsf	status,rp0
		movf	TxRx_Buf,w			;判断LoadKey执行是否正确
		andlw	0x40
		btfss	status,z
		goto	Load_Key_EE_err

		bcf	status,rp0
		bcf	status,c
		return
Load_Key_EE_Err:
		bcf	status,rp0
		bsf	status,c
		return
;***********************************************************************
;FM1702SL向存在于其发射范围内的卡片发射认证信号
;Authentication	发送Authentication信号
;	入口:
;		Block_num:	块号
;	出口:
;		c = 1:		三重认证执行失败
;		c = 0:		三重认证执行成功
;影响的寄存器:
;		w,status,fsr,Temp5,Temp4,Temp3,Temp2,Loop_Count,SpiAddress
;		Send_Length,comm_set
;***********************************************************************
Authentication:
		;写ChannelRedundancy寄存器
		bsf	status,rp0
		movlw	ChannelRedundancy_Reg
		movwf	SpiAddress
		movlw	0x01
		movwf	Loop_Count
		movlw	TxRx_Buf
		movwf	fsr
		movlw	0x0f
		movwf	indf
		call	SPIWrite
		btfsc	status,c
		return
;		;改写control寄存器
;		movlw	Control_Reg
;		movwf	SpiAddress
;		movlw	0x01
;		movwf	Loop_Count
;		movlw	TxRx_Buf
;		movwf	fsr
;		call	SPIRead
;		btfsc	status,c
;		return
;
;		movlw	TxRx_Buf
;		movwf	fsr
;		movf	indf,w
;		andlw	0xf7
;		movwf	indf
;		movlw	Control_Reg
;		movwf	SpiAddress
;		movlw	0x01
;		movwf	Loop_Count
;		call	SPIWrite
;		btfsc	status,c
;		return
		;将待发送数据写入FIFO中,并执行Authent1命令
		bsf	status,rp0
		bsf	status,rp1
		movlw	0x60
		movwf	Send_Buf
		bcf	status,rp1
		movf	Block_num,w
		bsf	status,rp1
		movwf	Send_Buf+1
		bcf	status,rp1
		bcf	status,rp0
		movlw	Send_Buf+2
		movwf	temp1
		movlw	Card_UID
		movwf	temp0
		movlw	.4
		movwf	temp2
		bcf	source_irp
		bsf	destination_irp
		movlw	high copydata
		movwf	pclath
		call	copydata
		movlw	high Authentication
		movwf	pclath

		bsf	status,rp0
		movlw	0x06
		movwf	Send_Length
		movlw	Authent1
		movwf	comm_set
		call	command_send
		btfsc	status,c
		return
		;读取ErrorFlag寄存器
		bsf	status,rp0
		movlw	ErrorFlag_Reg
		movwf	SpiAddress
		movlw	0x01
		movwf	Loop_Count
		movlw	TxRx_Buf
		movwf	fsr
		call	SPIRead
		btfsc	status,c
		return
		;判断Authent1执行是否正确
		bsf	status,rp0
		movf	TxRx_Buf,w
		andlw	0x0e
		btfss	status,z
		goto	Authentication_Err
		;执行Authent2命令
		clrf	Send_Length
		movlw	Authent2
		movwf	comm_set
		call	command_send
		btfsc	status,c
		return
		;读取ErrorFlag寄存器
		bsf	status,rp0
		movlw	ErrorFlag_Reg
		movwf	SpiAddress
		movlw	0x01
		movwf	Loop_Count
		movlw	TxRx_Buf
		movwf	fsr
		call	SPIRead
		btfsc	status,c
		return
		;判断Authent2执行是否正确
		bsf	status,rp0
		movf	TxRx_Buf,w
		andlw	0x0e
		btfss	status,z
		goto	Authentication_Err
		;读取Control寄存器
		bsf	status,rp0
		movlw	Control_Reg
		movwf	SpiAddress
		movlw	0x01
		movwf	Loop_Count
		movlw	TxRx_Buf
		movwf	fsr
		call	SPIRead
		btfsc	status,c
		return
		;判断Authent2认证通过否
		bsf	status,rp0
		movf	TxRx_Buf,w
		andlw	0x08
		btfsc	status,z
		goto	Authentication_Err

		bcf	status,rp0
		bcf	status,c
		return
Authentication_Err:
		bcf	status,rp0
		bsf	status,c
		return
;***********************************************************************
;FM1702SL向存在于其发射范围内的卡片发射读块信号
;Read_Block	发送Read_Block信号
;	入口:
;		Block_num:	块号
;	出口:
;		Send_Buf	读出数据置于Send_Buf中
;		c = 1:		读块操作执行失败
;		c = 0:		读块操作执行成功
;影响的寄存器:
;		w,status,fsr,Temp5,Temp4,Temp3,Temp2,Loop_Count,SpiAddress
;		Send_Length,comm_set
;***********************************************************************
Read_Block:
		;写ChannelRedundancy寄存器
		bsf	status,rp0
		movlw	ChannelRedundancy_Reg
		movwf	SpiAddress
		movlw	0x01
		movwf	Loop_Count
		movlw	TxRx_Buf
		movwf	fsr
		movlw	0x0f
		movwf	indf
		call	SPIWrite
		btfsc	status,c
		return
		;为不影响Send_Buf缓冲区,做如下处理
		;movf	Send_Buf,w
		;movwf	Send_Buf+10h
		;movf	Send_Buf+1,w
		;movwf	Send_Buf+11h
		;将待发送数据写入FIFO中,并执行发送接收命令
		bsf	status,rp0
		bsf	status,rp1
		movlw	0x30
		movwf	Send_Buf
		bcf	status,rp1
		movf	Block_num,w
		bsf	status,rp1
		movwf	Send_Buf+1
		bcf	status,rp1
		movlw	0x02
		movwf	Send_Length
		movlw	Transceive
		movwf	comm_set
		call	command_send
		btfsc	status,c
		return
		;将Send_Buf缓冲区复原
		;movf	Send_Buf+10h,w
		;movwf	Send_Buf
		;movf	Send_Buf+11h,w
		;movwf	Send_Buf+1
		;读取ErrorFlag寄存器
		bsf	status,rp0
		movlw	ErrorFlag_Reg
		movwf	SpiAddress
		movlw	0x01
		movwf	Loop_Count
		movlw	TxRx_Buf
		movwf	fsr
		call	SPIRead
		btfsc	status,c
		return
		;判断ReadBlock执行是否正确
		bsf	status,rp0
		movf	TxRx_Buf,w
		andlw	0x0e
		btfss	status,z
		goto	Read_Block_Err
		;读出FIFO寄存器的内容
		call 	Read_FIFO
		btfsc	status,c
		return
		;判断回读的应答数据位数为16否
		bsf	status,rp0
		movf	ReadLength,w
		sublw	0x10
		btfss	status,z
		goto	Read_Block_Err

		bcf	status,rp0
		bcf	status,c
		return
Read_Block_Err:
		bcf	status,rp0
		bsf	status,c
		return
;***********************************************************************
;FM1702SL向存在于其发射范围内的卡片发射写块信号
;Write_Block	发送Write_Block信号
;	入口:
;		Send_Buf	待写入卡片Block中的16bytes数据
;		Block_num:	块号
;	出口:
;		c = 1:		写块操作执行失败
;		c = 0:		写块操作执行成功
;影响的寄存器:
;		w,status,fsr,Temp5,Temp4,Temp3,Temp2,Loop_Count,SpiAddress
;		Send_Length,comm_set
;***********************************************************************
Write_Block:
		bsf	status,rp0
		movlw	ChannelRedundancy_Reg		;写ChannelRedundancy寄存器
		movwf	SpiAddress
		movlw	0x01
		movwf	Loop_Count
		movlw	TxRx_Buf
		movwf	fsr
		movlw	0x07
		movwf	indf
		call	SPIWrite
		btfsc	status,c
		return

		bsf	status,rp0
		bsf	status,rp1
		movf	Send_Buf,w
		movwf	Send_Buf+10h
		movf	Send_Buf+1,w
		movwf	Send_Buf+11h

		movlw	0xa0				;将待发送数据写入FIFO中,并执行发送接收命令
		movwf	Send_Buf
		bcf	status,rp1
		movf	Block_num,w
		bsf	status,rp1
		movwf	Send_Buf+1
		bcf	status,rp1
		movlw	0x02
		movwf	Send_Length
		movlw	Transceive
		movwf	comm_set
		call	command_send
		btfsc	status,c
		return

		call 	Read_FIFO			;读出FIFO寄存器的内容
		btfsc	status,c
		return

		bsf	status,rp0
		movf	ReadLength,w			;判断回读的应答数据长度为1否
		sublw	0x01
		btfss	status,z
		goto	Write_Block_Err

		bsf	status,rp1
		movf	Send_Buf,w			;判断回应的数据是否为0a
		bcf	status,rp1
		sublw	0x0a
		btfss	status,z
		goto	Write_Block_Err

		bsf	status,rp1
		movf	Send_Buf+10h,w
		movwf	Send_Buf
		movf	Send_Buf+11h,w
		movwf	Send_Buf+1
		bcf	status,rp1

		movlw	0x10				;将待发送数据写入FIFO中,并执行发送接收命令
		movwf	Send_Length
		movlw	Transceive
		movwf	comm_set
		call	command_send
		btfsc	status,c
		return

		call 	Read_FIFO			;读出FIFO寄存器的内容
		btfsc	status,c
		return

		bsf	status,rp0
		movf	ReadLength,w			;判断回读的应答数据长度为1否
		sublw	0x01
		btfss	status,z
		goto	Write_Block_Err

		bsf	status,rp1
		movf	Send_Buf,w			;判断回应的数据是否为0a
		bcf	status,rp1
		sublw	0x0a
		btfss	status,z
		goto	Write_Block_Err

		bcf	status,rp0
		bcf	status,c
		return
Write_Block_Err:
		bcf	status,rp0
		bsf	status,c
		return
		
;====================================================================
;射频卡操作
;入口参数:N/A
;出口参数:c=1	射频卡操作失败
;	   c=0  射频卡操作正常
;====================================================================
RF_Operation:
		;对FM1702SL进行复位操作
		bcf	FM1702SL_RST
		
		movlw	0xff
		movwf	temp1
		movlw	0x10
		movwf	temp2
Rst_Loop:
		nop
		nop
		nop
		nop
		decfsz 	temp1,f
		goto	Rst_Loop
		
		movlw	0xff
		movwf	temp1
		decfsz	temp2,f
		goto	Rst_Loop
		;初始化SPI接口及1702SL
		call	SPI_Init
		call	FM1702SL_Init		
;		bsf	status,rp0
;		bsf	status,rp1
;		movlw	0x80
;		movwf	Send_Buf
;		clrf	Send_Buf+.1
;		movlw	0xff
;		movwf	Send_Buf+.2
;		movwf	Send_Buf+.3
;		movwf	Send_Buf+.4
;		movwf	Send_Buf+.5
;		movwf	Send_Buf+.6
;		movwf	Send_Buf+.7
;		bcf	status,rp1
;		bcf	status,rp0
;		call	Load_Key	
		;进行Request操作
		call	Request		
		;进行GET_UID操作
		call	Get_UID
		;进行Select_Tag操作
		call	Select_Tag					
		;进行Load_Key_EE操作,验证密码在FM1702SL EE中的0x0080地址起
		bsf	status,rp0
		bsf	status,rp1
		movlw	0x80
		movwf	Send_Buf
		clrf	Send_Buf + .1
		bcf	status,rp1
		call	Load_Key_EE
		;进行Authentication操作
		bsf	status,rp0
		movlw	0x07
		movwf	Block_num
		call	Authentication
		;进行Write_Block操作
		bsf	status,rp0
		bsf	status,rp1
		movlw	0x11
		movwf	Send_Buf
		movlw	0x22
		movwf	Send_Buf + .1
		movlw	0x33
		movwf	Send_Buf + .2
		movlw	0x44
		movwf	Send_Buf + .3
		movlw	0x55
		movwf	Send_Buf + .4
		movlw	0x66
		movwf	Send_Buf + .5
		movlw	0x77
		movwf	Send_Buf + .6
		movlw	0x88
		movwf	Send_Buf + .7
		movlw	0x99
		movwf	Send_Buf + .8
		movlw	0xaa
		movwf	Send_Buf + .9
		movlw	0xbb
		movwf	Send_Buf + .10
		movlw	0xcc
		movwf	Send_Buf + .11
		movlw	0xdd
		movwf	Send_Buf + .12
		movlw	0xee
		movwf	Send_Buf + .13
		movlw	0xff
		movwf	Send_Buf + .14
		movlw	0x16
		movwf	Send_Buf + .15
		bcf	status,rp1
		bcf	status,rp0
		
		bsf	status,rp0
		movlw	0x04
		movwf	Block_num
		call	Write_Block
		;进行Read_Block操作
		bsf	status,rp0
		movlw	0x04
		movwf	Block_num
		call	Read_Block
		
		bsf	FM1702SL_RST
		return

		End

⌨️ 快捷键说明

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