单片机 MM32 HAL EG2801 电量计 代码 中文资料 例子 代码

#include "main.h"
#include "hal_iic.h"
#include "eg2801.h"

EG2801_RAM EG2801_RAM_DATA =
{
    .Flags = 0,
    .StateOfCharge = 100,
};
const IIC_PIN EG2801_GPIO =
{
    .SCL_PORT  = GPIOB,
    .SCL_PIN   = GPIO_Pin_6,

    .SDA_PORT  = GPIOB,
    .SDA_PIN   = GPIO_Pin_7,

    .DelayTick = 2,
    .ADDR      = 0x28
};

/*********************************************
函数名:EG2801_Init
功  能:电池管理器件初始化
形  参:
返回值:
备  注:
作  者:薛建强
时  间:2019/06/28
使  用:
**********************************************/
void EG2801_Init(void)
{
    /*赋初值*/
    EG2801_RAM_DATA.Flags = 0;
    EG2801_RAM_DATA.StateOfCharge = 100;
    IIC_Init(&EG2801_GPIO);//IIC初始化
}

/*********************************************
函数名:EG2801_read_DATA
功  能:利用IIC从EG2801读取2个字节
形  参:ACK--1应答 0不应答
返回值:读取到的2个字节
备  注:
作  者:薛建强
时  间:2019/06/28
使  用:
**********************************************/
uint16_t EG2801_read_DATA(uint8_t ACK)
{
    uint8_t eg2801_buff[2] = {0};
    IIC_ReadByte(&EG2801_GPIO, &eg2801_buff[0]);
    IIC_ACK(&EG2801_GPIO);
    IIC_ReadByte(&EG2801_GPIO, &eg2801_buff[1]);
    if (ACK)
        IIC_ACK(&EG2801_GPIO);
    else
        IIC_NACK(&EG2801_GPIO);
    return ((eg2801_buff[1] << 8) | eg2801_buff[0]);
}


/*********************************************
函数名:EG2801_read_Vcc
功  能:读取EG2801数据
形  参:
返回值:
备  注:EG2801_RAM_DATA.StateOfCharge --电池剩余电量百分比

作  者:薛建强
时  间:2019/06/28
使  用:
**********************************************/
uint8_t temp = 0;
void EG2801_read_Vcc(void)
{

    /*读取剩余电量*/
    IIC_Start(&EG2801_GPIO);
    IIC_WriteByte(&EG2801_GPIO, EG2801_GPIO.ADDR); //写命令
    IIC_Wait_ACK(&EG2801_GPIO);
    IIC_WriteByte(&EG2801_GPIO, 0x55);             //命令码
    IIC_Wait_ACK(&EG2801_GPIO);
    IIC_WriteByte(&EG2801_GPIO, 0x2C);             //寄存器地址--电池剩余电量百分比
    IIC_Wait_ACK(&EG2801_GPIO);

    IIC_Start(&EG2801_GPIO);
    IIC_WriteByte(&EG2801_GPIO, EG2801_GPIO.ADDR | 1); //读命令
    IIC_Wait_ACK(&EG2801_GPIO);
    temp = EG2801_read_DATA(0);
    if (temp <= 100)
    {
        EG2801_RAM_DATA.StateOfCharge =  temp;
    }
    else
    {
        EG2801_RAM_DATA.StateOfCharge = 0;
    }

    IIC_Stop(&EG2801_GPIO);

    /*读取电池状态 EG2801中文资料.pdf--9页*/
    IIC_Start(&EG2801_GPIO);
    IIC_WriteByte(&EG2801_GPIO, EG2801_GPIO.ADDR); //写命令
    IIC_Wait_ACK(&EG2801_GPIO);
    IIC_WriteByte(&EG2801_GPIO, 0x55);             //命令码
    IIC_Wait_ACK(&EG2801_GPIO);
    IIC_WriteByte(&EG2801_GPIO, 0x0A);             //寄存器地址
    IIC_Wait_ACK(&EG2801_GPIO);

    IIC_Start(&EG2801_GPIO);
    IIC_WriteByte(&EG2801_GPIO, EG2801_GPIO.ADDR | 1); //读命令
    IIC_Wait_ACK(&EG2801_GPIO);
    temp = EG2801_read_DATA(0);
    if (temp != 0xff)
    {
        EG2801_RAM_DATA.Flags =  temp;
    }

    IIC_Stop(&EG2801_GPIO);
}




#ifndef __EG2801_
#define __EG2801_
#include "main.h"
#include "eg2801.h"


typedef struct  _EG2801_RAM
{

//    uint16_t    Temperature;
//    uint16_t    Voltage;
    uint16_t    Flags;
//    uint16_t    RemainingCapacity;
//    uint16_t    FullChargeCapacity;
//    uint16_t    AverageCurrent;
//    uint16_t    TimeToEmpty;
//    uint16_t    TimeToFull;
//    uint16_t    StandbyCurrent;
//    uint16_t    StandbyTimeToEmpty;
//    uint16_t    MaxLoadCurrent;
//    uint16_t    MaxLoadTimeToEmpty;
//    uint16_t    AvailableEnerge;
//    uint16_t    AveragePower;
//    uint16_t    TimeToEptyAtCnstPwr;
//    uint16_t    Internal_Temp;
//    uint16_t    CycleCount;
    uint16_t    StateOfCharge;
//    uint16_t    StateOfHealth;

uint8_t ChargeImg;          //0--没有充电  1--开始充电
} EG2801_RAM;

extern EG2801_RAM EG2801_RAM_DATA;

void EG2801_Init(void);
void EG2801_read_Vcc(void);

#endif


猜你喜欢

转载自blog.csdn.net/qq_29246181/article/details/105480932