Example program for reading and writing 24LCxx series EEPROM

Example program for reading and writing 24LCxx series EEPROM

;************************************************* *******
;* *
;* ? PIC16F877A ?I2C Master Home ???????? EEPROM ?绦ㄒ祘Α *
;* *
;* Written by: Richard Yang *
;* Sr. Corporate ApplicaTIon Engineer *
;* Microchip Technology Inc. *
;* Date: Oct. 2nd '2002 *
;* Revision: 1.00 *
;************************************************* *******


;*************************************************
This source code provides a demonstraTIon of the MSSP peripheral
; on the PIC16F87x MCU.
;
;
;*********** The subrouTInes for EEPROM *****************
;
; EE_Random_Read ; EEPROM Random address Read from EEPROM
; EE_SEQU_Read ; EEPROM SequenTIal Read form EEPROM
; EE_Ack_Check ; Polling current status of EEPROM
; EE_Page_Write ; Page Write function for EEPROM
; EE_Byte_Write ; Write a byte to EEPROM with address setting
;
;*********** The subroutines for I2C *****************
;
; Init_I2C_Master ; Initial I2C Module for Master Mode , 7-bit address
; StartI2C ; Send a START Condition !!
; StopI2C ; Send s STOP Condition
; RstartI2C ; Send a Repeat Start conditional to I2C
; Non_Ack ; Send a Non-acknowledge signal to I2C
An_Ack ; Send a acknowledge signal to I2C
; Sebd_Byte ; Send a byte to I2C bus
RecI2C ; Enable Read a byte form slave device
; I2C_Done : Wait the I2C completed the currect process
;
;************************************************* **

List p=16f877a
#i nclude

;
;***************************************
;) 竡 计 ?? RAM???
;
CBLOCK 0x20

I2C_Ctrl
I2C_Addr
I2C_Data
I2C_Page_Length
D_Count
ENDC
Cblock 0x40
I2C_Page_Buffer: 8
I2C_SEQU_Buffer: 8
Endc
;
;***************************************
; ) 竡い耞?

W_temp EQU 0x72
Status_temp EQU 0x73
Pclath_temp EQU 0x74
;
;***************************************
; )竡I2C ???の竲?
;
EEPROM_CMD equ 0xA0 ; Device adress of Slave Point
EE_Read equ .1
EE_Write equ .0

;
#define SCL PORTC,3 ; I2C SCL pin
#define SDA PORTC,4 ; I2C SDA pin

;

;********************************************
; Locates startup code @ the reset vector
;********************************************
Reset_Addr
Org 0x00
Nop
Goto Main_Init
;
Org 0x04
Goto ISR
;
;************************************************* ************
;**** The Start Address of ISR is 0x004
;**** "PUSH" & "POP" ?ㄏノ绦ㄒ: 続ノ? Hook PIC16F877 ΤSHARE BANK ?PIC
;************************************************* *************
ISR
Push movwf w_temp ; save off current W register contents
Movf STATUS,w ; move status register into W register
Movwf status_temp ; save off contents of STATUS register
Movf PCLATH, W
Movwf pclath_temp
;
; Put your interrupt code here
;
Pop movf pclath_temp,W
Movwf PCLATH
Movf status_temp,w ; retrieve copy of STATUS register
Movwf STATUS ; restore pre-isr STATUS register contents
Swapf w_temp,f
Swapf w_temp,w ; restore pre-isr W register contents
;
Retfie ; return from interrupt


;------------------------------------------------- ---------------------

Main_Init
Pagesel Init_I2C_Master ; Set PAGE to PCLATH Register
Call Init_I2C_Master ; Init the MSSP for I2C Master
;
Banksel I2C_Ctrl
Movlw EEPROM_CMD ; Load EEPROM command address @ 0xA0
Movwf I2C_Ctrl
;
;
Main
;
;------------------------------------------------- ---------------------
;
;* 代代刚祘Α穦?盢?计计?(0x40-0x47)粜??RMA0x40-0x47??伫
;* ?ノPage Write?よΑ盢8??じじ?戈?粜 EEPROM?
;* 籼 篈 盎 盎 盎 琌 琌 琌 盎 盎 琌 琌 琌 琌 盎 盎 盎 盎 盎 盎 盎 盎 盎
;*??戈??Sequential Read?よΑ眖EEPROM弄??I2C_SEQU_Bufferい
;

Test_Page_RW
Movlw 0x08
Movwf I2C_Page_Length
Movlw I2C_Page_Buffer
_Fill_RAM movwf FSR
Movwf INDF
Incf FSR,W
Decfsz I2C_Page_Length,F
Goto _Fill_RAM
;
Test_Page_Write
; banksel I2C_Ctrl
Movlw EEPROM_CMD ; Load EEPROM command address @ 0xA0
Movwf I2C_Ctrl
Movww 0x10 ; Select EEPROM location at 0x00
Movwf I2C_Addr
Movlw .8
Movwf I2C_Page_Length
Call EE_Page_Write
;
Call EE_Ack_Check
;
Banksel I2C_Ctrl
Movlw EEPROM_CMD ; Load EEPROM command address @ 0xA0
Movwf I2C_Ctrl
Movlw 0x10
Movwf I2C_Addr
Movlw .8
Movwf I2C_Page_Length
Call EE_SEQU_Read
;
Goto $
;
;------------------------------------------------- --------
;
;* 代代刚祘Α穦?粜????じ舱?戈?粜 EEPROM い
;* ?籼??篈?盎代琌??ЧΘ粜?笆程??盢?粜
;*??戈??眖EEPROM ㄓ?? I2C_Data both?
;
Test_1byte_RW

Banksel I2C_Ctrl
Movlw EEPROM_CMD ; Load EEPROM command address @ 0xA0
Movwf I2C_Ctrl
Movlw 0x00 ; Select EEPROM location at 0x00
Movwf I2C_Addr
Movlw 0xAA ; Write data 0x5A to location 0x00 of EEPROM
Movwf I2C_Data
Call EE_Byte_Write
;
Test2 call EE_Ack_Check ; Polling Acknowledge for next access
;
Banksel I2C_Data
Movlw 0x00 ; Clear I2C data buffer
Movwf I2C_Data
;
Test3
Banksel I2C_Ctrl
Movlw EEPROM_CMD ; Load EEPROM command address @ 0xA0
Movwf I2C_Ctrl
Movlw 0x00
Movwf I2C_Addr
Call EE_Random_Read
;
Goto $
;

;
;************************************************* *****
;* 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
;*
;************************************************* ******
;
; Send Command for BYTE WRITE :
; " Start+ 0xA0 + EE_Address + Write_Data + Stop "
;
EE_Byte_Write
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
;
Movf I2C_Data,W ; Send out the Data
Call Send_Byte
;
Call StopI2C ; Initial STOP Condition
Return
;
;************************************************* *********************
;
As following subroutines perform commonly used I2C functions.
You can use these subroutines for your I2C access
;
;************************************************* *********************
;
;*** Initial I2Cmodule fot 7-bit I2C Master Mode with 100Kcps @4MHz
;
Init_I2C_Master
BANKSEL TRISC ; Initial PortC, bit 3 & 4 as Input
Bsf SCL ; RC3 = SCL , RC4 = SDA
Bsf SDA
;
BANKSEL PORTC
Bsf SCL ; SCL = Hi
Bsf SDA ; SDA = Hi
;
Movlw b'00101000' ; I2C Master Mode, Clock Rate: FOSC/(4*SSPADD+1)
Movwf SSPCON ;
;
Banksel SSPADD
Movlw .9 ; This gives 100KHz I2C clock @ 4MHz
Movwf SSPADD ; (4MHz/4) / (9+1)= 100KHz
;
Movlw b'10000000' ; Disable slew rate control,
Movwf SSPSTAT ; and clear status bits
;
Movlw b'00000000' ; Set SCL, SDA into Ready status
Movwf SSPCON2
;
Return
;
;****** Send START condition to bus *******
;
StartI2C ; Initiate the I2C START condition.
Banksel SSPCON2
Bsf SSPCON2, SEN
Goto I2C_Done
;
;****** Send STOP condition to bus *******
;
StopI2C
Banksel SSPCON2
Bsf SSPCON2, PEN
Goto I2C_Done
;
;****** Send RESTART condition to bus *******
;
RstartI2C
Banksel SSPCON2
Bsf SSPCON2, RSEN
Goto I2C_Done
;
;****** Send a Non-Acknowledge status to bus (ACK=1) *******
;
Non_Ack
Banksel SSPCON2
Bsf SSPCON2, ACKDT; Set the ACK bit
Bsf SSPCON2,ACKEN ; Initiate the NACK sequence.
Goto I2C_Done
;
;****** Send an Acknowledge status to bus (ACK=0) *******
;
An_Ack
Banksel SSPCON2
Bcf SSPCON2, ACKDT ; Clear the ACK bit
Bsf SSPCON2,ACKEN ; Initiate the NACK sequence.
Goto I2C_Done
;
;****** Send data to I2C bus from Wreg. *******
;
Send_Byte
Banksel SSPBUF
Movwf SSPBUF
Goto I2C_Done
;
;****** Enable I2C Receive for Master Mode *******
;
RecI2C
Banksel SSPCON2
Bsf SSPCON2, RCEN ; Set the receive enable bit.
Goto I2C_Done
;
;****** Check the I2C stage is completed ********
;
I2C_Done
Banksel PIR1
Btfss PIR1, SSPIF ; Poll for SSPIF
Goto $-1
Bcf PIR1, SSPIF
Return
;
Delay_mS
Banksel D_Count
Movlw .124 ; Load .5mS Dealy Value
Movwf D_Count
_D_mS nop
Decfsz D_Count,F
Goto _D_mS
Return
;
; *********** End Of Program !!!!!
;
End

Fuse holder

Fuse Holder,Car Fuse Holder,Maxi Fuse Holder ,Fuse Tap

Dongguan Andu Electronic Co., Ltd. , https://www.idofuseholder.com