就三个函数 简单明了 初始化 读 写
int main(void)
{
u8 i;
SystemInit();
Iic1_Init();
LED_GPIO_Config();
I2C1_WriteByte(0xA0,1,0x89); //写EEPROM
i=I2C1_ReadByte(0xA0,1); //读EEPROM
if(i==0x89)
GPIO_ResetBits(GPIOB, GPIO_Pin_1);
while (1);
}
初始化
void Iic1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
//PB6-I2C1_SCL PB7-I2C1_SDA PB10-I2C2_SCL PB11-I2C2_SDA
/* Configure IO connected to IIC*********************/
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_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2C2_SLAVE_ADDRESS7;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 400000;
I2C_Cmd(I2C1, ENABLE);
I2C_Init(I2C1, &I2C_InitStructure);
/*允许1字节1应答模式*/
I2C_AcknowledgeConfig(I2C1, ENABLE);
}
/**********************************************************************/
/*IIC写一个字节 */
/* */
/**********************************************************************/
void I2C1_WriteByte(unsigned char id,unsigned char write_address,unsigned char byte)
{
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
//产生起始条件
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
//等待ACK
I2C_Send7bitAddress(I2C1,id,I2C_Direction_Transmitter);
//向设备发送设备地址
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//等待ACK
I2C_SendData(I2C1, write_address);
//寄存器地址
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//等待ACK
I2C_SendData(I2C1, byte);
//发送数据
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//发送完成
I2C_GenerateSTOP(I2C1, ENABLE);
//产生结束信号
do
{
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Read I2C1 SR1 register */
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, 0xA0, I2C_Direction_Transmitter);
}while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002));
/* Clear AF flag */
I2C_ClearFlag(I2C1, I2C_FLAG_AF);
/* STOP condition */
I2C_GenerateSTOP(I2C1, ENABLE);
}
/**********************************************************************/
/*IIC读数据 */
/* */
/**********************************************************************/
unsigned char I2C1_ReadByte(unsigned char id, unsigned char read_address)
{
unsigned char temp;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
//等待I2C
I2C_GenerateSTART(I2C1, ENABLE);
//产生起始信号
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
//EV5
I2C_Send7bitAddress(I2C1, id, I2C_Direction_Transmitter);
//发送地址
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//EV6
I2C_Cmd(I2C1, ENABLE);
//重新设置可以清楚EV6
I2C_SendData(I2C1, read_address);
//发送读得地址
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//EV8
I2C_GenerateSTART(I2C1, ENABLE);
//重新发送
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
//EV5
I2C_Send7bitAddress(I2C1, id, I2C_Direction_Receiver);
//发送读地址
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
//EV6
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
//关闭应答和停止条件产生
while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)));
temp = I2C_ReceiveData(I2C1);
I2C_AcknowledgeConfig(I2C1, ENABLE);
return temp;
}
只测试了EEPROM 正在测试IIC的 其他芯片
转载
|