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 fails, 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:STM8-Use the timing function of TIM2 to make the light flash every 1S
Next article:Using STM8's PWM input to capture and measure infrared code program
Recommended ReadingLatest update time:2024-11-23 15:24
- 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)
- 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
- parallel computing
- Have you ever experienced an unmanned supermarket using RF technology?
- IC open drain output pin Hiz
- NODEMCU-32-S2 development board similar to ESP32-S2-Saola-1
- Learn FPGA from Bruce Lee
- Scrapy crawler proxy connected to Selenium
- Share blog post: PCIE layout and wiring guide
- The future trend of development - customizable PMIC
- Help: About attenuators and phase shifters.
- [MSP430F5529 Review] 7. TFT Color Screen Snake