当前位置:网站首页>MCU: EEPROM multi byte read / write operation sequence

MCU: EEPROM multi byte read / write operation sequence

2022-06-13 03:54:00 DC-STDIO


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 .

原网站

版权声明
本文为[DC-STDIO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130337493162.html