Arduino AT24C02 detailed read and write address bits

AT24C02 detailed read and write address bits

If you just read the AT24C02 document, it is very abstract and difficult to understand. Combined with program and actual communication waveform; To describe.

  • After purchase, GND is connected by default. The address of installing AT24C02 is 0xA0

Sample program (write a character as an example to explain)

/*
 * Wire Library I2C basic reader
 * AT24C02 Read / write operation
 *
*/

#Include < Wire. H > / / to use the Wire library, you need to include the header file
const int16_t I2C_ADDR = 0x50;              // The IIC device address of AT24C02 is 0x50, which can be searched by I2C scanner.
#define button 7

bool buttonState = true; 
/* Description: AT24C02 writes a byte function
 * Parameter: date_wr: value to write
 *      WriteAddr:  Address to write
 * Return value: Communication succeeded: 0 
 *        Communication failure: 1 - > data overflow 2 - > slave receives NACK when sending addtests 
 *                3->NACK 4 - > other errors received while sending data
 */
uint8_t at24c02_write(char data_wr, uint8_t WriteAddr)
{
  /* 1. Start a transmission and set the I2C device address */
  Wire.beginTransmission(I2C_ADDR);
  /* 2. Where to write */
  Wire.write(WriteAddr);
  /* 3. Value to write */
  Wire.write(data_wr);
  /* 4. When an I2C communication is completed, a stop bit is sent by default */
  return Wire.endTransmission();
}
/* Description: AT24C02 reads a byte function
 * Parameter: date_wr: stored pointer to read value
 *      WriteAddr:  Address to read
 * Return value: Communication succeeded: 0  
 *        Communication failure: 1 - > data overflow 2 - > slave receives NACK when sending address 
 *                3->NACK received when sending data 4 - > data not received 5 - > others
 */
uint8_t at24c02_read(char *data_wr, uint8_t ReadAddr)
{
  uint8_t t = 200;
  uint8_t ret = 0;
  /* 1. Start a transmission and set the I2C device address */
  Wire.beginTransmission(I2C_ADDR);

  /* 2. Position to be read out */
  Wire.write(ReadAddr);

  /* 3. Complete an I2C communication and send a start bit (i.e. retransmission code) */
  ret = Wire.endTransmission(false);

  /* 4. Start reading once, set the I2C device address and read one byte of AT24C02 */
  Wire.requestFrom(I2C_ADDR, 1);

  /* 5. Read the value returned by AT24C02, write it to the cache variable after successful reading, and return the failure code if reading fails */
  while (!Wire.available()) 
  {
    t--;
    delay(1); 
    if(t == 0)
    {
      return 1;
    }
  }
  *data_wr= Wire.read(); // receive a byte as character
  return ret;
}

void setup() {
  /* The baud rate of initialization serial port is 115200 */
  Serial.begin(115200); 
  digitalWrite(LED_BUILTIN, LOW);//The board led is used to observe the data transmission state
  pinMode(button,INPUT);
  digitalWrite(button,HIGH);
  /* Initialize the IIC interface. If the address is not written, it will be the main device by default */  
  Wire.begin();
}

void loop() {
 buttonState = digitalRead(button);
    if(buttonState == LOW){
      delay(250);
          if(buttonState == LOW){
            char str0 = 'H';
      uint8_t Write = at24c02_write(str0, 0);
       Serial.println("Trigger!!!");
       Serial.println(Write);
       digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
       delay(2000);
//      char*str2;
//     uint8_t Read = at24c02_read(str2,0);
//      Serial.println(*str2);Serial.println(Read);
    }    
    }
}


IIC communication explanation

To establish communication between hardware, you need to know each other's address.

It's like making a phone call. Before making a phone call, you must know the other person's phone number.

  • Determine the hardware address to access AT24C02 (AT24C02 manual)

Fill in the hardware information of AT24C02 defined above:

const int16_t I2C_ADDR = 0x50;// The IIC device address of AT24C02 is 0x50, which can be accessed through

Why is the address 0x50? If 0x50 is converted to binary, it is 1010 0000
==Analysis: the previous (high-level MSB) 1010 has no explanation. The official data is dead and can't be changed. A2, A1 and A0, why are they customized? The three custom bits should be determined according to the design of the circuit schematic.

  • In schematic 1, if A2, A1 and A0 are connected to GND, then the custom address bits are 000. Add the eighth bit to determine the read-write bit. If it is read, it is 1 and write is 0. This example demonstrates that if you are sure to write data, it is 0, so the address is 1010 0000 and converted to hexadecimal is 0xA0
  • The oscilloscope captures the waveform when the MCU communicates with AT24C02. Purple represents SDA and yellow represents SCL

    Corresponding code:
  /* 1. Start a transmission and set the I2C device address */
  Wire.beginTransmission(I2C_ADDR);
  • If you want to mount 8 AT24C02 devices, you can connect them according to the following IIC addresses: 0x50 - 0x57

  • According to the different combinations of A2, A1 and A0, we can determine how many AT24C02 can be mounted on a bus, so as not to cause address duplication or conflict. Mount up to 8:

    Youdao translation:

equipment/Page address(A2,A1 and A0): A2,A1 and A0 Pin is the device address input
 They are AT24C02 Hard wire. 8 2 K Devices can be addressed on a bus system
(Device addressing will be discussed in detail in the device addressing section). 
AT24C04 use A2 and A1 The input is hard wired, with a total of 4 4 K equipment
 It is processed on a single bus system. A0 The pin is unconnected and can be connected to GND. 
AT24C08 Use only A2 The input is hard wired, and there are two 8 K equipment
 Addressing on a single bus system. A0 and A1 The pin is not connected and can be connected
GND. 
AT24C16 Device address pins are not used, which limits the number of individual devices on the bus
. A0,A1 and A2 The pin is not connected and can be connected to ground.

In the second stage, write the data address bit.

For example, express delivery is not enough to send items to a community unit. You also need to know the house number of a building in a unit, that is, the specific address bit stored.

  /* 2. Where to write */
  Wire.write(WriteAddr);

The third stage is to write the specific data into the address bit.

  Wire.write(data_wr);
  /* 4. When an I2C communication is completed, a stop bit is sent by default */

Tags: arduino

Posted on Tue, 23 Nov 2021 00:01:51 -0500 by Chezshire