本帖最后由 15698208161 于 2017-8-1 18:44 编辑
主要问题是STM32硬件I2C中SR寄存器的ADDR位一直读不出来,经过断点调试后发现
运行完语句I2C_Send7bitAddress(EEPROM_I2Cx, EEPROM_ADDRESS, I2C_Direction_Transmitter);后即使SR1寄存器是0x0482,即ADDR位为1,
往下执行SR1_Tmp = I2C_ReadRegister(EEPROM_I2Cx, I2C_Register_SR1);的时候ADDR会跳变为0,一开始以为是读操作会使ADDR为0,结果
发现读出来的的SR1寄存器的ADDR也位始终为0。把SR1_Tmp = I2C_ReadRegister(EEPROM_I2Cx, I2C_Register_SR1);换成
SR1_Tmp =I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED);或者
SR1_Tmp =I2C_GetFlagStatus (EEPROM_I2C,I2C_FLAG_ADDR);也不行。求各位大神给个解释。这个bug是发生在I2C_EE_ByteWrite函数中的。
主程序如下:int main(void)
{
// 程序来到main函数之前,启动文件:statup_stm32f10x_hd.s已经调用
// SystemInit()函数把系统时钟初始化成72MHZ
// SystemInit()在system_stm32f10x.c中定义
// 如果用户想修改系统时钟,可自行编写程序修改
uint8_t String[20]="transmit begin\n";
uint8_t String1[20]="transmit complete\n";
uint8_t String2[20]="receive begin\n";
uint8_t String3[20]="receive complete\n";
uint8_t aChar=0;
USART_Config();
I2C_EEPROM_config();
USART_SendString(DEBUG_USARTx,String);
EEPROM_ByteWrite(0x00,'a');
USART_SendString(DEBUG_USARTx,String1);
EEPROM_WaitStandbyState();
USART_SendString(DEBUG_USARTx,String2);
aChar=EEPROM_ByteRead(0x00);
USART_SendString(DEBUG_USARTx,String3);
USART_SendByte(DEBUG_USARTx,aChar);
while(1)
{
}
}
I2C_EE_ByteWrite函数功能是等待I2C准备好,如下:
void I2C_EE_WaitEepromStandbyState(void)
{
vu16 SR1_Tmp = 0;
do
{
/* Send START condition */
I2C_GenerateSTART(EEPROM_I2Cx, ENABLE);
/* Send EEPROM address for write */
I2C_Send7bitAddress(EEPROM_I2Cx, EEPROM_ADDRESS, I2C_Direction_Transmitter);
/* Read I2C1 SR1 register */
SR1_Tmp = I2C_ReadRegister(EEPROM_I2Cx, I2C_Register_SR1 & 0x0002));
}while(!SR1_Tmp);
/* Clear AF flag */
I2C_ClearFlag(EEPROM_I2Cx, I2C_FLAG_AF);
/* STOP condition */
I2C_GenerateSTOP(EEPROM_I2Cx, ENABLE);
}
调试信息如下:
|