新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 一個24C讀寫函數(shù),支持檢測器件型號

一個24C讀寫函數(shù),支持檢測器件型號

作者: 時間:2016-11-29 來源:網(wǎng)絡(luò) 收藏
//------------------------------------------------------------------------------------------------------//--檢測24C32器件的地址溢出//BOOL RW24(BYTE IIC_Addr,UINT Address,BOOL Type,BYTE *Data);//ADDRESS 2為猜測的最高地址BOOL Write_Test(UINT Address2,BOOL Add_Type){BYTE tmp[3];RW24(AT24C02_DeviceAdd_R,0,Add_Type,&tmp[0],1);//讀出0x0000單元數(shù)據(jù)RW24(AT24C02_DeviceAdd_R,Address2,Add_Type,&tmp[1],1);//讀出0x1000單元數(shù)據(jù)RW24(AT24C02_DeviceAdd_W,0,Add_Type,&TEST_DATA[1],1);//改變原有數(shù)據(jù),避免干擾RW24(AT24C02_DeviceAdd_W,Address2,Add_Type,&TEST_DATA[0],1);//寫入0xAA到單元去,產(chǎn)生地址溢出RW24(AT24C02_DeviceAdd_R,0,Add_Type,&tmp[2],1);//讀出0000單元內(nèi)容RW24(AT24C02_DeviceAdd_W,0,Add_Type,&tmp[0],1);//寫回數(shù)據(jù)if(tmp[2]==0xAA)//先前改變的數(shù)據(jù),如果產(chǎn)生溢出可以檢測到{//RW24(AT24C02_DeviceAdd_W,0x0000,Add_Type,&tmp[0],1);//寫回被(1)步驟更改的數(shù)據(jù)return TRUE;//返回正確}//RW24(AT24C02_DeviceAdd_W,0x0000,ADD_TYPE_16,&tmp[0],1);//寫回數(shù)據(jù)RW24(AT24C02_DeviceAdd_W,Address2,Add_Type,&tmp[1],1);//寫回數(shù)據(jù)return FALSE;}//------------調(diào)用此函數(shù)返回芯片類型BYTE Test24(){//測試24系列的容量//24C16:Address=1010 P2 P1 P0 R/WBYTE tmp[3];//// read:// 8 Bit series: |start|address_w|address|start|address_r|data|stop|// 16 bit series:|start|address_w|address|address|start|address_r|data|stop|// TEST: |START|address_w|address|start|address_r|data|stop|// write:// 8bit series: |start|address_w|address|data|stop|// 16bit series: |start|address_w|address|address|data|stop|// tmp[0]=0xff;if(RW24(AT24C02_DeviceAdd_R,0x00,ADD_TYPE_8,&tmp[0],1)==TRUE)//讀出0單元{//判斷是否是8bit seriesRW24(AT24C02_DeviceAdd_W,0x00,ADD_TYPE_8,&TEST_DATA[1],1);//寫入AARW24(AT24C02_DeviceAdd_R,0x00,ADD_TYPE_8,&tmp[1],1);//再次讀出 if(tmp[1]==0xBB){//8bit series無誤//我發(fā)現(xiàn)我還是在實際上出錯了%……實際上寫頁還是寫的進可是只有一個頁……RW24(AT24C02_DeviceAdd_W,0x00,ADD_TYPE_8,&tmp[0],1);//寫回數(shù)據(jù),RW搞錯了//測試是否24C01//24C01的地址范圍從0~127(0~7F),OK,向80H讀出,然后寫一個數(shù)據(jù)/*RW24(AT24C02_DeviceAdd_R,0x0000,ADD_TYPE_8,&tmp[0],1);//讀出0x0000單元數(shù)據(jù)【0】RW24(AT24C02_DeviceAdd_R,0x0080,ADD_TYPE_8,&tmp[1],1);//讀出0x80單元數(shù)據(jù)【1】RW24(AT24C02_DeviceAdd_W,0x0000,ADD_TYPE_8,&TEST_DATA[1],1);//寫入0000單元防止原來數(shù)據(jù)干擾RW24(AT24C02_DeviceAdd_W,0x0080,ADD_TYPE_8,&TEST_DATA[0],1);//寫入0000單元防止原來數(shù)據(jù)干擾RW24(AT24C02_DeviceAdd_R,0x0000,ADD_TYPE_8,&tmp[2],1);//寫入0000單元防止原來數(shù)據(jù)干擾RW24(AT24C02_DeviceAdd_W,0x0000,ADD_TYPE_8,&tmp[0],1);//寫回0單元數(shù)據(jù)if(tmp[2]==0xAA){//24C01return AT24C01;}*/if(Write_Test(0x80,ADD_TYPE_8)){return AT24C01;}//這應(yīng)該是24C02 etc……絕對不可能是24C01,除非器件的80H單元正好壞了//24C16:Address=1010 P2 P1 P0 R/W//RW24(AT24C02_DeviceAdd_W,0x00,ADD_TYPE_8,&tmp[0],1);//寫回原來的數(shù)據(jù)到0x80//RW24(AT24C02_DeviceAdd_W,0x80,ADD_TYPE_8,&tmp[1],1);//寫回原來的數(shù)據(jù)0x00//----------------------------------------------------------------//MAX——PAGE 02 04 08 16// 000 001 011 111//-------TEST 24C02if(Page_Test(0x02)){return AT24C02;}if(Page_Test(0x06)){return AT24C04;}if(Page_Test(0x0E)){return AT24C08;}return AT24C16; }else{//AT24C32的地址范圍從0~4095(0xFFF)//AT24C64從0~8191(0x1FFF)//AT24C128從0~16383(0x3FFF)//AT24C256從0~32767(0x7FFF)//AT24C512從0~65535(0xFFFF)//AT24C1024從0~65535(0xFFFF),地址中多一個P1選擇位//從小試到大//測試24C1024//1010 0010 = 0x02//就是產(chǎn)生傳說中的地址溢出了……//-----24C1024if(RW24(AT24C02_DeviceAdd_R | 0x02,0xFFFF,ADD_TYPE_16,&tmp[0],1))//讀出第二頁的0xFFFE{//成功代表是24C1024return AT24C1024;}//優(yōu)化算法//-----24C32if(Write_Test(0x1000,ADD_TYPE_16)){return AT24C32;}//24C64if(Write_Test(0x2000,ADD_TYPE_16)){return AT24C64;}//24C128if(Write_Test(0x4000,ADD_TYPE_16)){return AT24C128;}//24C256if(Write_Test(0x8000,ADD_TYPE_16)){return AT24C32;}//-------------24C512*/return AT24C512;}}else{return SCAN_ERROR;}}

完整版本的代碼下載地址:http://www.51hei.com/f/dfd33.rar

本文引用地址:http://m.butianyuan.cn/article/201611/323429.htm

上一頁 1 2 下一頁

評論


技術(shù)專區(qū)

關(guān)閉