|
ST网站上的例程是读写24c08的,他的读函数有问题,原来我在51下一直模拟I2C操作的,用起来心里很有数,按照模拟I2C的步骤重写了读写函数,连续读写,自动处理跨页问题,很容易修改成24C08/32/128/512的读写函数,把我的代码公布如下: void I2C_AcknowledgePolling(void){ do{ I2C_GenerateSTART(I2C1,ENABLE); I2C_ReadRegister(I2C1,I2C_Register_SR1);//清除ADDR位
I2C_Send7bitAddress(I2C1,0xa0,I2C_Direction_Transmitter); }while(!(I2C_ReadRegister(I2C1,I2C_Register_SR1)&0x0002)); I2C_ClearFlag(I2C1,I2C_FLAG_AF); } void I2C_SequentialRead_AT24C01_02(u8 sla,u8 suba,u8 *pHead,u8 len){ u8 i;
I2C_AcknowledgePolling(); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_SendData(I2C1,sla); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,suba); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_SendData(I2C1,sla+1); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
if(len>1){ for(i=0;i<len-1;i++){ while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)); *pHead=I2C_ReceiveData(I2C1); I2C_AcknowledgeConfig(I2C1,ENABLE); pHead++; } } while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)); *pHead=I2C_ReceiveData(I2C1);
I2C_AcknowledgeConfig(I2C1,DISABLE); I2C_GenerateSTOP(I2C1,ENABLE); } void I2C_PageWrite_AT24C01_02(u8 sla,u8 suba,u8 *pHead,u8 len){ u8 wLen,addr; wLen=len; addr=suba; ScrollPage:I2C_AcknowledgePolling(); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C_SendData(I2C1,sla); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,addr); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_SendData(I2C1,*pHead++); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); addr++; wLen--; while(wLen){ if((addr%8)!=0){ I2C_SendData(I2C1,*pHead++); while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); addr++; wLen--; } else{ I2C_GenerateSTOP(I2C1,ENABLE); goto ScrollPage; } } I2C_GenerateSTOP(I2C1, ENABLE); }
|
|