📄 las1.asm
字号:
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
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
movlw 8eh
movwf adcon1 ;AD0
movlw 3FH ;0011 1111b A4输出,A0,1,2,3,5输入。
movwf trisa
movlw 0ffh
movwf trisb
movlw 0bcH ;10111100=0a4h ;
movwf trisc
;PB口上拉
;PSA属于TMR0,4分频
movlw 8fh ;
movwf option_rg
bcf status,rp0
clrf intcon
bcf portc,0
movlw 补偿归一h
movwf bc_datah
movlw 补偿归一l
movwf bc_datal
bsf status,irp
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
;************************************
;************************************
;延时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
;************************************
;************************************
;延时10ms子程序 delay10ms
;使用了dtimeh,dtimel
delay10ms
movlw .140
movwf data1
delay10msl
movlw .200
movwf dtimel
dejk nop
btfsc pir1,rcif
call ok_rec
DECFSZ dtimel,F
GOTO dejk
DECFSZ data1,F
GOTO delay10msl
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
;===============================
;读出所有参数
;***********************************
读出所有参数
;读出传感器地址
;初始化传感器参数
movlw 10h
call read_byte
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 b'00000010';00h
movwf fsr00l
movlw 00h
movwf fsr00h
clrf fsr01l
clrf fsr01h
movlw 0bh
movwf fsr02l
movlw .1
movwf fsr02h
movlw 0cbh
movwf fsr04l
clrf fsr04h
movlw 12h
movwf fsr05l
clrf fsr05h
movlw 98h
movwf fsr06h
clrf fsr07l
clrf fsr07h
return
address_error
movlw 10h
movwf data1
movlw 08h
call write_byte
goto 读出所有参数
;***********************************
;I2C任意字节读
i2c_read_1
;地址在 i2c_addressh,l
;读出的数据在w中
;i2c初始化
movlw b'00101000'
movwf sspcon
movlw 10h
bsf status,rp0
movwf sspadd
;-------------------------
bsf sspcon2,sen
bcf status,rp0
call wait_ok
movlw write_mode
movwf sspbuf
call wait_ok
movf i2c_addressh,w
movwf sspbuf
call wait_ok
movf i2c_addressl,w
movwf sspbuf
call wait_ok
bsf status,rp0
bsf sspcon2,pen
bcf status,rp0
call wait_ok
bsf status,rp0
bsf sspcon2,sen
bcf status,rp0
call wait_ok
movlw read_mode
movwf sspbuf
call wait_ok
bsf status,rp0
bsf sspcon2,rcen
bcf status,rp0
call wait_ok
movf sspbuf,w
movwf ww
bsf status,rp0
bsf sspcon2,pen
bcf status,rp0
call wait_ok
return
;-------------------------------
;***********************************
;I2C任意字节读16
i2c_read_16
;地址在 i2c_addressh,l
;读出的数据在fsr中
;i2c初始化
movlw b'00101000'
movwf sspcon
movlw 10h
bsf status,rp0
movwf sspadd
;-------------------------
bsf sspcon2,sen
bcf status,rp0
call wait_ok
movlw write_mode
movwf sspbuf
call wait_ok
movf i2c_addressh,w
movwf sspbuf
call wait_ok
movf i2c_addressl,w
movwf sspbuf
call wait_ok
bsf status,rp0
bsf sspcon2,pen
bcf status,rp0
call wait_ok
bsf status,rp0
bsf sspcon2,sen
bcf status,rp0
call wait_ok
movlw read_mode
movwf sspbuf
call wait_ok
movlw .16
movwf ww
loop_16r
bsf status,rp0
bsf sspcon2,rcen
bcf status,rp0
call wait_ok
movf sspbuf,w
movwf indf
incf fsr
bsf status,rp0
bsf sspcon2,acken
bcf status,rp0
call wait_ok
decfsz ww
goto loop_16r
bsf status,rp0
bsf sspcon2,pen
bcf status,rp0
call wait_ok
return
;-------------------------------
wait_ok
btfss pir1,sspif
goto wait_ok
bcf pir1,sspif
return
;***********************************
;I2C任意字节写16
i2c_write_16
;地址在 i2c_addressh,l
;写入的数据在fsr中
;i2c初始化
movwf ww
movlw b'00101000'
movwf sspcon
movlw 10h
bsf status,rp0
movwf sspadd
;-------------------------
bsf sspcon2,sen
bcf status,rp0
call wait_ok
movlw write_mode
movwf sspbuf
call wait_ok
movf i2c_addressh,w
movwf sspbuf
call wait_ok
movf i2c_addressl,w
movwf sspbuf
call wait_ok
movlw .16
movwf ww
loop_16w
movf indf,w
movwf sspbuf
call wait_ok
bsf status,rp0
wait_ack
btfsc sspcon2,ackstat
goto wait_ack
bcf status,rp0
incf fsr
decfsz ww
goto loop_16w
bsf status,rp0
bsf sspcon2,pen
bcf status,rp0
call wait_ok
return
;***********************************
;I2C任意字节写
i2c_write_1
;地址在 i2c_addressh,l
;写入的数据在w中
;i2c初始化
movwf ww
movlw b'00101000'
movwf sspcon
movlw 10h
bsf status,rp0
movwf sspadd
;-------------------------
bsf sspcon2,sen
bcf status,rp0
call wait_ok
movlw write_mode
movwf sspbuf
call wait_ok
movf i2c_addressh,w
movwf sspbuf
call wait_ok
movf i2c_addressl,w
movwf sspbuf
call wait_ok
movf ww,w
movwf sspbuf
call wait_ok
bsf status,rp0
bsf sspcon2,pen
bcf status,rp0
call wait_ok
return
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -