本帖最后由 lzsky 于 2018-10-22 21:38 编辑
适用于PIC系列,汇编、内存紧张的朋友用
SDA EQU 5 ;'OC'
SCL EQU 4
ACKF EQU 0
EE_VCC EQU 2
EE_WP EQU 1 ;'H' DISABLE 'L'ENABLE
DO EQU 7
TX_BUF EQU 0X30
BUF_IN EQU 0X31
EEPROM EQU 0X32
BIT_COUNT EQU 0X33
byte_count equ 0x34
ACK EQU 0X35
rom_buf1 equ 0x36
rom_buf2 equ 0x37
rom_buf3 equ 0x38
rom_buf4 equ 0x39
rom_buf5 equ 0x3a
;**********************启动信号***************************************
BSTART:
BSF PORTA,SDA
NOP
NOP
NOP
NOP
NOP
NOP
BSF PORTA,SCL
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BCF PORTA,SDA
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BCF PORTA,SCL
NOP
NOP
RETLW 0
;***********************停止信号********************************
BSTOP:
BCF PORTA,SDA
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BSF PORTA,SCL
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BSF PORTA,SDA
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BCF PORTA,SCL
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
RETLW 0
;**********************发送N_ACK**************************
SEND_ACK:
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BCF PORTA,SDA
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BSF PORTA,SCL
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BCF PORTA,SCL
NOP
NOP
NOP
NOP
NOP
NOP
RETLW 0
;**********************发送N_ACK**************************
READ_ACK:
BSF TRISA,SDA ;SET SDA IN
CLRF ACK
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BSF PORTA,SCL
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BTFSS PORTA,SDA
INCF ACK,F
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BCF PORTA,SCL
BCF TRISA,SDA ;SET SDA OUT
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
RETLW ACK;0
;**********************发送一个字节**************************
BYTEOUT:
MOVLW D'8'
MOVWF BIT_COUNT
TXLP
nop
nop
CALL BITOUT
RLF TX_BUF,F
DECFSZ BIT_COUNT,F
GOTO TXLP
CALL READ_ACK
;CALL BITIN ;ACK
;BTFSC EEPROM,DI
; BSF PORTC,ACKF
RETLW 0
;************************接收一个字节*****************************
BYTEIN:
BSF TRISA,SDA ;SET SDA IN
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
CLRF BUF_IN
MOVLW D'8'
MOVWF BIT_COUNT
BCF C ;BCF STATUS,0
RXLP RLF BUF_IN,F
;BCF BUFIN,0
CALL BITIN
;BTFSC EEPROM,DI
;BSF BUFIN,0
DECFSZ BIT_COUNT,F
GOTO RXLP
;BSF EEPROM,DO
;CALL BITOUT
BCF TRISA,SDA ;SET SDA OUT
NOP
NOP
NOP
NOP
NOP
NOP
RETLW 0
;**********************发送********************************
BITOUT
;MOVLW B'00111111'
;MOVWF TRISA
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BTFSS TX_BUF,7
GOTO BITLOW
BSF PORTA,SDA
GOTO CLKOUT
BITLOW BCF PORTA,SDA
CLKOUT
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BSF PORTA,SCL
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BCF PORTA,SCL
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
RETLW 0
;**********************接收****************************
BITIN
BCF BUF_IN,0 ;EEPROM,DI
;BSF TRISA,SDA ;SET SDA IN
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BSF PORTA,SCL
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BTFSC PORTA,SDA
BSF BUF_IN,0
BCF PORTA,SCL
;BCF TRISA,SDA ;SET SDA OUT
RETLW 0
;-------------------------
;temp0:addr
;temp1:data
;-------------------------
writebyte
nop
nop
call BSTART
nop
movlw 0xa0
movwf TX_BUF
call BYTEOUT
movf temp0,W
movwf TX_BUF
call BYTEOUT
movf temp1,W
movwf TX_BUF
call BYTEOUT
nop
call BSTOP
nop
call BSTOP
return
;-------------------------
;INPUT:temp0 addr
;out:BUFIN
;-------------------------
readbyte
nop
nop
call BSTART
nop
movlw 0xa0
movwf TX_BUF
call BYTEOUT
nop
nop
movf temp0,W ;addr
movwf TX_BUF
call BYTEOUT
nop
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
nop
call BSTART
nop
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
movlw 0xa1
movwf TX_BUF
call BYTEOUT
nop
nop
nop
nop
call BYTEIN
call BSTOP
return
;-------------------------
;temp0:addr '0x10'add '0x20'data
;temp1:data
;-------------------------
writenbyte
nop
nop
call BSTART
nop
movlw 0xa0
movwf TX_BUF
call BYTEOUT
movf temp0,W
movwf TX_BUF
call BYTEOUT
movlw 0x5
movwf byte_count
movlw rom_buf1 ;取rom_buf1地址
movwf FSR
wx_lp:
movf INDF,W
movwf TX_BUF
call BYTEOUT
INCF FSR,F
DECFSZ byte_count,F
goto wx_lp
nop
nop
call BSTOP
return
;-------------------------
;temp0:addr '0x10'add '0x20'data
;temp1:data num
;-------------------------
readnbyte
nop
nop
call BSTART
nop
movlw 0xa0
movwf TX_BUF
call BYTEOUT
movf temp0,W
movwf TX_BUF
call BYTEOUT
NOP
call BSTART
nop
movlw 0xa1
movwf TX_BUF
call BYTEOUT
movlw 0x5
movwf byte_count
movlw rom_buf1 ;取rom_buf1地址
movwf FSR
rx_lp:
call BYTEIN
call SEND_ACK
movf BUF_IN,W
movwf INDF
INCF FSR,F
DECFSZ byte_count,F
goto rx_lp
nop
nop
call BSTOP
return
;--------------------------------
test_eeprom:
movlw 0x44
movwf add1
movlw 0x55
movwf add2
movlw 0x66
movwf add3
;movlw 0x20
;movwf temp0
;movlw add1 ;0x23 ;data *pt add1=0x23 flag 2d /2e
;movwf temp1
;call writenbyte
call save_add
call Delay100ms
call Delay100ms
;call Delay100ms
;call Delay100ms
clrf rom_buf1
clrf rom_buf5
movlw 0x20
movwf temp0
call readnbyte
return
;------------------------------------------
save_add:
movlw 0xaa
movwf rom_buf1
movf add1,W
movwf rom_buf2
movf add2,W
movwf rom_buf3
movf add3,W
movwf rom_buf4
movlw 0x55
movwf rom_buf5
movlw 0x20
movwf temp0
call writenbyte
return
;------------------------------------------
save_data:
movlw 0xaa
movwf rom_buf1
movf flag,W
andlw 0x03
movwf rom_buf2
movf LED_speed,W
movwf rom_buf3
movlw 0x08; add3,W
movwf rom_buf4
movlw 0x55
movwf rom_buf5
movlw 0x30
movwf temp0
call writenbyte
return
;-----------------------------------------
;0x20
;
read_add:
movlw 0x20
movwf temp0
call readnbyte
movlw 0xaa
subwf rom_buf1,W
btfss Z
goto rd_err
movlw 0x55
subwf rom_buf5,W
btfss Z
goto rd_err
;---add is ok----------------
movf rom_buf2,W
movwf add1
movf rom_buf3,W
movwf add2
movf rom_buf4,W
movwf add3
bsf match_fin_fb
rd_err
nop
movlw 0xb0
movwf add1
movlw 0x16
movwf add2
movlw 0x47
movwf add3
bsf match_fin_fb
nop
return
;------------------------------------------
;0x30
;-----------------------------------------
read_data:
movlw 0x30
movwf temp0
call readnbyte
movlw 0xaa
subwf rom_buf1,W
btfss Z
goto rdd_err
movlw 0x55
subwf rom_buf5,W
btfss Z
goto rdd_err
movf rom_buf2,W
andlw 0x03
iorwf flag,F
movf rom_buf3,W
movwf LED_speed
rdd_err
nop
nop
return
|