基于C8051F的AD UART0 KEY FLASH TIME3程序
void Pchange(uint PW)
{
TH = PW/10000; //放電萬位
TL = (PW-TH*10000)/1000; //千位
TT1 = (PW-TH*10000-TL*1000)/100;
TT2 = (PW-TH*10000-TL*1000-TT1*100)/10;
TT3 = (PW-TH*10000-TL*1000-TT1*100)%10;
if(!TH)
{
TH = 10;
if(!TL)
{
TL = 10;
if(!TT1)
{
TT1 = 10;
}
}
}
}
//-----------------------------------------------------------------------------
// Main 年:0xC4EA 月:0xD4C2 日:0xC8D5 時:0xCAB1: 分0xB7D6: 秒:0xC3EB 的漢字內(nèi)碼
//-----------------------------------------------------------------------------
void main(void)
{
uchar Page1,j;
uchar Runstate ; //Runstate為運(yùn)行狀態(tài)指示:DI1=1;DI2=1停機(jī);DI1=0;DI2=1充電;DI1=1;DI2=0放電;DI1=1;DI2=1故障
uint VT,AT,PT,PC1,PF1,PDIS,PdownCount; //電壓電流功率功率計數(shù)變量
uint ADC1,ADC2,ADC3,ADC4; //四路AD通道變量
long a,PT1,PT2; //P1,P2充放電累計功率
//P0 = 0;
//P1 = 0;
P4 =0xfe;
P2 = 0;
P3 = 0;
Init_Device();
PCA0MD &= ~0x40;
EA = 0;
//BLK = 1;
delayms(1000);
Ds1302_Init();
init_lcd ();
clrram ();
ADC0CF = ((SYSCLK/3000000)-1)<<3; // set SAR clock to 3MHz
ADC0CN |=0x10; //寫ADC0BUSY位啟動轉(zhuǎn)換
TMR3CN = 0x04; //啟動TM3
//Timer2_Init(SYSCLK/12/50); //20ms
EA = 1;
DE = 1;
TR1 = 1;
Ds1302_Read_Time();
while(1)
{
//PCA0CPH2 = 1; //喂狗
key_scan();
if(BGset)
{
BLK = 1;
}
if(!BGset)
{
BLK = 0;
} //按鍵掃描
if((DI1==1)&&(DI2==1)) Runstate = 0xa6; //停機(jī)
if((DI1==0)&&(DI2==1)) Runstate = 0xb7; //充電
if((DI1==1)&&(DI2==0)) Runstate = 0xc8; //放電
if((DI1==0)&&(DI2==0)) Runstate = 0xd9; //故障
//if(Runstate == 0xb7) //充電的FLASH存儲處理
if(!set)
{
if((time_buf1[2] != CPT_c[10])|| (time_buf1[3] != CPT_c[11])) //讀取的當(dāng)前時間與緩存不等的時候,所有內(nèi)容依次前移
{
Between[0]=CPT_c[3]; //依次前移
Between[1]=CPT_c[4];
Between[2]=CPT_c[5];
Between[3]=CPT_c[6];//年
Between[4]=CPT_c[7];//月
Between[5]=CPT_c[8];//日
Between[6]=CPT_c[9];
Between[7]=CPT_c[10];
Between[8]=CPT_c[11];
Between[9]=time_buf1[1];
Between[10]=time_buf1[2];
Between[11]=time_buf1[3];
Between[12]=CPT_c[14];
Between[13]=CPT_c[15];
Between[14]=CPT_c[16];
Between[15]=CPT_c[17];
Between[16]=CPT_c[18];
Between[17]=CPT_c[19];
Between[18] = PC1>>8;
Between[19] = PC1&255;
Flash_Erase_CTP();
Flash_Program_CTP(); //保存至FLASH
}
}
//if(Runstate == 0xc8) //放電時FLASH存儲處理
if(!set)
{
if((time_buf1[2] != FPT_c[10])|| (time_buf1[3] != FPT_c[11]))
{
Between[0]=FPT_c[3]; //依次前移
Between[1]=FPT_c[4];
Between[2]=FPT_c[5];
Between[3]=FPT_c[6];//年
Between[4]=FPT_c[7];//月
Between[5]=FPT_c[8];//日
Between[6]=FPT_c[9];
Between[7]=FPT_c[10];
Between[8]=FPT_c[11];
Between[9]=time_buf1[1];
Between[10]=time_buf1[2];
Between[11]=time_buf1[3];
Between[12]=FPT_c[14];
Between[13]=FPT_c[15];
Between[14]=FPT_c[16];
Between[15]=FPT_c[17];
Between[16]=FPT_c[18];
Between[17]=FPT_c[19];
Between[18] = PF1>>8;
Between[19] = PF1&255;
Flash_Erase_FTP();
Flash_Program_FTP(); //保存至FLASH
}
}
if(PDD)
{
PDD = 0;
if(Runstate == 0xb7) //如果充電
{
PdownCount++; //秒累加
PT1 = PT1+PT; //秒累計的功率,
PC1 = PT1/3600; //實際充電功率,按小時計算,注意此時累計電量為實際的10倍,為了送小數(shù)顯示
if(PdownCount > T) //超過半個小時開始寫入EEPROM
{
PdownCount = 0;
for(j=0;j<20;j++) //先讀出FLASH里的時間
{
Between[j]=CPT_c[j];
}
if((time_buf1[2] == Between[10])&& (time_buf1[3] == Between[11])) //當(dāng)前時間等于緩存時間,繼續(xù)寫入緩存
{
Between[18] = PC1>>8;
Between[19] = PC1&255;
Flash_Program_CTP(); //寫功率和時間到FLASH
}
}
}
if(Runstate == 0xc8) //如果放電
{
PdownCount++; //秒累加
PT2 = PT2+PT; //秒累計的功率
PF1 = PT2/3600; //實際放電功率,按小時計算,,注意此時累計電量為實際的10倍,為了送小數(shù)顯示
if(PdownCount > T) //超過半個小時開始寫入EEPROM
{
PdownCount = 0;
for(j=0;j<20;j++) //先讀出FLASH里的時間
{
Between[j]=FPT_c[j];
}
if((time_buf1[2] == Between[10])&& (time_buf1[3] == Between[11])) //當(dāng)前時間等于緩存時間,繼續(xù)寫入緩存
{
Between[18] = PF1>>8;
Between[19] = PF1&255;
Flash_Program_FTP(); //寫功率和時間到FLASH
}
}
}
}
if(Disptime)
{
Disptime = 0;
Ds1302_Read_Time();
if(Page!=Page1)
{
clrram (); //如果頁面變化,需要清屏;
}
switch(Page)
{
case 1: //畫面1:顯示當(dāng)前電壓電流
{
string_disp (1,0,0x80,10,tab1); //顯示電壓
string_disp (1,0,0x90,10,tab2); //顯示電流
string_disp (0,1,0x80,10,tab3); //顯示功率
string_disp (0,1,0x90,10,tab4);
if(Runstate == 0xa6)string_disp (0,1,0x96,4,run1); //顯示停機(jī)
if(Runstate == 0xb7)string_disp (0,1,0x96,4,run2); //顯示充電
if(Runstate == 0xc8)string_disp (0,1,0x96,4,run3); //顯示放電
if(Runstate == 0xd9)string_disp (0,1,0x96,4,run4); //顯示故障
TH= VT/1000; //拆分送顯示千位
TL = (VT%1000)/100; //拆分送顯示百位
TT1 = ((VT%1000)%100)/10; //拆分送顯示十位
TT2 = ((VT%1000)%100)%10; //拆分送顯示個位
if(!TH)
{
TH = 10;
if(!TL)
{
TL = 10;
}
}
number_disp (1,0,0x85,1,TAB[TH],TAB[TL]); //電壓送顯示
number_disp (1,0,0x86,1,TAB[TT1],0x2E);
number_disp (1,0,0x87,1,TAB[TT2],0x20);
number_disp (1,0,0x88,1,0x56,0x20);
TH= AT/1000; //拆分送顯示千位
TL = (AT%1000)/100; //拆分送顯示百位
TT1 = ((AT%1000)%100)/10; //拆分送顯示十位
TT2 = ((AT%1000)%100)%10; //拆分送顯示個位
if(!TH) //高位為零,滅零處理
{
TH = 10;
if(!TL)
{
TL = 10;
}
}
number_disp (1,0,0x95,1,TAB[TH],TAB[TL]); //電流送顯示
number_disp (1,0,0x96,1,TAB[TT1],0x2E);
number_disp (1,0,0x97,1,TAB[TT2],0x20);
number_disp (1,0,0x98,1,0x41,0x20);
TH= PT/1000; //拆分送顯示千位
TL = (PT%1000)/100; //拆分送顯示百位
TT1 = ((PT%1000)%100)/10; //拆分送顯示十位
TT2 = ((PT%1000)%100)%10; //拆分送顯示個位
if(!TH) //高位為零,滅零處理
{
TH = 10;
if(!TL)
{
TL = 10;
}
}
number_disp (0,1,0x85,1,TAB[TH],TAB[TL]); //功率送顯示