一、环境
基于 RT-Thread Studio 版本: 2.2.6
RT-Thread 版本:V4.1.1
开发板:stm32f407zgt6 野火开发板
二、工程创建
工程创建可参考文章
<手把手教 stm32f407 基于rtthread操作系统使用Arduino兼容层RTduino>
三、引脚配置
原理图引脚,使用 I2C1,引脚是 PB8(SCL),PB9(SDA).
工具 : pinout-generator工具,pinout-generator工具的主要特点包括:
1、使用图形界面进行操作,简单易用
2、支持多种芯片系列,可根据需要自行添加
3、快速生成对接 RT-Thread BSP 需要的各个文件
4、避免手动对接过程中的误操作问题
5、提高对接效率
下载地址:
https://github.com/RTduino/pinout-generator
https://gitee.com/rtduino/pinout-generator
使用的引脚有 串口1,I2C1,指示灯。
PA9、PA10;
PB8、PB9;
PC3
四、工程配置
将该工具生成的文件添加到 RT-Thread Studio 工程中的aoolications 中。
pins_arduino.c 添加如下 头文件
#include "drv_common.h"
根据 bord.h 中的提示,配置 I2C
添加软件包,如果提示
当前软件包不存在于rt-thread资源索引中. 请尝试右键项目选择Packages Update更新后重试 ID
解决办法:
进入 安装目录下
RT-ThreadStudio\platform\env_released\env\packages\
删除 packages,后重新
git clone https://gitee.com/rt-thread-mirror/packages.git
重新拉取就可以了。
同时使能 Wire.h
添加测试文件 arduino_mpu6050.cpp,添加头文件
#include <Arduino.h>
#include "RTduino.h"
并复制软件包测试例程中的内容到该文件,
软件包测试例程
arduino_mpu6050.cpp 文件内容如下:
#include <Arduino.h>
#include "RTduino.h"
#include <Wire.h>
#include <MPU6050.h>MPU6050 mpu;void checkSettings()
{Serial.println();Serial.print(" * Sleep Mode: ");Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled");Serial.print(" * Clock Source: ");switch(mpu.getClockSource()){case MPU6050_CLOCK_KEEP_RESET: Serial.println("Stops the clock and keeps the timing generator in reset"); break;case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break;case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break;case MPU6050_CLOCK_PLL_ZGYRO: Serial.println("PLL with Z axis gyroscope reference"); break;case MPU6050_CLOCK_PLL_YGYRO: Serial.println("PLL with Y axis gyroscope reference"); break;case MPU6050_CLOCK_PLL_XGYRO: Serial.println("PLL with X axis gyroscope reference"); break;case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println("Internal 8MHz oscillator"); break;}Serial.print(" * Gyroscope: ");switch(mpu.getScale()){case MPU6050_SCALE_2000DPS: Serial.println("2000 dps"); break;case MPU6050_SCALE_1000DPS: Serial.println("1000 dps"); break;case MPU6050_SCALE_500DPS: Serial.println("500 dps"); break;case MPU6050_SCALE_250DPS: Serial.println("250 dps"); break;}Serial.print(" * Gyroscope offsets: ");Serial.print(mpu.getGyroOffsetX());Serial.print(" / ");Serial.print(mpu.getGyroOffsetY());Serial.print(" / ");Serial.println(mpu.getGyroOffsetZ());Serial.println();
}void mpu6050_fun_setup()
{Serial.begin(115200);// Initialize MPU6050Serial.println("Initialize MPU6050");while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)){Serial.println("Could not find a valid MPU6050 sensor, check wiring!");delay(500);}// If you want, you can set gyroscope offsets// mpu.setGyroOffsetX(155);// mpu.setGyroOffsetY(15);// mpu.setGyroOffsetZ(15);// Calibrate gyroscope. The calibration must be at rest.// If you don't want calibrate, comment this line.mpu.calibrateGyro();// Set threshold sensivty. Default 3.// If you don't want use threshold, comment this line or set 0.mpu.setThreshold(3);// Check settingscheckSettings();
}void mpu6050_fun_loop()
{Vector rawGyro = mpu.readRawGyro();Vector normGyro = mpu.readNormalizeGyro();Serial.println("**************************************");Serial.println();Serial.print(" Xraw = ");Serial.print(rawGyro.XAxis);Serial.print(" Yraw = ");Serial.print(rawGyro.YAxis);Serial.print(" Zraw = ");Serial.println(rawGyro.ZAxis);Serial.print(" Xnorm = ");Serial.print(normGyro.XAxis);Serial.print(" Ynorm = ");Serial.print(normGyro.YAxis);Serial.print(" Znorm = ");Serial.println(normGyro.ZAxis);Serial.println();Serial.println("**************************************");delay(500);
}
static int rtduino_mpu6050_fun_init(void)
{rtduino_sketch_loader_create("mpu6050_fun", mpu6050_fun_setup, mpu6050_fun_loop);return 0;
}
INIT_COMPONENT_EXPORT(rtduino_mpu6050_fun_init);
修改 SConscript 文件,修改后同步配置到工程。
编译
五、测试结果
将可执行文件下载到开发板,运行结果如下:
移动开发板
参考:
https://docs.rtduino.com/#/zh/manual/adapt/bsp/pinout-generator/pinout-generator
https://gitee.com/rtduino/RTduino
https://gitee.com/rtduino
https://gitee.com/RT-Thread-Mirror/packages
https://docs.rtduino.com/#/zh/manual/multi-threading/sketch-loader