How to properly initialize I2C stm32?

╄→гoц情女王★ 提交于 2020-01-14 15:49:10

问题


I want to get data from ADXL345 accelerometer,but seems that I incorrectly connect it.

SCL- PC6(with 10k resistor)

SDA- PC7(with 10k resistor)

SDO- GND

CS - VCC

GND - GND

3.3v - VCC

Here is my code to initalize:

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

    RCC_AHBPeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
        // I2CG clock enable
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2CG, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_APB1Periph_I2CG, ENABLE);
    // GPIOB clock enable
    // I2CG SCL and SDA configuration
    GPIO_InitStructure.GPIO_Pin = SCL|SDA;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    // Enable I2CG reset state
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2CG, ENABLE);
       // Release I2CG from reset state
       RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2CG, DISABLE);
    I2C_DeInit(I2C1);
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_16_9;
    I2C_InitStructure.I2C_OwnAddress1 =  1;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;


    I2C_Init(I2CG, &I2C_InitStructure);
    I2C_Cmd(I2CG, ENABLE);
    I2C_AcknowledgeConfig(I2CG, ENABLE);
}

In one example I saw

GPIO_PinAFConfig(GPIOC,SCLSource,GPIO_AF_I2CG);

GPIO_PinAFConfig(GPIOC,SDASource,GPIO_AF_I2CG);

But I don't have this API available.

Please help me. I tried many solutions and also tried to connect through SPI, but no success :( Please help with I2C.


回答1:


SCL- PC6(with 10k resistor)

SDA- PC7(with 10k resistor)

SCL and SDA should be connected directly. You should use pull-up resistors like on this scheme: http://en.wikipedia.org/wiki/File:I2C.svg

Your initialization code looks ok, so maybe hardware wiring is wrong?




回答2:


I realise this is an old post but it's worth pointing out. You shouldn't be using APB flags to configure the AHB bus. Check the programmers manual RM0008 (assuming you're using a stm32f10x device) for the appropriate settings.




回答3:


I had the same problem as you. The start condition was generated but the I2Cx_SR1.SB bit doesn't get set. I thought that I had to enable the I2C alternate function, but I had to disable all the other functions in the pin too. In my case it was UART3 function conflicting with I2C.




回答4:


There's sample code and app notes for the STM32 by someone who claims to have gotten I2C working here:

http://www.stm32challenge.com/node/143

Maybe try to get something known like a serial EEPROM working before you try the unknown accelerometer. Once you are sure the microcontroller end works you can connect the accelerometer and see what you get.



来源:https://stackoverflow.com/questions/8312998/how-to-properly-initialize-i2c-stm32

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!