#include "PubDef.h"
#define LED1_CS 2
#define LED2_CS 4
#define LED3_CS 8
uchr Buf595[2]={0xFF,0xFF};
//CommentDsp[0] 段码
//CommentDsp[1] 1 2 3 片选。4 5 6 7指示灯
const uchr Dcode[11]={0X14,0XD7,0X4C,0X45,0X87,0X25,0X24,0X57,0X04,0X05,0XFB};
uchr LEDDsp[3]={0,0,0};
const uchr LED_CS[3]={0x0C,0x0A,0x06};
uchr CurrentPos=0;
uchr ABC=0Xfe;
//0..9 DP
void SpiWriteData(void);
uchr TestCode=0;
uint TimeBaseCount_10ms,TimeBaseCount_500ms,TimeBaseCount_1000ms,TimeBaseCount_100ms,TimeBaseCount_1Min;
uchr PowerOFF=0,NoPowerCount=0;
uchr PowerOnRelease;
uint ADC_ValueBuf[4];//801 424 641
//CurrentDSPID 0 电流,1 输出电压,2 温度,3 电池电压,4 电量,5 定时关机
uint FinalValue[6];
uchr DL;
uchr Tchannel;
//-LED_Display
//CurrentDSPID 0 温度,1 电量,2 输出电压,3 输出电流,4 电池电压,5 定时关机
uchr DotPostion;//0 1.11,1 11.1 2 111
uchr const LEDTable[6]={0x60,0xa0,0xc0,0x20,0x80,0x00};//4 3 2
uint* DTR=FinalValue;
uchr CurrentDSPID,AutoPowerOFFDelayTime,AutoPowerFlag;
void ChangeDisplay(void);
void GetBCD(uint temp16bit,uchr *buf);
void CountDisplay(void);
uchr BCD[5];
uchr LedDl;
//End of LED Display
//KeyProcess
uchr HasKeyFlag[2];
uchr KeyCount[2];
uchr KeyNoRealse[2];
uchr KeyValue;
void ProcessKey(void);
void ReadKey(uchr i);
//End of KEY Process
//过几秒不显示
uchr DLCount;
void DL_Indicate(void);
uchr MaskClear,MaskClearDelay;
void NoDisplay(void);
uchr GetDL(uint abc);
void Delay_us(void)
{
//uchr i=2;
//while(i--);
return;
}
void RefreshLED(void)
{
Buf595[0]=LED_CS[CurrentPos]|LEDTable[CurrentDSPID];
Buf595[1]=Dcode[LEDDsp[CurrentPos]];
if(CurrentPos==DotPostion)
Buf595[1]&=Dcode[10];
if(CurrentPos>1)
CurrentPos=0;
else
CurrentPos++;
Buf595[0]|=MaskClear;
Buf595[1]|=MaskClear;
SpiWriteData();
}
void SpiWriteData(void)
{
uchr tempvalue;
tempvalue=Buf595[0];
SCK_0;
for (uchr i=0;i<8;i++)
{
if(tempvalue&0x80)
{ SDA_1;}
else
{ SDA_0; }
Delay_us();
SCK_1;
tempvalue<<=1;
Delay_us();
SCK_0;
}
tempvalue=Buf595[1];
for (uchr i=0;i<8;i++)
{
if(tempvalue&0x80)
{SDA_1;}
else
{SDA_0;}
Delay_us();
SCK_1;
tempvalue<<=1;
Delay_us();
SCK_0;
}
CS_1;
Delay_us();
CS_0;
}
void ReadAdc()
{
uint AD_Value;
AD_Value=ADC_DRH;
AD_Value<<=2;
AD_Value|=ADC_DRL;
ADC_CSR &= 0x7F; //清除标志位
ADC_ValueBuf[Tchannel]=AD_Value;
if(Tchannel>2)
Tchannel=0;
else
Tchannel++;
ADC_CSR &= 0xF0;
ADC_CSR |= Tchannel+3;
ADC_CR1 |= 0x01; //开始转换
// while(!(ADC_CSR & 0x80)); //等待转换结束
}
uint ProcessVoltage(uint tmpin)
{
unsigned long int Temp;
Temp=tmpin;
Temp*=330;
Temp/=1024;
return ((uint)Temp)*2;
}
uint ProcessCurrent(uint tmpin)
{
if(tmpin>4)
{
tmpin-=4;
}
else
{
tmpin=0;
}
unsigned long int Temp;
Temp=tmpin;
Temp*=33000;
Temp/=(1024);
Temp/=100;
return (uint)Temp;
}
uint GetTemp(uint tmpin)
{
uint TempValue;
float rvalue,tempbuf;
tempbuf=(float)tmpin;
rvalue=tempbuf*100.00/(1024.00-tempbuf);
rvalue=rvalue/Rvalue;
tempbuf=log(rvalue);
tempbuf=1/((tempbuf/Temp_B)+1/298.15);
TempValue=(uint)(tempbuf*10);
TempValue-=2731;
return TempValue;
}
uint Tmp1,teda;
void TimeBase(void)
{
if(TimeBaseCount_10ms>4)//10ms
{
TimeBaseCount_10ms-=5;
ReadAdc();
KeyValue=PC_IDR_IDR3;
KeyValue<<=1;
KeyValue|=PB_IDR_IDR4;
ReadKey(0);
ReadKey(1);
}
if(TimeBaseCount_100ms>49)
{
TimeBaseCount_100ms-=50;
CountDisplay();
DL_Indicate();
}
if(TimeBaseCount_500ms>249)//500ms
{
TimeBaseCount_500ms-=250;
if(!PSW)
{
PowerOFF++;
if(PowerOFF>4)
{
POFF;
}
}
else
{
PowerOFF=0;
}
TOG(Buf595[0],4);
FinalValue[0]=ProcessCurrent(ADC_ValueBuf[0]);
FinalValue[1]=ProcessVoltage(ADC_ValueBuf[1]);
FinalValue[3]=ProcessVoltage(ADC_ValueBuf[3]);
FinalValue[4]=GetDL(FinalValue[3]);
LedDl=FinalValue[4]/20+1;
}
if(TimeBaseCount_1000ms>499)//1000ms
{
TimeBaseCount_1000ms-=500;
FinalValue[2]=GetTemp(ADC_ValueBuf[2]);
NoDisplay();
if((FinalValue[4]==0)||(FinalValue[0]==0))
{
NoPowerCount++;
if(NoPowerCount>20)
{
POFF;
}
}
else
{
NoPowerCount=0;
}
TimeBaseCount_1Min++;
if(TimeBaseCount_1Min>59)
{
TimeBaseCount_1Min-=60;
if (AutoPowerFlag)
{
if(FinalValue[5])
FinalValue[5]--;
else
{
POFF;
AutoPowerFlag=NO;
}
}
}
}
}
void IdleProcess(void)
{
TimeBase();
ProcessKey();
}
//这个表起始于3.58V,4.18V 共计60个数据
const uchr PowerTable[60]=
{
0,1,2,3,4,5,8,10,12,13,
14,15,17,20,25,30,35,38,
40,45,48,50,53,55,57,59,
60,62,64,65,67,69,70,72,
74,75,76,78,79,80,81,82,
83,84,84,85,86,87,88,89,
90,91,92,93,95,96,97,98,
99,100
};
//PC3 Key1 ; PB4 key2 ; PB5 Indicate
uchr GetDL(uint abc)
{
if(abc<358)
return 0;
if(abc>417)
return 100;
abc-=358;
return PowerTable[(uchr)abc];
}
void ReadKey(uchr i)
{
if(!(KeyValue&SB(i)))//按键1 按下了
{
if(!KeyNoRealse[i])
{
if(KeyCount[i]>DEBOUNCE_TIME)
{
HasKeyFlag[i]=YES;
KeyNoRealse[i]=YES;
}
else
KeyCount[i]++;
}
}
else
{
KeyCount[i]=0;
KeyNoRealse[i]=NO;
}
}
void ProcessKey(void)
{
if(HasKeyFlag[0]||HasKeyFlag[1])
{
MaskClearDelay=MSK_DELAY;
if(MaskClear==0xFF)
{
HasKeyFlag[0]=0;
HasKeyFlag[1]=0;
MaskClear=0x00;
return;
}
}
if(HasKeyFlag[0])//功能选择按钮
{
HasKeyFlag[0]=NO;
//CurrentDSPID 0 温度,1 电量,2 输出电压,3 输出电流,4 电池电压,5 定时关机
if(CurrentDSPID>4)
CurrentDSPID=0;
else
CurrentDSPID++;
ChangeDisplay();
}
if(HasKeyFlag[1])//功能调节按钮
{
HasKeyFlag[1]=NO;
if(CurrentDSPID!=5) return;//设置开关机如果为0,则不定时开关机
if(FinalValue[5]>=180)
{
FinalValue[5]=0;
AutoPowerFlag=NO;
}
else
{
FinalValue[5]+=10;
AutoPowerFlag=YES;
}
ChangeDisplay();
}
}
//0 0.00 1 10.0 2 000
//CurrentDSPID 0 电流,1 输出电压,2 温度,3 电池电压,4 电量,5 定时关机
void ChangeDisplay(void)
{
DTR=&FinalValue[CurrentDSPID];
switch (CurrentDSPID)
{
case 0:
DotPostion=0;
break;
case 1:
DotPostion=0;
break;
case 2:
DotPostion=1;
break;
case 3:
DotPostion=0;
break;
case 4:
DotPostion=2;
break;
case 5:
DotPostion=2;
break;
default:
DotPostion=0;
break;
}
CountDisplay();
}
void CountDisplay(void)
{
if(*DTR>999) {return;}
GetBCD(*DTR,BCD);
LEDDsp[0]=BCD[2];
LEDDsp[1]=BCD[1];
LEDDsp[2]=BCD[0];
}
void GetBCD(uint temp16bit,uchr *buf)
{
uchr temp8bit;
buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;buf[4]=0;
if(temp16bit>=10000)
{
if (temp16bit>=40000){buf[4]+=4;temp16bit-=40000;}
if (temp16bit>=20000){buf[4]+=2;temp16bit-=20000;}
if (temp16bit>=100