因此,在本教程中,我们将测量连接到 Adafruit nRF52 Feather Bluefruit 板的 LiPo 电池电压并将其显示在串行控制台中。 首先通过arduino IDE将此代码上传到开发板上。
#define VBAT_PIN (A7)
#define VBAT_MV_PER_LSB (0.73242188F)
#define VBAT_DIVIDER (0.71275837F)
#define VBAT_DIVIDER_COMP (1.403F)
int vbat_raw;
uint8_t vbat_per;
float vbat_mv,vbat_v;
void setup(void) {
Serial.begin(115200);
analogReference(AR_INTERNAL_3_0);
analogReadResolution(12); // Can be 8, 10, 12 or 14
delay(1);
}
void loop(void) {
vbat_raw = analogRead(VBAT_PIN);
vbat_per = mv_to_percent(vbat_raw * VBAT_MV_PER_LSB);
vbat_mv = (float)vbat_raw * VBAT_MV_PER_LSB * VBAT_DIVIDER_COMP;
vbat_v = vbat_mv/1000;
Serial.print("ADC = ");
Serial.print(vbat_raw * VBAT_MV_PER_LSB);
Serial.print(" mV (");
Serial.print(vbat_raw);
Serial.print(") ");
Serial.print("LIPO = ");
Serial.print(vbat_v);
Serial.print(" V (");
Serial.print(vbat_per);
Serial.println("%)");
delay(4000);
}
uint8_t mv_to_percent(float mvolts) {
uint8_t battery_level;
if (mvolts >= 3000) {
battery_level = 100;
} else if (mvolts > 2900) {
battery_level = 100 - ((3000 - mvolts) * 58) / 100;
} else if (mvolts > 2740) {
battery_level = 42 - ((2900 - mvolts) * 24) / 160;
} else if (mvolts > 2440) {
battery_level = 18 - ((2740 - mvolts) * 12) / 300;
} else if (mvolts > 2100) {
battery_level = 6 - ((2440 - mvolts) * 6) / 340;
} else {
battery_level = 0;
}
return battery_level;
}
接下来将带 JST 连接器的锂聚合物电池连接到 Adafruit nRF52 板连接器。然后使用 USB 线给开发板充电。确保充电期间黄色“CHG”LED 亮起。现在打开串口控制台查看电池充电状态,一旦黄色“CHG”LED 熄灭,表示电池充电完成。