24C256读写失败 24C256读写失败 程序停止在了while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); 测试i2c引脚都为低电平
//------------------------EEPROM 24C256 API--------------------------- void GPIO_I2c_Init(void){ GPIO_InitTypeDef GPIO_InitStruct; //CLOCK RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB, ENABLE); //----------------------------------------------------------- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStruct);
}
void I2C_24c256Init(void) { I2C_InitTypeDef I2C_InitStruct; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); GPIO_I2c_Init(); I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; I2C_InitStruct.I2C_ClockSpeed = I2C_Speed; I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // I2C_InitStruct.I2C_OwnAddress1 = I2C_Slave_Adress7; I2C_Init(I2C1,&I2C_InitStruct); I2C_AcknowledgeConfig (I2C1,ENABLE); I2C_Cmd(I2C1,ENABLE); }
void I2C_24c256ByteWrite(u16 WriteAddr,unsigned char Byte ) { NVIC_SETPRIMASK(); // Start the I2C I2C_GenerateSTART(I2C1,ENABLE);
//not recommanded, stupid way while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,EE24C256_ADDW,I2C_Direction_Transmitter);
// when get ACK, means Set Success while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //------------------------------地址高位 I2C_SendData(I2C1, (WriteAddr>>8));
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //------------------------------地址低位 I2C_SendData(I2C1, WriteAddr);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, Byte);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_GenerateSTOP(I2C1, ENABLE); NVIC_RESETPRIMASK();//开中断asm("CPSIE I"); }
void I2C_24c256ByteRead(u16 WriteAddr,unsigned char *Byte ) { NVIC_SETPRIMASK(); // Start the I2C I2C_GenerateSTART(I2C1,ENABLE); //not recommanded, stupid way while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,EE24C256_ADDW,I2C_Direction_Transmitter);
// when get ACK, means Set Success while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //------------------------------地址高位 I2C_SendData(I2C1, (WriteAddr>>8));
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //------------------------------地址低位 I2C_SendData(I2C1, WriteAddr);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_Send7bitAddress(I2C1,EE24C256_ADDR,I2C_Direction_Transmitter);
// when get ACK, means Set Success while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); *Byte = I2C_ReceiveData(I2C1);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)); I2C_GenerateSTOP(I2C1, ENABLE); NVIC_RESETPRIMASK(); } 硬件没有问题,我使用模拟i2c的方法可以正常的读写 查看寄存器 busy 一直为 1 |
|