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

📄 speed.asm

📁 用接近开关作速度传感器的PIC程序speed.asm RS485输出
💻 ASM
📖 第 1 页 / 共 2 页
字号:
;_______________________________________
;	速度传感器程序
;	speed.asm
;	  2004年2月17日
;	用于RS485输出
;____________________________________________
   w		equ	H'0000'
   f		equ	H'0001'

   indf			equ	H'0000'
   tmr0			equ	H'0001'
   pcl		equ	H'0002'
   status		equ	H'0003'
   fsr		equ	H'0004'
   porta		equ	H'0005'
   portb		equ	H'0006'

   pclath		equ	H'000A'
   intcon		equ	H'000B'

   option_rg	equ	H'0001'
   trisa		equ	H'0005'
   trisb		equ	H'0006'

;----- OPTION Bits --------------------------------------------------------

   not_rbpu	EQU	H'0007'
   intedg		EQU	H'0006'
   t0cs			EQU	H'0005'
   t0se			EQU	H'0004'
   psa		EQU	H'0003'
   ps2		EQU	H'0002'
   ps1		EQU	H'0001'
   ps0		EQU	H'0000'
   tmr1ie		equ	0
   tmr1if		equ	0
   tmr1on		equ	0

	t0ie	equ	.5
   gie		equ	7
   ccpxm3		equ	3

   tmr0ie		equ	.5
   peie			equ	.6
   gie		equ	.7
   t0if			equ	.2
   tmr2ie		equ	.1
   tmr2if		equ	.1

;----- status Bits --------------------------------------------------------
   irp		EQU	H'0007'

   rp0		EQU	H'0005'
   rp1		EQU	H'0006'
   z		EQU	H'0002'
   c		EQU	H'0000'

   ;----- Register Files --------------------------------------------------
      portc		EQU	H'0007'
	sda	EQU	H'0004'
	scl	EQU	H'0003'
      portd		EQU	H'0008'
      porte		EQU	H'0009'
      pir1		EQU	H'000C'
      pir2		EQU	H'000D'
      tmr1l		EQU	H'000E'
      tmr1h		EQU	H'000F'
      t1con		EQU	H'0010'
      tmr2		EQU	H'0011'
      t2con		EQU	H'0012'
      sspbuf	EQU	H'0013'
      sspcon	EQU	H'0014'
      ccpr1l		EQU	H'0015'
      ccpr1h	EQU	H'0016'
      ccp1con	EQU	H'0017'
      ccpr2l		EQU	H'001B'
      ccpr2h	EQU	H'001C'
      ccp2con	EQU	H'001D'

	ccp1if		equ	.2
	ccp2if		equ	.0
	txif		equ	.4
	rcif		equ	.5

      trisc		EQU	H'0007'
      trisd		EQU	H'0008'
      trise		EQU	H'0009'
      pie1		EQU	H'000C'
      pie2		EQU	H'000D'
      pcon		EQU	H'000E'
      pr2		EQU	H'0012'
      sspadd	EQU	H'0013'
      sspstat	EQU	H'0014'

      txsta		EQU	H'0018'	 ;*
      rcsta		EQU	H'0018'	 ;
      txreg		EQU	H'0019'	 ;
      rcreg		EQU	H'001a'	 ;
      spbrg		EQU	H'0019'	 ;*

      adcon0	EQU	1FH
      adcon1	EQU	1FH
      adres		EQU	1EH
      go_done	EQU	02H
;--------------------------------------------------------------------------
	eeadr	equ	0dh
	eeadrh	equ	0fh
	eedata	equ	0ch
	eedatah	equ	0eh
	eecon1	equ	0ch
	eecon2	equ	0dh
	eeif	equ	.4
	eepgd	equ	.7
	wrerr	equ	.3
	wren	equ	.2
	wr	equ	.1
	rd	equ	.0

;应用程序定义特殊寄存器,特殊控制线


	字间隔		equ	27h

	rec_num		equ	50h
	tx_num		equ	57h
	rtcom		equ	.5

	m_stau		equ	51h
	补偿杆标志位	equ	.0
	地面标志位	equ	.1
	探杆回波2	equ	.2

	w_mode		equ	5fh
;	.0=1	有脉冲
;	.0=0	无脉冲





	收发首地址	equ	20h

	crch		equ	58h
	crcl		equ	59h



	dtimeh		equ	24h
	dtimel		equ	25h


	data1			EQU	28H
	data2			EQU	29H
	data3			equ	2ah
	data4			equ	2bh


	x1000			equ	2ch
	x100			equ	2dh
	x10			equ	2eh
	x1			equ	2fh

	dirh			equ	3eh
	dirl			equ	3fh
	
	ox1000			equ	24h
	ox100			equ	25h
	ox10			equ	26h
	ox1			equ	27h
	
	
	oneb			equ	79h


	calc1h		equ	7dh
	calc1l		equ	7Ah
	calc2h		equ	7BH
	calc2l		EQU	7CH

	tx_pi		equ	52h


	ww1			equ	30h
	ww2			equ	31h
	ww3			equ	32h
	ww6			equ	35h
	ww8			equ	37h
	ww			equ	38h

	ww_bak		equ	70h
	status_bak	equ	71h
	pclath_bak	equ	72h



	circle0		equ	5eh
	circle1		equ	5fh
	fsr00l		equ	60h
	fsr00h		equ	61h
	fsr01l		equ	62h
	fsr01h		equ	63h
	fsr02l		equ	64h
	fsr02h		equ	65h
	fsr03l		equ	66h
	fsr03h		equ	67h
	fsr04l		equ	68h
	fsr04h		equ	69h
	fsr05l		equ	6ah
	fsr05h		equ	6bh
	fsr06l		equ	6ch
	fsr06h		equ	6dh
	fsr07l		equ	6eh
	fsr07h		equ	6fh

	speedh		equ	67h
	speedl		equ	66h
	
	距离hh		equ	65h
	距离hl		equ	64h	
	距离lh		equ	63h
	距离ll		equ	62h


	周期hh		equ	44h
	周期hl		equ	45h	
	周期lh		equ	46h
	周期ll		equ	47h	

	周期hhb		equ	48h
	周期hlb		equ	49h	
	周期lhb		equ	4ah
	周期llb		equ	4bh


	data1h		equ	3bh
	data1l		equ	3ch
	datal		equ	41h

	data2h		equ	42h
	data2l		equ	43h
	
	计数l	equ	4eh
	计数h	equ	4fh	
	

;速度归一参数
	归一hh	equ	1bh
	归一hl	equ	0b7h	
	归一lh	equ	56h
	归一ll	equ	40h	
;说明:
;	速度在4003单元
;	距离在4002,4001单元
	

;************************************
	org	000H
	clrf	pclath
	goto	main	;去主程序
;************************************

;************************************
	org	004H
	bcf intcon,7
	movwf ww_bak
	swapf status,w
	movwf status_bak
	swapf ww_bak,f
	bcf status,rp0
	bcf status,rp1
	bcf status,irp
	movf pclath,w
	movwf pclath_bak
	clrf pclath

	btfss pir1,tmr1if
	goto other_int1
	bcf pir1,tmr1if
	
	
	clrf 距离ll
	incf 周期hl
	btfsc status,z
	incf 周期hh
	
	movf 周期hhb,w
	subwf 周期hh,w
	btfsc status,z
	goto cn001
	btfss status,c
	goto fast01
	movf 周期hh,w
	movwf 周期hhb
	movf 周期hl,w
	movwf 周期hlb
	clrf 周期lhb
	clrf 周期llb			
	goto fast01
cn001
	movf 周期hlb,w
	subwf 周期hl,w
	btfss status,c
	goto fast01
	movf 周期hl,w
	movwf 周期hlb
	clrf 周期lhb
	clrf 周期llb	
	
fast01	
	btfss 周期hh,3
	goto int_return
	movlw 0ffh
	movwf 周期hh
	movwf 周期hhb	
	movlw 00h
	movwf 周期hl
	movwf 周期hlb	
	bsf w_mode,0
	
	goto int_return
	
other_int1	
	btfss intcon,t0if
	goto other_int2
	bcf intcon,t0if
	incf 距离lh
	btfss status,z
	goto int_return
	incf 距离hl
	btfss status,z
	goto int_return
	incf 距离hh
	goto int_return
		
other_int2
	btfss intcon,1;intf
	goto other_int3
	bcf intcon,1
	bcf t1con,tmr1on
	movf tmr0,w
	movwf  距离ll
	movf tmr1h,w
	movwf 周期lhb
	movf tmr1l,w
	movwf 周期llb
	movf 周期hh,w
	movwf 周期hhb
	movf 周期hl,w
	movwf 周期hlb
	clrf 周期hh
	clrf 周期hl
	bsf t1con,tmr1on			
	bsf w_mode,0
	goto int_return		
other_int3
	bcf intcon,0
	clrf pir2	
	bsf status,rp0
	clrf pie1
	clrf pie2
	bsf pie1,tmr1ie
	movlw 70h
	movwf intcon
	bcf status,rp0		
					
int_return
	movf pclath_bak,w
	movwf pclath
	swapf status_bak,w
	movwf status
	swapf ww_bak,w	
	bsf intcon,7
	retfie
;************************************




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

main
	call 系统初始化
	call delay1s
	call 读出所有参数

	
	call 通讯口初始化
;对T1初始化
	bcf status,rp0
	clrf rec_num

	bsf intcon,7
	
	
main_loop	
	btfsc pir1,rcif
	call  ok_rec
	call 计算周期
	incf 计数l
	btfsc status,z
	incf 计数h
	btfsc 计数h,4
	bsf portc,1
	btfss 计数h,4
	bcf portc,1	
	goto main_loop
;================================	
计算周期
	clrwdt

	btfss w_mode,0
	return
	movlw 归一hh
	movwf calc2h;x1000
	movlw 归一hl
	movwf calc2l;x100	
	movlw 归一lh
	movwf calc1h;x10
	movlw 归一ll
	movwf calc1l;x1
	movf 周期hhb,w
	movwf x1000;calc2h
	movf 周期hlb,w
	movwf x100;calc2l
	movf 周期lhb,w
	movwf x10;calc1h
	movf 周期llb,w
	movwf x1;calc1l
	call divsub4_4
	movf calc1h,w
	movwf speedh
	movf calc1l,w
	movwf speedl

	return			
		

;------------------------------------------------------------------
发送数据
;使用:dtimeh,dtimel
;使用:tx_num,ww3,tx_pi
	btfss m_stau,7;是否需要发送?
	return
	

	clrf tx_pi
	call delay900us	
	bsf portc,0	
	call delay900us	
	call delay900us
	call delay900us	
	call delay900us
	call delay900us		


sent_datab
	btfsc pir1,txif	;是否已发送完数据?
	call ok_tx
	clrwdt
	movf tx_num,w
	btfss status,z
	goto sent_datab
	call delay900us
	call delay900us
	bcf portc,0
	bsf m_stau,6	;设置刚发送标志
	return

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


;===================================
ok_tx
;数据发送子程序
;使用:tx_num,ww3,tx_pi
	movf tx_num,w
	btfsc status,z
	return
	movf fsr,w
	movwf ww3
	movf tx_pi,w
	addlw 收发首地址
	movwf fsr
	bsf status,irp
	movf indf,w
	movwf txreg
	movf ww3,w
	movwf fsr
	incf tx_pi
	decf tx_num
	movf tx_pi,w
	return
;===================================

计算数据并接受数据
	call 数据移位存储	;135us
	call 数据平均		;4000us
	return
;===================================


;===================================
数据移位存储
;135us
	movlw 0a0h
	movwf data1
	movlw .39
	movwf data2
	bcf ww6,5
	call 双移位
;	movlw 0eeh
;	movwf fsr
;	movf dm_datal,w
;	movwf indf
;	incf fsr
;	movf dm_datah,w
;	movwf indf
	return

;===================================
数据平均;同时数据接收
;4ms
;数据在0a0-0efh
;最大数据在1ao-1afh
;最小数据在1b0-1bfh
	clrf x1000
	clrf x100
	clrf x10
	clrf x1
	movlw .40
	movwf ww1
	movlw 0a0h
	movwf ww2
	clrf ww8
aver_loop
	bcf status,irp
    	movf ww2,w
    	movwf fsr
	movf indf,w
	movwf calc1l
	incf fsr
	movf indf,w
	movwf calc1h
	clrwdt
	movf calc1l,w
	addwf x1
	btfsc status,c
	incf calc1h
	movf calc1h,w
	addwf x10
	btfsc status,c
	incf x100
	incf ww2
	incf ww2
	btfsc pir1,rcif
	call  ok_rec
	incf ww8
	decfsz ww1,f
	goto aver_loop

	movlw .85;.48;.24*2
	movwf oneb
	call divsub3

;	movf x10,w
;	movwf last_data_h
;	movf x1,w
;	movwf last_data_l

	return




;===================================
双移位
;数据首地址=data1
;数据区在ww6,5=0	上区
;数据区在ww6,5=1	下区
;数据长度=data2
;使用:data1,data2,ww
	btfsc ww6,5
	bsf status,irp
	btfss ww6,5
	bcf status,irp

	movf data2,w
	sublw .1
	btfsc status,c
	return


	movf fsr,w
	movwf ww
	movf data1,w
	movwf fsr
move_loop
	incf fsr
	incf fsr
	movf indf,w
	decf fsr
	decf fsr
	movwf indf
	incf fsr
	incf fsr
	incf fsr
	movf indf,w
	decf fsr
	decf fsr
	movwf indf
	incf fsr
	decfsz data2,f
	goto move_loop
	movf ww,w
	movwf fsr
	return
;=========================================


;====================================================
;数据接收程序
ok_rec	



	bsf status,irp
	movf rcreg,w
	movwf ww
	bcf pir1,rcif
	
	bcf m_stau,6	;清除刚发送标志

	movf rec_num,w
	btfsc status,z
	goto rec_no1
	xorlw .8
	btfsc status,z
	goto 分析接收到的数据	;已收满一帧信息,返回
;未收满一帧信息
	btfsc pir1,tmr1if
	goto rec_no1
	movf tmr1h,w
	sublw 字间隔
	btfsc status,c
	goto 累加收
rec_no1
	movlw .1
	movwf rec_num
	bcf pir1,tmr1if
	clrf tmr1h
	clrf tmr1l
	movlw 收发首地址
	movwf fsr
	movf ww,w
	movwf indf
	return

累加收
	movf rec_num,w
	addlw 收发首地址
	movwf fsr
	bcf pir1,tmr1if
	clrf tmr1h
	clrf tmr1l
	movf ww,w
	movwf indf
	incf rec_num
	movf rec_num,w
	xorlw .8
	btfsc status,z
	goto 分析接收到的数据	;已收满一帧信息,返回
	return
;====================================================
分析接收到的数据
;是否要发送?
;m_stau,7=0 不发送
;m_stau,7=1 需发送
;发送的数据长度:tx_num
	bcf m_stau,7
	movf rec_num,w
	xorlw .8
	btfss status,z
	return	;未收满一帧信息,返回
	clrf rec_num
;分析接收到的信息

	movlw 收发首地址
	movwf fsr
	movf indf,w
	btfsc status,z
	goto address0_ok
	xorwf fsr06l,w
	btfss status,z
	return

;地址正确,非广播地址
	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 设定寄存器并发送
	movf indf,w
	xorlw 01h
	btfsc status,z
	goto 发送线圈状态
	movf indf,w
	xorlw 03h
	btfsc status,z
	goto 发送寄存器数据
	return

发送线圈状态
	movlw 收发首地址+2
	movwf fsr
	movlw .1
	movwf indf
	incf fsr
	movf indf,w
	andlw .1
	addlw 5eh
	movwf fsr
	bcf status,irp
	movf indf,w
	movwf ww
	movlw 收发首地址+3
	movwf fsr
	bsf status,irp
	movf ww,w
	movwf indf
	movlw 收发首地址
	movwf data2
	movlw .4
	movwf data1
	call 计算crc值
	movlw 收发首地址+4
	movwf fsr
	movf crcl,w
	movwf indf
	incf fsr
	movf crch,w
	movwf indf
	movlw .6
	bsf m_stau,7
	movwf tx_num
	call 发送数据	
	return



发送寄存器数据
	movlw 收发首地址+2
	movwf fsr
	movlw .2
	movwf indf
	incf fsr

⌨️ 快捷键说明

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