STM32
直播中

刘慧

7年用户 1535经验值
私信 关注
[问答]

STM32F030F4 IIC从模式无法触发中断的原因?

void I2C_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable GPIOA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/*!< sEE_I2C Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE);
/*!< GPIO configuration */
/*!< Configure sEE_I2C pins: SCL */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//GPIO_Mode_IN
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;//open-drain
GPIO_Init(GPIOA ,  GPIO_InitStruct);
/*!< Configure sEE_I2C pins: SDA */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA ,  GPIO_InitStruct);
/* Connect PXx to I2C_SCL*/
GPIO_PinAFConfig( GPIOA , GPIO_PinSource9, GPIO_AF_1);
/* Connect PXx to I2C_SDA*/
GPIO_PinAFConfig( GPIOA ,GPIO_PinSource10, GPIO_AF_1);
}
void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStruct;
/* I2C configuration */
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStruct.I2C_DigitalFilter = 0x00;
I2C_InitStruct.I2C_OwnAddress1 =0xA0;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStruct.I2C_Timing = 0x00100000;//100Kbits
/* Apply I2C configuration after enabling it */
I2C_ITConfig(I2C2,I2C_IT_ADDRI,ENABLE);  
I2C_Cmd(I2C1, ENABLE);
I2C_Init(I2C1,  I2C_InitStruct);
/* I2C Peripheral Enable */

}
void I2C1_IRQHandler(void)
{
  I2C_ClearITPendingBit(I2C1, I2C_ISR_ADDR|I2C_ISR_STOPF );
LED_ON();
}
#if 1
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
  

  /* Configure the NVIC Preemption Priority Bits */  
  NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn   ;               //I2C????
    NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;    //?????1  
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                 //??????
    NVIC_Init( NVIC_InitStructure);                             //??NVIC_InitStruct???????????NVIC???  
  
    //I2C_ITConfig(I2C2, I2C_IT_EVT, ENABLE);
}
#else
void NVIC_Configuration(void){}
#endif
int main(void)
{
ALL_Config();
NVIC_Configuration();
I2C_GPIO_Configuration();
I2C_Configuration();
while(1)
{
}
}

回帖(1)

CDCNKA

2024-5-11 17:22:47
STM32F030F4 IIC从模式无法触发中断的原因可能有以下几点:

1. 时钟配置问题:确保I2C外设时钟已经正确配置。在您的代码中,您已经启用了GPIOA和I2C1的时钟,但是还需要确保I2C1的时钟源和时钟频率是正确的。

2. GPIO配置问题:在您的代码中,您已经配置了SCL(GPIO_Pin_9)的GPIO_Mode为GPIO_Mode_AF(复用功能模式)。请确保SDA(通常是GPIO_Pin_10)也配置为GPIO_Mode_AF。

3. I2C配置问题:确保I2C外设的配置正确,包括I2C模式(主模式或从模式)、地址、时钟速率等。在您的代码中,您没有提供I2C初始化的代码,所以请检查这部分是否正确。

4. 中断配置问题:确保I2C外设的中断已经正确配置。这包括使能I2C中断(I2C_ITConfig()函数)和配置NVIC(嵌套向量中断控制器)以允许I2C中断。

5. 中断服务函数(ISR)问题:确保您已经实现了对应的I2C中断服务函数,并且在该函数中正确处理了中断事件。

6. 硬件连接问题:检查I2C设备的硬件连接,确保SCL和SDA引脚没有短路、开路或连接错误。

7. 其他外设冲突:确保没有其他外设与I2C共享SCL或SDA引脚,这可能导致I2C通信失败。

请检查以上几点,确保I2C从模式的配置和中断触发正确。如果问题仍然存在,请提供更详细的代码和硬件连接信息,以便进一步分析。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分