如何正确初始化I2C stm32?

发布于 2024-12-18 14:33:07 字数 1706 浏览 1 评论 0原文

我想从 ADXL345 加速度计获取数据,但似乎我错误地连接了它。

SCL- PC6(带 10k 电阻)

SDA- PC7(带 10k 电阻)

SDO- GND

CS - VCC

GND - GND

3.3v - VCC

这是我的初始化代码:

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);
}

在一个示例中我看到了

GPIO_PinAFConfig(GPIOC,SCLSource,GPIO_AF_I2CG);

GPIO_PinAFConfig(GPIOC,SDASource,GPIO_AF_I2CG);

但我没有可用的 API 。

请帮我。我尝试了很多解决方案,也尝试通过SPI连接,但没有成功:( 请帮忙解决I2C问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮萍、无处依 2024-12-25 14:33:07

我意识到这是一篇旧文章,但值得指出。您不应该使用 APB 标志来配置 AHB 总线。检查程序员手册 RM0008(假设您使用的是 stm32f10x 设备)以获取适当的设置。

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.

紫轩蝶泪 2024-12-25 14:33:07

SCL-PC6(带10k电阻)

SDA-PC7(带10k电阻)

SCL和SDA应直接连接。您应该像此方案一样使用上拉电阻: http://en.wikipedia.org /wiki/File:I2C.svg

你的初始化代码看起来没问题,所以也许硬件接线是错误的?

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?

红焚 2024-12-25 14:33:07

我和你有同样的问题。已生成启动条件,但 I2Cx_SR1.SB 位未置位。
我认为我必须启用 I2C 备用功能,但我也必须禁用引脚中的所有其他功能。
就我而言,这是 UART3 功能与 I2C 冲突。

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.

べ映画 2024-12-25 14:33:07

这里有一个声称已经使 I2C 工作的人提供的 STM32 示例代码和应用说明:

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

在尝试未知的加速度计之前,也许可以尝试让串行 EEPROM 等已知的东西正常工作。一旦确定微控制器端正常工作,您就可以连接加速度计并看看会得到什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文