开源硬件
Arduino
客制化键盘
Arduino_寄存器
二进制运算
寄存器+二进制运算
LCD-逐字显示
密码依次录入
等待输入
WiFi Duck(无线击键注入攻击平台)
WiFi Duc-New
WiFi Duc-Old
蓝牙无线烧录
ESP8266
ESP-NOW
ESP8266看门狗
ESP8266-休眠模式
ESP01/01S使用说明
WIFI_SD
ESP8266-Web服务器
ESP8266-WIFI自动认证
ESP32
ESP32 ADC2
ESP32_PWM
ESP32_CAM
ESP32 小坦克
ESP32_限电保护
Arduino IDE 添加 ESP32
ESP32-iPhone BLE攻击
STM32
STM32F103-虚拟键盘
STC
STC8G1K08(A)
树莓派-触摸屏
Arduino IDE
Arduino_自制库
Arduino库收集
常见排序算法
冒泡排序
选择排序
插入排序
希尔排序
归并排序
快速排序
计数排序
预处理
millis(运行时长)
Arduino IDE 2.X-修改数据位置
Mixly
Mixly安装教程
Mixly 模块介绍
Mixly-添加ESP32CAM支持
Mixly-库定制工具
模块
4G模块连接物联网
GPS模块
语音模块(JQ8900)
安信可VB语音识别
28BYJ-48(5V步进)
FreeRTOS
FreeRTOS-多任务基础
FreeRTOS-任务共享全局变量
FreeRTOS-多核多任务
FreeRTOS-MUTEX
FreeRTOS-常规程序改多任务
FreeRTOS-定时器
LaserGRBL(激光雕刻)
LaserGRBL-GRBL
GRBL-CNC Shield v4
MicroPython
Scratch
Wokwi(在线仿真)
html转无符号数组
待做开源项目
本文档使用 MrDoc 发布
-
+
首页
Arduino_自制库
有些程序很多地方使用都是一样,每次都整段程序放进去太麻烦了,也不便于阅读,对于这种可以封装成库文件来进行使用,和函数有点类似,都是简化程序 库文件可以简单分为四部分  ## 源程序 ``` #include <Wire.h> // MPU6050 Slave Device Address const uint8_t MPU6050SlaveAddress = 0x68; // Select SDA and SCL pins for I2C communication const uint8_t scl = D6; const uint8_t sda = D7; // sensitivity scale factor respective to full scale setting provided in datasheet const uint16_t AccelScaleFactor = 16384; const uint16_t GyroScaleFactor = 131; // MPU6050 few configuration register addresses const uint8_t MPU6050_REGISTER_SMPLRT_DIV = 0x19; const uint8_t MPU6050_REGISTER_USER_CTRL = 0x6A; const uint8_t MPU6050_REGISTER_PWR_MGMT_1 = 0x6B; const uint8_t MPU6050_REGISTER_PWR_MGMT_2 = 0x6C; const uint8_t MPU6050_REGISTER_CONFIG = 0x1A; const uint8_t MPU6050_REGISTER_GYRO_CONFIG = 0x1B; const uint8_t MPU6050_REGISTER_ACCEL_CONFIG = 0x1C; const uint8_t MPU6050_REGISTER_FIFO_EN = 0x23; const uint8_t MPU6050_REGISTER_INT_ENABLE = 0x38; const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H = 0x3B; const uint8_t MPU6050_REGISTER_SIGNAL_PATH_RESET = 0x68; int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ; void setup() { Serial.begin(9600); Wire.begin(sda, scl); MPU6050_Init(); } void loop() { double Ax, Ay, Az, T, Gx, Gy, Gz; Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H); //divide each with their sensitivity scale factor Ax = (double)AccelX/AccelScaleFactor; Ay = (double)AccelY/AccelScaleFactor; Az = (double)AccelZ/AccelScaleFactor; T = (double)Temperature/340+36.53; //temperature formula Gx = (double)GyroX/GyroScaleFactor; Gy = (double)GyroY/GyroScaleFactor; Gz = (double)GyroZ/GyroScaleFactor; Serial.print("Ax: "); Serial.print(Ax); Serial.print(" Ay: "); Serial.print(Ay); Serial.print(" Az: "); Serial.print(Az); Serial.print(" T: "); Serial.print(T); Serial.print(" Gx: "); Serial.print(Gx); Serial.print(" Gy: "); Serial.print(Gy); Serial.print(" Gz: "); Serial.println(Gz); delay(100); } void I2C_Write(uint8_t deviceAddress, uint8_t regAddress, uint8_t data){ Wire.beginTransmission(deviceAddress); Wire.write(regAddress); Wire.write(data); Wire.endTransmission(); } // read all 14 register void Read_RawValue(uint8_t deviceAddress, uint8_t regAddress){ Wire.beginTransmission(deviceAddress); Wire.write(regAddress); Wire.endTransmission(); Wire.requestFrom(deviceAddress, (uint8_t)14); AccelX = (((int16_t)Wire.read()<<8) | Wire.read()); AccelY = (((int16_t)Wire.read()<<8) | Wire.read()); AccelZ = (((int16_t)Wire.read()<<8) | Wire.read()); Temperature = (((int16_t)Wire.read()<<8) | Wire.read()); GyroX = (((int16_t)Wire.read()<<8) | Wire.read()); GyroY = (((int16_t)Wire.read()<<8) | Wire.read()); GyroZ = (((int16_t)Wire.read()<<8) | Wire.read()); } //configure MPU6050 void MPU6050_Init(){ delay(150); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SMPLRT_DIV, 0x07); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_1, 0x01); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_2, 0x00); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_CONFIG, 0x00); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_GYRO_CONFIG, 0x00);//set +/-250 degree/second full scale I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_CONFIG, 0x00);// set +/- 2g full scale I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_FIFO_EN, 0x00); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_INT_ENABLE, 0x01); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_USER_CTRL, 0x00); } ``` ## h 文件 ``` #ifndef Wire_H #define Wire_H #include <Wire.h> #endif #ifndef MPU6050_zwzw_H #define MPU6050_zwzw_H //定义库名称 class MPU6050_zwzw { public: //定义公共函数,外部可访问的 //首个主函数不需要viod这些类名,并且名称要与定义的库名称一致 MPU6050_zwzw(uint8_t sda,uint8_t scl); //定义软件IIC void Init(); //初始化MPU6050 void Run(); //实时更新数据,必须放在循环内!!! //读取数据使用的变量,所以需要放在公共函数中 double Ax, Ay, Az, T, Gx, Gy, Gz; //可读取数据,角度X,角度Y,角度Z,温度,加速度X,加速度Y,加速度Z private: //定义内部函数,仅限该库访问使用的 void I2C_Write(uint8_t deviceAddress, uint8_t regAddress, uint8_t data); void RawValue(uint8_t deviceAddress, uint8_t regAddress); //内部使用的变量,外部不需要调用,就放内部就行了 //MPU6050从设备地址 const uint8_t MPU6050SlaveAddress = 0x68; //数据表中提供的满量程设置的灵敏度比例系数 const uint16_t AccelScaleFactor = 16384; const uint16_t GyroScaleFactor = 131; //MPU6050少数配置寄存器地址 const uint8_t MPU6050_REGISTER_SMPLRT_DIV = 0x19; const uint8_t MPU6050_REGISTER_USER_CTRL = 0x6A; const uint8_t MPU6050_REGISTER_PWR_MGMT_1 = 0x6B; const uint8_t MPU6050_REGISTER_PWR_MGMT_2 = 0x6C; const uint8_t MPU6050_REGISTER_CONFIG = 0x1A; const uint8_t MPU6050_REGISTER_GYRO_CONFIG = 0x1B; const uint8_t MPU6050_REGISTER_ACCEL_CONFIG = 0x1C; const uint8_t MPU6050_REGISTER_FIFO_EN = 0x23; const uint8_t MPU6050_REGISTER_INT_ENABLE = 0x38; const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H = 0x3B; const uint8_t MPU6050_REGISTER_SIGNAL_PATH_RESET = 0x68; int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ; }; #endif ``` ### 定义头文件 `` #ifndef Wire_H # if no def,判断是否定义,如果没有执行下面的程序 #define Wire_H # 定义一下,防止重复定义 #include <Wire.h> # 这里放具体程序就行 #endif # 结束 `` ### 定义库函数 ``` class [名称] { public: [公共函数和变量] private: [私有函数和变量] } ``` `public:` 第一个函数应该和 `class` 的名称一致,算是主函数 ## cpp 文件 ``` #include "MPU6050_zwzw.h" MPU6050_zwzw::MPU6050_zwzw(uint8_t sda,uint8_t scl) { Wire.begin(sda, scl); //设置软件IIC } void MPU6050_zwzw::I2C_Write(uint8_t deviceAddress, uint8_t regAddress, uint8_t data){ Wire.beginTransmission(deviceAddress); Wire.write(regAddress); Wire.write(data); Wire.endTransmission(); } //读取所有14个寄存器 void MPU6050_zwzw::RawValue(uint8_t deviceAddress, uint8_t regAddress){ Wire.beginTransmission(deviceAddress); Wire.write(regAddress); Wire.endTransmission(); Wire.requestFrom(deviceAddress, (uint8_t)14); AccelX = (((int16_t)Wire.read()<<8) | Wire.read()); AccelY = (((int16_t)Wire.read()<<8) | Wire.read()); AccelZ = (((int16_t)Wire.read()<<8) | Wire.read()); Temperature = (((int16_t)Wire.read()<<8) | Wire.read()); GyroX = (((int16_t)Wire.read()<<8) | Wire.read()); GyroY = (((int16_t)Wire.read()<<8) | Wire.read()); GyroZ = (((int16_t)Wire.read()<<8) | Wire.read()); } //配置MPU6050 void MPU6050_zwzw::Init(){ delay(150); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SMPLRT_DIV, 0x07); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_1, 0x01); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_2, 0x00); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_CONFIG, 0x00); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_GYRO_CONFIG, 0x00);//set +/-250 degree/second full scale I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_CONFIG, 0x00);// set +/- 2g full scale I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_FIFO_EN, 0x00); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_INT_ENABLE, 0x01); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00); I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_USER_CTRL, 0x00); } //保持更新MPU6050 void MPU6050_zwzw::Run(){ RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H); //用它们的敏感度比例因子除以每个 Ax = (double)AccelX/AccelScaleFactor; Ay = (double)AccelY/AccelScaleFactor; Az = (double)AccelZ/AccelScaleFactor; T = (double)Temperature/340+36.53; //温度公式 Gx = (double)GyroX/GyroScaleFactor; Gy = (double)GyroY/GyroScaleFactor; Gz = (double)GyroZ/GyroScaleFactor; } ``` ### 加载 .h 文件 ``` #include "MPU6050_zwzw.h" ``` ### 主函数 ``` MPU6050_zwzw::MPU6050_zwzw(uint8_t sda,uint8_t scl) { Wire.begin(sda, scl); //设置软件IIC } ``` 设置主函数,这个函数`[库名称]::[库名称]`是 `public:` 中的第一个函数,应该与名称同名 ### 其他函数 void MPU6050_zwzw::[名称](){} 这个名称就是在`.h`文件中定义的名称 里面包含所需运行的程序 ## keywords 文件 这个文件主要是定义代码高亮的 格式:`keyword[tab]KEYWORD_TOKENTYPE` * keyword 是要用来高亮显示的关键字类名、函数名和常量名等 * [tab] 为一个tab缩进(不要是空格!!!) * KEYWORD_TOKENTYPE 的取值为 KEYWORD1, KEYWORD2, KEYWORD3, LITERAL1, LITERAL2 * 在Arduino IDE的关键字高亮中,会识别KEYWORD1为数据类型高亮,KEYWORD2为函数高亮,KEYWORD3为结构体高亮,LITERAL1为常量 ``` MPU6050_zwzw KEYWORD3 Init KEYWORD2 Run KEYWORD2 Ax LITERAL2 Ay LITERAL2 Az LITERAL2 T LITERAL2 Gx LITERAL2 Gy LITERAL2 Gz LITERAL2 ``` ## 示例程序 \examples\[名称]\[名称].ino  示例程序要符合 `ino` 文件特性,需要在和项目同名的 `项目文件夹` 下 项目文件夹中可以包含其他辅助文件 ``` #include <MPU6050_zwzw.h> MPU6050_zwzw MPU6050(4,5); void setup() { MPU6050.Init(); Serial.begin(9600); } void loop() { MPU6050.Run(); Serial.print("角度 Ax: "); Serial.print(MPU6050.Ax); Serial.print(" Ay: "); Serial.print(MPU6050.Ay); Serial.print(" Az: "); Serial.print(MPU6050.Az); Serial.print(" 温度 T: "); Serial.print(MPU6050.T); Serial.print(" 加速度 Gx: "); Serial.print(MPU6050.Gx); Serial.print(" Gy: "); Serial.print(MPU6050.Gy); Serial.print(" Gz: "); Serial.println(MPU6050.Gz); delay(100); } ```
造物者W
2022年1月11日 19:00
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码