董事会:STM32F4 Discovery
我想,我在初始化I2C时遇到了麻烦!当程序达到I2C_write函数时,它会挂起!我正在尝试与TI的温度和嗡嗡声传感器HDC1080进行通信。硬件还可以,我一直用cubeMX和HAL库测试它,它运行正常!
我有一个连接到PB8(SCL)和PB9(SDA)的saleae逻辑分析仪,没有任何活动!
我删除了init_usart部分,认为它们可能是因为它们共享相同的端口(GPIOB)。
void init_usart(void)
{
//enable AHB1 peripheral clock
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB, ENABLE);
//Init GPIOB
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
//Enable USART peripheral clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//Init usart
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate = 57600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No ;
USART_InitStruct.USART_Mode = USART_Mode_Tx;
USART_InitStruct.USART_HardwareFlowControl =
USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStruct);
USART_Cmd(USART1, ENABLE);
}
void InitializeI2C()
{
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; // 8=SCL 9=SDA
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
/* enable APB1 peripheral clock for I2C1*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE);
I2C_InitTypeDef I2C_InitStruct;
I2C_InitStruct.I2C_ClockSpeed = 100000;
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 = 0x01;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStruct);
I2C_Cmd(I2C1, ENABLE);
}
void I2C_write( uint8_t HW_address, uint8_t addr, uint8_t data)
{
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, HW_address, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1,
I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, addr);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, data);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
}
int main(void)
{
init_usart()
InitializeI2C();
while (1)
{
delay_div(50);
I2C_write( 0x40 << 1 , 0x01, 0x00); //here it stops!!!
}
一个可能的问题:您正在使用未初始化的数据。
从您的代码:
I2C_InitTypeDef I2C_InitStruct;
I2C_InitStruct.I2C_ClockSpeed = 100000;
//I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 = 0x01;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStruct);
至少I2C_Mode
没有在这里初始化,你不能假设值为0.它只是当时堆栈中的任何东西。
您要么将结构memset设置为0,要么在声明时将其他成员隐式初始化为零:
I2C_InitTypeDef I2C_InitStruct = {
.I2C_ClockSpeed = 100000,
//.I2C_Mode = I2C_Mode_I2C,
.I2C_DutyCycle = I2C_DutyCycle_2,
.I2C_OwnAddress1 = 0x01,
.I2C_Ack = I2C_Ack_Enable,
.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit,
};
I2C_Init(I2C1, &I2C_InitStruct);
我的温度和嗡嗡声传感器死了! I2C init很好,用i2c oled显示器试了一下,我可以在总线上看到活动!
只是一个简短的最后更新!拜托我吧!
我的温度和嗡嗡声传感器(HDC1080)还可以!在将电线焊接到新传感器上40分钟后(针脚为0.4mm),我发现这也无法正常工作。硬件方面没有任何变化,我使用HAL库进行了成功的初始测试,2 x 10k(这就是我所拥有的)面包板上拉电阻,来自stm32发现板的3V电源。似乎由于未知原因,HAL测试正在使用这两个10k引体向上但是SP库没有。由于我的SSD1306 oled显示器采用了已经安装了4.7k电阻的pcb,我能够发现我可以与温度和嗡嗡声传感器和显示器进行通信。最后的结论,上拉电阻到底是我的问题!