STM32F10x硬件I2c讀寫(xiě)AT24c02,程序卡死
? ? ? ? stm32是自帶硬件I2C,相比于軟件模擬I2c,硬件I2c效率更高。但是據(jù)說(shuō)不穩(wěn)定,這個(gè)我倒暫時(shí)還沒(méi)有體會(huì)到。
? ? ? ? 在最開(kāi)始使用硬件I2c的時(shí)候,程序總是卡死,要不從一開(kāi)始就卡死,要不從某一步開(kāi)始卡死。我初學(xué)stm32,所以每學(xué)習(xí)一個(gè)模塊都會(huì)把程序放進(jìn)去,程序相互疊加。但是今天移植別人的硬件I2C卻總是不能成功。一直是卡死在while等待;
? ? ? ?while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));??
? ? ? while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
? ? ? while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
? ? ? while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
下面進(jìn)行簡(jiǎn)單的分析:
? ? ? ?1,很多時(shí)候,如果在線(xiàn)調(diào)試中發(fā)現(xiàn)從程序的第一個(gè)while就卡死,那很可能是配置有問(wèn)題;比如SDA/SCL是否為開(kāi)漏,I2C1時(shí)鐘,GPIOB時(shí)鐘是否開(kāi)啟,是否重復(fù)使用IO口導(dǎo)致異常,這里有個(gè)好方法就是把程序精簡(jiǎn),就是把程序最簡(jiǎn)化,然后查錯(cuò),糾正錯(cuò)誤后再加;
? ? ? 2,可以用萬(wàn)用表看看你的IO口空閑時(shí)是不是上拉到高電平,我的程序就是用萬(wàn)用表測(cè)量SDA/SCL空閑不為高,這就有問(wèn)題了,我查了很長(zhǎng)時(shí)間沒(méi)有引腳用重復(fù),于是把多的程序全部刪掉就好了。
? ? ? 3,這位樓主STM32 庫(kù)I2c 調(diào)試成功?。。∫彩怯龅焦δ苡弥貜?fù)了,這里直接把帖子貼出來(lái)。
? ? ? 4,有位博主說(shuō)他的硬件I2C 10K速率時(shí)穩(wěn)定,但是我覺(jué)得不至于,我用200K讀寫(xiě)都很正常,如果上訴不能解決,可以調(diào)整下速率看看效果。
之前寫(xiě)了一個(gè)寫(xiě)單字節(jié)的讀寫(xiě)程序不能用,沒(méi)辦法我移植的是野火的《stm32庫(kù)開(kāi)發(fā)實(shí)戰(zhàn)指南》的硬件I2C讀寫(xiě)24C02的例程,下面貼上代碼:(加上了少部分注釋?zhuān)?/p>
? ? ? 5, 經(jīng)本人反復(fù)折騰發(fā)現(xiàn),引腳在你認(rèn)為的“空閑”時(shí)間表現(xiàn)為低電平的的另一種可能,就是程序?qū)懙挠袉?wèn)題,AT24c0x把總線(xiàn)拉低表現(xiàn)為等待主機(jī)響應(yīng),要想知道是什么原因?qū)е驴偩€(xiàn)電平被拉低,可以重新插從器件供電,并使主器件復(fù)位(因?yàn)榇藭r(shí)的主器件一般處于卡死循環(huán)狀態(tài)),在不觸發(fā)AT24c0x的情況下,測(cè)SDA,SCL的電平狀態(tài),查看是否為高電平,再觸發(fā)讀寫(xiě)從器件,結(jié)束后測(cè)SDA.SCL電平變化!
? ? ? 這個(gè)是頭文件:
#ifndef?__I2C_EE_H #define __I2C_EE_H #include/*?EEPROM?Addresses?defines?*/ #define?EEPROM_Block0_ADDRESS?0xA0???/*?E2?=?0?*///AT24C0x的I2C硬件地址 //#define?EEPROM_Block1_ADDRESS?0xA2?/*?E2?=?0?*/ //#define?EEPROM_Block2_ADDRESS?0xA4?/*?E2?=?0?*/ //#define?EEPROM_Block3_ADDRESS?0xA6?/*?E2?=?0?*/ void?I2C_EE_Init(void); void?I2C_EE_BufferWrite(u8*?pBuffer,?u8?WriteAddr,?u16?NumByteToWrite); void?I2C_EE_ByteWrite(u8*?pBuffer,?u8?WriteAddr); void?I2C_EE_PageWrite(u8*?pBuffer,?u8?WriteAddr,?u8?NumByteToWrite); void?I2C_EE_BufferRead(u8*?pBuffer,?u8?ReadAddr,?u16?NumByteToRead); void?I2C_EE_WaitEepromStandbyState(void); #endif?/*?__I2C_EE_H?*/
? ? ?下面是C文件:
/******************************************************************************** ?*?文件名??:i2c_ee.c ?*?描述????:i2c?EEPROM(AT24C02)應(yīng)用函數(shù)庫(kù)????????? ?*?實(shí)驗(yàn)平臺(tái):野火STM32開(kāi)發(fā)板 ?*?硬件連接:----------------- ?*??????????|?????????????????| ?*??????????|??PB6-I2C1_SCL ??| ?*??????????|??PB7-I2C1_SDA???| ?*??????????|?????????????????| ?*???????????----------------- ?*?庫(kù)版本??:ST3.5.0 ?*?作者????:保留? ?*?論壇????:http://www.amobbs.com/forum-1008-1.html ?*?淘寶????:http://firestm32.taobao.com **********************************************************************************/ #include?"i2c_ee.h" #define?I2C_Speed??????????????200000 #define?I2C1_OWN_ADDRESS7????0x0A???????????//stm32z自身I2C地址,自己定義的 #define?I2C_PageSize???????????8 /*?AT24C02每頁(yè)有8個(gè)字節(jié)?*/ u16?EEPROM_ADDRESS; /* ?*?函數(shù)名:I2C_GPIO_Config ?*?描述??:I2C1?I/O配置 ?*?輸入??:無(wú) ?*?輸出??:無(wú) ?*?調(diào)用??:內(nèi)部調(diào)用 ?*/ static?void?I2C_GPIO_Config(void) { ??GPIO_InitTypeDef??GPIO_InitStructure;? //?使能與?I2C1?有關(guān)的時(shí)鐘?/ ??//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); ??//RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);?? ???? ??//?PB6-I2C1_SCL、PB7-I2C1_SDA/ ??GPIO_InitStructure.GPIO_Pin?=??GPIO_Pin_6?|?GPIO_Pin_7; ??GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_50MHz; ??GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_AF_OD; ???????//?開(kāi)漏輸出 ??GPIO_Init(GPIOB,?&GPIO_InitStructure); } /* ?*?函數(shù)名:I2C_Configuration ?*?描述??:I2C?工作模式配置 ?*?輸入??:無(wú) ?*?輸出??:無(wú) ?*?調(diào)用??:內(nèi)部調(diào)用 ?*/ static?void?I2C_Mode_Configu(void) { ??I2C_InitTypeDef??I2C_InitStructure;? ??/*?I2C?配置?*/ ??I2C_InitStructure.I2C_Mode?=?I2C_Mode_I2C; ??I2C_InitStructure.I2C_DutyCycle?=?I2C_DutyCycle_2; ??I2C_InitStructure.I2C_OwnAddress1?=I2C1_OWN_ADDRESS7;? ??I2C_InitStructure.I2C_Ack?=?I2C_Ack_Enable?; ??I2C_InitStructure.I2C_AcknowledgedAddress?=?I2C_AcknowledgedAddress_7bit; ??I2C_InitStructure.I2C_ClockSpeed?=?I2C_Speed; ?? ??/*?使能?I2C1?*/ ??I2C_Cmd(I2C1,?ENABLE); ??/*?I2C1?初始化?*/ ??I2C_Init(I2C1,?&I2C_InitStructure); ??? } /* ?*?函數(shù)名:I2C_EE_Init ?*?描述??:I2C?外設(shè)(EEPROM)初始化 ?*?輸入??:無(wú) ?*?輸出??:無(wú) ?*?調(diào)用??:外部調(diào)用 ?*/ void?I2C_EE_Init(void) { ??I2C_GPIO_Config();? ? ??I2C_Mode_Configu(); /*?根據(jù)頭文件i2c_ee.h中的定義來(lái)選擇EEPROM要寫(xiě)入的地址?*/ #ifdef?EEPROM_Block0_ADDRESS ??/*?選擇?EEPROM?Block0?來(lái)寫(xiě)入?*/ ??EEPROM_ADDRESS?=?EEPROM_Block0_ADDRESS; #endif #ifdef?EEPROM_Block1_ADDRESS?? /*?選擇?EEPROM?Block1?來(lái)寫(xiě)入?*/ ??EEPROM_ADDRESS?=?EEPROM_Block1_ADDRESS; #endif #ifdef?EEPROM_Block2_ADDRESS?? /*?選擇?EEPROM?Block2?來(lái)寫(xiě)入?*/ ??EEPROM_ADDRESS?=?EEPROM_Block2_ADDRESS; #endif #ifdef?EEPROM_Block3_ADDRESS?? /*?選擇?EEPROM?Block3?來(lái)寫(xiě)入?*/ ??EEPROM_ADDRESS?=?EEPROM_Block3_ADDRESS; #endif } /* ?*?函數(shù)名:I2C_EE_BufferWrite ?*?描述??:將緩沖區(qū)中的數(shù)據(jù)寫(xiě)到I2C?EEPROM中 ?*?輸入??:-pBuffer?緩沖區(qū)指針 ?*?????????-WriteAddr?接收數(shù)據(jù)的EEPROM的地址 ?*?????????-NumByteToWrite?要寫(xiě)入EEPROM的字節(jié)數(shù) ?*?輸出??:無(wú) ?*?返回??:無(wú) ?*?調(diào)用??:外部調(diào)用 ?*/ void?I2C_EE_BufferWrite(u8*?pBuffer,?u8?WriteAddr,?u16?NumByteToWrite) { ??u8?NumOfPage?=?0,?NumOfSingle?=?0,?Addr?=?0,?count?=?0; ??Addr?=?WriteAddr?%?I2C_PageSize;//通過(guò)計(jì)算寫(xiě)入的初始地址是否從頁(yè)的開(kāi)始寫(xiě) ??count?=?I2C_PageSize?-?Addr;?????//計(jì)算寫(xiě)的第一頁(yè)需要寫(xiě)幾個(gè)字節(jié)(寫(xiě)的首地址不在頁(yè)的開(kāi)始) ??NumOfPage?=??NumByteToWrite?/?I2C_PageSize;//計(jì)算需要寫(xiě)幾個(gè)整頁(yè) ??NumOfSingle?=?NumByteToWrite?%?I2C_PageSize;//計(jì)算寫(xiě)完整頁(yè)后還剩幾個(gè)字節(jié)沒(méi)有寫(xiě) ? ??/*?If?WriteAddr?is?I2C_PageSize?aligned??*/ ??if(Addr?==?0)?//通寫(xiě)入的初始地址是從頁(yè)的開(kāi)始寫(xiě) ??{ ????/*?If?NumByteToWrite?<?I2C_PageSize?*/ ????if(NumOfPage?==?0)?//如果寫(xiě)的小于一頁(yè),也就是只寫(xiě)NumOfSingle個(gè)數(shù)據(jù) ????{ ??????I2C_EE_PageWrite(pBuffer,?WriteAddr,?NumOfSingle); ??????I2C_EE_WaitEepromStandbyState(); ????} ????/*?If?NumByteToWrite?>?I2C_PageSize?*/ ????else??//如果寫(xiě)的大于等于一頁(yè),也就是寫(xiě)NumOfPage個(gè)整頁(yè)????? ????{ ??????while(NumOfPage--) ??????{ ????????I2C_EE_PageWrite(pBuffer,?WriteAddr,?I2C_PageSize);? ???? I2C_EE_WaitEepromStandbyState(); ????????WriteAddr?+=??I2C_PageSize; ????????pBuffer?+=?I2C_PageSize; ??????} ??????if(NumOfSingle!=0)?//寫(xiě)完整頁(yè)后剩下的不夠一頁(yè)的 ??????{ ????????I2C_EE_PageWrite(pBuffer,?WriteAddr,?NumOfSingle); ????????I2C_EE_WaitEepromStandbyState(); ??????} ????} ??} ??/*?If?WriteAddr?is?not?I2C_PageSize?aligned??*/ ??else?//通寫(xiě)入的初始地址不是從頁(yè)的開(kāi)始寫(xiě),這個(gè)要比前面的要復(fù)雜很多 ??{ ????/*?If?NumByteToWrite?<?I2C_PageSize?*/ ????if(NumOfPage==?0)? ????{ ??????I2C_EE_PageWrite(pBuffer,?WriteAddr,?NumOfSingle); ??????I2C_EE_WaitEepromStandbyState(); ????} ????/*?If?NumByteToWrite?>?I2C_PageSize?*/ ????else ????{ ??????NumByteToWrite?-=?count; ??????NumOfPage?=??NumByteToWrite?/?I2C_PageSize; ??????NumOfSingle?=?NumByteToWrite?%?I2C_PageSize; ?????? ??????if(count?!=?0)?//先把第一頁(yè)的幾個(gè)數(shù)據(jù)寫(xiě)進(jìn)去例如我寫(xiě)的首地址為5,則count=3,則從5寫(xiě)到7 ??????{?? ????????I2C_EE_PageWrite(pBuffer,?WriteAddr,?count); ????????I2C_EE_WaitEepromStandbyState(); ????????WriteAddr?+=?count;???//寫(xiě)地址加上count,前面沒(méi)有對(duì)齊的數(shù)據(jù)寫(xiě)完,該寫(xiě)中間對(duì)齊的部分 ????????pBuffer?+=?count; ??????}? ?????? ??????while(NumOfPage--) ??????{ ????????I2C_EE_PageWrite(pBuffer,?WriteAddr,?I2C_PageSize); ????????I2C_EE_WaitEepromStandbyState(); ????????WriteAddr?+=??I2C_PageSize; ????????pBuffer?+=?I2C_PageSize;?? ??????} ??????if(NumOfSingle?!=?0)//最后寫(xiě)最后幾個(gè)沒(méi)有對(duì)齊的數(shù)據(jù),不夠一整頁(yè) ??????{ ????????I2C_EE_PageWrite(pBuffer,?WriteAddr,?NumOfSingle);? ????????I2C_EE_WaitEepromStandbyState(); ??????} ????} ??}?? } /* ?*?函數(shù)名:I2C_EE_ByteWrite ?*?描述??:寫(xiě)一個(gè)字節(jié)到I2C?EEPROM中 ?*?輸入??:-pBuffer?緩沖區(qū)指針 ?*?????????-WriteAddr?接收數(shù)據(jù)的EEPROM的地址? ?*?輸出??:無(wú) ?*?返回??:無(wú) ?*?調(diào)用??:外部調(diào)用 ?*/ void?I2C_EE_ByteWrite(u8*?pBuffer,?u8?WriteAddr) { ??/*?Send?STRAT?condition?*/ ??I2C_GenerateSTART(I2C1,?ENABLE); ??/*?Test?on?EV5?and?clear?it?*/ ??while(!I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_MODE_SELECT));?? ??/*?Send?EEPROM?address?for?write?*/ ??I2C_Send7bitAddress(I2C1,?EEPROM_ADDRESS,?I2C_Direction_Transmitter); ?? ??/*?Test?on?EV6?and?clear?it?*/ ??while(!I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); ?????? ??/*?Send?the?EEPROM's?internal?address?to?write?to?*/ ??I2C_SendData(I2C1,?WriteAddr); ?? ??/*?Test?on?EV8?and?clear?it?*/ ??while(!I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_BYTE_TRANSMITTED)); ??/*?Send?the?byte?to?be?written?*/ ??I2C_SendData(I2C1,?*pBuffer);? ??? ??/*?Test?on?EV8?and?clear?it?*/ ??while(!I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_BYTE_TRANSMITTED)); ?? ??/*?Send?STOP?condition?*/ ??I2C_GenerateSTOP(I2C1,?ENABLE); } /* ?*?函數(shù)名:I2C_EE_PageWrite ?*?描述??:在EEPROM的一個(gè)寫(xiě)循環(huán)中可以寫(xiě)多個(gè)字節(jié),但一次寫(xiě)入的字節(jié)數(shù) ?*?????????不能超過(guò)EEPROM頁(yè)的大小。AT24C02每頁(yè)有8個(gè)字節(jié)。 ?*?輸入??:-pBuffer?緩沖區(qū)指針 ?*?????????-WriteAddr?接收數(shù)據(jù)的EEPROM的地址? ?*?????????-NumByteToWrite?要寫(xiě)入EEPROM的字節(jié)數(shù) ?*?輸出??:無(wú) ?*?返回??:無(wú) ?*?調(diào)用??:外部調(diào)用 ?*/ void?I2C_EE_PageWrite(u8*?pBuffer,?u8?WriteAddr,?u8?NumByteToWrite) { ????while(I2C_GetFlagStatus(I2C1,?I2C_FLAG_BUSY));?//?Added?by?Najoua?27/08/2008 ???? ??/*?Send?START?condition?*/ ??I2C_GenerateSTART(I2C1,?ENABLE); ?? ??/*?Test?on?EV5?and?clear?it?*/ ??while(!I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_MODE_SELECT));? ?? ??/*?Send?EEPROM?address?for?write?*/ ??I2C_Send7bitAddress(I2C1,?EEPROM_ADDRESS,?I2C_Direction_Transmitter); ?? ??/*?Test?on?EV6?and?clear?it?*/ ??while(!I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));?? ??/*?Send?the?EEPROM's?internal?address?to?write?to?*/???? ??I2C_SendData(I2C1,?WriteAddr);?? ??/*?Test?on?EV8?and?clear?it?*/ ??while(!?I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_BYTE_TRANSMITTED)); ??/*?While?there?is?data?to?be?written?*/ ??while(NumByteToWrite--)?? ??{ ????/*?Send?the?current?byte?*/ ????I2C_SendData(I2C1,?*pBuffer);? ????/*?Point?to?the?next?byte?to?be?written?*/ ????pBuffer++;? ?? ????/*?Test?on?EV8?and?clear?it?*/ ????while?(!I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_BYTE_TRANSMITTED)); ??} ??/*?Send?STOP?condition?*/ ??I2C_GenerateSTOP(I2C1,?ENABLE); } /* ?*?函數(shù)名:I2C_EE_BufferRead ?*?描述??:從EEPROM里面讀取一塊數(shù)據(jù)。? ?*?輸入??:-pBuffer?存放從EEPROM讀取的數(shù)據(jù)的緩沖區(qū)指針。 ?*?????????-WriteAddr?接收數(shù)據(jù)的EEPROM的地址。? ?*?????????-NumByteToWrite?要從EEPROM讀取的字節(jié)數(shù)。 ?*?輸出??:無(wú) ?*?返回??:無(wú) ?*?調(diào)用??:外部調(diào)用 ?*/ void?I2C_EE_BufferRead(u8*?pBuffer,?u8?ReadAddr,?u16?NumByteToRead) {?? ??//*((u8?*)0x4001080c)?|=0x80;? ????while(I2C_GetFlagStatus(I2C1,?I2C_FLAG_BUSY));?//?Added?by?Najoua?27/08/2008 ???? ???? ??/*?Send?START?condition?*/ ??I2C_GenerateSTART(I2C1,?ENABLE); ??//*((u8?*)0x4001080c)?&=~0x80; ?? ??/*?Test?on?EV5?and?clear?it?*/ ??while(!I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_MODE_SELECT)); ??/*?Send?EEPROM?address?for?write?*/ ??I2C_Send7bitAddress(I2C1,?EEPROM_ADDRESS,?I2C_Direction_Transmitter); ??/*?Test?on?EV6?and?clear?it?*/ ??while(!I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); ?? ??/*?Clear?EV6?by?setting?again?the?PE?bit?*/ ??I2C_Cmd(I2C1,?ENABLE); ??/*?Send?the?EEPROM's?internal?address?to?write?to?*/ ??I2C_SendData(I2C1,?ReadAddr);?? ??/*?Test?on?EV8?and?clear?it?*/ ??while(!I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_BYTE_TRANSMITTED)); ?? ??/*?Send?STRAT?condition?a?second?time?*/?? ??I2C_GenerateSTART(I2C1,?ENABLE); ?? ??/*?Test?on?EV5?and?clear?it?*/ ??while(!I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_MODE_SELECT)); ?? ??/*?Send?EEPROM?address?for?read?*/ ??I2C_Send7bitAddress(I2C1,?EEPROM_ADDRESS,?I2C_Direction_Receiver); ?? ??/*?Test?on?EV6?and?clear?it?*/ ??while(!I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); ?? ??/*?While?there?is?data?to?be?read?*/ ??while(NumByteToRead)?? ??{ ????if(NumByteToRead?==?1) ????{ ??????/*?Disable?Acknowledgement?*/ ??????I2C_AcknowledgeConfig(I2C1,?DISABLE); ?????? ??????/*?Send?STOP?Condition?*/ ??????I2C_GenerateSTOP(I2C1,?ENABLE); ????} ????/*?Test?on?EV7?and?clear?it?*/ ????if(I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_BYTE_RECEIVED))?? ????{?????? ??????/*?Read?a?byte?from?the?EEPROM?*/ ??????*pBuffer?=?I2C_ReceiveData(I2C1); ??????/*?Point?to?the?next?location?where?the?byte?read?will?be?saved?*/ ??????pBuffer++;? ?????? ??????/*?Decrement?the?read?bytes?counter?*/ ??????NumByteToRead--;???????? ????}??? ??} ??/*?Enable?Acknowledgement?to?be?ready?for?another?reception?*/ ??I2C_AcknowledgeConfig(I2C1,?ENABLE); } /* ?*?函數(shù)名:I2C_EE_WaitEepromStandbyState ?*?描述??:Wait?for?EEPROM?Standby?state? ?*?輸入??:無(wú) ?*?輸出??:無(wú) ?*?返回??:無(wú) ?*?調(diào)用??:? ?*/ void?I2C_EE_WaitEepromStandbyState(void)?????? { ??vu16?SR1_Tmp?=?0; ??do ??{ ????/*?Send?START?condition?*/ ????I2C_GenerateSTART(I2C1,?ENABLE); ????/*?Read?I2C1?SR1?register?*/ ????SR1_Tmp?=?I2C_ReadRegister(I2C1,?I2C_Register_SR1); ????/*?Send?EEPROM?address?for?write?*/ ????I2C_Send7bitAddress(I2C1,?EEPROM_ADDRESS,?I2C_Direction_Transmitter); ??}while(!(I2C_ReadRegister(I2C1,?I2C_Register_SR1)?&?0x0002)); ?? ??/*?Clear?AF?flag?*/ ??I2C_ClearFlag(I2C1,?I2C_FLAG_AF); ????/*?STOP?condition?*/???? ????I2C_GenerateSTOP(I2C1,?ENABLE);? } /*************************END?OF?FILE*************************************/