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