The file system program source code is as follows:
Keywords:AT24C512
Reference address:Systematic Management of Data Files in AT24C512
. Common subroutines:
RD24C: Read the contents of the AT24C512 cell pointed to by DPTR and put it into the RAM cell pointed to by (R1, R0). R6 is the data length.
WR24C: Write the contents of the RAM cell pointed to by (R1, R0) to the AT24C512 cell pointed to by DPTR, R6 is the data length
WR_ONE: Write the contents of the RAM unit pointed to by DPTR to the AT24C512 unit pointed to by DPTR
TZDPTR: MUL AB; DPTR=DPTR+A*B
ADD A, DPL
MOV DPL, A
MOV A, DPH
ADDC A, B
MOV DPH, A
RET
AT24C512 system formatting: Initialize the directory area and page allocation area to 0EEEEH
AT_INIT: MOV DPTR, #AT24C
MOV R7, #80H
MOV A, #0EEH
ATINIT1:MOVX @DPTR, A
INC DPTR
DJNZ R7, ATINIT1
MOV R7, #54
ATINIT2:MOV DPTR, #AT24C
MOV R1, DPH
MOV R0, DPL
MOV R6, #80H
MOV A, R7
MOV B, #80H
MOV DPTR, #0
LCALL TZDPTR
PUSH 7
LCALL WR24C
POP 7
DJNZ R7, ATINIT2
RET
For ease of operation, the contents of the directory area and the page allocation area are read into the same address of RAM each time the computer is turned on. The procedure is as follows:
R_HEAD:CLR IE.7
MOV R5, #0
RHEAD1:MOV A, R5
MOV B, #80H
MOV DPTR, #0
LCALL TZDPTR
MOV R1, DPH
MOV R0, DPL
MOV R6, #80H
PUSH 5
LCALL RD24C
POP 5
INC R5
CJNE R5, #54, RHEAD1
SETB IE.7
RET
The procedure for adding files is as follows:
YB_SAVE: MOV PAGE_NO,#0 AGES=number of pages required
MOV DPTR, #AT_FAT AGE_NO is the number of free pages
YBSAVE1: MOVX A, @DPTR ; AT_FAT is the starting address of the page allocation table
CJNE A, #0EEH, YBSAVE2
INC PAGE_NO
YBSAVE2: INC DPTR
INC DPTR
MOV A, PAGE_NO
CLR C
SUBB A, PAGES
JZ YBSAVE4
MOV A, DPH
CJNE A, #1BH, YBSAVE1; 1B00H is the end address of the page allocation table
YBSAVE3: LCALL ERRDSP; Display error message
RET
YBSAVE4: MOV R7, #0
YBSAVE5: MOV DPTR, #YB_BEG; YB_BEG is the starting address of the directory area
MOV A, R7
MOV B, #10
LCALL TZDPTR
MOVX A, @DPTR
CJNE A, #0EEH, YBSAVE16
SJMP YBSAVE6
YBSAVE16: INC R7
CJNE R7, #153, YBSAVE5; can store up to 153 files
SJMP YBSAVE3 ; Already full
YBSAVE6: CLR IE.7
PUSH 7; save the sequence number of the free directory area found
LCALL DO_DSP ; Display "Storing"
MOV DPTR, #AT_FAT
YBSAVE7: MOVX A, @DPTR
CJNE A, #0EEH, YBSAVE8
SJMP YBSAVE9
YBSAVE8: INC DPTR
INC DPTR
SJMP YBSAVE7
YBSAVE9: PUSH DPH; find the first free page and protect the address
PUSH DPL
MOV R1, DPH
MOV R0, DPL
MOV A, #0FFH
MOVX @DPTR, A
INC DPTR
MOVX @DPTR, A ; temporarily treat it as the last page of the file
PUSH DPH
PUSH DPL
PUSH 0
PUSH 1
LCALL WR_ONE
POP 1
POP 0
POP DPL
POP DPH
INC DPTR
MOV R7, PAGES
DEC R7
MOV A, R7
JZ YBSAVE13; If only one page is needed, the page allocation table modification ends
YBSAVE10: MOVX A, @DPTR
CJNE A, #0EEH, YBSAVE11
PUSH DPH
PUSH DPL
MOV DPH, R1; R1, R0 is the page allocation table address of the previous page
MOV DPL, R0
POP 0
POP 1
MOV A, R1
MOVX @DPTR, A
INC DPTR
MOV A, R0
MOVX @DPTR, A ;Write the new address to the address unit of the previous page
PUSH 0
PUSH 1
PUSH 7
LCALL WR_ONE; write the modification information to AT24C512
POP 7
POP 1
POP 0
MOV DPH, R1
MOV DPL, R0
INC DPTR
INC DPTR
SJMP YBSAVE12
YBSAVE11: INC DPTR
INC DPTR
YBSAVE12: DJNZ R7, YBSAVE10
MOV DPH, R1
MOV DPL, R0
MOV A, #0FFH
MOVX @DPTR, A
INC DPTR
MOVX @DPTR, A ; write 0FFFFH, indicating the end of file allocation
LCALL WR_ONE
YBSAVE13: POP 0
POP 1
POP 0
POP 1; R1R0 = DPTR = the first free page found
POP ACC; ACC=R7=free directory area number
PUSH ACC
MOV DPTR, #YB_BEG+8
MOV B, #10
LCALL TZDPTR ; Determine the address of the directory free area
MOV A,R1
Previous article:NAND FLASH sector management
Next article:Using Flash-based MCUs to store user data
- Popular Resources
- Popular amplifiers
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
MoreDaily News
- New breakthrough! Ultra-fast memory accelerates Intel Xeon 6-core processors
- New breakthrough! Ultra-fast memory accelerates Intel Xeon 6-core processors
- Consolidating vRAN sites onto a single server helps operators reduce total cost of ownership
- Consolidating vRAN sites onto a single server helps operators reduce total cost of ownership
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
Guess you like
- Ended | Live broadcast [Cloud-based secure authentication | Microchip embedded security solutions]
- ST60 millimeter wave water medium data transmission test report
- Schematic diagram of a simple TWS charging box consisting of a common LTH7 charging chip + lithium battery boost and protection
- Please tell me, how to make a dual power automatic switching circuit?
- Decimal to hexadecimal C language function
- Simple driver for 64Mb PSRAM
- How to tell how many layers a PCB has
- MCU drives 5V relay circuit
- [Sipeed LicheeRV 86 Panel Review] II. GPIO pins are brought out, RGB tri-color LED flashes
- [MM32 eMiniBoard Review] + Display driver for color OLED screen