Almost all AVR microcontrollers are equipped with Flash ROM, SRAM, and EEPROM memories. Flash ROM is a program memory that is not afraid of power failure; SRAM is a static data memory that loses data after power failure; EEPROM is an electrically erasable memory that is not afraid of power failure and is generally used to store data that needs to be modified frequently.
There are generally two ways to erase and write EEPROM. One is to erase and write the EEPROM with the microcontroller's own program when the microcontroller is running normally, and the other is to use a programmer to erase and write the EEPROM independently.
1: The following is an example of erasing the EEPROM by the microcontroller's own program when the microcontroller is running normally: '
EEPROM reading and writing examples'www.avrdiy.com 'BASCOM-AVR1.11.8.1 'Program function, Press the INT0 button, then the PC5 port will output a 0.5Hz square wave, ' Press the INT0 button again, then the PC5 port will output a 0.2Hz square wave, ' The output frequency of PC5 after restarting is the same as before restarting '------------------------------------------------------------------------------- $regfile = "m88def.dat" $crystal = 1000000 Config Int0 = Falling 'Define falling edge to trigger INT0 interrupt Ddrd.2 = 0 Portd.2 = 1 Enable Interrupts 'Turn on global interrupt Enable Int0 'Turn on INT0 interrupt On Int0 Int0_ok 'Interrupt INT0 entry '------------------------------------------------------------------------------- 'Define variable A as byte type and store it in EEPROM. The storage location is automatically arranged by the compiler. 'The main program does not assign a value to A, so the data of A may be 255 when it is run for the first time. Dim A As Eram Byte Dim B As Byte '------------------------------------------------------------------------------- Ddrc.5 = 1 Portc.5 = 0 B = A Do Waitms B Portc.5 = Not Portc.5 'Invert operation Loop End '------------------------------------------------------------------------------- Int0_ok: If A = 100 Then A = 250 Else A = 100 End If B = A Return The above program can also be rewritten as follows: 'EEPROM reading and writing example'www.avrdiy.com ' BASCOM-AVR1.11.8.1 $regfile = "m88def.dat" $crystal = 1000000 Config Int0 = Falling 'Define falling edge to trigger INT0 interrupt Ddrd.2 = 0 Portd.2 = 1 Enable Interrupts 'Turn on global interrupt Enable Int0 'Turn on INT0 interrupt On Int0 Int0_ok 'Interrupt INT0 entry '------------------------------------------------------------------------------ Dim A As Byte '------------------------------------------------------------------------------- Ddrc.5 = 1 Portc.5 = 0 Readeeprom A,2 'Read the value of EEPROM address 2 and assign it to A Do Waitms A Portc.5 = Not Portc.5 'Invert operation Loop End '------------------------------------------------------------------------------- Int0_ok: If A = 100 Then A = 250 Else A = 100 End If Writeeeprom A,2 'Save the value of A in address 2 of EEPROM Return
2: When using the programmer to perform independent write operations on the EEPROM, you must first have an EEP file ready to be written. BASCOM-AVR can generate such a file. The program is as follows:
'EEPROM reading and writing examples
' www.avrdiy.com
'BASCOM-AVR1.11.8.1
$regfile = "m8515.dat"
$crystal = 1000000
Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7 , E = Portb.2 , Rs = Portb.3
Config Lcd = 16 * 2
Dim A(9) As Byte
Dim I As Byte
Cls
Waitms 100
For I = 3 To 7
Readeeprom A(i) , I 'Read data from address I of the EEPROM and assign it to array A(I)
Lcd A(i) ; "|";
Next I
End
$eeprom 'Tell the compiler that the data in array Shuzu will be stored in EEPROM
$eepromhex 'Ask the compiler to generate a hex file suitable for burning EEPROM
Shuzu:
Data 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
In the above program, the LCD display result is 4|5|6|7|8|
Keywords:EEPROM
Reference address:EEPROM read and write examples
There are generally two ways to erase and write EEPROM. One is to erase and write the EEPROM with the microcontroller's own program when the microcontroller is running normally, and the other is to use a programmer to erase and write the EEPROM independently.
1: The following is an example of erasing the EEPROM by the microcontroller's own program when the microcontroller is running normally: '
EEPROM reading and writing examples'www.avrdiy.com 'BASCOM-AVR1.11.8.1 'Program function, Press the INT0 button, then the PC5 port will output a 0.5Hz square wave, ' Press the INT0 button again, then the PC5 port will output a 0.2Hz square wave, ' The output frequency of PC5 after restarting is the same as before restarting '------------------------------------------------------------------------------- $regfile = "m88def.dat" $crystal = 1000000 Config Int0 = Falling 'Define falling edge to trigger INT0 interrupt Ddrd.2 = 0 Portd.2 = 1 Enable Interrupts 'Turn on global interrupt Enable Int0 'Turn on INT0 interrupt On Int0 Int0_ok 'Interrupt INT0 entry '------------------------------------------------------------------------------- 'Define variable A as byte type and store it in EEPROM. The storage location is automatically arranged by the compiler. 'The main program does not assign a value to A, so the data of A may be 255 when it is run for the first time. Dim A As Eram Byte Dim B As Byte '------------------------------------------------------------------------------- Ddrc.5 = 1 Portc.5 = 0 B = A Do Waitms B Portc.5 = Not Portc.5 'Invert operation Loop End '------------------------------------------------------------------------------- Int0_ok: If A = 100 Then A = 250 Else A = 100 End If B = A Return The above program can also be rewritten as follows: 'EEPROM reading and writing example'www.avrdiy.com ' BASCOM-AVR1.11.8.1 $regfile = "m88def.dat" $crystal = 1000000 Config Int0 = Falling 'Define falling edge to trigger INT0 interrupt Ddrd.2 = 0 Portd.2 = 1 Enable Interrupts 'Turn on global interrupt Enable Int0 'Turn on INT0 interrupt On Int0 Int0_ok 'Interrupt INT0 entry '------------------------------------------------------------------------------ Dim A As Byte '------------------------------------------------------------------------------- Ddrc.5 = 1 Portc.5 = 0 Readeeprom A,2 'Read the value of EEPROM address 2 and assign it to A Do Waitms A Portc.5 = Not Portc.5 'Invert operation Loop End '------------------------------------------------------------------------------- Int0_ok: If A = 100 Then A = 250 Else A = 100 End If Writeeeprom A,2 'Save the value of A in address 2 of EEPROM Return
2: When using the programmer to perform independent write operations on the EEPROM, you must first have an EEP file ready to be written. BASCOM-AVR can generate such a file. The program is as follows:
'EEPROM reading and writing examples
' www.avrdiy.com
'BASCOM-AVR1.11.8.1
$regfile = "m8515.dat"
$crystal = 1000000
Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7 , E = Portb.2 , Rs = Portb.3
Config Lcd = 16 * 2
Dim A(9) As Byte
Dim I As Byte
Cls
Waitms 100
For I = 3 To 7
Readeeprom A(i) , I 'Read data from address I of the EEPROM and assign it to array A(I)
Lcd A(i) ; "|";
Next I
End
$eeprom 'Tell the compiler that the data in array Shuzu will be stored in EEPROM
$eepromhex 'Ask the compiler to generate a hex file suitable for burning EEPROM
Shuzu:
Data 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
In the above program, the LCD display result is 4|5|6|7|8|
Previous article:Simple electronic metronome made with M8
Next article:A concise frequency counter and clock program
- Popular Resources
- Popular amplifiers
Recommended Content
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
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- P22-009_Butterfly E3106 Cord Board Solution
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
Guess you like
- The list of materials for the 2022 provincial competition has been released. What do you think?
- How to use C2000 CSM
- Tianjin in my eyes in 2020
- 6678 I am debugging the phy network. If I encounter any problems, please help me. Thank you.
- RSL10 Development Board Circuit Overview
- TI DSP C6000 structural knowledge
- LSD-FET430UIF wiring diagram
- Grab the post! A wave of high-quality tutorials is coming, comment and forward the tutorials to get gifts! Cheer for the 2019 National Competition~
- [N32L43X Review] 5. Hardware SPI driver for OLED
- Methods for reducing board size and battery consumption in low voltage H-bridge applications