EEPROM is a memory often used in microcontroller application systems. It is mainly used to save some data that needs to remain unchanged after power failure. In previous microcontroller systems, an EEPROM chip was usually added outside the microcontroller. This method not only increases costs, but also reduces reliability. Now, many microcontroller companies have launched microcontrollers with integrated small-capacity EEPROM, which is convenient to use, reduces costs, and improves reliability.
The STM8 microcontroller chip also integrates EEPROM, with a capacity ranging from 640 bytes to 2K bytes. The most unique feature is that in the STM8 microcontroller, access to the EEPROM is just like regular RAM, which is very convenient. The address space of the EEPROM is uniformly addressed with the memory, starting from 004000H, and the size depends on the different chip models.
For ease of management, define the address of the EEPROM
#define EP_HEADER_ADDR 0x4000
Operating the EEPROM requires initialization, which is FLASH unlocking. Unlocking requires two keys, and the order cannot be reversed.
void EEPROM_init(void)
{
do
{
FLASH_CR1=0x00;
FLASH_CR2=0x00;
FLASH_NCR2=0xFF;
FLASH_DUKR = 0xAE; // Write the first key
FLASH_DUKR = 0x56; // Write the second key
} while((FLASH_IAPSR & 0x08) == 0); // If unlocking is unsuccessful, try again
}
There is also a locking function. Generally speaking, lock protection is required after operating the EEPROM.
void EEPROM_lock(void)
{
FLASH_IAPSR=(u8)(~0x08); //Relock
}
Next are two basic operations of reading and writing.
//Write a data to the specified address of EEPROM addr: relative address dat: data
void EEPROM_write(unsigned char addr, unsigned char dat)
{
unsigned char *p;
p=(unsigned char *)(EP_HEADER_ADDR + addr);
*p=dat;
while(!(FLASH_IAPSR&0x04)); //Wait for write operation to succeed
}
//Read a data from the specified address of EEPROM addr: relative address
unsigned char EEPROM_read(unsigned char addr)
{
unsigned char *p;
p=( unsigned char *)(EP_HEADER_ADDR + addr);
return *p;
}
The address given here is an absolute address. There is an offset address operation in the function (offset 4000H).
Fried chicken is simple, isn’t it?
Introduction to EEPROM and FLASH: http://blog.csdn.net/yuanlulu/article/details/6163106
Attached is the project of stm8s in IAR environment, including the initialization code of on-chip hardware such as SPI, IIC, PWM, AWU, USART, EEPROM, etc.
http://download.csdn .NET /detail/devintt/9454188
Previous article:How to write the bootloader and app for IAP upgrade of stm8
Next article:stm8s development (eight) use of IIC: IIC host communication!
Recommended ReadingLatest update time:2024-11-16 14:47
- Popular Resources
- Popular amplifiers
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- Monocular semantic map localization for autonomous vehicles
- ICCV2023 Paper Summary: Efficient and Scalable Vision
- Chip Manufacturing: A Practical Tutorial on Semiconductor Process Technology (Sixth Edition)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- 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!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- The New Year is coming soon. What activities and benefits are there in the forum? Is there a year-end bonus? Haha
- [Zero Knowledge ESP8266 Tutorial] Quick Start 26 Blynk Mobile APP Displays Indoor Temperature and Humidity
- Voltage signal amplification and waveform conversion
- Please recommend a single-supply precision dual op amp, millivolt level, preferably in SOP-8 package
- Power failure recovery through power consumption calculation utility
- Master these steps to fully utilize the performance of eFPGA, SoC architects know the selection skills
- [ATmega4809 Curiosity Nano Review] Main Technical Data
- Division of the electromagnetic spectrum
- Why is the DCDC step-down chip easily damaged?
- Setting trial time limit