#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
};
void EG2801_Init(void)
{
EG2801_RAM_DATA.Flags = 0;
EG2801_RAM_DATA.StateOfCharge = 100;
IIC_Init(&EG2801_GPIO);
}
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]);
}
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);
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 Flags;
uint16_t StateOfCharge;
uint8_t ChargeImg;
} EG2801_RAM;
extern EG2801_RAM EG2801_RAM_DATA;
void EG2801_Init(void);
void EG2801_read_Vcc(void);
#endif