/************单片机:HT66018********/
/************ 日期:160315-160323********/
//20160318 更改单片机 改为018 添加单节电压采集
//20160323 总电压电阻配比更改。
//20160323 放电中止时,添加充电器高压判断。
//20160323 硬件需改动。电压采样方式改用MOS,LED外加上拉,充电加二极管。
//版本号V1.0
#include "HT66F018.h"
//宏定义
#define VERSION_HIGH1
#define VERSION_LOW0
#define TIME2MS5_10MS4
#define TIME2MS5_20MS8
#define TIME10MS_20MS2
#define TIME10MS_30MS3
#define TIME10MS_50MS5
#define TIME10MS_100MS10
#define TIME10MS_200MS20
#define TIME10MS_300MS30
#define TIME10MS_500MS50
#define TIME10MS_1S100
#define PWM_MAX1024
#define PWM_START200
#define CHG_OVER_TIMETIME_4H
#define TIME_4H14400
//充电状态
#define CHG_HIGH_STATE 1
#define CHG_STATE 2
#define DHG_STATE 3
#define CHG_HIGH_VOLTAGECHG_VOLTAGE_10V9//4.2*n+0.4+2.1
#define CHG_LOW_VOLTAGECHG_VOLTAGE_2V
//配比电阻为47K/15K 15/62*4096/3.3*u=300.29u
#define CHG_VOLTAGE_10V9 3273
#define CHG_VOLTAGE_2V 601
//错误状态标志位
#define CHG_HIGH_FLAG1
#define BAT_DEAD_FLAG2
#define CUT_WIRE_FLAG3
//充放电错误区分标志
#define CHG_ERROR_FLAG 1
//工作模式
#define POWER_ON_MODE0
#define CHG_MODE 1
#define CHG_END_MODE2
#define DHG_MODE3
#define ERROR_MODE4
#define CHG_OVTEMP_MODE5
#define CHG_OVVOL_MODE6
#define END_MODE7
//电池状态
#define BAT_LEVEL_FULL BAT_VOLTAGE_8V2//4.1*2
#define BAT_LEVEL_LOWBAT_VOLTAGE_6V//3*2
#define BAT_PWM_LEVEL_HIGHBAT_VOLTAGE_5V//2.5*2
#define BAT_PWM_LEVEL_LOWBAT_VOLTAGE_3V//1.5*2
//电阻配比 47K/15K 15/62*u*4096/3.3=300.293u
#define BAT_VOLTAGE_8V2 2462
#define BAT_VOLTAGE_6V1802
#define BAT_VOLTAGE_5V1501
#define BAT_VOLTAGE_3V901
#define BAT_FULL_STATE 1
#define BAT_OK_STATE2
#define BAT_LOW_STATE 3
#define BAT_PWM_STATE4
#define BAT_DEAD_STATE5
//单节检测 电阻配比47K/30K 30/77*u*4096/3.3=483.589u
//和总压采样倍率是483.589/300.293=1.61
#define CELL_CUTCELL_VOLTAGE_1V
#define CELL_LOWCELL_VOLTAGE_2V7
#define CELL_FULLCELL_VOLTAGE_4V18
#define CELL_VOLTAGE_1V484
#define CELL_VOLTAGE_2V71306
#define CELL_VOLTAGE_4V182021
#define CELL_STATE_OK 0
#define CELL_STATE_CUT 1
#define CELL_STATE_LOW 2
#define CELL_STATE_FULL 3
#define CURRENT_OVER_STATE 1
#define CURRENT_OK_STATE 0
#define CURRENT_OVER_LEVEL_HIGHCURRENT_20A
#define CURRENT_OVER_LEVEL_LOWCURRENT_10A
//采样电阻4毫欧 4*i*0.001*4096/3.3=4.965i
#define CURRENT_20A99
#define CURRENT_10A50
#define TEMP_65C697 //2.051K
#define TEMP_50C1082 //3.588K
#define TEMP_40C1420 //5.309K
#define TEMP_0C3123 //32.116K
#define TEMP_N10C3448 //53.198K
//AD通道宏定义 66F018
#define AD_TEMP_CHANNEL2//AN2 温度
#define AD_BAT_CHANNEL4//AN4 电池总压
#define AD_CHG_CHANNEL3//AN3 充电器识别
#define AD_CURRENT_CHANNEL5//AN5 电流
#define AD_BAT1_CHANNEL6//AN6 第一节电池电压
//控制端口宏定义 66F018
#define PWM_PORT_HIGH _pb|=0b00001000//pb3 PWM 端口拉高
#define PWM_PORT_LOW _pb&=0b11110111 //pb3 PWM 端口拉低
#define PWM_ON _t2on=1//tp2 放电PWM输出
#define PWM_OFF _t2on=0
#define CHG_ON _pa|=0b00001000//pa3 输出高
#define CHG_OFF _pa&=0b11110111//pa3 输出低
#define BAT_AD_ON _pb|=0b01000000//pb6 高总电压采样IO控制口的打开
#define BAT_AD_OFF _pb&=0b10111111//pb6 低总电压采样IO控制口的关闭
#define BAT1_AD_ON _pb|=0b00100000//pb5 高第一节电压采样IO控制口的打开
#define BAT1_AD_OFF _pb&=0b11011111//pb5 低第一节电压采样IO控制口的关闭
#define LEDR_ON _pb&=0b11111110//pb0红灯低亮
#define LEDR_OFF _pb|=0b00000001//pb0 红灯高灭
#define LEDG_ON _pb&=0b11111101//pb1 绿灯低亮
#define LEDG_OFF _pb|=0b00000010//pb1 绿灯高灭
#define VER_HIGH_pb|=0b00010000//pb4 版本号管脚
#define VER_LOW_pb&=0b11101111//pb4 版本号管脚
//变量定义
unsigned int uiReset;
unsigned charucTime_10mS;
//AD变量
unsigned char ucADChannel;
//AD充电变量
unsigned char ucADCHGFirst;
unsigned int uiADCHGValue;
unsigned int uiADCHGValueSUM;
unsigned char ucADCHGTime1;
unsigned char ucADCHGTime2;
unsigned char ucADCHGTime3;
unsigned char ucCHGDHGState;
//AD总电压变量
unsigned char ucADBATFirst;
unsigned int uiADBATValue;
unsigned int uiADBATValueSUM;
unsigned char ucADBATTime1;
unsigned char ucADBATTime2;
unsigned char ucADBATTime3;
unsigned char ucADBATTime4;
unsigned char ucADBATTime5;
unsigned char ucBATState;
//AD第一节电压变量
unsigned char ucADBAT1First;
unsigned int uiADBAT1Value;
unsigned int uiADBAT1ValueSUM;
unsigned char ucADBAT1Time1;
unsigned char ucADBAT1Time2;
unsigned char ucADBAT1Time3;
unsigned char ucCell1State;
//AD第二节电压变量
unsigned int uiADBAT2Value;
unsigned char ucADBAT2Time1;
unsigned char ucADBAT2Time2;
unsigned char ucADBAT2Time3;
unsigned char ucCell2State;
//AD电流变量
unsigned char ucADCurrFirst;
unsigned int uiADCURRValue;
unsigned int uiADCURRValueSUM;
unsigned char ucADCURRTime1;
unsigned char ucADCURRTime2;
//unsigned char ucADCHGTime3;
unsigned char ucCURRState;
//AD温度变量
unsigned char ucADTempFirst;
unsigned int uiADTempValue;
unsigned int uiADTempValueSUM;
unsigned char ucADTempTime1;
unsigned char ucADTempTime2;
unsigned char ucADTempTime3;
unsigned char ucTempState;
//充放电控制等变量
unsigned char ucWorkMode;
unsigned char ucTime_1S;
unsigned char ucBATStateFlag;
unsigned char ucPWMTime;
unsigned char uiCHGTime;
unsigned int uiPWMNow;
unsigned int uiPWMGoal;
unsigned char ucDHGPWMTime;
unsigned char ucVersionTime;
unsigned char ucERRORFlag;
//函数声明
void SystemInit(void);//系统初始化
void VariInit(void);//变量初始化
void Timer(void);//变量初始化
void FeedWDT(void);//清看门狗
unsigned int Get_AD_Value(unsigned char ucADChannel);
void Control_Function(void);
void Delay_MS(unsigned char);
void AD_Manage(void);
void Version_Fution(void);
//函数定义
void SystemInit()//系统初始化
{
_smod=0b11100000;//系统时钟4M
_lvrc=0b00110011;//低压复位2.55V
_cpc=0x08;//关闭比较器 关闭CX
_acerl=0b01111100;//AN2-AN6
_tmpc=0b00000100;//tp2使能 tp0 tp1除能 系统时钟输出关闭 PB4作IO口
LEDG_OFF;//初始化关闭灯
LEDR_OFF;
BAT_AD_OFF;//关闭总电压AD开关
BAT1_AD_OFF;//关闭第一节电压AD开关
CHG_OFF;//充电控制口关闭
PWM_OFF;//放电PWM 关闭
PWM_PORT_HIGH;//放电控制口关闭 MOS GS强制拉低
VER_LOW;//版本号初始为低
//端口初始化
_pawu=0x00;//关闭唤醒
_papu=0x00;//关闭上拉
_pac=0b11110000;//pa4-pa7 ad输入 其余输出 0输出1输入
_pa&=0b11111110;//pa0
_pa&=0b11111101;//pa1
_pa&=0b11111011;//pa2
_pbpu=0x00;//关闭上拉
_pbc=0b00000100;//PB2 AD 输入 0输出1输入
_pcpu=0x00;//关闭上拉
_pcc=0x00;//输出
_pc=0x00;
//定时器2.5MS
_tm0c0=0b00000000;//频率1M
_tm0c1=0b11000001;//定时模式 比较器A匹配
_tm0al=0xc4;
_tm0ah=0x09;//2500 定时2.5MS
_t0ae=0;//禁止中断
_t0on=1;//打开定时器。开始定时
//放电MOS
_tm2c0=0b00000000;//频率1M
_tm2c1=0b10100100;//PWM输出 CCRP周期CCRA占空比 反向
_tm2rp=0x04;//周期为4*256*1us=1024us
//AD
_adcr1=0b00000010;//频率1M 内部参考源
_ade=0;//关中断
//WDT
_wdtc=0b01010010;//128MS
FeedWDT();
//再次初始化端口
LEDG_OFF;//初始化关闭灯
LEDR_OFF;
BAT_AD_OFF;//关闭总电压AD开关
BAT1_AD_OFF;//关闭第一节电压AD开关
CHG_OFF;//充电控制口关闭
PWM_OFF;//放电PWM 关闭
PWM_PORT_HIGH;//放电控制口关闭 MOS GS强制拉低
VER_LOW;//版本号初始为低
}
/*********变量初始化**********/
void VariInit()
{
ucTime_10mS=0;
//AD变量
ucADChannel=AD_CHG_CHANNEL;
//AD充电器判断变量
ucADCHGFirst=0;
uiADCHGValue=0;
uiADCHGValueSUM=0;
ucADCHGTime1=0;
ucADCHGTime2=0;
ucADCHGTime3=0;
ucCHGDHGState=0;
//AD总电压变量
ucADBATFirst=0;
uiADBATValue=0;
uiADBATValueSUM=0;
ucADBATTime1=0;
ucADBATTime2=0;
ucADBATTime3=0;
ucADBATTime4=0;
ucADBATTime5=0;
ucBATState=0;
//AD第一节电压变量
ucADBAT1First=0;
uiADBAT1Value=0;
uiADBAT1ValueSUM=0;
ucADBAT1Time1=0;
ucADBAT1Time2=0;
ucADBAT1Time3=0;
ucCell1State=0;
//AD第二节电压变量
uiADBAT2Value=0;
ucADBAT2Time1=0;
ucADBAT2Time2=0;
ucADBAT2Time3=0;
ucCell2State=0;
//AD电流变量
ucADCurrFirst=0;
uiADCURRValue=0;
uiADCURRValueSUM=0;
ucADCURRTime1=0;
ucADCURRTime2=0;
ucCURRState=0;
//AD温度变量
ucADTempFirst=0;
uiADTempValue=0 ;
uiADTempValueSUM=0;
ucADTempTime1=0;
ucADTempTime2=0;
ucADTempTime3=0;
ucTempState=0;
//AD充放电控制等变量
ucWorkMode=POWER_ON_MODE;
ucTime_1S=0;
ucBATStateFlag=0;
ucPWMTime=0;
uiCHGTime=0;
uiPWMNow=0;
uiPWMGoal=0;
ucDHGPWMTime=0;
ucVersionTime=0;
ucERRORFlag=0;
}
/*********定时**********/
void Timer()
{
ucDHGPWMTime++;//放电PWM软启动计时
ucTime_10mS++;
if(ucTime_10mS>=TIME2MS5_10MS)//10MS计时
{
ucTime_10mS=0;
//AD充电器判断变量
ucADCHGTime1++;
ucADCHGTime2++;
ucADCHGTime3++;
//AD总电压变量
ucADBATTime1++;
ucADBATTime2++;
ucADBATTime3++;
ucADBATTime4++;
ucADBATTime5++;
//AD第一节电压变量
ucADBAT1Time1++;
ucADBAT1Time2++;
ucADBAT1Time3++;
//AD第二节电压变量
ucADBAT2Time1++;
ucADBAT2Time2++;
ucADBAT2Time3++;
//AD电流变量
ucADCURRTime1++;
ucADCURRTime2++;
//AD温度变量
ucADTempTime1++;
ucADTempTime2++;
ucADTempTime3++;
//AD充放电控制等变量
ucPWMTime++;//PWM 充电计时
ucVersionTime++;//版本号计时
ucTime_1S++;//1秒计时
if(ucTime_1S>=TIME10MS_1S)
{
ucTime_1S=0;
if(ucWorkMode==CHG_MODE)
{
uiCHGTime++;//充电计时
}
}
}
}
/*********清看门狗**********/
void FeedWDT()
{
asm("CLR WDT");
}
/*********系统延时空指令**********/
void Delay_MS(unsigned char j)//毫秒 不超过255毫秒
{
unsigned char i;
while(j--)
{
for(i=0;i<94;i++)
FeedWDT();
}
}
/*********AD函数控制处理函数**********/
void AD_Manage()
{
unsigned int uiADTemp;
switch(ucADChannel)
{
case AD_CHG_CHANNEL:
ucADChannel=AD_BAT_CHANNEL;//充电器充电状态AD判断
uiADTemp=Get_AD_Value(AD_CHG_CHANNEL);//滚动平均值
if(ucADCHGFirst==0)
{
ucADCHGFirst=1;
uiADCHGValueSUM=uiADTemp<<2;
}
else
{
uiADCHGValueSUM=uiADCHGValueSUM+uiADTemp;
}
uiADCHGValue=uiADCHGValueSUM>>2;
uiADCHGValueSUM=uiADCHGValueSUM-uiADCHGValue;
if(uiADCHGValue>=CHG_HIGH_VOLTAGE)//充电器高压
{
ucADCHGTime2=0;
ucADCHGTime3=0;
if(ucADCHGTime1>=TIME10MS_100MS)
{
ucADCHGTime1=TIME10MS_100MS;
ucCHGDHGState=CHG_HIGH_STATE;
}
}
else if(uiADCHGValue>=CHG_LOW_VOLTAGE)//充电状态
{
ucADCHGTime1=0;
ucADCHGTime3=0;
if(ucADCHGTime2>=TIME10MS_50MS)
{
ucADCHGTime2=TIME10MS_50MS;
ucCHGDHGState=CHG_STATE;
BAT_AD_ON; //打开总电压AD控制端口
BAT1_AD_ON; //打开第一节电压AD控制端口
}
}
else//放电状态
{
ucADCHGTime1=0;
ucADCHGTime2=0;
if(ucADCHGTime3>=TIME10MS_50MS)
{
ucADCHGTime3=TIME10MS_50MS;
ucCHGDHGState=DHG_STATE;
BAT_AD_ON; //打开总电压AD控制端口
BAT1_AD_ON; //打开第一节电压AD控制端口
}
}
break;
case AD_BAT_CHANNEL://总电压AD
ucADChannel=AD_BAT1_CHANNEL;//下一次电流AD
uiADTemp=Get_AD_Value(AD_BAT_CHANNEL);//滚动平均值
if(ucADBATFirst==0)
{
ucADBATFirst=1;
uiADBATValueSUM=uiADTemp<<2;
}
else
{
uiADBATValueSUM=uiADBATValueSUM+uiADTemp;
}
uiADBATValue=uiADBATValueSUM>>2;
uiADBATValueSUM=uiADBATValueSUM-uiADBATValue;
if(uiADBATValue>=BAT_LEVEL_FULL)//大于4.1*2
{
ucADBATTime2=0;
ucADBATTime3=0;
ucADBATTime4=0;
ucADBATTime5=0;
if(ucADBATTime1>=TIME10MS_100MS)
{
ucADBATTime1=TIME10MS_100MS;
ucBATState=BAT_FULL_STATE;
}
}
else if(uiADBATValue<=BAT_PWM_LEVEL_LOW)//小于1.5*2
{
ucADBATTime1=0;
ucADBATTime3=0;
ucADBATTime4=0;
ucADBATTime5=0;
if(ucADBATTime2>=TIME10MS_100MS)
{
ucADBATTime2=TIME10MS_100MS;
ucBATState=BAT_DEAD_STATE;
}
}
else if(uiADBATValue<=BAT_PWM_LEVEL_HIGH)//小于2.5*2 大于1.5*2
{
ucADBATTime1=0;
ucADBATTime2=0;
ucADBATTime4=0;
ucADBATTime5=0;
if(ucADBATTime3>=TIME10MS_100MS)
{
ucADBATTime3=TIME10MS_100MS;
ucBATState=BAT_PWM_STATE;
}
}
else if(uiADBATValue<=BAT_LEVEL_LOW)//小于3.0*2 大于2.5*2
{
ucADBATTime1=0;
ucADBATTime2=0;
ucADBATTime3=0;
ucADBATTime5=0;
if(ucADBATTime4>=TIME10MS_100MS)
{
ucADBATTime4=TIME10MS_100MS;
ucBATState=BAT_LOW_STATE;
}
}
else//
{
ucADBATTime1=0;
ucADBATTime2=0;
ucADBATTime3=0;
ucADBATTime4=0;
if(ucADBATTime5>=TIME10MS_50MS)
{
ucADBATTime5=TIME10MS_50MS;
ucBATState=BAT_OK_STATE;
}
}
break;
/////////////////////////////
case AD_BAT1_CHANNEL://总电压AD
ucADChannel=AD_CURRENT_CHANNEL;//下一次电流AD
uiADTemp=Get_AD_Value(AD_BAT1_CHANNEL);//滚动平均值
if(ucADBAT1First==0)
{
ucADBAT1First=1;
uiADBAT1ValueSUM=uiADTemp<<2;
}
else
{
uiADBAT1ValueSUM=uiADBAT1ValueSUM+uiADTemp;
}
uiADBAT1Value=uiADBAT1ValueSUM>>2;
uiADBAT1ValueSUM=uiADBAT1ValueSUM-uiADBAT1Value;
/****/
if(uiADBAT1Value>=CELL_FULL)//大于4.18
{
ucADBAT1Time2=0;
ucADBAT1Time3=0;
if(ucADBAT1Time1>=TIME10MS_1S)
{
ucADBAT1Time1=TIME10MS_1S;
ucCell1State=CELL_STATE_FULL;
}
}
else if(uiADBAT1Value<=CELL_CUT)//小于1V
{
ucADBAT1Time1=0;
ucADBAT1Time3=0;
if(ucADBAT1Time2>=TIME10MS_1S)
{
ucADBAT1Time2=TIME10MS_1S;
ucCell1State=CELL_STATE_CUT;
}
}
else if(uiADBAT1Value<=CELL_LOW)//小于2.7V
{
ucADBAT1Time1=0;
ucADBAT1Time2=0;
if(ucADBAT1Time3>=TIME10MS_1S)
{
ucADBAT1Time3=TIME10MS_1S;
ucCell1State=CELL_STATE_LOW;
}
}
else
{
ucADBAT1Time1=0;
ucADBAT1Time2=0;
ucADBAT1Time3=0;
ucCell1State=CELL_STATE_OK;
}
//////////////
if((unsigned int) (uiADBATValue*1.61)>uiADBAT1Value)//采样配比差异 放大后,一样比例。
{
uiADBAT2Value=(unsigned int) (uiADBATValue*1.61)-uiADBAT1Value;
if(uiADBAT2Value>=CELL_FULL)//大于4.18
{
ucADBAT2Time2=0;
ucADBAT2Time3=0;
if(ucADBAT2Time1>=TIME10MS_1S)
{
ucADBAT2Time1=TIME10MS_1S;
ucCell2State=CELL_STATE_FULL;
}
}
else if(uiADBAT2Value<=CELL_CUT)//小于1V
{
ucADBAT2Time1=0;
ucADBAT2Time3=0;
if(ucADBAT2Time2>=TIME10MS_1S)
{
ucADBAT2Time2=TIME10MS_1S;
ucCell2State=CELL_STATE_CUT;
}
}
else if(uiADBAT2Value<=CELL_LOW)//小于2.7V
{
ucADBAT2Time1=0;
ucADBAT2Time2=0;
if(ucADBAT2Time3>=TIME10MS_1S)
{
ucADBAT2Time3=TIME10MS_1S;
ucCell2State=CELL_STATE_LOW;
}
}
else
{
ucADBAT2Time1=0;
ucADBAT2Time2=0;
ucADBAT2Time3=0;
ucCell2State=CELL_STATE_OK;
}
}
///////
break;
///////////////////////////////
case AD_CURRENT_CHANNEL:
ucADChannel=AD_TEMP_CHANNEL;
uiADTemp=Get_AD_Value(AD_CURRENT_CHANNEL);
if(uiADCURRValue>=CURRENT_OVER_LEVEL_HIGH)
{
ucADCURRTime2=0;
if(ucADCURRTime1>=TIME10MS_500MS)
{
ucADCURRTime1=TIME10MS_500MS;
ucCURRState=CURRENT_OVER_STATE;
}
}
else if(uiADCURRValue>=CURRENT_OVER_LEVEL_LOW)
{
ucADCURRTime1=0;
if(ucADCURRTime2>=TIME10MS_1S)
{
ucADCURRTime2=TIME10MS_1S;
ucCURRState=CURRENT_OVER_STATE;
}
}
else
{
ucADCURRTime1=0;
ucADCURRTime2=0;
ucCURRState=CURRENT_OK_STATE;
}
break;
case AD_TEMP_CHANNEL:
ucADChannel=AD_CHG_CHANNEL;
uiADTemp=Get_AD_Value(AD_TEMP_CHANNEL);
if(ucADTempFirst==0)
{
ucADTempFirst=1;
uiADTempValueSUM=uiADTemp<<2;
}
else
{
uiADTempValueSUM=uiADTempValueSUM+uiADTemp;
}
uiADTempValue=uiADTempValueSUM>>2;
uiADTempValueSUM=uiADTempValueSUM-uiADTempValue;
if((uiADTempValue>=TEMP_N10C)||(uiADTempValue<=TEMP_65C))
{
ucADTempTime2=0;
ucADTempTime3=0;
if(ucADTempTime1>=TIME10MS_1S)
{
ucADTempTime1=TIME10MS_1S;
ucTempState=1;//放电过温<-10 >65
}
}
else if((uiADTempValue>=TEMP_0C)||(uiADTempValue<=TEMP_50C))
{
ucADTempTime1=0;
ucADTempTime3=0;
if(ucADTempTime2>=TIME10MS_1S)
{
ucADTempTime2=TIME10MS_1S;
ucTempState=2;//充电过温<0 >50
}
}
else if((uiADTempValue>TEMP_50C)&&(uiADTempValue<=TEMP_40C))
{
ucADTempTime1=0;
ucADTempTime2=0;
if(ucADTempTime3>=TIME10MS_1S)
{
ucADTempTime3=TIME10MS_1S;
ucTempState=3;//40-50°
}
}
else//0-40
{
ucADTempTime1=0;
ucADTempTime2=0;
ucADTempTime3=0;
ucTempState=0;
}
break;
default:
ucADChannel=AD_CHG_CHANNEL;
break;
}
}
/*********充放电控制处理函数**********/
void Control_Function()
{
switch(ucWorkMode)
{
case POWER_ON_MODE:
LEDG_OFF;//初始化关闭灯
LEDR_OFF;
PWM_OFF;//电机停止
PWM_PORT_HIGH;//拉高 电机停止
CHG_OFF;//充电停止
if(ucCHGDHGState==CHG_HIGH_STATE)//充电器高压
{
ucWorkMode=ERROR_MODE;
ucBATStateFlag=CHG_HIGH_FLAG;
}
else if(ucCHGDHGState==CHG_STATE)//充电状态
{
if(ucBATState==BAT_FULL_STATE)
{
ucWorkMode=CHG_END_MODE;//充满
}
else if(ucBATState==BAT_DEAD_STATE)//电池不良电压小于1.5*n
{
ucWorkMode=ERROR_MODE;
ucBATStateFlag=BAT_DEAD_FLAG;
ucERRORFlag=CHG_ERROR_FLAG;
}
else
{
ucWorkMode=CHG_MODE;
ucPWMTime=0;
uiCHGTime=0;
}
}
else if(ucCHGDHGState==DHG_STATE)
{
if((ucBATState==BAT_DEAD_STATE)||(ucBATState==BAT_PWM_STATE)||(ucBATState==BAT_LOW_STATE))//小于3.0*2V
{
ucWorkMode=END_MODE;
}
else if((ucBATState==BAT_OK_STATE)||(ucBATState==BAT_FULL_STATE))
{
ucWorkMode=DHG_MODE;
uiPWMNow=PWM_START;
uiPWMGoal=PWM_MAX;
LEDG_OFF;
LEDR_OFF;
PWM_ON;
}
}
break;
case CHG_MODE:
LEDR_ON;
LEDG_OFF;
_pa2=~_pa2;
if(ucBATState==BAT_PWM_STATE)//脉冲充电
{
if(ucPWMTime>=TIME10MS_200MS)
{
ucPWMTime=0;
CHG_OFF;
}
else if(ucPWMTime>=TIME10MS_100MS)
{
CHG_ON;
}
else
{
CHG_OFF;
}
}
else//正常充电
{
CHG_ON;
_pa0=~_pa0;
}
if((ucCell1State==CELL_STATE_FULL)||(ucCell2State==CELL_STATE_FULL))//单节充满
{
CHG_OFF;
ucWorkMode=CHG_END_MODE;
}
else if((ucCell1State==CELL_STATE_CUT)||(ucCell2State==CELL_STATE_CUT))//断线
{
CHG_OFF;
ucWorkMode=ERROR_MODE;
ucBATStateFlag=CUT_WIRE_FLAG;
ucERRORFlag=CHG_ERROR_FLAG;
}
if(uiCHGTime>=CHG_OVER_TIME)//充电计时溢出
{
CHG_OFF;
ucWorkMode=CHG_END_MODE;
}
if((ucTempState==1)||(ucTempState==2))//充电过温大于50度小于0度
{
CHG_OFF;
ucWorkMode=CHG_OVTEMP_MODE;
}
if(ucCHGDHGState==DHG_STATE)//拔掉充电器关机
{
ucWorkMode=END_MODE;
}
break;
case CHG_END_MODE:
LEDG_ON;
LEDR_OFF;
CHG_OFF;
if(ucCHGDHGState==DHG_STATE)//拔掉充电器关机
{
ucWorkMode=END_MODE;
}
break;
case DHG_MODE:
LEDG_ON;
if(ucCHGDHGState==CHG_STATE)//充电状态
{
if(ucBATState==BAT_FULL_STATE)
{
ucWorkMode=CHG_END_MODE;//充满
}
else if(ucBATState==BAT_DEAD_STATE)//电池不良电压小于1.5*n
{
ucWorkMode=ERROR_MODE;
ucBATStateFlag=BAT_DEAD_FLAG;
ucERRORFlag=CHG_ERROR_FLAG;
}
else
{
ucWorkMode=CHG_MODE;
ucPWMTime=0;
uiCHGTime=0;
}
}
if(ucDHGPWMTime>=TIME2MS5_20MS)//软启动
{
ucDHGPWMTime=0;
unsigned int uiPWMTemp;
uiPWMTemp=0;
if(uiPWMNow
{
uiPWMTemp=uiPWMGoal-uiPWMNow;
if(uiPWMTemp>=20)
{
uiPWMNow=uiPWMNow+20;
}
else
{
uiPWMNow=uiPWMNow+1;
}
}
_tm2al=(unsigned char)uiPWMNow;
_tm2ah=(unsigned char)(uiPWMNow>>8);
}
if(ucCURRState==CURRENT_OVER_STATE)//过流
{
LEDG_OFF;//关闭灯
LEDR_OFF;
PWM_OFF;//电机停止
PWM_PORT_HIGH;//拉高 电机停止
ucWorkMode=END_MODE;
}
if((ucCell1State==CELL_STATE_CUT)||(ucCell2State==CELL_STATE_CUT))//断线
{
LEDG_OFF;//关闭灯
LEDR_OFF;
PWM_OFF;//电机停止
PWM_PORT_HIGH;
ucWorkMode=ERROR_MODE;
ucBATStateFlag=CUT_WIRE_FLAG;
}
else if((ucCell1State==CELL_STATE_LOW)||(ucCell2State==CELL_STATE_LOW))//欠压
{
LEDG_OFF;//关闭灯
LEDR_OFF;
PWM_OFF;//电机停止
PWM_PORT_HIGH;//拉高 电机停止
ucWorkMode=END_MODE;
}
if(ucTempState==1)//过温
{
LEDG_OFF;//关闭灯
LEDR_OFF;
PWM_OFF;//电机停止
PWM_PORT_HIGH;//拉高 电机停止
ucWorkMode=END_MODE;
}
if(ucBATState==BAT_LOW_STATE)//小于3.0*2V
{
if(ucTime_1S>=TIME10MS_1S)//低压报警显示 绿灯常亮红灯闪
{
ucTime_1S=0;
LEDR_OFF;
}
else if(ucTime_1S>=TIME10MS_500MS)
{
LEDR_ON;
}
else
{
LEDR_OFF;
}
}
break;
case ERROR_MODE:
PWM_OFF;//电机停止
PWM_PORT_HIGH;//拉高 电机停止
CHG_OFF;
if(ucBATStateFlag==CHG_HIGH_FLAG)//充电器高压 红绿灯闪
{
if(ucTime_1S>=TIME10MS_1S)
{
ucTime_1S=0;
LEDG_OFF;
LEDR_ON;
}
else if(ucTime_1S>=TIME10MS_500MS)
{
LEDG_ON;
LEDR_OFF;
}
else
{
LEDG_OFF;
LEDR_ON;
}
if(ucCHGDHGState==DHG_STATE)//拔掉充电器关机
{
ucWorkMode=END_MODE;
}
}
else if((ucBATStateFlag==BAT_DEAD_FLAG)||(ucBATStateFlag==CUT_WIRE_FLAG))//电池超低压或者断线 红灯闪
{
if(ucTime_1S>=TIME10MS_1S)
{
ucTime_1S=0;
LEDR_OFF;
LEDG_OFF;
}
else if(ucTime_1S>=TIME10MS_500MS)
{
LEDR_ON;
LEDG_OFF;
}
else
{
LEDR_OFF;
LEDG_OFF;
}
if(ucERRORFlag==CHG_ERROR_FLAG)//充电断线超低压错误
{
if(ucCHGDHGState==DHG_STATE)//拔掉充电器关机
{
ucWorkMode=END_MODE;
ucERRORFlag=0;
}
}
}
break;
case CHG_OVTEMP_MODE:
LEDR_ON;
CHG_OFF;
if(ucTempState==0)
{
ucWorkMode=CHG_MODE;
}
if(ucCHGDHGState==DHG_STATE)//拔掉充电器关机
{
ucWorkMode=END_MODE;
}
break;
case END_MODE:
LEDG_OFF;//初始化关闭灯
LEDR_OFF;
PWM_OFF;//电机停止
PWM_PORT_HIGH;//拉高 电机停止
CHG_OFF;
ucCURRState=CURRENT_OK_STATE;
if(ucCHGDHGState==CHG_HIGH_STATE)//充电器高压
{
ucWorkMode=ERROR_MODE;
ucBATStateFlag=CHG_HIGH_FLAG;
}
else if(ucCHGDHGState==CHG_STATE)//充电状态
{
if(ucBATState==BAT_FULL_STATE)
{
ucWorkMode=CHG_END_MODE;//充满
}
else if(ucBATState==BAT_DEAD_STATE)//电池不良电压小于1.5*n
{
ucWorkMode=ERROR_MODE;
ucBATStateFlag=BAT_DEAD_FLAG;
ucERRORFlag=CHG_ERROR_FLAG;
}
else//充电
{
ucWorkMode=CHG_MODE;
ucPWMTime=0;
uiCHGTime=0;
}
}
break;
default:
ucWorkMode=POWER_ON_MODE;
}
}
/****************版本函数**************/
void Version_Fution()
{
unsigned char ucvertemplongth,ucvertemplow;
//总长度=版本高位+版本低位+2
ucvertemplongth=VERSION_HIGH+VERSION_LOW+2;
//低电平时间=版本低位+1
ucvertemplow=VERSION_LOW+1;
if(ucVersionTime>=ucvertemplongth)
{
ucVersionTime=0;
VER_LOW;
}
else if(ucVersionTime>=ucvertemplow)//高电平时间
{
VER_HIGH;
}
else //低电平时间
{
VER_LOW;
}
}
/*********AD返回值**********/
unsigned int Get_AD_Value(unsigned char ucADChannel)
{
unsigned int uiADValue;
unsigned int uiADTemp;
_adcr0=ucADChannel;
_start=0;//开启AD
_start=1;
_start=0;
while(_eocb==1);
uiADValue=_adrh;
uiADValue= uiADValue<<4;
uiADTemp=_adrl;
uiADTemp=uiADTemp>>4;
uiADValue=uiADValue+uiADTemp;
return(uiADValue);
}
/*********主函数**********/
void main()
{
_emi=0;
SystemInit();
Delay_MS(10);
SystemInit();
//if(uiReset!=0x2535)
//{
VariInit();
// uiReset=0x2535;
//}
_emi=1;
while(1)
{
FeedWDT();
Timer();
AD_Manage();
Control_Function();
Version_Fution();
while(_t0af==0);
_t0af=0;
}
}