当前位置:网站首页>MCU: EEPROM multi byte read / write operation sequence
MCU: EEPROM multi byte read / write operation sequence
2022-06-13 03:54:00 【DC-STDIO】
List of articles
EEPROM Multi byte read / write operation timing
We read EEPROM It's very simple when you ,EEPROM According to the time sequence we sent , Directly send out the data , But write EEPROM But it is not so simple . to EEPROM After sending the data , Save it in EEPROM The cache of ,EEPROM The data in the cache must be moved to “ Nonvolatile ” Region , To achieve the effect of power failure without loss . Writing to the nonvolatile area takes some time , Every device is not exactly the same ,ATMEL The company's 24C02 The maximum write time of is no more than 5 ms. In the process of writing to the nonvolatile region ,EEPROM Will no longer respond to our visit , Not only can we not receive our data , We use I2C Standard addressing mode to address ,EEPROM Will not answer , Just like there is no such device on the bus . After the data is written to the nonvolatile area ,EEPROM Return to normal again , I can read and write normally .
Careful classmates , When I read the previous section of the program, I will find that , The code that we wrote the data , In fact, we have to read the reply bit ACK, But when we read the reply bit, we didn't do anything . This is because we only write one byte of data at a time , Wait until the next time you power on , It must have taken much longer than 5 ms, But if we write several bytes in succession , It is necessary to consider the problem of reply bit . After writing a byte , Before writing the next byte , We must wait EEPROM Respond again , Please pay attention to the writing of our program , You can learn .
Before, we knew how much to write .c The convenience of file migration , The procedure in this section is the same as that in the previous section Lcd1602.c Document and I2C.c The files are exactly the same , So this time we just put main.c The document is sent out to everyone , Help us analyze and understand .
But the students can't do this , The students are beginners , A lot of knowledge and skills need more practice to consolidate , Therefore, each program is still recommended to everyone in your Keil Code by code on the software . /I2C.c File program source code **/ ( Omit here , Refer to the code in the previous chapter ) /Lcd1602.c File program source code **/ ( Omit here , Refer to the code in the previous chapter )
/*****************************main.c File program source code ******************************/
#include <reg52.h>
extern void InitLcd1602();
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
extern void I2CStart();
extern void I2CStop();
extern unsigned char I2CReadACK();
extern unsigned char I2CReadNAK();
extern bit I2CWrite(unsigned char dat);
void E2Read(unsigned char *buf, unsigned char addr, unsigned char len);
void E2Write(unsigned char *buf, unsigned char addr, unsigned char len);
void MemToStr(unsigned char *str, unsigned char *src, unsigned char len);
void main(){
unsigned char i;
unsigned char buf[5];
unsigned char str[20];
InitLcd1602(); // Initialize the LCD
E2Read(buf, 0x90, sizeof(buf)); // from E2 Read a piece of data from
MemToStr(str, buf, sizeof(buf)); // Convert to hexadecimal string
LcdShowStr(0, 0, str); // Display on the LCD
for (i=0; i<sizeof(buf); i++){
// Data in turn +1,+2,+3...
buf[i] = buf[i] + 1 + i;
}
E2Write(buf, 0x90, sizeof(buf)); // Write back to E2 in
while(1);
}
/* Converts a piece of memory data to a string in hexadecimal format , str- String pointer ,src- Source data address ,len- Data length */
void MemToStr(unsigned char *str, unsigned char *src, unsigned char len){
unsigned char tmp;
while (len--){
tmp = *src >> 4; // Take height first 4 position
if (tmp <= 9){
// Convert to 0-9 or A-F
*str++ = tmp + '0';
}else{
*str++ = tmp - 10 + 'A';
}
tmp = *src & 0x0F; // Take the lower one 4 position
if (tmp <= 9){
// Convert to 0-9 or A-F
*str++ = tmp + '0';
}else{
*str++ = tmp - 10 + 'A';
}
*str++ = ' '; // Add a space after converting a byte
src++;
}
}
/* E2 Read function ,buf- Data receiving pointer ,addr-E2 Starting address in ,len- Read length */
void E2Read(unsigned char *buf, unsigned char addr, unsigned char len){
do {
// Use the addressing operation to query whether the current read-write operation is available
I2CStart();
if (I2CWrite(0x50<<1)){
// The reply jumps out of the loop , If there is no response, the next query will be made
break;
}
I2CStop();
} while(1);
I2CWrite(addr); // Write starting address
I2CStart(); // Send repeat start signal
I2CWrite((0x50<<1)|0x01); // Addressing device , The following is the read operation
while (len > 1){
// Read continuously len-1 Bytes
*buf++ = I2CReadACK(); // The last byte is preceded by a read operation + The reply
len--;
}
*buf = I2CReadNAK(); // The last byte is the read operation + Non response
I2CStop();
}
/* E2 Write function ,buf- Source data pointer ,addr-E2 Starting address in ,len- Write length */
void E2Write(unsigned char *buf, unsigned char addr, unsigned char len){
while (len--){
do {
// Use the addressing operation to query whether the current read-write operation is available
I2CStart();
if (I2CWrite(0x50<<1)){
// The reply jumps out of the loop , If there is no response, the next query will be made
break;
}
I2CStop();
} while(1);
I2CWrite(addr++); // Write starting address
I2CWrite(*buf++); // Write a byte of data
I2CStop(); // End write operation , To wait for the write to complete
}
}
function MemToStr: You can convert a piece of memory data into a hexadecimal string . Because we started from EEPROM What you read is normal data , and 1602 The LCD receives ASCII Code character , Therefore, if we want to display the data through LCD, we must first go through one-step conversion . The algorithm is very simple , It is to increase the data of each byte 4 Bit and low 4 Bit separation , and 9 Compare , If less than or equal to 9, Then add „0‟ To 0~9 Of ASCII code ; If it is greater than 9, Then subtract it first 10 add „A‟ Can be converted to A~F Of ASCII code .
function E2Read: Before we read , To query whether the current read / write operation is available ,EEPROM Normal response can be carried out . After , Read before the last byte , Give all ACK, And read the last byte , We're going to give a NAK.
function E2Write: Before each write operation , We all need to query and judge the current EEPROM Whether to respond to , Data can only be written after normal response .
边栏推荐
- Multi thread implementation of selling tickets and producers and consumers
- 19 MySQL database optimization methods
- [MySQL] index and transaction
- [test development] installation of test management tool Zen path
- 单片机:红外遥控通信原理
- Cache read / write -- write
- Database object, sequence, view, index
- Oracle built-in functions
- 高等数学(第七版)同济大学 习题1-3 个人解答
- 单片机串口通信原理和控制程序
猜你喜欢
机器人避障系统基础
SQL injection case demonstration and preventive measures
UnionPay commerce - merchant statistics service platform
Tencent cloud instant messaging IM
【测试开发】用例篇
Interpretation of mobile phone private charging protocol
无人机避障四种常见技术中,为何大疆首选双目视觉
[note]vs2015 compilation of masm32 using MASM32 Library
UDP connection map collection
MySQL 8.0 enables remote root user access and solves the problem of you are not allowed to create a user with Grant
随机推荐
USB-IF BC1.2充电协议解读
机器人避障系统基础
[test development] automatic test selenium (I)
Wechat payment configuration
Precautions for stream flow
【测试开发】测试的相关基本概念
Database object, sequence, view, index
footstep
[test development] blog system - LoadRunner performance test (publish blog function benchmark test)
Lambda终结操作查找与匹配anyMatch
Use lodash to merge the values of the same fields in an array object
Alipay open platform
【测试开发】测试管理工具禅道的安装
【測試開發】自動化測試selenium(三)——unittest框架解析
单片机:A/D 差分输入信号
Field * doesn't have a default value problem
Interpretation of mobile phone private charging protocol
Big Five personality learning records
LVS four layer load balancing cluster (3) cluster function classification - HPC
LVS 4 - tier Load Balancing Cluster (3) Cluster Function Classification - HPC