EEPROM read and write examples

Publisher:meilidaowlLatest update time:2016-10-19 Source: eefocusKeywords:EEPROM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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

Previous article:Simple electronic metronome made with M8
Next article:A concise frequency counter and clock program

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号