我正在尝试使用 C/C++ SDK 从我的树莓派 pico 读取传感器数据 (BNO085)(由于时间限制,电路 Python 不可行)。我正在使用 I2C。
我编写了一个基本的 C 程序来尝试读取原始数据,但输出没有多大意义。
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "pico/binary_info.h"
// Define the I2C instance and pins for Pico W
#define I2C_PORT i2c0
#define SDA_PIN 4
#define SCL_PIN 5
static int bno_addr = 0x4A;
// Main function
int main() {
// Initialize stdio for output
stdio_init_all();
sleep_ms(2000); // Wait a bit for USB serial connection to establish
// Initialize I2C (open-flush)
i2c_init(I2C_PORT, 100 * 1000); // Initialize at 100kHz
gpio_set_function(SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(SDA_PIN);
gpio_pull_up(SCL_PIN);
uint8_t send_product_ID[2] = {0xF9, 0x00};
printf("%d\n", i2c_write_blocking(I2C_PORT, bno_addr, send_product_ID, sizeof(send_product_ID), false));
sleep_ms(100);
uint8_t recieveArrp[16];
printf("%d\n", i2c_read_blocking(I2C_PORT, bno_addr, recieveArrp, 16, true));
for(int i = 0; i < 16; i++) {
printf("\n%02x", recieveArrp[i]);
}
printf("\n");
return 0;
}
输出如下:
2
16
14
01
00
00
00
01
04
00
00
00
00
80
06
31
2e
30
看似正确发送和接收数据,但实际结果没有意义,因为 第一个字节应该是报告 ID 0XF8。
您省略了四个 byre SHTP 标头,该标头必须按照数据表第 1.3 节中的定义处理报告 ID 请求。