21.1實(shí)驗(yàn)內(nèi)容
通過本實(shí)驗(yàn)主要學(xué)習(xí)以下內(nèi)容:
21.2實(shí)驗(yàn)原理
21.2.1AT24C16 EEPROM的工作原理
下圖為AT24CXX系列EEPROM相關(guān)參數(shù),由該圖可知,AT24C16的存儲(chǔ)容量為16Kbit,共2048字節(jié),共128頁,每頁為16字節(jié)。
由下圖可知,AT24C16由8塊組成,每塊256字節(jié)。
I2C開始信號(hào)后,第一個(gè)字節(jié)為器件地址,由1010+3位塊地址+1位讀寫標(biāo)志組成,3位塊地址剛好可以表示8個(gè)塊, 所以一次寫完256字節(jié),換到下一下塊的時(shí)候,要重新更改器件地址。
AT24C16支持頁寫入模式,一次最多可支持寫入16字節(jié)。主機(jī)每發(fā)送一個(gè)字節(jié),24c16收到確認(rèn),內(nèi)部地址遞增(僅限低4bit,所以1次可寫16字節(jié))。
21.2.2IIC接口原理
GD32F30X系列MCU的I2C接口模塊實(shí)現(xiàn)了I2C協(xié)議的標(biāo)速模式,快速模式以及快速+模式,具備CRC計(jì)算和校驗(yàn)功能、支持SMBus(系統(tǒng)管理總線)和PMBus(電源管理總線),此外還支持多主機(jī)I2C總線架構(gòu),其主要特性如下:
?并行總線至I2C總線協(xié)議的轉(zhuǎn)換及接口;
?同一接口既可實(shí)現(xiàn)主機(jī)功能又可實(shí)現(xiàn)從機(jī)功能;
?主從機(jī)之間的雙向數(shù)據(jù)傳輸;
?支持7位和10位的地址模式和廣播尋址;
?支持I2C多主機(jī)模式;
?支持標(biāo)速(最高100 KHz),快速(最高400 KHz)和快速+模式(最高1MHz);
?從機(jī)模式下可配置的SCL主動(dòng)拉低;
?支持DMA模式;
?兼容SMBus 2.0和PMBus;
?兩個(gè)中斷:字節(jié)成功發(fā)送中斷和錯(cuò)誤事件中斷;
?可選擇的PEC(報(bào)文錯(cuò)誤校驗(yàn))生成和校驗(yàn)。
IIC模塊結(jié)構(gòu)框圖如下所示。
21.3硬件設(shè)計(jì)
EEPROM硬件電路圖如下所示,IIC引腳使用PB10和PB11引腳,SDA和SCL總線通過4.7K電阻上拉,且對(duì)地接30pf電容以及100歐姆串阻濾波。
21.4代碼解析
21.4.1EEPROM初始化配置函數(shù)
EEPROM初始化配置函數(shù)如下,主要實(shí)現(xiàn)對(duì)IIC總線引腳配置以及IIC模塊配置。
C void bsp_eeprom_init_AT24C16(void) { driver_i2c_init(&EEPROM_I2C); } void driver_i2c_init(typdef_i2c_struct *i2cx) { rcu_periph_clock_enable(i2cx->rcu_i2c_x); i2c_deinit(i2cx->i2c_x); driver_gpio_general_init(i2cx->i2c_scl_gpio); driver_gpio_general_init(i2cx->i2c_sda_gpio); /* I2C clock configure */ i2c_clock_config(i2cx->i2c_x, i2cx->frequency, I2C_DTCY_2); /* I2C address configure */ i2c_mode_addr_config(i2cx->i2c_x, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, i2cx->slave_addr); /* enable I2C0 */ i2c_enable(i2cx->i2c_x); /* enable acknowledge */ i2c_ack_config(i2cx->i2c_x, I2C_ACK_ENABLE); } |
21.4.2EEPROM buf寫入接口函數(shù)
EEPROM buf寫入接口函數(shù)實(shí)現(xiàn)如下,通過該函數(shù)可實(shí)現(xiàn)對(duì)AT24C16任意地址的多字節(jié)寫入。內(nèi)部已根據(jù)地址和寫入長度自動(dòng)識(shí)別從機(jī)地址以及對(duì)應(yīng)的塊,然后寫入正確的地址空間。
C EEPROM_STATE eeprom_buffer_write_AT24C16(uint8_t* p_buffer, uint16_t write_address, uint16_t number_of_byte) { uint8_t number_of_page = 0, number_of_single = 0, address = 0, count = 0; uint8_t deviceId; address = write_address % I2C_PAGE_SIZE; count = I2C_PAGE_SIZE - address; number_of_page = number_of_byte / I2C_PAGE_SIZE; number_of_single = number_of_byte % I2C_PAGE_SIZE; if(write_address+write_address>EEPROM_SIZE) { return EEPROM_ERROR; } /* if write_address is I2C_PAGE_SIZE aligned */ if(0 == address){ while(number_of_page--){ deviceId=(write_address>>8)>0 ? (EEPROM_ADDR | (uint8_t)((write_address>>7)&0x0E)):EEPROM_ADDR ; if(driver_i2c_mem_poll_write(&EEPROM_I2C,deviceId,write_address,MEM_ADDRESS_8BIT,p_buffer,I2C_PAGE_SIZE) == DRV_ERROR) { return EEPROM_ERROR; } if(eeprom_wait_standby_state(&EEPROM_I2C) == EEPROM_ERROR) { return EEPROM_ERROR; } write_address += I2C_PAGE_SIZE; p_buffer += I2C_PAGE_SIZE; } if(0 != number_of_single){ deviceId=(write_address>>8)>0 ? (EEPROM_ADDR | (uint8_t)((write_address>>7)&0x0E)):EEPROM_ADDR ; if(driver_i2c_mem_poll_write(&EEPROM_I2C,deviceId,write_address,MEM_ADDRESS_8BIT,p_buffer, number_of_single)==DRV_ERROR) { return EEPROM_ERROR; } if(eeprom_wait_standby_state(&EEPROM_I2C) == EEPROM_ERROR) { return EEPROM_ERROR; } } return EEPROM_SUCCESS; }else{ /* if write_address is not I2C_PAGE_SIZE aligned */ if(number_of_byte < count){? deviceId=(write_address>>8)>0 ? (EEPROM_ADDR | (uint8_t)((write_address>>7)&0x0E)):EEPROM_ADDR ; if(driver_i2c_mem_poll_write(&EEPROM_I2C,deviceId,write_address,MEM_ADDRESS_8BIT,p_buffer, number_of_byte)==DRV_ERROR) { return EEPROM_ERROR; } if(eeprom_wait_standby_state(&EEPROM_I2C)==EEPROM_ERROR) { return EEPROM_ERROR; } }else{ number_of_byte -= count; number_of_page = number_of_byte / I2C_PAGE_SIZE; number_of_single = number_of_byte % I2C_PAGE_SIZE; if(0 != count){ deviceId=(write_address>>8)>0 ? (EEPROM_ADDR | (uint8_t)((write_address>>7)&0x0E)):EEPROM_ADDR ; if(driver_i2c_mem_poll_write(&EEPROM_I2C,deviceId,write_address,MEM_ADDRESS_8BIT,p_buffer, count)==DRV_ERROR) { return EEPROM_ERROR; } if(eeprom_wait_standby_state(&EEPROM_I2C)==EEPROM_ERROR) { return EEPROM_ERROR; } write_address += count; p_buffer += count; } /* write page */ while(number_of_page--){ deviceId=(write_address>>8)>0 ? (EEPROM_ADDR | (uint8_t)((write_address>>7)&0x0E)):EEPROM_ADDR ; if(driver_i2c_mem_poll_write(&EEPROM_I2C,deviceId,write_address,MEM_ADDRESS_8BIT,p_buffer, I2C_PAGE_SIZE)==DRV_ERROR) { return EEPROM_ERROR; } if(eeprom_wait_standby_state(&EEPROM_I2C)==EEPROM_ERROR) { return EEPROM_ERROR; } write_address += I2C_PAGE_SIZE; p_buffer += I2C_PAGE_SIZE; } /* write single */ if(0 != number_of_single){ deviceId=(write_address>>8)>0 ? (EEPROM_ADDR | (uint8_t)((write_address>>7)&0x0E)):EEPROM_ADDR ; if(driver_i2c_mem_poll_write(&EEPROM_I2C,deviceId,write_address,MEM_ADDRESS_8BIT,p_buffer, number_of_single)==DRV_ERROR) { return EEPROM_ERROR; } if(eeprom_wait_standby_state(&EEPROM_I2C)==EEPROM_ERROR) { return EEPROM_ERROR; } } } return EEPROM_SUCCESS; } } |
21.4.3EEPROM buf讀取接口函數(shù)
EEPROM buf讀取接口函數(shù)實(shí)現(xiàn)如下,通過該函數(shù)可實(shí)現(xiàn)對(duì)EEPROM任意地址的多字節(jié)數(shù)據(jù)讀取,內(nèi)部也對(duì)讀取的地址進(jìn)行自動(dòng)識(shí)別從機(jī)地址。
C EEPROM_STATE eeprom_buffer_read_AT24C16(uint8_t* p_buffer, uint16_t read_address, uint16_t number_of_byte) { uint8_t rNum=0; //讀取的數(shù)據(jù)長度 uint16_t lenLeft=number_of_byte;//剩余的數(shù)據(jù)長度 uint8_t deviceId;//讀取的器件地址 if(read_address+number_of_byte>EEPROM_SIZE)//如果讀取的長度加上讀取地址超過了EEPROM的空間大小,則報(bào)錯(cuò)誤 { return EEPROM_ERROR; } /*calculate the current read position to know how many word can read continully*/ rNum=16-read_address & 0x0F; if(rNum == 0) rNum=16; rNum = lenLeft>=rNum ? rNum : lenLeft;//剩余未讀字節(jié)數(shù)如果大于rNum, 則讀rNum個(gè),如果小于rNum,則一次讀完了 /*read the data from e2prom*/ while(lenLeft) { //這里計(jì)算頁地址,當(dāng)?shù)刂沸∮?56時(shí),右移8位會(huì)小于0,所以器件地址為基地址A1 //如果讀取的地址大于256時(shí),右移8位則不會(huì)小于0,所以器件地址為 基地址A1 | 3位頁地址 deviceId=(read_address>>8)>0 ? (EEPROM_ADDR | (uint8_t)((read_address>>7)&0x0E)):EEPROM_ADDR ; if(driver_i2c_mem_poll_read(&EEPROM_I2C,deviceId,read_address,MEM_ADDRESS_8BIT,p_buffer,rNum)==DRV_ERROR) { // printf("i2c read error\r\n"); return EEPROM_ERROR; } read_address+=rNum;//已經(jīng)讀了rNum個(gè)了,所以地址后移rNum個(gè) lenLeft-=rNum;//剩余未讀數(shù)據(jù)減少rNum個(gè) p_buffer+=rNum; rNum=lenLeft>16? 16 : lenLeft;//如果剩余大于16個(gè),則下次再讀16個(gè),如果小于,則一次讀完 } return EEPROM_SUCCESS; } |
21.4.4EEPROM讀寫實(shí)驗(yàn)主函數(shù)
EEPROM讀寫實(shí)驗(yàn)主函數(shù)如下所示。通過該實(shí)驗(yàn)實(shí)現(xiàn)對(duì)AT24C16任意地址256字節(jié)的寫入、讀取以及校驗(yàn)測(cè)試。
C int main(void) { uint16_t i; uint8_t i2c_buffer_write[BUFFER_SIZE]; uint8_t i2c_buffer_read[BUFFER_SIZE]; bsp_eeprom_init_AT24C16(); /* initialize i2c_buffer_write */ for(i = 0;i < BUFFER_SIZE;i++){? i2c_buffer_write[i]=i; // printf("0x%02X ",i2c_buffer_write[i]); // if(15 == i%16){ // printf("\r\n"); // } } if(eeprom_buffer_write_AT24C16(i2c_buffer_write,0x0153,BUFFER_SIZE)==EEPROM_SUCCESS) { __nop(); } if(eeprom_buffer_read_AT24C16(i2c_buffer_read,0x0153,BUFFER_SIZE)==EEPROM_SUCCESS) { __nop(); } /* compare the read buffer and write buffer */ for(i = 0;i < BUFFER_SIZE;i++){ if(i2c_buffer_read[i] != i2c_buffer_write[i]){ __nop(); // printf("0x%02X ", i2c_buffer_read[i]); // printf("Err:data read and write aren't matching.\n\r"); // return I2C_FAIL; } //printf("0x%02X ", i2c_buffer_read[i]); // if(15 == i%16){ // printf("\r\n"); // } } __nop(); // printf("I2C-AT24C02 test passed!\n\r"); while (1) { } } |
21.5實(shí)驗(yàn)結(jié)果
將本實(shí)驗(yàn)歷程燒錄到紅楓派開發(fā)板中,運(yùn)行后,可通過串口打印測(cè)試結(jié)果,可實(shí)現(xiàn)對(duì)于AT24C16任意地址寫入、讀取以及校驗(yàn)。
本教程由GD32 MCU方案商聚沃科技原創(chuàng)發(fā)布,了解更多GD32 MCU教程,關(guān)注聚沃科技官網(wǎng)
-
單片機(jī)
+關(guān)注
關(guān)注
6032文章
44514瀏覽量
632941 -
嵌入式
+關(guān)注
關(guān)注
5068文章
19014瀏覽量
303228 -
EEPROM
+關(guān)注
關(guān)注
9文章
1010瀏覽量
81404 -
I2C
+關(guān)注
關(guān)注
28文章
1481瀏覽量
123278 -
開發(fā)板
+關(guān)注
關(guān)注
25文章
4943瀏覽量
97188
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論