我写了一个I2C读取的函数,用最简单的查询,首先我已经证明该函数是可以正常运行,然后给他改了一下,添加了一个变量I2C_TypeDef* I2Cx,想让他可以被任何I2C口读取。然后我把I2Cx赋值为I2C1(PS:此函数在I2C1下已经可以可以使用),就无法运行,好像I2Cx根本没有被替换成I2C1,求大神指教,代码如下
- void I2C_Query_Read(I2C_TypeDef* I2Cx,u8 IMU_Address,u8 Address,u8 *IMU_Data,u8 NumByteToRead)
- {
- /*I2C_TypeDef* I2Cx;
- #if USED_I2C1
- I2Cx = I2C1;
- #endif */
- /********if I2C BUS busy,wait*********/
- time_Out = Time_limit;
- while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
- (Time_Out--)?delay_ms(1):I2C_TimeOut(I2Cx);
- /**********start I2C*********/
- I2C_GenerateSTART(I2Cx, ENABLE);
- /***********check the EV5********/
- Time_Out = Time_limit;
- while(!I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_MODE_SELECT ))
- (Time_Out--)?delay_ms(1):I2C_TimeOut(I2Cx);
- /************send Address to I2C*******/
- I2C_Send7bitAddress(I2Cx,IMU_Address, I2C_Direction_Transmitter);
- /***********check EV6***********/
- Time_Out = Time_limit;
- while(!I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
- (Time_Out--)?delay_ms(1):I2C_TimeOut(I2Cx);
-
- I2C_Cmd(I2Cx, ENABLE);
- /**********send IMU's register Address************/
- I2C_SendData(I2Cx, Address);
- /************clean EV8*****************************/
- Time_Out = Time_limit;
- while(!I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_BYTE_TRANSMITTED ))
- (Time_Out--)?delay_ms(1):I2C_TimeOut(I2Cx);
- /***********Start I2C again*********/
- I2C_GenerateSTART(I2Cx, ENABLE);
- Time_Out = Time_limit;
- while(!I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_MODE_SELECT ))
- (Time_Out--)?delay_ms(1):I2C_TimeOut(I2Cx);
-
- I2C_Send7bitAddress(I2Cx,IMU_Address, I2C_Direction_Receiver);
- Time_Out = Time_limit;
- while(!I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
- (Time_Out--)?delay_ms(1):I2C_TimeOut(I2Cx);
- /* While there is data to be read */
- while(NumByteToRead)
- {
- if(NumByteToRead == 1)
- {
- /* Disable Acknowledgement */
- I2C_AcknowledgeConfig(I2Cx, DISABLE);
- /* Send STOP Condition */
- I2C_GenerateSTOP(I2Cx, ENABLE);
- }
- /* Test on EV7 and clear it */
- if(I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))
- {
- /* Read a byte from the MPU */
- *IMU_Data = I2C_ReceiveData(I2Cx);
- /* Point to the next location where the byte read will be saved */
- IMU_Data++;
- /* Decrement the read bytes counter */
- NumByteToRead--;
- }
- }
- /* Enable Acknowledgement to be ready for another reception */
- I2C_AcknowledgeConfig(I2Cx, ENABLE);
-
-
- }
复制代码
一周热门 更多>