基于飛思卡爾DZ60的AD 1302 KEY 485 CAN FLASH LCD程序
//*************前1次的放電記錄*********************************
p=(byte *)(0x1511); //
MonthF2= *p;
p=(byte *)(0x1512); //時間
DateF2= *p;
p=(byte *)(0x1513); //電量的最低位
MW33L= *p;
p=(byte *)(0x1514); //
MW33H= *p;
p=(byte *)(0x1515); //
MW34L= *p;
p=(byte *)(0x1516); //
MW34H= *p;
if(MonthF2>12) //
{
MonthF2 = 0;
DateF2= 0;
MW33L = 0;
MW33H = 0;
MW34L = 0;
MW34H = 0;
}
//***************緩存的放電記錄,用于比較計算******************
p=(byte *)(0x1521); //
MonthF3= *p;
p=(byte *)(0x1522); //時間
DateF3= *p;
p=(byte *)(0x1523); //電量的最低位
FBETW0L= *p;
p=(byte *)(0x1524); //
FBETW0H= *p;
p=(byte *)(0x1525); //
FBETW1L= *p;
p=(byte *)(0x1526); //
FBETW1H= *p;
if(MonthF3>12) //
{
MonthF3 = 0;
DateF3= 0;
FBETW0L = 0;
FBETW0H = 0;
FBETW1L = 0;
FBETW1H = 0;
}
p=(byte *)(0x1601); //
MW8L= *p;
p=(byte *)(0x1602); //時間
MW8H= *p;
p=(byte *)(0x1603); //電量的最低位
MW9L= *p;
p=(byte *)(0x1604); //
MW9H= *p;
if((MW8L==0xff)&&(MW8H==0xff)&&(MW9L==0xff)&&(MW9H==0xff)) //第一次上電
{
MW8L = 0;
MW8H = 0;
MW9L = 0;
MW9H = 0;
P2 = 0;
}
else{
Premain = MW9L+(MW9H<<8);
P2= (Premain<<16)+ (MW8L+(MW8H<<8));
P2 = P2*3600*1000;
}
if(Pchange==0x01) //充電時比較
{
if((time_buf1[2] == MonthC3)&& (time_buf1[3] == DateC3))
{
MW4L = CBETW0L;
MW4H = CBETW0H;
MW5L = CBETW1L;
MW5H = CBETW1H;
Premain = MW5L+(MW5H<<8);
P3= (Premain<<16)+ (MW4L+(MW4H<<8));
P3 = P3*3600;
}
else
{
MW4L = 0;
MW4H = 0;
MW5L = 0;
MW5H = 0;
P3 = 0;
}
}
if(Pchange==0x02) //放電時比較
{
if((time_buf1[2] == MonthF3)&& (time_buf1[3] == DateF3))
{
MW6L = FBETW0L;
MW6H = FBETW0H;
MW7L = FBETW1L;
MW7H = FBETW1H;
Premain = MW7L+(MW7H<<8);
P1= (Premain<<16)+ (MW6L+(MW6H<<8));
P1 = P1*3600;
}
else
{
MW6L = 0;
MW6H = 0;
MW7L = 0;
MW7H = 0;
P1 = 0;
}
}
EnableInterrupts; //開中斷
for(;;)
{
__RESET_WATCHDOG(); //喂內部看門狗
if(!DI1)
{ //充電
Pchange = 0x01;
} //放電
if(!DI2)
{
Pchange = 0x02;
}
if(DI1&&DI2)
{
Pchange = 0; //停機狀態(tài)
}
if(Pchange==0x02)
{
if((time_buf1[2] != MonthF3)|| (time_buf1[3] != DateF3)) //只要是 時間不等,就要把當前記錄寫入EEPROM,開始新的計算
{
p=( unsigned char *)(0x1500); //指定地址 緩存的時間寫入前1天,前一天的時間寫入前1天前1天
*p=0x01;
FCMD=0x40; //擦除命令
FSTAT_FCBEF=1; //啟動命令
Delay(1);
while(FSTAT_FCCF==0); //等待完成
if(FSTAT_FACCERR==1)
FSTAT_FACCERR=1;
p=(byte *)(0x1500); //指定地址
*p = 0x01;
WEEP();
p=(byte *)(0x1501);
*p = MonthF2;
WEEP();
p=(byte *)(0x1502);
*p = DateF2;
WEEP();
p=(byte *)(0x1503);
*p = MW33L;
WEEP();
p=(byte *)(0x1504);
*p = MW33H;
WEEP();
p=(byte *)(0x1505);
*p = MW34L;
WEEP();
p=(byte *)(0x1506);
*p = MW34H;
WEEP();
p=( unsigned char *)(0x1510); //指定地址 //緩存的時間寫入前1天,前一天的時間寫入前1天前1天
*p=0x01;
FCMD=0x40; //擦除命令
FSTAT_FCBEF=1; //啟動命令
Delay(1);
while(FSTAT_FCCF==0); //等待完成
if(FSTAT_FACCERR==1)
FSTAT_FACCERR=1;
p=(byte *)(0x1510); //指定地址
*p = 0x01;
WEEP();
p=(byte *)(0x1511); //如果時間變了,原緩存存入前一天的內存
*p = MonthF3;
WEEP();
p=(byte *)(0x1512);
*p = DateF3;
WEEP();
p=(byte *)(0x1513);
*p = FBETW0L;
WEEP();
p=(byte *)(0x1514);
*p = FBETW0H;
WEEP();
p=(byte *)(0x1515);
*p = FBETW1L;
WEEP();
p=(byte *)(0x1516);
*p = FBETW1H;
WEEP();
P1 = 0;
MW29L = MW33L;
MW29H = MW33H;
MW30L = MW34L;
MW30H = MW34H;
MonthF1 = MonthF2;
DateF1 = DateF2;
MW33L = FBETW0L;
MW33H = FBETW0H;
MW34L = FBETW1L;
MW34H = FBETW1H;
MonthF2 = MonthF3;
DateF2 = DateF3;
asm(nop);
MonthF3 =time_buf1[2]; //當前時間送入緩存
DateF3= time_buf1[3];
//MonthF3 = time_buf1[2];
//DateF3 = time_buf1[3];
FBETW0L = MW6L;
FBETW0H = MW6H;
FBETW1L = MW7L;
FBETW1H = MW7H;
p=( unsigned char *)(0x1520); //指定地址
*p=0x01;
FCMD=0x40; //擦除命令
FSTAT_FCBEF=1; //啟動命令
Delay(1);
while(FSTAT_FCCF==0); //等待完成
if(FSTAT_FACCERR==1)
FSTAT_FACCERR=1;
p=(byte *)(0x1520); //指定地址
*p = 0x01;
WEEP();
p=(byte *)(0x1521);
*p = time_buf1[2];
WEEP();
p=(byte *)(0x1522);
*p = time_buf1[3];
WEEP();
p=(byte *)(0x1523);
*p = MW6L;
WEEP();
p=(byte *)(0x1524);
*p = MW6H;
WEEP();
p=(byte *)(0x1525);
*p = MW7L;
WEEP();
p=(byte *)(0x1526);
*p = MW7H;
WEEP();
}
}
if(Pchange == 0x01) //充電的時候
{
if((time_buf1[2] != MonthC3)|| (time_buf1[3] != DateC3)) //只要時間不等,開始新的計算
{
p=( unsigned char *)(0x1400); //指定地址 緩存的時間寫入前1天,前一天的時間寫入前1天前1天
*p=0x01;
FCMD=0x40; //擦除命令
FSTAT_FCBEF=1; //啟動命令
Delay(1);
while(FSTAT_FCCF==0); //等待完成
if(FSTAT_FACCERR==1)
FSTAT_FACCERR=1;
p=(byte *)(0x1400); //指定地址
*p = 0x01;
WEEP();
p=(byte *)(0x1401);
*p = MonthC2;
WEEP();
p=(byte *)(0x1402);
*p = DateC2;
WEEP();
p=(byte *)(0x1403);
*p = MW25L;
WEEP();
p=(byte *)(0x1404);
*p = MW25H;
WEEP();
p=(byte *)(0x1405);
*p = MW26L;
WEEP();
p=(byte *)(0x1406);
*p = MW26H;
WEEP();
p=( unsigned char *)(0x1410); //指定地址 緩存的時間寫入前1天,前一天的時間寫入前1天前1天
*p=0x01;
FCMD=0x40; //擦除命令
FSTAT_FCBEF=1; //啟動命令
Delay(1);
while(FSTAT_FCCF==0); //等待完成
if(FSTAT_FACCERR==1)
FSTAT_FACCERR=1;
p=(byte *)(0x1410); //指定地址
*p = 0x01;
WEEP();
p=(byte *)(0x1411);
*p = MonthC3;
WEEP();
p=(byte *)(0x1412);
*p = DateC3;
WEEP();
p=(byte *)(0x1413);
*p = CBETW0L;
WEEP();
p=(byte *)(0x1414);
*p = CBETW0H;
WEEP();
p=(byte *)(0x1415);
*p = CBETW1L;
WEEP();
p=(byte *)(0x1416);
*p = CBETW1H;
WEEP();
MW21L = MW25L;
MW21H = MW25H;
MW22L = MW26L;
MW22H = MW26H;
MonthC1 = MonthC2;
DateC1 = DateC2;
MW25L = CBETW0L;
MW25H = CBETW0H;
MW26L = CBETW1L;
MW26H = CBETW1H;
MonthC2 = MonthC3;
DateC2 = DateC3;
P3 = 0;
MonthC3 =time_buf1[2]; //當前時間送入緩存
DateC3= time_buf1[3];
//MonthC3 = time_buf1[2];
//DateC3 = time_buf1[3];
CBETW0L = MW4L;
CBETW0H = MW4H;
CBETW1L = MW5L;
CBETW1H = MW5H;
p=( unsigned char *)(0x1420); //指定地址
*p=0x01;
FCMD=0x40; //擦除命令
FSTAT_FCBEF=1; //啟動命令
Delay(1);
while(FSTAT_FCCF==0); //等待完成
if(FSTAT_FACCERR==1)
FSTAT_FACCERR=1;
p=(byte *)(0x1420); //指定地址
*p = 0x01;
WEEP();
p=(byte *)(0x1421);
*p = time_buf1[2];
WEEP();
p=(byte *)(0x1422);
*p = time_buf1[3];
WEEP();
p=(byte *)(0x1423);
*p = MW4L;
WEEP();
p=(byte *)(0x1424);
*p = MW4H;
WEEP();
p=(byte *)(0x1425);
*p = MW5L;
WEEP();
p=(byte *)(0x1426);
*p = MW5H;
WEEP();
}
}
if(!RR)
{
Ds1302_Write_Time();
Delay(10);
T20 = 1;
}
if(T20) //切換到2界面
{
T20 = 0;
TENT = 0;
DE485 = 1; //保證切換到發(fā)射
Delay(200);
SCI1send (0x01); //站號
SCI1send (0x57); //寫命令
SCI1send (0x12); //18號地址跳轉頁面指令,跳轉到1界面
SCI1send (0x01); //1個長度
SCI1send (0x00); //18號地址
SCI1send (0x01);
SCI1send (0x6c);
Delay(100); //需加一段長延時
SCI1send (0x01); //站號
SCI1send (0x57); //寫命令
SCI1send (0x12); //18號地址跳轉頁面指令,跳轉到2界面
SCI1send (0x01); //1個長度
SCI1send (0x00); //18號地址
SCI1send (0x02);
SCI1send (0x6d);
Delay(200); //必須加段延時確保數(shù)據(jù)發(fā)送出去
RR = 1; //1分鐘內ENT沒有按下,轉回正常模式
asm(nop);
}
if(ADSET) //處理AD;
{
ADSET = 0;
if(ADcount == 1)
{
filter();
ADC1= (sum/(N-2));
if(ADC1<7){ ADC1 = 0; //屏蔽掉待機狀態(tài)時的不干凈的初值
}
if (ADC1>163)
{
Failure = Failure|0x01; //置過壓標志
}
else Failure = Failure&0xfe; //清過壓標志
//b = 750*50;
//b = b/255;
b = (long)7500*ADC1; //放大10倍,為了送顯示
//ADC1 = b*ADC1;
//ADC1 = ADC1/5;
ADC1 = b>>10; //除以1024
ADC1H = ADC1>>8;
ADC1L = ADC1&255;
ADch_s = 11;
ADCSC1=0x6b;
}
if(ADcount == 2)
{
filter();
ADC2= (sum/(N-2));
if(ADC2<7) { ADC2 = 0;
}
if (ADC2>191) //過流
{
Failure = Failure|0x02; //置過流標志
}
else Failure = Failure&0xfd; //清過流標志
//a= 400*50;
//a= a/255;
a = (long)4000*ADC2;
//ADC2 = a*ADC2; //轉換成電壓值 ,取小數(shù)點后一位,所以先擴大50倍
//ADC2 = ADC2/5; //10倍電流送顯示,用于顯示小數(shù)點
ADC2 = a>>10;
ADC2H = ADC2>>8;
ADC2L = ADC2&255;
ADC1 = ADC1/10;
ADC2 = ADC2/10;
P =(long)ADC1*ADC2;
PH = P>>16;
PL = P&0xffff;
P1H = PH>>8;
P1L = PH&255;
P0H = PL>>8;
P0L = PL&255;
if(Pchange==0x02) //如果放電
{
PdownCount++; //秒累加
P2 = P2+P; //一直累積的放電電量
Psum=(long)(P2/3600/1000);
BetweenH = Psum>>16;
BetweenL = Psum&0xffff;
MW9H = BetweenH>>8;
MW9L = BetweenH&255;
MW8H = BetweenL>>8;
MW8L = BetweenL&255;
P1 = P1+P; //秒功率累加
Pdown = (long)P1/3600; //實際放電功率,按小時計算
BetweenH = Pdown>>16;
BetweenL = Pdown&0xffff;
MW7H = BetweenH>>8;
MW7L = BetweenH&255;
MW6H = BetweenL>>8;
MW6L = BetweenL&255;
if(PdownCount > T) //超過半個小時開始寫入EEPROM
{
PdownCount = 0;
p=( unsigned char *)(0x1600); //指定地址
*p=0x01;
FCMD=0x40; //擦除命令
FSTAT_FCBEF=1; //啟動命令
Delay(1);
while(FSTAT_FCCF==0); //等待完成
if(FSTAT_FACCERR==1)
FSTAT_FACCERR=1;
p=(byte *)(0x1600); //指定地址
*p = 0x01;
WEEP();
p=(byte *)(0x1601); //總放電量寫入EEPROM
*p = MW8L;
WEEP();
p=(byte *)(0x1602);
*p = MW8H;
WEEP();
p=(byte *)(0x1603);
*p = MW9L;
WEEP();
p=(byte *)(0x1604);
*p = MW9H;
WEEP();
asm(nop);
if((time_buf1[2] == MonthF3)&& (time_buf1[3] == DateF3)) //當前時間等于緩存時間,繼續(xù)寫入緩存
{
MonthF3 = time_buf1[2];
DateF3 = time_buf1[3];
FBETW0L = MW6L;
FBETW0H = MW6H;
FBETW1L = MW7L;
FBETW1H = MW7H;
p=( unsigned char *)(0x1520); //指定地址
*p=0x01;
FCMD=0x40; //擦除命令
FSTAT_FCBEF=1; //啟動命令
Delay(1);
while(FSTAT_FCCF==0); //等待完成
if(FSTAT_FACCERR==1)
FSTAT_FACCERR=1;
p=(byte *)(0x1520); //指定地址
*p = 0x01;
WEEP();
p=(byte *)(0x1521);
*p = time_buf1[2];
WEEP();
p=(byte *)(0x1522);
*p = time_buf1[3];
WEEP();
p=(byte *)(0x1523);
*p = MW6L;
WEEP();
p=(byte *)(0x1524);
*p = MW6H;
WEEP();
p=(byte *)(0x1525);
*p = MW7L;
WEEP();
p=(byte *)(0x1526);
*p = MW7H;
WEEP();
}
}
}
if(Pchange == 0x01) //如果充電
{
PupCount++; //秒累加
P3 =P3+P; //秒功率累加
Pup = (long)P3/3600; //實際放電功率,按小時計算
BetweenH = Pup>>16;
BetweenL = Pup&0xffff;
MW5H = BetweenH>>8;
MW5L = BetweenH&255;
MW4H = BetweenL>>8;
MW4L = BetweenL&255;
if(PupCount >T)
{
PupCount = 0; //超過半個小時開始寫入EEPROM
if((time_buf1[2] == MonthC3)&& (time_buf1[3] == DateC3)) //當前時間等于緩存時間,繼續(xù)寫入緩存
{
MonthC3 = time_buf1[2];
DateC3 = time_buf1[3];
CBETW0L = MW4L;
CBETW0H = MW4H;
CBETW1L = MW5L;
CBETW1H = MW5H;
p=( unsigned char *)(0x1420); //指定地址
*p=0x01;
FCMD=0x40; //擦除命令
FSTAT_FCBEF=1; //啟動命令
Delay(1);
while(FSTAT_FCCF==0); //等待完成
if(FSTAT_FACCERR==1)
FSTAT_FACCERR=1;
p=(byte *)(0x1420); //指定地址
*p = 0x01;
WEEP();
p=(byte *)(0x1421);
*p = time_buf1[2];
WEEP();
p=(byte *)(0x1422);
*p = time_buf1[3];
WEEP();
p=(byte *)(0x1423);
*p = MW4L;
WEEP();
p=(byte *)(0x1424);
*p = MW4H;
WEEP();
p=(byte *)(0x1425);
*p = MW5L;
WEEP();
p=(byte *)(0x1426);
*p = MW5H;
WEEP(); //如果相等則認為是當前時間,繼續(xù)
}
}
}
asm(nop);
ADch_s = 3;
ADCSC1=0x63;
}
if(ADcount == 3)
{
filter();
ADC3= (sum/(N-2));
ADC3H = (sum/(N-2))>>8;
ADC3L = (sum/(N-2))&255;
ADch_s = 4;
ADCSC1=0x64;
}
if(ADcount == 4)
{
filter();
ADC4= (sum/(N-2));
ADC4H = (sum/(N-2))>>8;
ADC4L = (sum/(N-2))&255;
ADch_s = 19;
ADCSC1=0x73;
}
if(RR ==2)
{
DE485 = 1;
Delay(2);
SCI1send (0x01); //站號
SCI1send (0x52); //讀命令
SCI1send (0x14); //20號地址判斷ENT鍵狀態(tài)
SCI1send (0x01); //1個地址
SCI1send (0x68);
Delay(1); //必須加段延時確保數(shù)據(jù)發(fā)送出去
DE485 = 0; //485切換到接收狀態(tài)
asm(nop);
}
if(RR ==3)
{
DE485 = 1;
Delay(2);
SCI1send (0x01); //站號
SCI1send (0x57); //寫命令
SCI1send (0x14); //20號地址清零
SCI1send (0x01); //1個長度
SCI1send (0x00); //18號地址
SCI1send (0x00);
SCI1send (0x6D);
Delay(50);
RR =4;
}
if(RR ==4) //讀時間
{
DE485 = 1;
Delay(5);
SCI1send (0x01); //站號
SCI1send (0x52); //讀命令
SCI1send (0x0B); //11號地址
SCI1send (0x06); //6個地址
SCI1send (0x64);
Delay(1); //必須加段延時確保數(shù)據(jù)發(fā)送出去
DE485 = 0; //485切換到接收狀態(tài)
//asm(nop);
}
評論