2011-12-06 09:19 AM
Hello. I have a device connected to stm32 through I2C1. Am I right initialize it?
&sharpdefine DEV_ADDR 0x53
uint8_t I2C_ByteRead(uint8_t Addr){ uint8_t tmp; while(I2C_GetFlagStatus(I2CG, I2C_FLAG_BUSY)); // While the bus is busy I2C_GenerateSTART(I2CG, ENABLE); // Send START condition while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_MODE_SELECT)); // Test on EV5 and clear it I2C_Send7bitAddress(I2CG, DEV_ADDR<< 1, I2C_Direction_Transmitter); // Send address for write (SAD+W) while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); // Test on EV6 and clear it I2C_SendData(I2CG, Addr); // Send the internal address to read from: MSB of the address first while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); // Test on EV8 and clear it I2C_GenerateSTART(I2CG, ENABLE); // Send STRAT condition a second time while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_MODE_SELECT)); // Test on EV5 and clear it I2C_Send7bitAddress(I2CG, DEV_ADDR<< 1, I2C_Direction_Receiver); // Send address for read while(!I2C_CheckEvent(I2CG,I2C_EVENT_MASTER_BYTE_RECEIVED)); // Test on EV6 and clear it tmp=I2C_ReceiveData(I2CG); I2C_AcknowledgeConfig(I2CG, DISABLE); I2C_GenerateSTOP(I2CG, ENABLE); // Send STOP Condition return tmp;}void I2CG_Init(void){ GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // I2CG clock enable RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2CG, ENABLE); // GPIOB clock enable // I2CG SCL and SDA configuration GPIO_InitStructure.GPIO_Pin = SCL|SDA; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);// GPIO_PinAFConfig(GPIOC,SCLSource,GPIO_AF_I2CG); // GPIO_PinAFConfig(GPIOC,SDASource,GPIO_AF_I2CG); // Enable I2CG reset state RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2CG, ENABLE); // Release I2CG from reset state RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2CG, DISABLE); // I2C_DeInit(I2C1); 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 = ClockSpeed; I2C_AcknowledgeConfig(I2CG, ENABLE); I2C_Init(I2CG, &I2C_InitStructure); I2C_Cmd(I2CG, ENABLE);}I can't read a device ID(0xE5)if(I2C_ByteRead(0x00)!=0xE5)
I always stuck atwhile(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); in fuction ''
I2C_ByteRead''
Please help me understand what I'm doing wrong. #stm32 #i2c2011-12-08 01:41 AM
Are you facing some problem with this?
2011-12-08 05:26 AM
evidentlu <i> can't read a device ID(0xE5)</i>
2011-12-08 05:26 AM
evidently <i> can't read a device ID(0xE5)</i>
2011-12-08 05:28 AM
sorry about the many replies, had trouble with the connection
2011-12-08 07:23 AM
sorry about the many replies, had trouble with the connection
The forum has been very flaky over the last couple of days, save your work people!2011-12-12 02:32 PM
Sorry for the late answer. My problem solved.