树莓派PICO使用INA226测量电流和总线电压(4)

        这篇文章将写一些题外话,关于INA226的地址,如果不清楚开发板的I2C地址,可以通过总线扫描的办法来获取开发板设定的地址。

#include "hardware/i2c.h"
#include "pico/binary_info.h"
#define I2C_SDA 16
#define I2C_SCL 17

void setup() 
{
    // put your setup code here, to run once:
    Serial.begin( 115200 );
    i2c_init( i2c_default, 100*1000 );
    gpio_set_function(I2C_SDA, GPIO_FUNC_I2C);
    gpio_set_function(I2C_SCL, GPIO_FUNC_I2C);
    gpio_pull_up(I2C_SDA);
    gpio_pull_up(I2C_SCL);
    bi_decl(bi_2pins_with_func(I2C_SDA, I2C_SCL,GPIO_FUNC_I2C));
}

void loop() {
  
  Serial.printf("\nI2C Bus Scan\n");
  Serial.printf("   0 1 2 3 4 5 6 7 8 9 A B C D E F\n");

  for (int addr = 0; addr < (1 << 7); ++addr) {
    if (addr % 16 == 0) {
      Serial.printf("%02x ", addr);
    }

    // Perform a 1-byte dummy read from the probe address. If a slave
    // acknowledges this address, the function returns the number of bytes
    // transferred. If the address byte is ignored, the function returns
    // -1.

    // Skip over any reserved addresses.
    int ret;
    uint8_t rxdata;
    if (reserved_addr(addr))
      ret = PICO_ERROR_GENERIC;
    else
      ret = i2c_read_blocking(i2c_default, addr, &rxdata, 1, false);

    Serial.printf(ret < 0 ? "." : "@");
    Serial.printf(addr % 16 == 15 ? "\n" : " ");
  }
  Serial.printf("Done.\n");
}

bool reserved_addr(uint8_t addr) {
  return (addr & 0x78) == 0 || (addr & 0x78) == 0x78;
}

 我的开发板的地址就是0x40。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值