STM32在SCL上使用HMC5883L未生成100KHz。

huangapple go评论51阅读模式
英文:

stm32 not generating 100KHz on SCL with HMC5883L

问题

我正在尝试将HMC5883L与STM32 L476RG NUCLEO进行接口连接。我已经尝试过使用Arduino接口该传感器,它可以完美运行并在SCL线上生成100KHz的时钟信号,我使用逻辑分析仪进行了验证。

但是现在,我尝试使用STM32进行接口连接,但无法成功,SCL引脚未生成100KHz的时钟信号,即使我已经在STM32CUBEMX IDE中设置了I2C为100KHz模式。

由于以上原因,我从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从因为以上原因,我收到了从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从从 0 到 100 中选中一个整数。如果你选中了奇数,则电脑随机生成1到 100之间的一个奇数,请你来猜一猜这个数字是多少。

如果你猜错,电脑会告诉你你猜的数字过大还是过小。
此外,你可以只猜 10 次。

提示:

1 <= x <= 100

*/
public class GuessNumberHigherOrLower {
// Time: O(logn), Space: O(1)
public int guessNumber(int n) {
int left = 1;
int right = n;
while (left <= right) {
int mid = left + (right - left) / 2;
int res = guess(mid);
if (res == 0) {
return mid;
} else if (res == 1) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}

// 模拟给定的API
private int guess(int num) {
    // 这里的数是随机生成的,实际调用时需要替换成具体的题目要求
    int target = 6;
    if (num == target) {
        return 0;
    } else if (num < target) {
        return -1;
    } else {
        return 1;
    }
}

public static void main(String[] args) {
    GuessNumberHigherOrLower solution = new GuessNumberHigherOrLower();
    int result = solution.guessNumber(10);
    System.out.println(result);
}

}




### 22. 翻转链表

**题目描述**

反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1




**分析**

- 迭代法

![](https://img2020.cnblogs.com/iimge/27995/20200204155301390.png)

```java
public ListNode reverseList(ListNode head) {
    ListNode prev = null;
    ListNode curr = head;
    while (curr != null) {
        ListNode nextTemp = curr.next;
        curr.next = prev;
        prev = curr;
        curr = nextTemp;
    }
    return prev;
}
  • 递归法
public ListNode reverseList(ListNode head) {
    if (

<details>
<summary>英文:</summary>

I am trying to interface HMC5883L with STM32 L476RG NUCLEO. I have already tried to interface the sensor with arduino and it works perfectly and generates 100KHz on SCL line, which I verified using Logic Analyzer.

But now I am trying to interface it using STM32 I&#39;m unable to do so, the SCL pin is not generating 100KHz, even I have set up the I2C for 100KHZ mode from STM32CUBEMX IDE.

Because of these above reasons I am getting NACK from slave

The code and logic analyzer output is attached below.

void HMC5883L_Init(void)
{
uint8_t kk = 0x1E;
HAL_I2C_Master_Transmit(&hi2c3, kk<<1, 0x02, 1, 5000);
HAL_I2C_Master_Transmit(&hi2c3, kk<<1, 0x00, 1, 5000);
}




[![enter image description here][1]][1]

[![enter image description here][2]][2]

I have tried using different I2c instances but didn&#39;t work.


I have tried both i2c1, i2c3 instance.

I tried using different i2c ports but didn&#39;t help.


  [1]: https://i.stack.imgur.com/vdssy.png
  [2]: https://i.stack.imgur.com/Apvcn.png

</details>


# 答案1
**得分**: 1

找到解决方案,实际上内部上拉电阻不足,我不得不将约4K欧的外部电阻连接到SCL线与VCC。 Github存储库和逻辑分析仪结果 - [github.com/abhisheeekkk/STM32_WITH_HMC5883L/tree/main][1] 


  [1]: https://github.com/abhisheeekkk/STM32_WITH_HMC5883L/tree/main

<details>
<summary>英文:</summary>

Found the solution, actually the internal pull up resistor were not enough, I had to connect around 4K ohms of external resistor to SCL line with VCC. Github repo and Logic Analyzer results - [github.com/abhisheeekkk/STM32_WITH_HMC5883L/tree/main][1] 


  [1]: https://github.com/abhisheeekkk/STM32_WITH_HMC5883L/tree/main

</details>



huangapple
  • 本文由 发表于 2023年6月1日 02:42:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定