#define ADDR_24CXX 0xA0
void I2C_24CXX_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure;
/* GPIOB Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE); /* I2C1 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* Configure I2C1 pins: SCL and 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; GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_DeInit(I2C1);
/* I2C1 Init */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x00; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 200000; I2C_Init(I2C1, &I2C_InitStructure);
/* I2C1 Init */ I2C_Cmd(I2C1, ENABLE); }
//void I2C_24C512_Write(u8 RegName, u16 RegValue) void I2C_24CXX_Write(u16 nAddr, u8* pDat, u16 nLen) { u16 i = 0; /* 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 */ { }
/* Send slave address for write */ I2C_Send7bitAddress(I2C1, ADDR_24CXX, I2C_Direction_Transmitter);
/* Test on I2C1 EV6 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6 */ { }
/* Send the high part of memory address */ /* I2C_SendData(I2C1, (u8)(nAddr>>8));
// Test on I2C1 EV8 and clear it while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) // EV8 { } */
/* Send the low part of memory address */ I2C_SendData(I2C1, (u8)nAddr);
/* Test on I2C1 EV8 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */ { }
for(i=0;i<nLen;i++) { /* Send I2C1 data */ I2C_SendData(I2C1, pDat);
/* 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); }
void I2C_24CXX_Read(u16 nAddr, u8* pDat, u16 nLen) { u16 i = 0; /* 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 */ { }
/* Send 24CXX slave address for write */ I2C_Send7bitAddress(I2C1, ADDR_24CXX, I2C_Direction_Transmitter);
/* Test on I2C1 EV6 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6 */ { }
/* // Send the high part of memory address I2C_SendData(I2C1, (u8)(nAddr>>8));
// Test on I2C1 EV8 and clear it while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) // EV8 { } */ /* Send the low part of memory address */ I2C_SendData(I2C1, (u8)nAddr);
/* 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 24CXX slave address for read */ I2C_Send7bitAddress(I2C1, ADDR_24CXX, I2C_Direction_Receiver);
/* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) /* EV6 */ { }
for(i=0;i<nLen;i++) { if((i+1)>=nLen) { /* 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 */ pDat = I2C_ReceiveData(I2C1); } } |