C51io Port analog I²c bus driver AT24C16 (EEPROM part)

Source: Internet
Author: User
Tags rollback

/*
Name: C51io Port analog i²c bus driver AT24C16
Description: About EEPROM, that is, the AT24C16 here is a special form of flash memory, but its capacity is generally less. More suitable for storing small amounts of data.

The AT24C16 communication interface is the standard i²c communication, which means that we need to manipulate the EEPROM device according to the I²C communication protocol. About the various operations of AT24C16, here is not detailed, a brief introduction.

(1), the host writes a byte to the AT24C16: first need to send the device address, and then send the memory address that needs to be accessed. The data to be written is then sent. The creation of signals such as start, end, and confirmation are omitted here.

(2), the specified page writes n bytes: and (1) The basic operation is very similar. The difference is that n data can be written consecutively. The small thing to note here is that if the data being written reaches the page boundary (that is, n more than 16 bytes) A rollback is generated, that is, it is written back from the beginning of the page, which may overwrite the original data. The implementation of the specified page of this program is written from the beginning of the page, and of course it is possible to write data from any address, although it is important to note the page boundary problem.

(3), the host randomly reads a byte from the AT24C16: Here the operation Steps first the host writes the device address to the AT24C16 and the memory address to be accessed (this is also called the mute write operation: In order to AT24C16 load the random address). Then re-initiates the read operation, and finally receives the data from the host at the specified address transmitted by AT24C16

(4), specify the address sequence read N bytes: Here the operation and (3) is similar, the difference is to read out at the same time can read n data.

One thing to note here: In my experiment, the continuous reading of n data does not result in a rollback when the page boundary is reached, where n can be larger than the page size. In other words, the continuous read operation address automatically crosses the page boundary. However, if the boundary of the address is reached, it should be rolled back. (I'm not sure about that, the general data shows that the arrival page boundary also produces a rollback, but my experiment did not produce a rollback when I read it continuously).

The

Finally says here that the AT24C16 memory is 128 pages (number of pages) * 16 bytes (page size). So the device address structure of 1-3 bits also as the page address of the high 3 bits, and then the memory address of the high 4 bits as the page address of the low 4 bits, together just 7 bits, can access 128 pages. The low 4 bits of the memory address are then offset by the page, which can access 16 bytes of content.
*/

At24c16 writes a byte (int in Keil is 2 bytes, here only 11 valid data bits) int at24c16_bytewrite (unsigned int addr,uchar _data) {bit ret_val = 0;     Uchar high_addr = (Uchar) (addr>>8);                High 8-bit address uchar low_addr = (uchar) addr; Low 8-bit address uchar dev_addr = 0xA0 |      ((high_addr&0x0f) <<1);
    Compose the device address, which includes 4-6 bits is the page address, if (Addr > Addrmax) {return outofaddr;

    }//Open i²c Communication start_i2c ();

    Send Device Address Ret_val = SENDBYTE_I2C (DEV_ADDR);

        if (ret_val! = 0) return ackerror;

    Send the address to be accessed Ret_val = SENDBYTE_I2C (LOW_ADDR);

        if (ret_val! = 0) return ackerror;

    Send the address to be accessed Ret_val = SENDBYTE_I2C (_data);

    if (ret_val! = 0) return ackerror;

    Stop bus stop_i2c ();           Delay_ms (10);

Delay a period of time, wait for the write operation to complete return SEND_OK; }//Specify page write n bytes data (n<16) int at24c16_pagewrite (Uchar page,uchar* p,uchar N) {Uchar dev_addr;//device Address Uchar Low
    _ADDR;
    Uchar High_addr;
    Uchar tmp = 0; BiT Ret_val; if ((n > 16) | (
    Page > 128))//depending on the read-write device, the number of pages and bytes per page {return Outofrang; } high_addr = (page) >> 4;     Arrive at the top of the page address low_addr = ((page & 0x0F) <<4); Get the last four-bit page address, which makes up the storage address of the high four-bit dev_addr = 0xa0 |

    ((High_addr & 0x0F) << 1);

    Turn on the i²c Communication START_I2C ();

     Send Device Address Ret_val = SENDBYTE_I2C (DEV_ADDR);

        if (ret_val! = 0) return ackerror;
     Send the storage address, starting at the top of the page Ret_val = SENDBYTE_I2C (LOW_ADDR);

        if (ret_val! = 0) return ackerror;           Delay_ms (10);
         Delay for a period of time, waiting for the write operation to complete while (n--) {ret_val = SENDBYTE_I2C (*p);
        p++;

        if (ret_val! = 0) return ackerror;           Delay_ms (10);
     Delay a period of time, waiting for the write operation to complete}//Stop I²c Communication stop_i2c ();          Delay_ms (10);

Delay a period of time, wait for the write operation to complete return SEND_OK;
    }//AT24C16 randomly reads a byte uchar at24c16_randomread (unsigned int addr) {Uchar dev_addr;//device address Uchar low_addr; Uchar HIGH_ADDR;
    Uchar tmp = 0;

    Bit ret_val = 0;
    Low_addr = (uchar) addr;
    High_addr = (Uchar) (addr>>8); Dev_addr = 0xA0 |

    ((high_addr&0x0f) <<1);
    if (Addr > Addrmax) {return outofaddr;

    }//Open i²c Communication start_i2c ();
     Send Device Address Ret_val = SENDBYTE_I2C (DEV_ADDR);

        if (ret_val! = 0) return ackerror;
     Send memory address Ret_val = SENDBYTE_I2C (LOW_ADDR);



    if (ret_val! = 0) return ackerror;
    Re-open the I²c communication start_i2c (); Dev_addr = 0xa1 |        ((high_addr&0x0f) <<1);
     Regenerate the device address, this time the read operation//send device Address Ret_val = SENDBYTE_I2C (DEV_ADDR);


    if (ret_val! = 0) return ackerror;

    Read a byte of tmp = RECBYTE_I2C () from I²c;             ACK_I2C (1);

    Send non-answer bit//send stop bit stop_i2c ();

return TMP; }//Specify the address order to read N bytes: (note may reach the final address boundary, resulting in rollback) int at24c16_sequentialread (unsigned int addr,int N,uchar * p) {Uchar de V_ADDR;
        Device Address Uchar low_addr; UchaR High_addr;
        Uchar tmp = 0;


    Bit ret_val = 0;
    if ((Addr > Addrmax))//depending on the read-write device, the number of pages and bytes per page {return outofaddr;      } high_addr = (Uchar) (addr>>8);             High 8-bit address low_addr = (uchar) addr; Low 8-bit address dev_addr = 0xA0 |       ((high_addr&0x0f) <<1);


    Compose the device address, which includes 4-6 bits is the page address,//Open i²c Communication start_i2c ();
     Mute write operation, let memory load device address and storage address//Send device Address Ret_val = SENDBYTE_I2C (DEV_ADDR);

        if (ret_val! = 0) return ackerror;
     Send the storage address, starting at the top of the page Ret_val = SENDBYTE_I2C (LOW_ADDR);

    if (ret_val! = 0) return ackerror;
    Re-open the I²c communication start_i2c (); Dev_addr = 0xa1 |        ((high_addr&0x0f) <<1);
     Compose the device address, which includes 4-6 bits is the page address,//Resend device address, this time the purpose is to read operation Ret_val = SENDBYTE_I2C (DEV_ADDR);


    if (ret_val! = 0) return ackerror;
        Start read operation while (n--) {*p = RECBYTE_I2C ();

        p++;             if (n>0) ack_i2c (0); After the data is not accepted, the sending shouldThe answer is else ack_i2c (1);

    Data received, send non-answer bit} stop_i2c ();

return REV_OK; }

Specific full driver source code: Can download the driver download address for downloading

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.