CW2015是一款超紧凑、低成本电量计IC,适用于手持式和便携式设备中的锂离子(Li+)电池。
cw2015.cpp
#include "cw2015.h"CW2015::CW2015(TwoWire *pWire)
{_pWire = pWire;
}void CW2015::initCW2015(void)//初始化IIC
{_pWire->begin();
}uint16_t CW2015::readAnalogVoltage(void)//读取adc量化数据14Bit
{uint16_t AnalogVoltage=0x0000;_pWire->beginTransmission(CW_ADDR);_pWire->write(CW_VCELL);_pWire->endTransmission();_pWire->requestFrom(CW_ADDR, (uint8_t)2);AnalogVoltage = _pWire->read();AnalogVoltage<<=8;AnalogVoltage += _pWire->read();return (AnalogVoltage&0x3fff);
}
float CW2015::readBatVoltage(void)//电压
{float Voltage=0.0;uint16_t AnalogVoltage;AnalogVoltage=readAnalogVoltage();Voltage=AnalogVoltage*0.000305;return (Voltage);
}
uint8_t CW2015::readBatQuantityLowPre(void)//读取电量,低精度1%
{uint8_t BatQuantity = readRegister(CW_SOC);return (BatQuantity);
}
float CW2015::readBatQuantityHighPre(void)//读取电量,高精度1/256%
{float BatQuantity=0.0;uint8_t BatQuantityH = readRegister(CW_SOC);uint8_t BatQuantityL = readRegister(CW_SOC+1);BatQuantity=BatQuantityH+BatQuantityL/256.0;return (BatQuantity);
}
void CW2015::checkCW2015(void)//读版本寄存器,对比版本号
{byte check;check=readRegister(CW_VERSION);if(check!=0x70)Serial.println("cw2015 error!");//版本可能会有所不同else Serial.println("cw2015 ok!");
}
void CW2015::setSleep(void)//进入睡眠模式
{writeRegister(0xc0,CW_MODE);
}
void CW2015::wakeUp(void)//从睡眠模式中唤醒
{writeRegister(0x00,CW_MODE);
}
void CW2015::quickStart(void)//快速启动
{writeRegister(0x30,CW_MODE);
}
void CW2015::powerReset(void)//电源复位
{writeRegister(0x0f,CW_MODE);
}
byte CW2015::readRegister(byte reg)//读寄存器,单字节
{byte rdat;_pWire->beginTransmission(CW_ADDR);_pWire->write(reg);_pWire->endTransmission();_pWire->requestFrom(CW_ADDR, (uint8_t)1);rdat = _pWire->read();return (rdat);
}void CW2015::writeRegister(byte val,byte reg)//写寄存器,单字节
{_pWire->beginTransmission(CW_ADDR);_pWire->write(reg);_pWire->write(val);_pWire->endTransmission();
}
cw2015.h
#ifndef __CW2015_H__
#define __CW2015_H__#include <Arduino.h>
#include <Wire.h>#define CW_ADDR 0X62
#define CW_VERSION 0x00
#define CW_VCELL 0x02
#define CW_SOC 0x04
#define CW_RRT_ALET 0x06
#define CW_CONFIG 0x08
#define CW_MODE 0x0a
class CW2015
{
public:CW2015(TwoWire *pWire=&Wire);void initCW2015(void);uint16_t readAnalogVoltage(void);uint8_t readBatQuantityLowPre(void);//低精度%1float readBatQuantityHighPre(void);//高精度1/256%float readBatVoltage(void);//电压void checkCW2015(void);void setSleep(void);void wakeUp(void);void quickStart(void);void powerReset(void);protected:byte readRegister(byte reg);void writeRegister(byte val,byte reg);private:uint8_t _addr; // I2C communication address of CW2015TwoWire *_pWire; // The pointer to I2C communication method
};#endif
示例
芯片上电默认进入睡眠模式,需要唤醒才能读取数据。
#include "cw2015.h"/*** Hardware Connections:* -VCC = 3.3V* -GND = GND* -SDA = 18 (use inline 330 ohm resistor if your board is 5V)* -SCL = 23 (use inline 330 ohm resistor if your board is 5V)*/
TwoWire I2Cone = TwoWire(0);
CW2015 cw2015(&I2Cone);void setup()
{Serial.begin(115200);pinMode(22, INPUT);I2Cone.begin(18,23, 400000UL); //SDA0,SCL0cw2015.initCW2015();delay(100);Serial.println("Sensor init finish!");//cw_2015.powerReset();cw2015.wakeUp();delay(100);//cw2015.checkCW2015();
}void loop()
{float val = cw2015.readBatVoltage();uint8_t eleq = cw2015.readBatQuantityLowPre();Serial.print("Time:");Serial.print(millis()); // Get the system time from ArduinoSerial.print(" val:");Serial.print(val, 1); // Only print one decimal placeSerial.print("V");Serial.print(" eleq:");Serial.print(eleq); // Only print one decimal placeSerial.print("%");Serial.println();delay(1000);
}