一個24C讀寫函數(shù),支持檢測器件型號
上代碼:
/* FILE 24C.CWrite by 萬致遠*/#include#include #include #include #include <24c.h>#define AT24C02_DeviceAdd_W 0xa0 //器件地址(寫)#define AT24C02_DeviceAdd_R 0xa1 //器件地址(讀)#define ADD_TYPE_8 0#define ADD_TYPE_16 1#define AT24C01 0#define AT24C02 1#define AT24C04 2#define AT24C08 3#define AT24C16 4#define AT24C32 5#define AT24C64 6#define AT24C128 7#define AT24C256 8#define AT24C512 9#define AT24C1024 10#define SCAN_ERROR 0xFFULONG __code Length_Chip[11]={0x7f,0xff,0x1ff,0x3ff,0xfff,0x7ff,0x1fff,0x3fff,0x7fff,0xffff,0x1ffff};//各類ROM的容量表~ BYTE __code TEST_DATA[2]={0xAA,0xBB}; //測試數(shù)據(jù)#define W_DELAY 4 //4 ms DELAY //----------------------------------------------//24C操作函數(shù),頁寫。//IIC_ADDR器件地址//Address 16BIT地址//TYPE 地址模式//Data指針//Len長度//本函數(shù)不支持翻頁,請自行翻頁,控制IIC_AddrBOOL RW24(BYTE IIC_Addr,UINT Address,BOOL Type,BYTE *Data,BYTE Len){BYTE i=0;//----------------------------選中芯片程序開始-----------I2C_Start(); // 啟動I2C總線I2C_Write8Bit(IIC_Addr & 0xFE); // 發(fā)送器件地址(寫)1111 1110if( I2C_Check_Ack() == 0 ) // 檢測從機應(yīng)答{I2C_Stop();return 0; }if (Type==ADD_TYPE_8){//8位地址的話I2C_Write8Bit(Address); // 發(fā)送將要處理的數(shù)據(jù)的地址,8位}else{//16位地址的話I2C_Write8Bit(((Address & 0xFF00)>>8)); //發(fā)送高八位地址if( I2C_Check_Ack() == 0 ) // 檢測從機應(yīng)答{I2C_Stop();return 0; }I2C_Write8Bit((Address & 0xFF)); //低八位地址發(fā)送}if( I2C_Check_Ack() == 0 ) // 檢測從機應(yīng)答{I2C_Stop();return 0; }//---------------------------------結(jié)束-------------------if((IIC_Addr & 0x01)==1){//是讀操作*Data=0xFF; //FF=NULLI2C_Start(); // 再次啟動IIC總線I2C_Write8Bit(IIC_Addr); //發(fā)送讀出地址if( I2C_Check_Ack() == 0 ) // 檢測從機應(yīng)答{I2C_Stop();return 0; }*Data = I2C_Read8Bit(); // 讀取第一個數(shù)據(jù)Data++;for(i=1;i
評論