初中生
最后登录1970-1-1
在线时间 小时
注册时间2021-1-24
|
问题:通过IIC往EEPROM中写入一个数据 ,但是从该地址读取的数据总为0.
代码如下:
static void I2C_GPIO_Config(void)
{
GPIO_InitTypeDef I2C_GPIO_InitStruct;
RCC_AHB1PeriphClockCmd(I2C_SCL_CLK|I2C_SDA_CLK, ENABLE);
GPIO_PinAFConfig(I2C_SDA_PORT, I2C_SDA_PINSOURCE, GPIO_AF_I2C1);
GPIO_PinAFConfig(I2C_SCL_PORT, I2C_SCL_PINSOURCE, GPIO_AF_I2C1);
I2C_GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
I2C_GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; /*经测试必须是OD输出,而不能是PP输出*/
I2C_GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
I2C_GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
I2C_GPIO_InitStruct.GPIO_Pin = I2C_SCL_PIN;
GPIO_Init(I2C_SCL_PORT, &I2C_GPIO_InitStruct);
I2C_GPIO_InitStruct.GPIO_Pin = I2C_SDA_PIN;
GPIO_Init(I2C_SDA_PORT, &I2C_GPIO_InitStruct);
}
void I2C_Config(void)
{
I2C_GPIO_Config();
RCC_APB1PeriphClockCmd(I2C_CLK, ENABLE);
I2C_InitTypeDef I2C_InitStruct;
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_ClockSpeed = 400000;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable ;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 = 0xA0;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStruct);
I2C_Cmd (I2C1, ENABLE);
}
static uint32_t ErrorCallback(u8 error_code)
{
printf("\r\nerror code:%hhd\n\r", error_code);
return 1;
}
/*
@brief: 写入一个字节
@para1: 指向1个字节数据源的指针
@retval: 状态值,0表示正常
*/
uint8_t I2C_SendByte(uint8_t* pBuffer, uint8_t Addr_of_EE)
{
////////////////////
I2C_GenerateSTART(I2C, ENABLE);
uint32_t timer = TIMEOUT;
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_MODE_SELECT))
{
timer--;
if(timer == 0) ErrorCallback(1);
}
////////////////////
I2C_Send7bitAddress(I2C, EEPROM_ADDR, I2C_Direction_Transmitter);
timer = TIMEOUT;
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
timer--;
if(timer == 0 ) ErrorCallback(2);
}
////////////////////////
I2C_SendData(I2C, Addr_of_EE);
timer = TIMEOUT;
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
timer--;
if(timer == 0 ) ErrorCallback(3);
}
I2C_SendData(I2C, *pBuffer);
timer = TIMEOUT;
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
timer--;
if(timer == 0 ) ErrorCallback(4);
}
I2C_GenerateSTOP(I2C, ENABLE);
return 0;
}
/*
@brief: 读取字节
@
@retval:
*/
uint8_t I2C_EE_Read(uint8_t* pDstBuffer, uint8_t Addr_of_EE, uint16_t size)
{
Wait_EE_Ready();
I2C_GenerateSTART(I2C, ENABLE);
uint32_t timer = TIMEOUT;
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_MODE_SELECT))
{
timer--;
if(timer == 0) ErrorCallback(9);
}
I2C_Send7bitAddress(I2C, EEPROM_ADDR, I2C_Direction_Transmitter);
timer = TIMEOUT;
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
timer--;
if(timer == 0) ErrorCallback(10);
}
I2C_SendData(I2C, Addr_of_EE);
timer = TIMEOUT;
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if(timer-- == 0 ) ErrorCallback(11);
}
Wait_EE_Ready();
I2C_GenerateSTART(I2C, ENABLE);
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_MODE_SELECT))
{
timer--;
if(timer == 0) ErrorCallback(12);
}
I2C_Send7bitAddress(I2C, EEPROM_ADDR, I2C_Direction_Receiver);
timer =TIMEOUT;
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if(timer-- == 0) ErrorCallback(13);
}
I2C_AcknowledgeConfig(I2C, ENABLE);
while(size)
{
if(size == 1)
{
I2C_AcknowledgeConfig(I2C, DISABLE);
I2C_GenerateSTOP(I2C, ENABLE);
}
timer =TIMEOUT;
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
if(timer-- == 0) ErrorCallback(14);
}
*pDstBuffer = I2C_ReceiveData(I2C);
pDstBuffer++;
size--;
}
I2C_AcknowledgeConfig(I2C, ENABLE);
return 0;
}
求大神帮忙debug
|
|