Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAX17043 sensor integration #18788

Merged
merged 12 commits into from
Jul 22, 2023
Merged
1 change: 1 addition & 0 deletions CODE_OWNERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ In addition to @arendst the following code is mainly owned by:
| xsns_106_gdk101 | @Szewcson
| xsns_107_gm861 | @arendst
| xsns_108_tc74 | Michael Loftis
| xsns_110_max17043 | Vincent de Groot
| |
| Libraries |
| |
Expand Down
1 change: 1 addition & 0 deletions I2CDEVICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,4 @@ Index | Define | Driver | Device | Address(es) | Description
80 | USE_TC74 | xsns_108 | TC74 | 0x48 - 0x4F | Temperature sensor
81 | USE_PCA9557 | xdrv_69 | PCA95xx | 0x18 - 0x1F | 8-bit I/O expander as virtual button/switch/relay
82 | USE_SGP4X | xsns_109 | SGP4X | 0x59 | Gas (TVOC/NOx index)
83 | USE_MAX17043 | xsns_110 | MAX17043 | 0x36 | Fuel-gauge for 3.7 Volt Lipo battery
68 changes: 68 additions & 0 deletions lib/lib_i2c/DFRobot_MAX17043/DFRobot_MAX17043.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*!
* @file DFRobot_MAX17043.cpp
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [ouki.wang](ouki.wang@dfrobot.com)
* @version V1.0
* @date 2018-4-14
* @url https://github.com/DFRobot/DFRobot_MAX17043
*/

#include "DFRobot_MAX17043.h"

DFRobot_MAX17043::DFRobot_MAX17043() {}

int DFRobot_MAX17043::begin()
{
// write16(MAX17043_COMMAND, 0x0054); //power on reset
write16(MAX17043_COMMAND, 0x5400); //power on reset
delay(10);
_DBG_CODE(Serial.print("\nbegin: "));
_DBG_CODE(Serial.println(read16(MAX17043_CONFIG), HEX));
if(read16(MAX17043_CONFIG) == 0x971c) { //default 0x971c
write16(MAX17043_MODE, 0x4000); //quick start
write16(MAX17043_CONFIG, 0x9700);
_DBG_CODE(Serial.println(read16(MAX17043_CONFIG), HEX));
delay(10);
return 0;
}
return -1;
}

float DFRobot_MAX17043::readVoltage()
{
return (1.25f * (float)(read16(MAX17043_VCELL) >> 4));
}

float DFRobot_MAX17043::readPercentage()
{
uint16_t per = read16(MAX17043_SOC);
return (float)((per >> 8) + 0.003906f * (per & 0x00ff));
}

void DFRobot_MAX17043::setInterrupt(uint8_t per)
{
uint16_t temp;
if(per > 32)
temp = 32;
else if(per < 1)
temp = 1;
temp = 32 - temp;
writeRegBits(MAX17043_CONFIG, temp, 0x01f, 0);
}

void DFRobot_MAX17043::clearInterrupt()
{
writeRegBits(MAX17043_CONFIG, 0, 0x01, 5);
}

void DFRobot_MAX17043::setSleep()
{
writeRegBits(MAX17043_CONFIG, 1, 0x01, 7);
}

void DFRobot_MAX17043::setWakeUp()
{
writeRegBits(MAX17043_CONFIG, 0, 0x01, 7);
}
121 changes: 121 additions & 0 deletions lib/lib_i2c/DFRobot_MAX17043/DFRobot_MAX17043.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*!
* @file DFRobot_MAX17043.h
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [ouki.wang](ouki.wang@dfrobot.com)
* @version V1.0
* @date 2018-4-14
* @url https://github.com/DFRobot/DFRobot_MAX17043
*/

#ifndef __DFROBOT_MAX17043_H
#define __DFROBOT_MAX17043_H

#include "Arduino.h"
#include "Wire.h"

#define _DBG 0

#if(_DBG)
#define _DBG_CODE(x) x
#else
#define _DBG_CODE(x)
#endif

#define MAX17043_ADDRESS 0x36

#define MAX17043_VCELL 0x02
#define MAX17043_SOC 0x04
#define MAX17043_MODE 0x06
#define MAX17043_VERSION 0x08
#define MAX17043_CONFIG 0x0c
#define MAX17043_COMMAND 0xfe

class DFRobot_MAX17043 {
public:
/**
* @fn DFRobot_MAX17043
* @brief create MAX17043 object
* @return MAX17043 object
*/
DFRobot_MAX17043();
/**
* @fn begin
* @brief MAX17043 begin and test moudle
*
* @return initialization result
* @retval 0 successful
* @retval -1 faild
*/
int begin();
/**
* @fn readVoltage
* @brief read battery voltage in mV
* @return voltage in mV
*/
float readVoltage();
/**
* @fn readPercentage
* @brief read battery remaining capacity in percentage
*
* @return battery remaining capacity in percentage
*/
float readPercentage();
/**
* @fn setInterrupt
* @brief set MAX17043 interrput threshold
*
* @param per interrupt threshold as %1 - 32% (integer)
*/
void setInterrupt(uint8_t per);
/**
* @fn clearInterrupt
* @brief clear MAX17043 interrupt
*/
void clearInterrupt();
/**
* @fn setSleep
* @brief set MAX17043 in sleep mode
*
*/
void setSleep();
/**
* @fn setWakeUp
* @brief weak up MAX17043
*
*/
void setWakeUp();

private:
void write16(uint8_t reg, uint16_t dat) {
Wire.begin();
Wire.beginTransmission(MAX17043_ADDRESS);
Wire.write(reg);
Wire.write(dat >> 8);
Wire.write(dat);
Wire.endTransmission();
}

uint16_t read16(uint8_t reg) {
uint16_t temp;
Wire.begin();
Wire.beginTransmission(MAX17043_ADDRESS);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(MAX17043_ADDRESS, 2);
temp = (uint16_t)Wire.read() << 8;
temp |= (uint16_t)Wire.read();
Wire.endTransmission();
return temp;
}

void writeRegBits(uint8_t reg, uint16_t dat, uint16_t bits, uint8_t offset) {
uint16_t temp;
temp = read16(reg);
temp = (temp & (~(bits << offset))) | (dat << offset);
write16(reg, temp);
}
};

#endif
7 changes: 7 additions & 0 deletions lib/lib_i2c/DFRobot_MAX17043/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2010 DFRobot Co.Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
121 changes: 121 additions & 0 deletions lib/lib_i2c/DFRobot_MAX17043/README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# DFRobot_MAX17043

* [English Version](./README.md)

Gravity 3.7V锂电池电量计使用Gravity I2C接口,超低工作电流,通过Maxim专利算法,实时跟踪电池的相对充电状态(SOC,State-Of-Charge),无需充放电学习过程,无积累误差,即插即用,准确地测量锂电池的当前电压和剩余电量。模块预留低电量报警中断引脚,当电池电量低于指定电量时,该引脚产生一个下跳脉冲,触发主控的外部中断。

![产品效果图](./resources/images/DFR0563.jpg)


## 产品链接([https://www.dfrobot.com.cn/goods-1743.html](https://www.dfrobot.com.cn/goods-1743.html))
SKU: DFR0563

## 目录

* [概述](#概述)
* [连接](连接)
* [库安装](#库安装)
* [方法](#方法)
* [兼容性](#兼容性)
* [历史](#历史)
* [创作者](#创作者)

## 概述

提供 Arduino 库,用于通过 I2C 读取和解释 MAX17043 数据。

## 连接
相同颜色的线连接在一起,我们只举例说明主板是如何连接到电量计的。接线时要注意管脚的对应关系,接线图如下:

* Arduino UNO
<br>
<img src="./resources/images/UNO.jpg">
<br>

* ESP32
<br>
<img src="./resources/images/esp32.jpg">
<br>


## 库安装

这里有2种安装方法:
1. 使用此库前,请首先下载库文件,将其粘贴到\Arduino\libraries目录中,然后打开examples文件夹并在该文件夹中运行演示。
2. 直接在Arduino软件库管理中搜索下载 DFRobot_MAX17043 库

## 方法

```C++
/**
* @fn DFRobot_MAX17043
* @brief 构造MAX17043对象
* @return MAX17043 类对象
*/
DFRobot_MAX17043();
/**
* @fn begin
* @brief MAX17043 初始化
*
* @return 初始化结果
* @retval 0 成功
* @retval -1 失败
*/
int begin();
/**
* @fn readVoltage
* @brief 读电池电压,单位: mV
* @return 电池电压,单位:mV
*/
float readVoltage();
/**
* @fn readPercentage
* @brief 读取剩余电池容量的百分比
*
* @return 剩余电池容量的百分比
*/
float readPercentage();
/**
* @fn setInterrupt
* @brief 设置 MAX17043 中断阈值
*
* @param per 中断阈值范围: %1 - 32% (整数)
*/
void setInterrupt(uint8_t per);
/**
* @fn clearInterrupt
* @brief 清除 MAX17043 中断
*/
void clearInterrupt();
/**
* @fn setSleep
* @brief 设置 MAX17043 进入睡眠模式
*
*/
void setSleep();
/**
* @fn setWakeUp
* @brief 唤醒 MAX17043
*/
void setWakeUp();
```

## 兼容性

| MCU | Work Well | Work Wrong | Untested | Remarks |
| ------------------ | :-------: | :--------: | :------: | ------- |
| FireBeetle-ESP32 | √ | | |
| FireBeetle-ESP8266 | √ | | |
| Arduino uno | √ | | |

## 历史

- 2018/04/14 - 1.0.0 版本

## 创作者

Written by ouki.wang(ouki.wang@dfrobot.com), 2018. (Welcome to our [website](https://www.dfrobot.com/))




Loading