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

📄 speed.asm

📁 用接近开关作速度传感器的PIC程序speed.asm RS485输出
💻 ASM
📖 第 1 页 / 共 2 页
字号:
	movf indf,w
	andlw 0fh
	movwf ww
	bcf status,c
	rlf ww,w
	addlw 60h
	movwf fsr
	bcf status,irp
	movf indf,w
	movwf data1
	incf fsr
	movf indf,w
	movwf data2
	movlw 收发首地址+3
	movwf fsr
	bsf status,irp
	movf data2,w
	movwf indf
	incf fsr
	movf data1,w
	movwf indf
	


	movlw 收发首地址
	movwf data2
	movlw .5
	movwf data1
	call 计算crc值
	movlw 收发首地址+5
	movwf fsr
	movf crcl,w
	movwf indf
	incf fsr
	movf crch,w
	movwf indf
	movlw .7
	bsf m_stau,7
	movwf tx_num
	call 发送数据
	
	return





设定线圈并发送
	bsf m_stau,7
	movlw 收发首地址+4
	movwf fsr
	movf indf,w
	movwf ww
	decf fsr
	movf indf,w
	andlw .1
	addlw 5eh
	movwf fsr
	bcf status,irp
	movf ww,w
	movwf indf
	bsf status,irp
	movlw .8
	movwf tx_num
	call 发送数据	
	return
	
设定寄存器并发送
	movlw 收发首地址+5
	movwf fsr
	movf indf,w
	movwf data1	;数据低位
	decf fsr
	movf indf,w
	movwf data2	;设定数据高位
	movlw 收发首地址+3
	movwf fsr
	movf indf,w
	andlw .7
	movwf ww
	xorlw .2
	btfsc status,z
	return
	movf ww,w
	xorlw .6
	btfsc status,z
	goto 存储传感器地址
	bcf status,c
	rlf ww,w
	addlw 60h
	movwf fsr
	bsf status,irp
	movf data1,w
	movwf indf
	incf fsr
	movf data2,w
	movwf indf
	movlw .8
	movwf tx_num
	bsf m_stau,7
	call 发送数据	
	return
存储传感器地址

;	movf data2,w
;	movwf fsr06h
;	movf data1,w
;	movwf fsr06l
;	call wr_参数
	movlw .8
	movwf tx_num
	bsf m_stau,7
	call 发送数据	
	return


address0_ok
;地址正确,是广播地址
	call 校验程序	;120us
	btfsc m_stau,4
	return		;crc错误
;分析功能

	incf fsr01l	;指令累加
	btfsc status,z
	incf fsr01h

	movlw 收发首地址+1
	movwf fsr
	movf indf,w
	xorlw 05h
	btfsc status,z
	goto 设定线圈
	movf indf,w
	xorlw 06h
	btfsc status,z
	goto 设定寄存器
	return
设定线圈
	movlw 收发首地址+4
	movwf fsr
	movf indf,w
	movwf ww
	decf fsr
	movf indf,w
	andlw .1
	addlw 5eh
	movwf fsr
	bcf status,irp
	movf ww,w
	movwf indf
	return
设定寄存器
	movlw 收发首地址+5
	movwf fsr
	movf indf,w
	movwf data1	;数据低位
	decf fsr
	movf indf,w
	movwf data2	;设定数据高位
	movlw 收发首地址+3
	movwf fsr
	movf indf,w
	andlw .7
	movwf ww
	xorlw .2
	btfsc status,z
	return
	movf ww,w
	xorlw .6
	btfsc status,z
	goto 存储传感器地址1
	bcf status,c
	rlf ww,w
	addlw 60h
	movwf fsr
	bcf status,irp
	movf data2,w
	movwf indf
	incf fsr
	movf data1,w
	movwf indf
	bsf status,irp
	return
存储传感器地址1

      	movf data1,w
      	sublw .7
      	btfsc status,c
      	return
       	movf data1,w
      	sublw 0fh
      	btfss status,c
      	return 


	movf data2,w
	movwf fsr06h
	movf data1,w
	movwf fsr06l
	movlw 10h
	movwf data1
	movf fsr06l,w
	call write_byte
	return




;====================================================
校验程序
;16bit crc校验
;数据首地址=data2
;数据量=data1
;m_stau,4=0 crc	ok
;m_stau,4=1 crc	err

	movlw 收发首地址
	movwf data2
	movlw .6
	movwf data1
	call 计算crc值
	bcf m_stau,4
	movf indf,w
	xorwf crcl,w
	btfss status,z
	bsf m_stau,4
	incf fsr
	movf indf,w
	xorwf crch,w
	btfss status,z
	bsf m_stau,4
	return


;====================================================
计算crc值
;16bit crc校验
;数据首地址=data2
;数据量=data1
;结果=crch,crcl
;if data1=6 then time=578/5us=115us
;使用:data1,data2,fsr
	movlw 0ffh
	movwf crch
	movwf crcl
	movf data2,w
	movwf fsr
	bsf status,irp
crc_calc_loop
	movf indf,w
	xorwf crcl
	movlw .8
	movwf data2
rrf_crc
	bcf status,c
	rrf crch
	rrf crcl
	btfss status,c
	goto ok_poly
	movlw 0a0h
	xorwf crch
	movlw 01h
	xorwf crcl
ok_poly
	decfsz data2
	goto rrf_crc
	incf fsr
	decfsz data1
	goto crc_calc_loop
	return
;======================================


;======================================
系统初始化
	bsf status,rp0
	bcf status,rp1
	movlw 87h
	movwf adcon1	;no AD
	movlw 3FH	;0011 1111b A4输出,A0,1,2,3,5输入。
	movwf trisa
	movlw 0ffh
	movwf trisb
	movlw 90H	;10010000=090h ;
	movwf trisc

	clrf pie1
	clrf pie2

;PB口上拉
;PSA属于TMR0,4分频
;定义tmr0
	movlw 03fh	;
	movwf option_rg
	bcf status,rp0
	clrf intcon
	clrf tmr0		
	bsf intcon,t0ie
	clrf pir1
	clrf pir2
;定义tmr1
	clrf tmr1h
	clrf tmr1l
	movlw B'00000001'
	movwf t1con
	bsf intcon,peie
	bsf status,rp0
	bsf pie1,tmr1ie
	bcf status,rp0
;定义外部中断0
	bsf intcon,4;inte

	bcf portc,0
	return
;======================================



;======================================
通讯口初始化
	bsf status,rp0
	movlw .129;.103;.25	;波特率=9615
	movwf spbrg
	movlw 24h	;00100100b
	movwf txsta
;	bcf txsta,6	;tx8/9=0
;	bcf txsta,4	;sync=0	异步
;	bsf txsta,2	;brgh=1	高速
;	bsf txsta,5	;txen=1	可以发

	bcf status,rp0
	movlw 90h	;10010000b
	movwf rcsta
;	bsf rcsta,7	;sci方式
;	bcf rcsta,6	;八位接收
	bcf rcsta,4	;现在接收
	bsf rcsta,4	;现在接收
	movf rcreg,w	;清接收寄存器
	movf rcreg,w	;清接收寄存器
	movf rcreg,w	;清接收寄存器
	return
;======================================








;************************************
;双字节乘以双字节子程序
;入口:(calc1h,calc1l)*(data2h,data2l)
;use: data4
;出口:(x1000,x100,x10,x1)
;270/5=50us
dmul	clrf x1000
	clrf x100
	clrf x10
	clrf x1
	movlw .16
	movwf data4
	bcf status,c
loop17	rrf data2h
	rrf data2l
	btfss status,c
	goto loop161
	movf calc1l,w
	addwf x100,f
	btfsc status,c
	incf x1000
	movf calc1h,w
	addwf x1000,f
loop161
	rrf x1000,f
	rrf x100,f
	rrf x10,f
	rrf x1,f
	decfsz data4
	goto loop17
	return
;************************************





;**************************************
;	 除法子程序
; (calc2l,calc1h,calc1l)/oneb=(data1h,data2h,data2l)
; (x100,x10,x1)/oneb=(x100,x10,x1)=(data1h,data2h,data2l)
;**************************************
divsub3	clrf data2h	;结果置零
	clrf data2l
	clrf datal
	movlw .24
	movwf data1
	clrf data2
divlop3
	bcf status,c
	rlf x1
	rlf x10
	rlf x100
	rlf data2
	btfsc status,c
	goto div33

	movf oneb,w
	subwf data2,w
	btfsc status,c
	goto div13
	bcf status,c
	goto div23
div33
	movf oneb,w
	subwf data2,w
div13	movwf data2
	bsf status,c
div23
       rlf data2l
      rlf data2h
      rlf data1h
      decfsz data1,f
      goto divlop3
      movf data2h,w
      movwf x10
      movf data2l,w
      movwf x1
      movf data1h,w
      movwf x100
      return


;************************************


;**************************************
;	 四字节/四字节 除法子程序
;(calc2h,calc2l,calc1h,calc1l)/(x1000,x100,x10,x1)=(calc2h,calc2l,calc1h,calc1l)
;
; 使用:dirh,dirl,crch,crcl 
;**************************************
divsub4_4
	CLRF dirh	;结果置零
	CLRF dirl
	clrf crch
	clrf crcl
	
	movlw .32
	movwf data1
	clrf ox1000
	clrf ox100
	clrf ox10
	clrf ox1

divlopp3
	bcf status,c
	rlf calc1l
	rlf calc1h
	rlf calc2l
	rlf calc2h
	rlf ox1
	rlf ox10
	rlf ox100
	rlf ox1000	
	btfsc status,c
	goto div13k
;比较ox与x的大小	
	movf x1000,w
	subwf ox1000,w
	btfsc status,z
	goto cm110
	btfsc status,c
	goto div13k
	bcf status,c
	goto div23k
cm110
	movf x100,w
	subwf ox100,w
	btfsc status,z
	goto cm111
	btfsc status,c
	goto div13k
	bcf status,c
	goto div23k		
cm111
	movf x10,w
	subwf ox10,w
	btfsc status,z
	goto cm112
	btfsc status,c
	goto div13k
	bcf status,c
	goto div23k
cm112
	movf x1,w
	subwf ox1,w
	btfsc status,c
	goto div13k
	bcf status,c
	goto div23k	
		
div13k
;ox-x=ox
	comf x1,w
	addwf ox1
	btfss status,c
	goto add441
	incf ox10
	btfss status,z
	goto add441
	incf ox100
	btfss status,z
	goto add441		
	incf ox1000
add441	
	comf x10,w
	addwf ox10
	btfss status,c
	goto add442
	incf ox100
	btfss status,z
	goto add442
	incf ox1000
add442	
	comf x100,w
	addwf ox100
	btfss status,c
	goto add443
	incf ox1000
add443
	comf x1000,w
	addwf ox1000	
add1
	incf ox1
	btfss status,z
	goto add444
	incf ox10
	btfss status,z
	goto add444		
	incf ox100
	btfsc status,z
	incf ox1000	
add444
      	bsf status,c
div23k  
      	rlf crcl
      	rlf crch
      	rlf dirl
      	rlf dirh
      	decfsz data1,f
      	goto divlopp3
      
 
      movf dirh,w
      btfss status,z
      goto 大于10000
      movf dirl,w
      btfss status,z
      goto 大于10000
      movf crch,w
      movwf calc1h
      movf crcl,w
      movwf calc1l
       return
      
大于10000
      movlw 0ffh
      movwf calc1h
      movlw 0ffh
      movwf calc1l      
      return

;**********************************************





;************************************
;延时900us子程序 delay900us
;使用了dtimeh,dtimel
delay900us
	movlw .9
	movwf dtimeh
dejj2jkk5
	clrwdt
	call delay100us
	DECFSZ dtimeh,F
	GOTO dejj2jkk5
	RETURN
;************************************

;************************************
;延时1s子程序 delay1s
;使用了dtimeh,dtimel
delay1s
	movlw .1
	movwf data2
delay1s2
	movlw .110
	movwf data1
delay1slop
	call delay900us
	DECFSZ data1,F
	GOTO delay1slop
	decfsz data2
	goto delay1s2

	RETURN
;************************************
;************************************
;延时20ms子程序	delay20ms
;使用了dtimeh,dtimel
delay20ms

	movlw .20
	movwf data1
delay20msl
	call delay900us
	DECFSZ data1,F
	GOTO delay20msl
	RETURN
;************************************




;************************************

;延时100us子程序 delay100us
;使用了dtimeh,dtimel
delay100us
		movlw .100
	movwf dtimel
dejj1jk	nop
	nop
	nop
	DECFSZ dtimel,F
	GOTO dejj1jk
	RETURN

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



;=============================
;读出eeprom一个字节
;地址在data1中
;读出数据在w中
read_byte
	movwf data1
	bsf status,rp1
	bcf status,rp0
	movwf eeadr
	bsf status,rp0
	bcf eecon1,eepgd
	bsf eecon1,rd
	bcf status,rp0
	movf eedata,w
	bcf status,rp1
	incf data1
	return
	
	
;=============================
;写入eeprom一个字节
;地址在data1中;data1在70-7fh
;数据在w中
write_byte
	bcf pir2,eeif
	bsf status,rp1
	bcf status,rp0
	movwf eedata
	movf data1,w
	movwf eeadr
	bsf status,rp0
	bcf eecon1,eepgd
	bsf eecon1,wren
	bcf intcon,gie
	movlw 55h
	movwf eecon2
	movlw 0aah
	movwf eecon2
	bsf eecon1,wr
	bcf status,rp0
	bcf status,rp1
	incf data1
	call delay20ms
;wait_write
;	btfss pir2,eeif
;	goto wait_write
;	bcf pir2,eeif
	return
;===============================



;读出所有参数
;***********************************
读出所有参数
;读出传感器地址
;初始化传感器参数

	clrf w_mode

	movlw 10h
	call read_byte
	
	movlw .8
	
       	movwf fsr06l

      	movf fsr06l,w
      	sublw .7
      	btfsc status,c
      	goto address_error
       	movf fsr06l,w
      	sublw 0fh
      	btfss status,c
      	goto address_error   
      	
    clrf circle0
	clrf circle1
	movlw .3
	movwf fsr00l
	movlw 00h
	movwf fsr00h
	clrf fsr01l
	clrf fsr01h
	clrf fsr02l
	clrf fsr02h
	clrf fsr03h
	clrf fsr03h	
	movlw 0cbh
	movwf fsr04l
	clrf fsr04h
	movlw 12h
	movwf fsr05l
	clrf fsr05h
	movlw 98h
	movwf fsr06h
	clrf fsr07l
	clrf fsr07h 
	movlw 20h
	movwf 周期hhb
	clrf 周期hh
  	return

address_error
	movlw 10h
	movwf data1
	movlw 08h
	call write_byte
	goto 读出所有参数	
       
;***********************************


end






⌨️ 快捷键说明

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