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[256] = {0}; 8 uint8_t at24c02_read_buf[256] = {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; 99100 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 }106107 addr += AT24C02_PAGE_SIZE;108 pData += AT24C02_PAGE_SIZE;109 len -= AT24C02_PAGE_SIZE;110111 /* For auto sync write */112 Fn_DelayMs(5);113 }114115 /* Write cpm */116 if(len == 0)117 {118 return H_SUCCESS;119 }120121 /* Last page remain number */122 if(H_SUCCESS != Fn_AT24C02_WritePage((H_AT24C02_PAGE_T)addr, pData, len))123 {124 return H_ERROR;125 }126127 return H_SUCCESS;128 }129130131 /**132 * @brief AT24C02 read current address data.133 * @param pData, Read data buffer.134 * @retval H_SUCCESS or H_ERROR.135 */136 H_STATUS_T Fn_AT24C02_ReadCurrentByte(uint8_t *pData)137 {138 H_PARA_ASSERT(pData == NULL);139140 if(HAL_OK != HAL_I2C_Master_Receive(&hi2c1, AT24C02_ADDR, pData, 1, 0xFF))141 {142 return H_ERROR;143 }144145 return H_SUCCESS;146 }147148149 /**150 * @brief AT24C02 read a byte.151 * @param addr, AT24C02 read data memory.152 pData, Read data buffer.153 * @retval H_SUCCESS or H_ERROR.154 */155 H_STATUS_T Fn_AT24C02_ReadByte(uint8_t addr, uint8_t *pData)156 {157 H_PARA_ASSERT(pData == NULL);158159 if(HAL_OK != HAL_I2C_Mem_Read(&hi2c1, AT24C02_ADDR, addr, I2C_MEMADD_SIZE_8BIT, pData, 1, 0xFF))160 {161 return H_ERROR;162 }163164 return H_SUCCESS;165 }166167168 /**169 * @brief AT24C02 read serial data.170 * @param addr, AT24C02 read data memory.171 pData, Read data buffer.172 len, Read data length.173 * @retval H_SUCCESS or H_ERROR.174 */175 H_STATUS_T Fn_AT24C02_ReadData(uint8_t addr, uint8_t *pData, uint16_t len)176 {177 H_PARA_ASSERT((pData == NULL) || (len > AT24C02_MAX_BYTE_NUM));178179 if(HAL_OK != HAL_I2C_Mem_Read(&hi2c1, AT24C02_ADDR, addr, I2C_MEMADD_SIZE_8BIT, pData, len, 0xFFFF))180 {181 return H_ERROR;182 }183184 return H_SUCCESS;185 }186187188 /**189 * @brief AT24C02 clear memory data.190 * @param None.191 * @retval H_SUCCESS or H_ERROR.192 */193 H_STATUS_T Fn_AT24C02_ClearMemData(void)194 {195 uint32_t i = 0;196 uint8_t page_addr = 0x00;197 uint8_t data[AT24C02_PAGE_SIZE] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};198199 for(i = 0; i < AT24C02_PAGE_NUM; i++)200 {201 if(H_SUCCESS != Fn_AT24C02_WritePage((H_AT24C02_PAGE_T)page_addr, data, AT24C02_PAGE_SIZE))202 {203 return H_ERROR;204 }205206 page_addr += AT24C02_PAGE_SIZE;207208 /* For auto sync write */209 Fn_DelayMs(5);210 }211212 return H_SUCCESS;213 }六:代码参考:.h文件
推荐阅读
- 天玑810跑分多少分_天玑810跑分详情
- 苹果13Pro参数配置_参数详情
- iQOO Z5参数_iQOO Z5参数配置
- ipadmini6参数_ipadmini6配置参数
- iPhone13参数配置_参数详情
- iqooz3的缺点_iqooz3骂声一片
- 暗区突围怎么更换角色
- 阴阳师寻香行御魂用什么
- 一加9r参数_一加9r参数配置详细
- 魅族18x评测_魅族18x体验评测