📄 pic16f77_i2c.asm
字号:
;PIC16F77单片机I2C读写程序
;=============================
;读出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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -