;** 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!
;****************************
Previous article:Misunderstandings and skills in the application of EMC8BIT microcontroller instructions
Next article:Microcontroller simulation of I2C bus and AT24C01 application examples
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Isolated DC-DC power supply module, input and output common ground problem.
- BMS Battery Management System Debugging Guide V1.0
- PCB Star Decoration
- Introduction to TTL level, CMOS level, and RS232 level
- Overcurrent protection of power modules
- The ins and outs of mobile phone chargers,,,,
- RedMonk Programming Language Survey: Python Remains at No. 2
- What is the functional difference between these two capacitors?
- ATmega4809 Curiosity Nano Review Summary
- RF Simulation of Very Small Aperture Terminal Power Amplifier Link