AT24C02是一款拥有256bytes(32Page)的EEPROM.

一 :特点(部分)

1:双线接口;

2:双向数据传输协议;

3:400KHz波特率;

4:硬件写保护;

5:最大5ms写入同步;

6:100万次写操作;

7:100年数据存储。

基本框图

二:绝对参数

三:引脚定义

四:读写操作

AT24C02使用I2C进行读写操作,最大波特率为400KHz。

1:设备地址

设备地址由7bits组成,其中高4位默认为1010b,低三位由A0~A2控制。

2:写操作

1)单字节写

AT24C02单字节写,需要先写入设备地址,之后写入内存地址,最后写入数据。当IIC最后产生STOP模式后,AT24C02将会进入最大5ms的同步时间,此时无法对AT24C02进行操作。

写入时序如下:

2)页写

AT24C02支持按页写操作,每页最大8个字节,一共32页。当进行按页写的时候,AT24C02内存地址会自动增加,如果地址已经是本页的最后地址,但是写操作还未完成,则地址会返回本页起始地址,数据将会覆盖。

3:读操作

AT24C02支持当前地址读取,任意地址读取,连续读取。

1)当前地址读取

AT24C02在上电的时候,每次读写操作,都会自动同步地址。当使用IIC读取数据时,如果在写入设备地址时进行读操作,会读取当前同步地址的值。

2)任意地址读取

AT24C02任意地址读取即为单字节读取。在写完设备地址,内存地址后,进行重起始操作,对数据进行读取。

3)连续读取

AT24C02连续读取时,在读取第一个字节后,IIC不产生STOP条件,则AT24C02会继续传出数据,同时内存地址会自动更新,直到读取到设备的最大地址后,如果继续读取,则会读取设备内存的首地址值。

五:代码参考(IIC 阻塞读写)–.c文件

  1 #include "at24c02.h"  2   3   4 #define AT24C02_ADDR    (0xA0)  5   6   7 uint8_t at24c02_write_buf[AT24C02_MAX_BYTE_NUM] = {0};  8 uint8_t at24c02_read_buf[AT24C02_MAX_BYTE_NUM]  = {0};  9  10  11 /** 12   * @brief  AT24C02 write data. 13   * @param  page, AT24C02 write page. 14             data, Write data buffer. 15   * @retval H_SUCCESS or H_ERROR. 16   */ 17 H_STATUS_T Fn_AT24C02_WriteByte(uint8_t addr, uint8_t data) 18 { 19     uint8_t write_byte[1] = {data}; 20     HAL_StatusTypeDef status = HAL_OK; 21  22     status = HAL_I2C_Mem_Write(&hi2c1, AT24C02_ADDR, addr, I2C_MEMADD_SIZE_8BIT, write_byte, 1, 0xFF); 23  24     if(status != HAL_OK) 25     { 26         return H_ERROR; 27     } 28  29     return H_SUCCESS; 30 } 31  32  33 /** 34   * @brief  AT24C02 write page data. 35   * @param  page, AT24C02 write page. 36             pData, Write data buffer. 37             len, Write data length. 38   * @retval H_SUCCESS or H_ERROR. 39   */ 40 H_STATUS_T Fn_AT24C02_WritePage(H_AT24C02_PAGE_T page, uint8_t* pData, uint8_t len) 41 { 42     HAL_StatusTypeDef status = HAL_OK; 43     uint8_t page_addr = (uint8_t)page; 44  45     H_PARA_ASSERT((pData == NULL) || (len > AT24C02_PAGE_SIZE)); 46  47     status = HAL_I2C_Mem_Write(&hi2c1, AT24C02_ADDR, page_addr, I2C_MEMADD_SIZE_8BIT, pData, len, 0xFFFF); 48  49     if(status != HAL_OK) 50     { 51         return H_ERROR; 52     } 53  54     return H_SUCCESS; 55 } 56  57  58 /** 59   * @brief  AT24C02 write serial data. 60   * @param  addr, AT24C02 write data memory. 61             pData, Write data buffer. 62             len, Write data length. 63   * @retval H_SUCCESS or H_ERROR. 64   */ 65 H_STATUS_T Fn_AT24C02_WriteData(uint8_t addr, uint8_t* pData, uint16_t len) 66 { 67     uint8_t start_page_remain = 0; 68     uint8_t remain_page_num = 0; 69     uint8_t write_num = 0; 70     uint32_t i = 0; 71  72     H_PARA_ASSERT(((AT24C02_MAX_BYTE_NUM - (addr + 1)) < len) || (pData == NULL)); 73  74     /* How many memory remain for start page */ 75     start_page_remain = AT24C02_PAGE_SIZE - addr % AT24C02_PAGE_SIZE; 76  77     /* Write first page */ 78     write_num = len > start_page_remain ? start_page_remain : len; 79     for(i = 0; i < write_num; i++) 80     { 81         if(H_SUCCESS != Fn_AT24C02_WriteByte(addr++, *pData++)) 82         { 83             return H_ERROR; 84         } 85         len--; 86  87         /* For auto sync write */ 88         Fn_DelayMs(5); 89     } 90  91     /* Write completely */ 92     if(len == 0) 93     { 94         return H_SUCCESS; 95     } 96  97     /* How many completely page to write */ 98     remain_page_num = len / AT24C02_PAGE_SIZE; 99 100     for(i = 0; i < remain_page_num; i++)101     {102         if(H_SUCCESS != Fn_AT24C02_WritePage((H_AT24C02_PAGE_T)addr, pData, AT24C02_PAGE_SIZE))103         {104             return H_ERROR;105         }106 107         addr += AT24C02_PAGE_SIZE;108         pData += AT24C02_PAGE_SIZE;109         len -= AT24C02_PAGE_SIZE;110 111         /* For auto sync write */112         Fn_DelayMs(5);113     }114 115     /* Write cpm */116     if(len == 0)117     {118         return H_SUCCESS;119     }120 121     /* Last page remain number */122     if(H_SUCCESS != Fn_AT24C02_WritePage((H_AT24C02_PAGE_T)addr, pData, len))123     {124         return H_ERROR;125     }126 127     /* Note: if called Read function after this function, Please insert a delay time between write / read */128     return H_SUCCESS;129 }130 131 132 /**133   * @brief  AT24C02 read current address data.134   * @param  pData, Read data buffer.135   * @retval H_SUCCESS or H_ERROR.136   */137 H_STATUS_T Fn_AT24C02_ReadCurrentByte(uint8_t *pData)138 {139     H_PARA_ASSERT(pData == NULL);140 141     if(HAL_OK != HAL_I2C_Master_Receive(&hi2c1, AT24C02_ADDR, pData, 1, 0xFF))142     {143         return H_ERROR;144     }145 146     return H_SUCCESS;147 }148 149 150 /**151   * @brief  AT24C02 read a byte.152   * @param  addr, AT24C02 read data memory.153             pData, Read data buffer.154   * @retval H_SUCCESS or H_ERROR.155   */156 H_STATUS_T Fn_AT24C02_ReadByte(uint8_t addr, uint8_t *pData)157 {158     H_PARA_ASSERT(pData == NULL);159 160     if(HAL_OK != HAL_I2C_Mem_Read(&hi2c1, AT24C02_ADDR, addr, I2C_MEMADD_SIZE_8BIT, pData, 1, 0xFF))161     {162         return H_ERROR;163     }164 165     return H_SUCCESS;166 }167 168 169 /**170   * @brief  AT24C02 read serial data.171   * @param  addr, AT24C02 read data memory.172             pData, Read data buffer.173             len, Read data length.174   * @retval H_SUCCESS or H_ERROR.175   */176 H_STATUS_T Fn_AT24C02_ReadData(uint8_t addr, uint8_t *pData, uint16_t len)177 {178     H_PARA_ASSERT((pData == NULL) || (len > AT24C02_MAX_BYTE_NUM));179 180     if(HAL_OK != HAL_I2C_Mem_Read(&hi2c1, AT24C02_ADDR, addr, I2C_MEMADD_SIZE_8BIT, pData, len, 0xFFFF))181     {182         return H_ERROR;183     }184 185     return H_SUCCESS;186 }187 188 189 /**190   * @brief  AT24C02 clear memory data.191   * @param  None.192   * @retval H_SUCCESS or H_ERROR.193   */194 H_STATUS_T Fn_AT24C02_ClearMemData(void)195 {196     uint32_t i = 0;197     uint8_t  page_addr = 0x00;198     uint8_t  data[AT24C02_PAGE_SIZE] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};199 200     for(i = 0; i < AT24C02_PAGE_NUM; i++)201     {202         if(H_SUCCESS != Fn_AT24C02_WritePage((H_AT24C02_PAGE_T)page_addr, data, AT24C02_PAGE_SIZE))203         {204              return H_ERROR;205         }206 207         page_addr += AT24C02_PAGE_SIZE;208 209         /* For auto sync write */210         Fn_DelayMs(5);211     }212 213     return H_SUCCESS;214 }


六:代码参考:.h文件
  1 #ifndef __AT24C02_H__  2 #define __AT24C02_H__  3   4   5 #include "iic.h"  6   7 #define AT24C02_BASE_ADDR           (0x00)  8   9 #define AT24C02_PAGE_SIZE           (0x08) 10  11 #define AT24C02_PAGE_NUM            (32) 12  13 #define AT24C02_MAX_BYTE_NUM        (256) 14  15 #define AT24C02_MAX_MEM_ADDR        (0xFF) 16  17 typedef enum 18 { 19     AT24C02_PAGE0  = AT24C02_BASE_ADDR +  0 * AT24C02_PAGE_SIZE, 20     AT24C02_PAGE1  = AT24C02_BASE_ADDR +  1 * AT24C02_PAGE_SIZE, 21     AT24C02_PAGE2  = AT24C02_BASE_ADDR +  2 * AT24C02_PAGE_SIZE, 22     AT24C02_PAGE3  = AT24C02_BASE_ADDR +  3 * AT24C02_PAGE_SIZE, 23     AT24C02_PAGE4  = AT24C02_BASE_ADDR +  4 * AT24C02_PAGE_SIZE, 24     AT24C02_PAGE5  = AT24C02_BASE_ADDR +  5 * AT24C02_PAGE_SIZE, 25     AT24C02_PAGE6  = AT24C02_BASE_ADDR +  6 * AT24C02_PAGE_SIZE, 26     AT24C02_PAGE7  = AT24C02_BASE_ADDR +  7 * AT24C02_PAGE_SIZE, 27     AT24C02_PAGE8  = AT24C02_BASE_ADDR +  8 * AT24C02_PAGE_SIZE, 28     AT24C02_PAGE9  = AT24C02_BASE_ADDR +  9 * AT24C02_PAGE_SIZE, 29     AT24C02_PAGE10 = AT24C02_BASE_ADDR + 10 * AT24C02_PAGE_SIZE, 30     AT24C02_PAGE11 = AT24C02_BASE_ADDR + 11 * AT24C02_PAGE_SIZE, 31     AT24C02_PAGE12 = AT24C02_BASE_ADDR + 12 * AT24C02_PAGE_SIZE, 32     AT24C02_PAGE13 = AT24C02_BASE_ADDR + 13 * AT24C02_PAGE_SIZE, 33     AT24C02_PAGE14 = AT24C02_BASE_ADDR + 14 * AT24C02_PAGE_SIZE, 34     AT24C02_PAGE15 = AT24C02_BASE_ADDR + 15 * AT24C02_PAGE_SIZE, 35     AT24C02_PAGE16 = AT24C02_BASE_ADDR + 16 * AT24C02_PAGE_SIZE, 36     AT24C02_PAGE17 = AT24C02_BASE_ADDR + 17 * AT24C02_PAGE_SIZE, 37     AT24C02_PAGE18 = AT24C02_BASE_ADDR + 18 * AT24C02_PAGE_SIZE, 38     AT24C02_PAGE19 = AT24C02_BASE_ADDR + 19 * AT24C02_PAGE_SIZE, 39     AT24C02_PAGE20 = AT24C02_BASE_ADDR + 20 * AT24C02_PAGE_SIZE, 40     AT24C02_PAGE21 = AT24C02_BASE_ADDR + 21 * AT24C02_PAGE_SIZE, 41     AT24C02_PAGE22 = AT24C02_BASE_ADDR + 22 * AT24C02_PAGE_SIZE, 42     AT24C02_PAGE23 = AT24C02_BASE_ADDR + 23 * AT24C02_PAGE_SIZE, 43     AT24C02_PAGE24 = AT24C02_BASE_ADDR + 24 * AT24C02_PAGE_SIZE, 44     AT24C02_PAGE25 = AT24C02_BASE_ADDR + 25 * AT24C02_PAGE_SIZE, 45     AT24C02_PAGE26 = AT24C02_BASE_ADDR + 26 * AT24C02_PAGE_SIZE, 46     AT24C02_PAGE27 = AT24C02_BASE_ADDR + 27 * AT24C02_PAGE_SIZE, 47     AT24C02_PAGE28 = AT24C02_BASE_ADDR + 28 * AT24C02_PAGE_SIZE, 48     AT24C02_PAGE29 = AT24C02_BASE_ADDR + 29 * AT24C02_PAGE_SIZE, 49     AT24C02_PAGE30 = AT24C02_BASE_ADDR + 30 * AT24C02_PAGE_SIZE, 50     AT24C02_PAGE31 = AT24C02_BASE_ADDR + 31 * AT24C02_PAGE_SIZE, 51 }H_AT24C02_PAGE_T; 52  53  54  55 /** 56   * @brief  AT24C02 write data. 57   * @param  page, AT24C02 write page. 58             data, Write data buffer. 59   * @retval H_SUCCESS or H_ERROR. 60   */ 61 H_STATUS_T Fn_AT24C02_WriteByte(uint8_t addr, uint8_t data); 62  63  64 /** 65   * @brief  AT24C02 write page data. 66   * @param  page, AT24C02 write page. 67             pData, Write data buffer. 68             len, Write data length. 69   * @retval H_SUCCESS or H_ERROR. 70   */ 71 H_STATUS_T Fn_AT24C02_WritePage(H_AT24C02_PAGE_T page, uint8_t* pData, uint8_t len); 72  73  74 /** 75   * @brief  AT24C02 write serial data. 76   * @param  addr, AT24C02 write data memory. 77             pData, Write data buffer. 78             len, Write data length. 79   * @retval H_SUCCESS or H_ERROR. 80   */ 81 H_STATUS_T Fn_AT24C02_WriteData(uint8_t addr, uint8_t* pData, uint16_t len); 82  83  84 /** 85   * @brief  AT24C02 read current address data. 86   * @param  pData, Read data buffer. 87   * @retval H_SUCCESS or H_ERROR. 88   */ 89 H_STATUS_T Fn_AT24C02_ReadCurrentByte(uint8_t *pData); 90  91  92 /** 93   * @brief  AT24C02 read a byte. 94   * @param  addr, AT24C02 read data memory. 95             pData, Read data buffer. 96   * @retval H_SUCCESS or H_ERROR. 97   */ 98 H_STATUS_T Fn_AT24C02_ReadByte(uint8_t addr, uint8_t *pData); 99 100 101 /**102   * @brief  AT24C02 read serial data.103   * @param  addr, AT24C02 read data memory.104             pData, Read data buffer.105             len, Read data length.106   * @retval H_SUCCESS or H_ERROR.107   */108 H_STATUS_T Fn_AT24C02_ReadData(uint8_t addr, uint8_t *pData, uint16_t len);109 110 111 /**112   * @brief  AT24C02 clear memory data.113   * @param  None.114   * @retval H_SUCCESS or H_ERROR.115   */116 H_STATUS_T Fn_AT24C02_ClearMemData(void);117 118 119 extern uint8_t at24c02_write_buf[AT24C02_MAX_BYTE_NUM];120 extern uint8_t at24c02_read_buf[AT24C02_MAX_BYTE_NUM];121 122 123 #endif