日安
的目标是使用带有ST芯片的LSM9DS0。
是扫描器返回的I2C地址(ST环境)与Arduino I2C扫描器的地址不一样。我正在使用一个STM32核心-F 429和ESP32开发工具包。
当我使用下面的代码扫描I2C地址时,它返回以下四个地址:
0x3A
0x3B
0xD6
0xD7
但是,我以前在ESP32上使用过这个IMU分支,我注意到地址是不一样的。当我运行I2C扫描代码时,我会得到以下地址。
0x1D
0x6B
STM32代码: Src文件由CubeMX生成。如果需要i2c.h/c,请告诉我。但它们应该很标准。
for (uint16_t i = 0; i < 255; i++)
{
if (HAL_I2C_IsDeviceReady(&hi2c1, i, 5, 50) == HAL_OK)
{
HAL_GPIO_TogglePin(LED_RED_PORT, LED_RED_PIN);
char I2cMessage[10];
sprintf(I2cMessage, "%d , 0x%X\r\n", i, i);
HAL_UART_Transmit(&huart2, (uint8_t *)I2cMessage, strlen(I2cMessage), 10);
}
}
Arduino代码:
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(115200);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
有人知道这是为什么吗?这是个问题吗?
发布于 2022-03-23 21:41:26
我在HAL的描述中找到了答案。API要求7bt地址向左移动1位。
在文件:stm32F4xx_hal_i2c.c
中,HAL_I2C_IsDeviceReady()
API的描述如下:
* @param DevAddress Target device address: The device 7 bits address value
* in datasheet must be shifted to the left before calling the interface
因此,按如下方式更改IsDeviceReady参数,它将工作。
for (uint16_t i = 0; i < 255; i++)
{
if (HAL_I2C_IsDeviceReady(&hi2c1, i<<1, 5, 10) == HAL_OK)
{
// Do crazy stuff
}
}
这对我有用。希望它能帮助任何有同样问题的人。
发布于 2022-09-27 10:08:01
您可以使用以下代码在I2C上找到ESP32设备:
void I2Cscanner() {
Serial.println ();
Serial.println ("I2C scanner. Scanning ...");
byte count = 0;
Wire.begin();
for (byte i = 8; i < 120; i++)
{
Wire.beginTransmission (i); // Begin I2C transmission Address (i)
if (Wire.endTransmission () == 0) // Receive 0 = success (ACK response)
{
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX); // PCF8574 7 bit address
Serial.println (")");
count++;
}
}
Serial.print ("Found ");
Serial.print (count, DEC); // numbers of devices
Serial.println (" device(s).");
}
https://stackoverflow.com/questions/71514546
复制相似问题