24CXX series reading and writing program (EMC instruction version)

Publisher:Tianyun2021Latest update time:2014-12-18 Source: laogu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
;******************************************
;** 24CXX interface I2C bus read/write program
;** (All timings are based on 4MHZ crystal oscillator frequency)
;******************************************
/*
;********************************
;Random read/write test program (demonstration program)
;****************************
TEST:
MOV A,@0XAE ;A2=A1=A0=1
MOV SLAVE_24,A ;Set device code
MOV A,@2 ;R/W LOC. = 2
MOV ADDR_24,A ;Set address code
MOV A,@55 ;
MOV DATA_24,A ;Write 55 to E2PROM
CALL WRBYTE ;Write a byte
CALL DL10MS ;Delay 10MS, wait for the write operation to complete (note, this statement is very important)
CALL RDBYTE ;Read back the original data
MOV A,@55 ;
XOR A,DATA_24 ;
JBS R3,Z ;Read data = write data jump
WRONG:
JMP WRONG ;Verification failed
CORRECT:
JMP CORRECT ;Verification passed
DL10MS:
RET
;****************************
;Memory definition
;****************************
PROT_I2C EQU 0X05 ;I2C operation port
SDA EQU 3 ;Data pin
SCL EQU 2 ;Clock pin

ADDR_24 EQU 0X1B ;Address register
DATA_24 EQU 0X1C ;Write/read from I2C data register
SLAVE_24 EQU 0X1D ;Slave device address register (1010XXX0)
DBUF_24 EQU 0X1E ;Send/receive from SDA port data buffer
COUNT_24 EQU 0X1F ;Bit counter
TT_24 EQU 0X20 ;Flag register
REND_24 EQU 1 ;Read completion flag (0=not completed, 1=completed)
NUM_24 EQU 0X21 ;Page write/page read byte number

*/
;****************************
;Macro definition
;****************************
;=================
;Set SCL, SDA as output port
;================
SDA_OUT MACRO
MOV A,@0B00000001
IOW PROT_I2C
ENDM
;=================
;Set SCL as output port, SDA as input port
;================
SDA_IN MACRO
MOV A,@0B00001001
IOW PROT_I2C
ENDM
;****************************
;Byte write program
;Function: Write a byte to the EEPROM device
;Entry: DATA_24 = data to be written
; ADDR_24 = data address
; SLAVE_24 = slave device address 1010XXX0)
;****************************
WRBYTE:
MOV A,SLAVE_24
MOV DBUF_24,A
CALL BSTART ;Send start bit
CALL TX ;Send slave device address and detect response signal
MOV A,ADDR_24
MOV DBUF_24,A
CALL TX ;Send data address and detect response signal
MOV A,DATA_24
MOV DBUF_24,A
CALL TX ;Send data and detect response signal
CALL BSTOP ;Send stop bit
RET
;****************************
;Byte read program
;Function: Read a byte from the EEPROM device
;Entry: ADDR_24 = data address
; SLAVE_24 = slave device address (1010XXX0)
; export: DATA_24 = read data
; ******************************
RDBYTE:
MOV A,SLAVE_24
MOV DBUF_24,A
CALL BSTART; send start bit
CALL TX; send slave device address and detect response signal
MOV A,ADDR_24
MOV DBUF_24,A
CALL TX ; send data address and detect response signal
; enter read state
CALL BSTART ; send start bit
MOV A,SLAVE_24
MOV DBUF_24,A
BS DBUF_24,0
CALL TX ; send slave device address and detect response signal
BS TT_24,REND_24
CALL RX ; read data and send response signal
CALL BSTOP ; send stop bit
MOV A,DBUF_24
MOV DATA_24,A ; save data to DATA_24
RET
;****************************
; page write procedure
; function: write a byte to EEPROM device
; entry: R4 = the first address of the data to be written in RAM
; NUM_24 = number of bytes
; ADDR_24 = data address
; SLAVE_24 = slave device address 1010XXX0)
;****************************
WRPAGE:
MOV A,SLAVE_24
MOV DBUF_24,A
CALL BSTART ; send start bit
CALL TX ; send slave device address and detect response signal
MOV A,ADDR_24
MOV DBUF_24,A
CALL TX ; send data address and detect response signal
WRPAGE1:
MOV A,R0
MOV DBUF_24,A
CALL TX ; send data and detect response signal
INC R4
DJZ NUM_24
JMP WRPAGE1 ; continue before writing
CALL BSTOP ; send stop bit
RET
;**************************** ; page read program ; function: read a byte from EEPROM device ; entry: ADDR_24 = data address ; SLAVE_24 = slave device address (1010XXX0) ; NUM_24 = number of bytes ; exit: R4 = the first address of the read data stored in RAM ;****************************
RDPAGE : MOV A,SLAVE_24 MOV DBUF_24,A CALL BSTART ; send start bit CALL TX ;Send slave device address and detect response signal MOV A,ADDR_24 MOV DBUF_24,A CALL TX ;Send data address and detect response signal ;Enter read state CALL BSTART ;Send start bit MOV A,SLAVE_24 MOV DBUF_24,A BS DBUF_24,0 CALL TX ;Send slave device address and detect response signal RDPAGE1: MOV A,@1 XOR A,NUM_24 BS TT_24,REND_24 JBS R3,Z ;NUM_24=1 jump;Judge whether it is the last byte and determine the response signal BC TT_24,REND_24 CALL RX ;Read data and send response signal MOV A,DBUF_24 MOV R0,A ;Save data to R0 INC R4 DJZ NUM_24 JMP RDPAGE1 ;Continue if reading is not completed CALL BSTOP ;Send stop bit RET ;**************************** ;I2C operation subroutine (timing) collection ;**************************** ;================= ;Send start bit program ;Description: When SCL=1, send a falling edge to SDA ;================= BSTART: SDA_OUT ;Set SDA as output port BS PROT_I2C,SDA ;SDA=1 NOP ;Delay 0.6US BS PROT_I2C,SCL ;SCL=1 NOP ;Delay 0.6US BC PROT_I2C,SDA ;SDA=0 NOP ;Delay 0.6US BC PROT_I2C,SCL ;SCL=0 NOP ;Delay 0.6US RET ;================= ;Send end bit program ;Description: When SCL=1, send a rising edge to SDA ; ================== BSTOP: SDA_OUT ; Set SDA to output port BC PROT_I2C,SDA ; SDA=0 NOP ; Delay 0.6US




























































BS PROT_I2C,SCL ;SCL=1
NOP ;Delay 0.6US
BS PROT_I2C,SDA ;SDA=1
NOP ;Delay 0.6US
RET
;=================
;Bit input subroutine
;Description: When SCL=1, read the level from SDA
;Export: C=bit value read from SDA
;================
BITIN:
SDA_IN ;Set SDA as input port
BS PROT_I2C,SDA ;SDA=1
NOP ;Delay 0.6US
BS PROT_I2C,SCL ;SCL=1
NOP ;Delay 0.6US
BC R3,C
JBC PROT_I2C,SDA
BS R3,C ;C=SDA
NOP ;Delay 0.6US
BC PROT_I2C,SCL ;SCL=0
NOP ;Delay 0.6US
RET
;=================
;Bit output subroutine
;Description: Whenever SCL=0, rewrite the level on SDA
;Entry: C=bit value to be written to SDA
;=================
BITOUT:
SDA_OUT ;Set SDA as output port
JBS R3,C
JMP BIT0
BS PROT_I2C,SDA ;SDA=C=1
JMP CLK1
BIT0:
BC PROT_I2C,SDA ;SDA=C=0
CLK1:
NOP ;Delay 0.6US
BS PROT_I2C,SCL ;SCL=1
BIT2:
NOP
NOP
BC PROT_I2C,SCL ;SCL=0
RET
;=============================
;Receive data subroutine
; Entry: TT_24.REND_24 = Read completion flag
; Exit: DBUF_24 = Received data (8_BIT)
;=============================
RX:
MOV A,@8 ; Number of loops = 8
MOV COUNT_24,A
CLR DBUF_24
RXLP:
CALL BITIN ; Input 1_BIT
RLC DBUF_24 ; Shift left (with C)
DJZ COUNT_24 ; End of loop?
JMP RXLP
; Set the acknowledge signal bit, if the reading is completed, send 1 (NO_ACK) to stop receiving, otherwise send 0 (ACK) to continue receiving
BS R3,C
JBS TT_24,REND_24 ; Read completion signal->C
BC R3,C
CALL BITOUT ;Response
RET
;=============================
;Send data subroutine
;Entry: DBUF_24 =data to be sent (8_BIT)
;=============================
TX:
MOV A,@8 ;Number of loops = 8
MOV COUNT_24,A
TXLP:
RLC DBUF_24 ;Left shift (with C)
CALL BITOUT ;Output 1_BIT
DJZ COUNT_24 ;End of loop?
JMP TXLP
CALL BITIN ;Read response signal
RET

;****************************
;I2C bus read/write procedures are all over!
;****************************
Reference address:24CXX series reading and writing program (EMC instruction version)

Previous article:Misunderstandings and skills in the application of EMC8BIT microcontroller instructions
Next article:Microcontroller simulation of I2C bus and AT24C01 application examples

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号