INCLUDE
;*************************定义变量************************
STATUS EQU 3H
PORTB EQU 6H
PORTC EQU 7H
EEPROM EQU 08H
ADDR EQU 09H
BUFIN EQU 0AH
BUFOUT EQU 0BH
SLAVE EQU 0CH
TXBUF EQU 0DH
BIT EQU 0EH
DLY1 EQU 0FH
DLY2 EQU 10H
DI EQU 7
DO EQU 6
SDA EQU 7
SCL EQU 6
ACKF EQU 0
;**********************程序开始***************************
ORG 7FFH
GOTO MAIN
ORG 000H
;**********************启动信号***************************************
BSTART
BSF PORTB,SDA
BSF PORTB,SCL
MOVLW B'00111111'
TRIS PORTB
NOP
NOP
NOP
NOP
NOP
NOP
BCF PORTB,SDA
NOP
NOP
NOP
NOP
NOP
BCF PORTB,SCL
NOP
NOP
RETLW 0
;***********************停止信号********************************
BSTOP
BCF PORTB,SDA
MOVLW B'00111111'
TRIS PORTB
BCF PORTB,SDA
NOP
NOP
NOP
BSF PORTB,SCL
NOP
NOP
NOP
BSF PORTB,SDA
NOP
NOP
BCF PORTB,SCL
NOP
NOP
RETLW 0
;**********************发送一个字节**************************
TX
MOVLW D'8'
MOVWF BIT
TXLP BCF EEPROM,DO
BTFSC TXBUF,7
BSF EEPROM,DO
CALL BITOUT
RLF TXBUF
DECFSZ BIT
GOTO TXLP
CALL BITIN
BTFSC EEPROM,DI
BSF PORTC,ACKF
RETLW 0
;************************接收一个字节*****************************
RX
CLRF BUFIN
MOVLW D'8'
MOVWF BIT
BCF STATUS,0
RXLP RLF BUFIN
BCF BUFIN,0
CALL BITIN
BTFSC EEPROM,DI
BSF BUFIN,0
DECFSZ BIT
GOTO RXLP
BSF EEPROM,DO
CALL BITOUT
RETLW 0
;**********************发送********************************
BITOUT
MOVLW B'00111111'
tris PORTB
BTFSS EEPROM,DO
GOTO BITLOW
BSF PORTB,SDA
GOTO CLKOUT
BITLOW BCF PORTB,SDA
CLKOUT BSF PORTB,SCL
NOP
NOP
NOP
NOP
BCF PORTB,SCL
RETLW 0
;**********************接收****************************
BITIN
BSF EEPROM,DI
MOVLW B'10111111'
TRIS PORTB
BSF PORTB,SCL
NOP
NOP
NOP
NOP
NOP
BTFSS PORTB,SDA
BCF EEPROM,DI
BCF PORTB,SCL
RETLW 0
;***********************主程序**********************
MAIN
MOVLW B'00000000'
TRIS PORTC
CLRF PORTC
WRITE ;写存储器
CLRF PORTC
MOVLW B'10100000'
MOVWF SLAVE
MOVLW B'10101010'
MOVWF BUFOUT
CLRF ADDR
LOOP0 CALL BSTART
MOVF SLAVE,0
MOVWF TXBUF
CALL TX
MOVF ADDR,0
MOVWF TXBUF
CALL TX
MOVF BUFOUT,0
MOVWF TXBUF
CALL TX
CALL BSTOP
DELAY10MS
MOVLW D'13'
MOVLW DLY1
MOVLW 0FFH
MOVWF DLY2
LP0 DECFSZ DLY2
GOTO $-1
DECFSZ DLY1
GOTO $-5
READ ;读存储器
CLRF PORTC
MOVLW B'10100000'
MOVWF SLAVE
CLRF ADDR
LOOP1 CALL BSTART
MOVF SLAVE,0
MOVWF TXBUF
CALL TX
MOVF ADDR,0
MOVWF TXBUF
CALL TX
CALL BSTART
MOVLW B'10100001'
MOVWF TXBUF
CALL TX
CALL RX
CALL BSTOP
NOP
NOP
NOP
NOP
NOP
NOP
MOVF BUFIN,0
MOVWF PORTC
LOOP2 NOP
GOTO $-1
;************************
END
上一篇:PIC单片机实例二:基于I2C的SAA1064数码管显示
下一篇:PIC单片机应用专题二:显示接口及编程
推荐阅读最新更新时间:2024-03-16 15:19