/*This guy very faineant*/
/*===========================================================*/
#pragma sfr
#pragma di
#pragma ei
#pragma interrupt INTTM000 MD_INTTM000
/*===========================================================*/
unsigned int batt_voltage=0;
unsigned int max_batt_voltage=0;
unsigned char time_100ms=0;
unsigned char time_s=0;
unsigned char time_m=0;
unsigned char time_h=0;
unsigned int minus_vol=0;
/*===========================================================*/
static bit batt_full_f;
static bit time_s_f;
static bit time_m_f;
static bit time_h_f;
static bit max_charge_time_f;
/*===========================================================*/
#define TM_TM00_CLOCK 0x2
#define TM_TM00_INTERVALVALUE 0xc34
#define ClrIORBit(Reg, ClrBitMap) Reg &= ~ClrBitMap
#define SetIORBit(Reg, SetBitMap) Reg |= SetBitMap
/*===========================================================*/
#define BATT_FULL_VOLTAGE 585
#define START_CHARGE_VOLTAGE 410
#define START_MINUS_VOLTAGE 495
#define MINUS_VOLTAGE 10
#define MAX_CHARGE_TIME_H 1
#define MAX_CHARGE_TIME_M 15
/*===========================================================*/
__interrupt void MD_INTTM000( )
{
if(++time_100ms >= 10)
{
time_100ms = 0;
time_s_f = 1;
if(++time_s >= 60)
{
time_s = 0;
time_m_f = 1;
if(++time_m >= 60)
{
time_m = 0;
time_h_f = 1;
++time_h;
}
}
}
}
/*===========================================================*/
void Clock_Init( void )
{
SetIORBit(LSRCM, 0x01);
ClrIORBit(PPCC, 0x03);
ClrIORBit(PCC, 0x02);
}
/*===========================================================*/
void TM00_Init( void )
{
TMC00 = 0x00;
SetIORBit(MK0, 0x20);
SetIORBit(MK0, 0x40);
ClrIORBit(IF0, 0x20);
ClrIORBit(IF0, 0x40);
PRM00 |= TM_TM00_CLOCK;
ClrIORBit(CRC00, 0x01);
CR000 = TM_TM00_INTERVALVALUE;
CR010 = 0xffff;
}
/*===========================================================*/
void work_Init( void )
{
batt_full_f=0;
time_s_f=0;
time_m_f=0;
time_h_f=0;
P4.0=1;
P4.3=1;
}
/*===========================================================*/
void delay()
{
int i;
for(i=0;i<10000;i++);
}
/*===========================================================*/
void TM00_Start( void )
{
ClrIORBit(MK0, 0x20);
TMC00 = 0x0c;
}
/*===========================================================*/
unsigned int ADC_conversion( unsigned char channel)
{
unsigned int batt_vol=0;
unsigned int batt_vol_or=0;
unsigned char sample_counter=0;
do
{
ADS=channel;
ADCE=1;
ADCS=1;
while(!ADIF)
batt_vol=ADCR;
batt_vol_or += batt_vol;
ADCS=0;
ADIF=0;
++sample_counter;
}
while(sample_counter < 50);
batt_vol = batt_vol_or/50;
return batt_vol;
}
/*===========================================================*/
void main()
{
LSRSTOP=1;
PM4.3=0;
PM4.0=0;
Clock_Init();
TM00_Init();
TM00_Start();
work_Init();
EI();
delay();
delay();
delay();
delay();
delay();
delay();
delay();
batt_voltage = ADC_conversion(0);
/*===========================================================*/
while(1)
{
if(time_m_f ==1)
{
time_m_f =0;
P4.0=1;
delay();
delay();
batt_voltage = ADC_conversion(0);
}
if(batt_voltage >= START_MINUS_VOLTAGE)
{
if(batt_voltage >= max_batt_voltage)
{
max_batt_voltage = batt_voltage;
}
else
{
minus_vol = max_batt_voltage - batt_voltage;
if(minus_vol > MINUS_VOLTAGE)
{
batt_full_f = 1;
}
}
}
else
{
max_batt_voltage = batt_voltage;
}
/*===========================================================*/
if((time_h == MAX_CHARGE_TIME_H)&(time_m >= MAX_CHARGE_TIME_M))
{
batt_full_f = 1;
}
/*===========================================================*/
if(batt_full_f == 1)
{
P4.0=1;
P4.3=1;
if(batt_voltage <= START_CHARGE_VOLTAGE)
{
batt_full_f = 0;
}
}
else
{
P4.0=0;
switch(time_100ms)
{
case 5: P4.3=1;break;
case 0: P4.3=0;break;
default:;
}
if(batt_voltage >= BATT_FULL_VOLTAGE)
{
batt_full_f = 1;
}
}
}
}
/*===========================================================*/