下方以 Unit Roller485 为例,输入电源同理,连接时请注意正负极的连接对应。
measureSingleshot
函数设置。startPeriodicMeasurement
函数设置,stopPeriodicMeasurement
函数停止。本教程中使用的主控设备为 Basic v2.7 ,搭配 Unit INA226-10A(若搭配 Unit INA226-1A 请使用下方代码中的 #define USING_UNIT_INA226_1A
)测量 Unit Roller485 的电压及电流。本电流电压检测单元采用 I2C 的方式通讯,根据实际的电路连接修改程序中的引脚定义,设备连接后对应的 I2C 引脚为 G22 (SCL)
,G21 (SDA)
。
#include <M5Unified.h>
#include <M5UnitUnified.h>
#include <M5UnitUnifiedMETER.h>
#include <Wire.h>
// *************************************************************
// Choose one define symbol to match the unit you are using
// *************************************************************
#if !defined(USING_UNIT_INA226_1A) && !defined(USING_UNIT_INA226_10A)
// #define USING_UNIT_INA226_1A
#define USING_UNIT_INA226_10A
#endif
namespace {
auto& lcd = M5.Display;
m5::unit::UnitUnified Units;
#if defined(USING_UNIT_INA226_1A)
#pragma message "Using 1A"
m5::unit::UnitINA226_1A unit_ina226;
#elif defined(USING_UNIT_INA226_10A)
#pragma message "Using 10A"
m5::unit::UnitINA226_10A unit_ina226;
#else
#error "Choose unit"
#endif
} // namespace
void setup()
{
M5.begin();
// The screen shall be in landscape mode
if (lcd.height() > lcd.width()) {
lcd.setRotation(1);
}
auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl);
Wire.end();
Wire.begin(pin_num_sda, pin_num_scl, 400000U);
if (!Units.add(unit_ina226, Wire) || !Units.begin()) {
M5_LOGE("Failed to begin");
lcd.clear(TFT_RED);
while (true) {
m5::utility::delay(10000);
}
}
lcd.setFont(&fonts::FreeMonoBoldOblique9pt7b);
lcd.setTextColor(TFT_BLACK);
M5_LOGI("M5UnitUnified has been begun");
M5_LOGI("%s", Units.debugInfo().c_str());
lcd.clear(TFT_WHITE);
lcd.setCursor(0, 120);
lcd.printf("Press BtnA or touch the screen to switch the measurement mode");
}
void loop()
{
using namespace m5::unit::ina226;
M5.update();
auto touch = M5.Touch.getDetail();
Units.update();
if (unit_ina226.updated()) {
lcd.startWrite();
lcd.fillRect(0, 10, lcd.width(), 24 * 4, TFT_WHITE);
lcd.setCursor(0, 10);
lcd.printf(
" C:%5.2f mA\n"
"SV:%5.2f mV\n"
"BV:%5.2f mV\n"
" P:%5.2f mW",
unit_ina226.current(), unit_ina226.shuntVoltage(), unit_ina226.voltage(), unit_ina226.power());
lcd.endWrite();
}
if (M5.BtnA.wasClicked() || touch.wasClicked()) {//switch measurement mode
static bool single{};
single = !single;
if (single) {//single measurement
M5.Speaker.tone(1500, 20);//prompt tone
Data d{};
unit_ina226.stopPeriodicMeasurement();
if (unit_ina226.measureSingleshot(d)) {
M5.Log.printf("Single:A:%f SV:%f BV:%f W:%f\n", unit_ina226.current(), unit_ina226.shuntVoltage(), unit_ina226.voltage(),
unit_ina226.power());
} else {
M5_LOGE("Failed to measureSingleshot");
}
} else {//periodic measurement
M5.Speaker.tone(2500, 20);//prompt tone
M5.Log.printf("Start periodic\n");
unit_ina226.startPeriodicMeasurement();
}
}
}
其中 C 表示电流,SV 表示分流电阻电压,BV 表示总线电压,P 表示功率;按动按键 A 或触摸屏幕(支持无实体按键主控设备)可切换测量模式为单次测量或周期测量。