IP5306电量显示-Arduino中文社区 - Powered by Discuz! Archiver

vany5921 发表于 2020-2-24 10:27

IP5306电量显示

以下示例展示如何将电池电量按百分比分4段显示
已知5306的IIC地址为0x75,寄存器地址0x78为查询电量百分比,具体函数在M5Stack的库中查看Power.cpp,颜色显示采用了RGB565的方式

#include <M5Stack.h>

#define RGB(r,g,b) (int16_t)(b + (g << 5) + (r << 11)) //RGB888转RGB565

unsigned long currentMillis;
unsigned long lastupdateMillis = 0;
int lastbattery = -1;

void displayBatteryLevel()
{
    if (currentMillis - lastupdateMillis > 1000) {
      int battlevel = 0;
      byte retval;
      Wire.beginTransmission(0x75);
      Wire.write(0x78);
      if (Wire.endTransmission(false) == 0 && Wire.requestFrom(0x75, 1)) {
            retval = Wire.read() & 0xF0;
            if (retval == 0xE0) battlevel = 25;
            else if (retval == 0xC0) battlevel = 50;
            else if (retval == 0x80) battlevel = 75;
            else if (retval == 0x00) battlevel = 100;
      }
      if (lastbattery != battlevel){
            M5.Lcd.fillRect(250, 5, 56, 21, RGB(31, 63, 31));
            M5.Lcd.fillRect(306, 9, 4, 13, RGB(31, 63, 31));
            M5.Lcd.fillRect(252, 7, 52, 17, RGB(0, 0, 0));
            if (battlevel <= 25)
                M5.Lcd.fillRect(253, 8, battlevel/2, 15, RGB(31, 20, 10));
            else
                M5.Lcd.fillRect(253, 8, battlevel/2, 15, RGB(20, 40, 31));
            lastbattery = battlevel;
      }
      lastupdateMillis = currentMillis;
    }
}

void setup() {
    M5.begin();
    Wire.begin();
    M5.Lcd.clear();
}

void loop() {
    currentMillis = millis();
    displayBatteryLevel();
}

Zoologist 发表于 2020-2-24 10:47

我记得 Ip5306 是没有 I2C 接口的啊

https://wenku.baidu.com/view/cdc877fe66ec102de2bd960590c69ec3d4bbdb0e.html

vany5921 发表于 2020-2-24 11:17

Zoologist 发表于 2020-2-24 10:47
我记得 Ip5306 是没有 I2C 接口的啊

https://wenku.baidu.com/view/cdc877fe66ec102de2bd960590c69ec3d4bb ...

这个是可以定制的

botao258 发表于 2021-2-14 13:20

你好,请问为啥,或取不到电量信息呢?看到官网Power相关的API里有这样写;说老版本的M5Stack不能与IP5306通讯,可以怎么改到能通讯吗?
Power related functions depend on the IP5306 chip. Please refer to the data sheet IP5306 as required.

The older M5STACK hardware does not support communication with IP5306 chip. When using functions, also consider supporting out of control cases. *
Use initialization, communication check, and control in this order, as shown in the example below.

M5.Power.begin();
if(!M5.Power.canControl()) {
    //can't control.
    return;
}
M5.Power.lightSleep(SLEEP_SEC(5));

botao258 发表于 2021-2-14 16:45

老版本的黑色M5Stack是用的普通版本IP5306,所以没法I2C通讯,看能否尝试换成I2C版本的
页: [1]
查看完整版本: IP5306电量显示