STM32F4 I2C EV6卡住了

时间:2016-05-09 16:31:07

标签: stm32f4discovery

我正在完成我的最后一年项目,并且我正在使用STM32F4发现通过I2C与高度传感器进行通信。我使用USART检查我的代码并找到它的位置,我发现它在时阻塞(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); 。我看到许多人在互联网上遇到同样的问题,但没有解决方案。有什么建议吗?

以下是代码:

uint8_t Read_IIC2(uint8_t SlaveAddress, uint8_t REG_Address)
{
uint8_t REG_data;

while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));
I2C_AcknowledgeConfig(I2C2, DISABLE);

I2C_GenerateSTART(I2C2,ENABLE);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

I2C_Send7bitAddress(I2C2,SlaveAddress<<1,I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
uart_conf();
    USART_puts(USART1, "rread\n\r");
I2C_SendData(I2C2,REG_Address);
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

I2C_GenerateSTART(I2C2,ENABLE);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

I2C_Send7bitAddress(I2C2,SlaveAddress,I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

while(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED));

REG_data = I2C_ReceiveData (I2C2);
return REG_data;
}

void I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
I2C_InitTypeDef I2C_InitStruct;

// enable APB1 peripheral clock for I2C2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
// enable clock for SCL and SDA pins
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

/* setup SCL and SDA pins
 * You can connect I2C2 to two different
 * pairs of pins:
 * 1. SCL on PB6 and SDA on PB7
 * 2. SCL on PB8 and SDA on PB9
 */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; // we are going to use PB6 and PB7
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;           // set pins to alternate function
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;      // set GPIO speed
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;         // set output to open drain --> the line has to be only pulled low, not driven high
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;           // enable pull up resistors
GPIO_Init(GPIOB, &GPIO_InitStruct);                 // init GPIOB

// Connect I2C2 pins to AF
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2);    // SCL
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2); // SDA

// configure I2C2
I2C_InitStruct.I2C_ClockSpeed = 400000;         // 100kHz
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;         // I2C mode
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard
I2C_InitStruct.I2C_OwnAddress1 = 0x00;          // own address, not relevant in master mode
I2C_InitStruct.I2C_Ack = I2C_Ack_Disable;       // disable acknowledge when reading (can be changed later on)
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses
I2C_Init(I2C2, &I2C_InitStruct);                // init I2C2

// enable I2C2
I2C_Cmd(I2C2, ENABLE);
}

0 个答案:

没有答案