STM32如何设置从I2C端口

时间:2014-01-14 20:54:47

标签: i2c stm32 slave

我对STM32F0 Discovery有一个问题。 我正在使用I2C1(如主设备)在同一块板上向I2C2(从设备)发送启动条件和地址。 我能够生成属性信号,但IC2(从机)无法在第9个clk生成ACK

怎么了? 这里是关于设置的代码:

 void set_I2C(void){
    GPIO_InitTypeDef GPIO_InitStructure;
    I2C_InitTypeDef I2C_InitStructure;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
    /* Configure the I2C clock source. The clock is derived from the HSI */
    RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);


    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1);


    //Configure pins: SCL and SDA ------------------
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_Init(GPIOB, &GPIO_InitStructure);



    I2C_DeInit(I2C1);
    I2C_Cmd(I2C1, DISABLE);
    I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
    I2C_InitStructure.I2C_DigitalFilter = 0x00;
    I2C_InitStructure.I2C_OwnAddress1 = 0x40;
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_Timing =0xB0420F13; //100KHz        

          I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

    I2C_Init(I2C1, &I2C_InitStructure);
    I2C_Cmd(I2C1, ENABLE);


    //Set up of I2C2
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);


    GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_1);

    //Configure pins: SCL and SDA ------------------
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    I2C_DeInit(I2C2);
    I2C_Cmd(I2C2, DISABLE);
    I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Disable;
    I2C_InitStructure.I2C_DigitalFilter = 0x00;
    I2C_InitStructure.I2C_OwnAddress1 = 0x30; 

    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

    I2C_InitStructure.I2C_Timing =0xB0420F13;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

    I2C_Init(I2C2, &I2C_InitStructure);
     I2C_Cmd(I2C2, ENABLE);



        I2C_ITConfig(I2C2, I2C_AcknowledgedAddress_7bit, ENABLE);

  NVIC_InitTypeDef NVIC_InitStructure;
 /* Configure the I2C2 interrupt priority */
 NVIC_InitStructure.NVIC_IRQChannel = I2C2_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelPriority=1;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);

}

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。这是由于缺少I2C外部上拉电阻所需的。您不能只使用内部MCU的上拉电阻。你必须使用外部的。

这些文章中提供了详细说明:

http://www.robot-electronics.co.uk/acatalog/I2C_Tutorial.html

https://electronics.stackexchange.com/questions/1849/is-there-a-correct-resistance-value-for-pull-up-resistors

答案 1 :(得分:1)

我遇到了类似的问题。 就我而言,解决方案是在初始化后添加一个 HAL_I2C_EnableListen_IT() 以激活 I2C 从设备(我使用的是 HAL 库,但您可以查看上述函数的详细内容)。