附上i2c读写24c的程序参考,基本上按照开发板上示例搬来的.
附上i2c读写24c的程序参考,基本上按照开发板上示例搬来的.
ErrorStatus I2C_24c16_Status(void) { u32 I2C_TimeOut = 0x3FFFF;
/* Clear the I2C1 AF flag */ I2C_ClearFlag(I2C1, I2C_FLAG_AF);
/* Enable I2C1 acknowledgement if it is already disabled by other function */ I2C_AcknowledgeConfig(I2C1, ENABLE);
/*----- Transmission Phase -----*/ /* Send I2C1 START condition */ I2C_GenerateSTART(I2C1, ENABLE); /* Test on I2C1 EV5 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */ { } //I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT); /* Send STLM75 slave address for write */ I2C_Send7bitAddress(I2C1, AT24C512_Addr, I2C_Direction_Transmitter); while((!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) && I2C_TimeOut)/* EV6 */ { I2C_TimeOut--; }
if(I2C_GetFlagStatus(I2C1, I2C_FLAG_AF) != 0x0) { return ERROR; } else { return SUCCESS; } }
/******************************************************************************* * Function Name : Read24c16 * Description : Displays the temperature in Celsius and fahrenheit degree. * Input : None * Output : None * Return : None *******************************************************************************/ void Read24c16(u8 *RamAddress,u16 RomAddress,u8 bytes) {
if(I2C_24c16_Status() == SUCCESS) { I2C_SendData(I2C1, (u8)(RomAddress >> 8)); /* Test on I2C1 EV8 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */ { } I2C_SendData(I2C1, (u8)(RomAddress &0xff)); /* Test on I2C1 EV8 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */ { } /*----- Reception Phase -----*/ /* Send Re-STRAT condition */ I2C_GenerateSTART(I2C1, ENABLE); /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */ { } /* Send STLM75 slave address for read */ I2C_Send7bitAddress(I2C1, AT24C512_Addr, I2C_Direction_Receiver); /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) /* EV6 */ { }
/* Test on EV7 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */ { } /* Store I2C1 received data */ while(bytes!=1) { *RamAddress=I2C_ReceiveData(I2C1); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */ { } RamAddress++; bytes--; } /* Disable I2C1 acknowledgement */ I2C_AcknowledgeConfig(I2C1, DISABLE); /* Send I2C1 STOP Condition */ I2C_GenerateSTOP(I2C1, ENABLE); /* Test on EV7 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */ { } /* Store I2C1 received data */ *RamAddress=I2C_ReceiveData(I2C1); /* Return Temperature value */ } } void Write24c16(u8 *Wdata,u16 RomAddress,u8 bytes) { if(I2C_24c16_Status() == SUCCESS) { I2C_SendData(I2C1, (u8)(RomAddress >> 8)); /* Test on I2C1 EV8 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */ { } I2C_SendData(I2C1, (u8)(RomAddress &0xff)); /* Test on I2C1 EV8 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */ { } for(;bytes!=0;bytes--) { // Write8Bit(*Wdata); I2C_SendData(I2C1, *Wdata++); /* Test on I2C1 EV8 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */ { } } /* Send I2C1 STOP Condition */ I2C_GenerateSTOP(I2C1, ENABLE); Delay(10); } } |
|