这篇文章将写一些题外话,关于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。