24LCxx系列的EEPROM讀寫的程序?qū)嵗?/h1>
本文引用地址:http://2s4d.com/article/171137.htm;
;******************************************************
;* Random Read a Byte from EEPROM
;*
;* Input:
;* - I2C_Ctrl : Control Byte of EEPROM
;* - I2C_Addr : Location of EEPROM
;* Output:
;* - I2C_Data : Read Data from EEPROM
;******************************************************
;
; Send Command for RANDOM READ :
; Start+ 0xA0 + EE_Address + ReStart + 0xA1 + Read_Data + NAck + Stop
;
EE_Random_Read
call StartI2C ; Set SSPCON2.SEN
;
bcf I2C_Ctrl,0 ; set for write Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
;
movf I2C_Addr,W ; Send out the Rendom address of EEPROM
call Send_Byte
;
call RstartI2C ; Send a Repeat Start to I2C
;
bsf I2C_Ctrl,0 ; set for Read Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
;
call RecI2C ; Enable I2C Receive
;
BANKSEL SSPBUF
movf SSPBUF,W ; Save to I2C_Data First !!
movwf I2C_Data
call Non_Ack ; Initial NACK Response !!
call StopI2C ; Initial STOP Condition
return
;
;***************************************************************
;* Sequential Read from EEPROM
;*
;* Input:
;* - I2C_Ctrl : Control Byte of EEPROM
;* - I2C_Addr : Start Location of EEPROM
;* - I2C_Page_Length : How many byte need to read
;* Output:
;* - I2C_SEQU_Buffer : Sequential Read Data buffer
;*
;***************************************************************
;
; Send Command for RANDOM READ :
; Start+ 0xA0 + EE_Address + ReStart + 0xA1 + Read_Data + NAck + Stop
;
EE_SEQU_Read
call StartI2C ; Set SSPCON2.SEN
;
bcf I2C_Ctrl,0 ; set for write Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
;
movf I2C_Addr,W ; Send out the Rendom address of EEPROM
call Send_Byte
;
call RstartI2C ; Send a Repeat Start to I2C
;
bsf I2C_Ctrl,0 ; set for Read Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
;
movlw I2C_SEQU_Buffer
movwf FSR
;
_Sequ_Loop call RecI2C ; Enable I2C Receive
BANKSEL SSPBUF
movf SSPBUF,W ; Save to I2C_Data First !!
movwf INDF
incf FSR,F
decfsz I2C_Page_Length,F
goto _Cont_Read
goto _End_Read
_Cont_Read call An_Ack
goto _Sequ_Loop
;
_End_Read call Non_Ack ; Initial NACK Response !!
call StopI2C ; Initial STOP Condition
return
;
;******************************************************
;* EEPROM Acknowledge Polling
;*
;* -- The routine will polling the ACK
;* response from EEPROM
;* -- ACK=0 return
;* -- ACK=1 send Restart loop check
;*
;******************************************************
;
EE_Ack_Check
call StartI2C ; Set SSPCON2.SEN
bcf I2C_Ctrl,0 ; Clear for Write Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
_Ack_Polling
BANKSEL SSPCON2
btfss SSPCON2,ACKSTAT ; Check ACKSTAT bit , 0 = ACK , 1 = NACK
goto ACK_Return ; Ack = 0 ; EEPROM is Ready
_Ack_Hi ; Ack = 1 ; EEPROM is Busy for Write
call Delay_mS ; Delay 500uS for next Ack polling
call RstartI2C ; Send a Repeat Start to I2C
bcf I2C_Ctrl,0 ; Clear for Write Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
goto _Ack_Polling
ACK_Return
call StopI2C ; Initial STOP Condition
return
;
;**********************************************************
;* Page Write 1 to 8 Bytes to EEPROM
;*
;* Input:
;* - I2C_Ctrl : Control Byte of EEPROM
;* - I2C_Addr : Location of EEPROM
;* - I2C_Page_Buffer: RAM location of Data
;* - I2C_Page_Length : Data length count
;*
;**********************************************************
;
; Send Command for PAGE WRITE :
; Start+ 0xA0 + EE_Address + W_Data 0+ .. + W_Data N + Stop
;
EE_Page_Write
movf I2C_Page_Length,W
btfsc STATUS,Z
return
;
call StartI2C ; Set SSPCON2.SEN
;
bcf I2C_Ctrl,0 ; Clear for Write Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
;
movf I2C_Addr,W ; Send out the Command
call Send_Byte
;
movlw I2C_Page_Buffer
movwf FSR
_W1 movf INDF,W
call Send_Byte
incf FSR,F
decfsz I2C_Page_Length,F
goto _W1
;
call StopI2C ; Initial STOP Condition
return
;
;
;*******************************************************
;* Write a Byte to EEPROM
;*
;* Input:
;* - I2C_Ctrl : Control Byte of EEPROM
;* - I2C_Addr : Location of EEPROM
;* - I2C_Data : Data to EEPROM
本文引用地址:http://2s4d.com/article/171137.htm
;
;******************************************************
;* Random Read a Byte from EEPROM
;*
;* Input:
;* - I2C_Ctrl : Control Byte of EEPROM
;* - I2C_Addr : Location of EEPROM
;* Output:
;* - I2C_Data : Read Data from EEPROM
;******************************************************
;
; Send Command for RANDOM READ :
; Start+ 0xA0 + EE_Address + ReStart + 0xA1 + Read_Data + NAck + Stop
;
EE_Random_Read
call StartI2C ; Set SSPCON2.SEN
;
bcf I2C_Ctrl,0 ; set for write Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
;
movf I2C_Addr,W ; Send out the Rendom address of EEPROM
call Send_Byte
;
call RstartI2C ; Send a Repeat Start to I2C
;
bsf I2C_Ctrl,0 ; set for Read Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
;
call RecI2C ; Enable I2C Receive
;
BANKSEL SSPBUF
movf SSPBUF,W ; Save to I2C_Data First !!
movwf I2C_Data
call Non_Ack ; Initial NACK Response !!
call StopI2C ; Initial STOP Condition
return
;
;***************************************************************
;* Sequential Read from EEPROM
;*
;* Input:
;* - I2C_Ctrl : Control Byte of EEPROM
;* - I2C_Addr : Start Location of EEPROM
;* - I2C_Page_Length : How many byte need to read
;* Output:
;* - I2C_SEQU_Buffer : Sequential Read Data buffer
;*
;***************************************************************
;
; Send Command for RANDOM READ :
; Start+ 0xA0 + EE_Address + ReStart + 0xA1 + Read_Data + NAck + Stop
;
EE_SEQU_Read
call StartI2C ; Set SSPCON2.SEN
;
bcf I2C_Ctrl,0 ; set for write Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
;
movf I2C_Addr,W ; Send out the Rendom address of EEPROM
call Send_Byte
;
call RstartI2C ; Send a Repeat Start to I2C
;
bsf I2C_Ctrl,0 ; set for Read Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
;
movlw I2C_SEQU_Buffer
movwf FSR
;
_Sequ_Loop call RecI2C ; Enable I2C Receive
BANKSEL SSPBUF
movf SSPBUF,W ; Save to I2C_Data First !!
movwf INDF
incf FSR,F
decfsz I2C_Page_Length,F
goto _Cont_Read
goto _End_Read
_Cont_Read call An_Ack
goto _Sequ_Loop
;
_End_Read call Non_Ack ; Initial NACK Response !!
call StopI2C ; Initial STOP Condition
return
;
;******************************************************
;* EEPROM Acknowledge Polling
;*
;* -- The routine will polling the ACK
;* response from EEPROM
;* -- ACK=0 return
;* -- ACK=1 send Restart loop check
;*
;******************************************************
;
EE_Ack_Check
call StartI2C ; Set SSPCON2.SEN
bcf I2C_Ctrl,0 ; Clear for Write Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
_Ack_Polling
BANKSEL SSPCON2
btfss SSPCON2,ACKSTAT ; Check ACKSTAT bit , 0 = ACK , 1 = NACK
goto ACK_Return ; Ack = 0 ; EEPROM is Ready
_Ack_Hi ; Ack = 1 ; EEPROM is Busy for Write
call Delay_mS ; Delay 500uS for next Ack polling
call RstartI2C ; Send a Repeat Start to I2C
bcf I2C_Ctrl,0 ; Clear for Write Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
goto _Ack_Polling
ACK_Return
call StopI2C ; Initial STOP Condition
return
;
;**********************************************************
;* Page Write 1 to 8 Bytes to EEPROM
;*
;* Input:
;* - I2C_Ctrl : Control Byte of EEPROM
;* - I2C_Addr : Location of EEPROM
;* - I2C_Page_Buffer: RAM location of Data
;* - I2C_Page_Length : Data length count
;*
;**********************************************************
;
; Send Command for PAGE WRITE :
; Start+ 0xA0 + EE_Address + W_Data 0+ .. + W_Data N + Stop
;
EE_Page_Write
movf I2C_Page_Length,W
btfsc STATUS,Z
return
;
call StartI2C ; Set SSPCON2.SEN
;
bcf I2C_Ctrl,0 ; Clear for Write Command
movf I2C_Ctrl,W ; Send Slave Address to I2C Bus
call Send_Byte
;
movf I2C_Addr,W ; Send out the Command
call Send_Byte
;
movlw I2C_Page_Buffer
movwf FSR
_W1 movf INDF,W
call Send_Byte
incf FSR,F
decfsz I2C_Page_Length,F
goto _W1
;
call StopI2C ; Initial STOP Condition
return
;
;
;*******************************************************
;* Write a Byte to EEPROM
;*
;* Input:
;* - I2C_Ctrl : Control Byte of EEPROM
;* - I2C_Addr : Location of EEPROM
;* - I2C_Data : Data to EEPROM
評論