#include <GLOBAL.h> #include <stdlib.h> #include <stdbool.h> #include <msp430x41x.h> //--------------------------------------------------------------------------------------------------------------------------数据转换区 //-----***-unsigned char HexToBcd(unsigned char InByte)-***-----// //---功能描述:单字HEXTOBCD。 //---输入条件:InByte //---输出项目:RetByte unsigned char HexToBcd(unsigned char InByte) { unsigned char i,RetByte; i=0; while(InByte>=0x0a) { InByte=InByte-0x0a; i++; } RetByte=16*i+InByte; return RetByte; } //-----***-unsigned char BcdToHex(unsigned char InByte)-***-----// //---功能描述:单字BcdToHex。 //---输入条件:InByte //---输出项目:RetByte unsigned char BcdToHex(unsigned char InByte) { unsigned char i,j,RetByte; i=InByte & 0xf0; j=InByte & 0x0f; RetByte=10*(i>>4)+j; return RetByte; } //-----***-unsigned long BcdToLong(unsigned char *DataDptr,unsigned char dataLend)-***-----// //---功能描述:多字节BCD数据转换位Long类型。 //---输入条件:*DataDptr->指定数组的首地址,DataLend->需要转换的数据长度 //---输出项目:RetData unsigned long BcdToLong(unsigned char *DataDptr,unsigned char DataLend) { unsigned char i,j,k; unsigned long l,RetData; RetData=BcdToHex(*DataDptr); for(i=1;i<DataLend;i++) { k=i*2; DataDptr++; l=BcdToHex(*DataDptr); for(j=0;j<k;j++) { l=l*10; } RetData=RetData+l; } return RetData; } //-----***-unsigned long HexToLong(unsigned char *DataDptr,unsigned char DataLend)-***-----// //---功能描述:多字节Hex数据转换位Long类型。 //---输入条件:*DataDptr->指定数组的首地址,DataLend->需要转换的数据长度 //---输出项目:RetData unsigned long HexToLong(unsigned char *DataDptr,unsigned char DataLend) { unsigned char i; unsigned long j,RetData; j=0; RetData=0; for(i=DataLend;i>0;i--) { j=*DataDptr; j=j<<((i-1)*8); RetData=RetData+j; DataDptr++; } return RetData; } //-----***-void LongToBcd(unsigned char *DataDptr,unsigned long LongData)-***-----// //---功能描述:Long数据转换为4字节的BCD数据。 //---输入条件:LongData->Long数据 //---输出项目:*DataDptr->指定数组的首地址 void LongToBcd(unsigned char *DataDptr,unsigned long LongData) { unsigned char i; unsigned long Data; Data=LongData; for(i=0;i<4;i++) { if(i<3) { *DataDptr=Data%100; Data=Data/100; *DataDptr=HexToBcd(*DataDptr); } else { *DataDptr=Data; *DataDptr=HexToBcd(*DataDptr); } DataDptr++; } } //-----***-void LongToHex(unsigned char *DataDptr,unsigned long LongData)-***-----// //---功能描述:Long数据转换为4字节的hex数据。 //---输入条件:LongData->Long数据 //---输出项目:*DataDptr->指定数组的首地址 void LongToHex(unsigned char *DataDptr,unsigned long LongData) { unsigned char i; unsigned long Data; Data=LongData; for(i=4;i>0;i--) { Data=LongData; *DataDptr=Data>>((i-1)*8);; DataDptr++; } } //--------------------------------------------------------------------------------------------------------------------------数据校验区 //-----***-bool ChkCardAdd(void)-***-----// //---功能描述:比对卡地址数据是否等于表地址数据 //---输入条件: //---输出项目:CardSnOk bool ChkCardAdd(void) { bool CardSnOk; unsigned char i; CardSnOk=1; RamCopy(&GloBuf[1],&MeterNum[1],3); RamCopy(&GloBuf[4],&ChongZhi[2],2); for(i=1;i<6;i++) { if(CardBuf!=GloBuf) { CardSnOk=0; break; } } return CardSnOk; } //-----***-bool DataChk(unsigned char *DataOneDptr,unsigned char *DataTwoDptr,unsigned char DataLend)-***-----// //---功能描述:比对两组数据是否相同 //---输入条件:*DataOneDptr-->指定数组A的首地址,*DataTwoDptr-->指定数组A的首地址,DataLend-->需要比对的长度 //---输出项目:ChkSt bool DataChk(unsigned char *DataOneDptr,unsigned char *DataTwoDptr,unsigned char DataLend) { bool ChkOk; unsigned char i; ChkOk=1; for(i=0;i<DataLend;i++) { if(*DataOneDptr!=*DataTwoDptr) { ChkOk=0; break; } else { DataOneDptr++; DataTwoDptr++; } } return ChkOk; } //-----***-bool DataXorChk(unsigned char *DataDptr,unsigned char DataLend)-***-----// //---功能描述:数据异或校验 //---输入条件:*DataDptr-->指定数组的首地址,DataLend-->需要校验的数据长度 //---输出项目:ChkOk bool DataXorChk(unsigned char *DataDptr,unsigned char DataLend) { bool ChkOk; unsigned char i,j,k; unsigned char DataOne,DataTwo; CardChkData=0; DataOne=*DataDptr; DataTwo=*(DataDptr+1); DataDptr++; for(i=0;i<DataLend-1;i++) { k=0x80; for(j=0;j<8;j++) { CardChkData<<=1; if((DataOne & k)== (DataTwo & k)) { CardChkData&=0xfe; } else { CardChkData|=0x01; } k>>=1; } DataDptr++; DataTwo=*DataDptr; DataOne=CardChkData; } if(CardChkData==*DataDptr) { ChkOk=1; } else { ChkOk=0; } return ChkOk; } //--------------------------------------------------------------------------------------------------------------------------数据运算 //-----***-void DataAddup(unsigned char *DataDptr,unsigned char DataLend)-***-----// //---功能描述:不大于100的16进纸数组累加 //---输入条件:*DataDptr-->指定数组的首地址,DataLend-->需要累加的长度 //---输出项目: void DataAddup(unsigned char *DataDptr,unsigned char DataLend) { unsigned char i; *DataDptr=BcdToHex(*DataDptr); *DataDptr=*DataDptr+1; for(i=0;i<DataLend;i++) { if(*DataDptr>=0x64) { *DataDptr=0; DataDptr++; *DataDptr=BcdToHex(*DataDptr); *DataDptr=*DataDptr+1; } else { *DataDptr=HexToBcd(*DataDptr); break; } } } //-----***-void DptrDecData(unsigned char *DataOneDptr,unsigned char *DataTwoDptr,unsigned char *DataThrDptr)-***-----// //---功能描述:数组减法(4字节BCD) //---输入条件:DataOneDptr-->被减数数组首地址,DataTwoDptr-->减数,数组首地址,DataThrDptr-->查数组首地址 //---输出项目: void DptrDecData(unsigned char *DataOneDptr,unsigned char *DataTwoDptr,unsigned char *DataThrDptr) { unsigned char i; for(i=0;i<4;i++) { GloBuf=*DataOneDptr; GloBuf[i+4]=*DataTwoDptr; DataOneDptr++; DataTwoDptr++; } DataOne=BcdToLong(&GloBuf[0],4); DataTwo=BcdToLong(&GloBuf[4],4); DataThr=0; if(DataOne>DataTwo) { DataThr=DataOne-DataTwo; } LongToBcd(&GloBuf[8],DataThr); for(i=0;i<4;i++) { *DataThrDptr=GloBuf[i+8]; DataThrDptr++; } } //-----***-void DataAdd(unsigned char *DataOneDptr,unsigned char *DataTwoDptr,unsigned char *DataThrDptr)-***-----// //---功能描述:数组相加(4字节BCD) //---输入条件:DataOneDptr-->被加数,数组首地址,DataTwoDptr-->加数,数组首地址,DataThrDptr-->累加和,数组首地址 //---输出项目: void DataAdd(unsigned char *DataOneDptr,unsigned char *DataTwoDptr,unsigned char *DataThrDptr) { unsigned char i,DataLow,DataHigh,TempBufOne,TempBufTwo,DataOver; DataOver=0; for(i=0;i<4;i++) { TempBufOne=*DataOneDptr; TempBufTwo=*DataTwoDptr; DataLow=(TempBufOne&0x0f) + (TempBufTwo&0x0f) + DataOver; if(DataLow>0x09) { DataLow=DataLow-10; DataOver=1; } else { DataOver=0; } DataHigh=(TempBufOne>>4) + (TempBufTwo>>4) + DataOver; if(DataHigh>0x09) { DataHigh=DataHigh-10; DataOver=1; } else { DataOver=0; } *DataThrDptr= DataLow + (DataHigh<<4); DataOneDptr++; DataTwoDptr++; DataThrDptr++; } } //--------------------------------------------------------------------------------------------------------------------------数据存储以及提取 //-----***-void DataPick(void)-***-----// //---功能描述:提取表水量信息 //---输入条件: //---输出项目: void DataPick(void) { FlashLongRead(0x1000,MeterNum,0);//----------------------------------表号 FlashLongRead(0x1004,AddsBuy,1);//-----------------------------------总购买量 FlashLongRead(0x1008,Overal,1);//------------------------------------累计量 FlashLongRead(0x100c,MeterSt,1);//-----------------------------------表状态字 FlashLongRead(0x1010,BaoJing,1);//-----------------------------------报警两 FlashLongRead(0x1014,Regrate,1);//-----------------------------------囤积量 FlashLongRead(0x1018,BuyData,1);//-----------------------------------本次购买 FlashLongRead(0x101c,Residual,1);//----------------------------------剩余量 FlashLongRead(0x1020,OverData,1);//----------------------------------透支量 FlashLongRead(0x1024,ChongZhi,1);//----------------------------------充值次数和地区代码 } //-----***-void DataSave(void)-***-----// //---功能描述:存储表水量信息 //---输入条件: //---输出项目: void DataSave(void) { FlashErase(0x1000); FlashLongWrite(0x1000,MeterNum,0);//----------------------------------表号 FlashLongWrite(0x1004,AddsBuy,1);//-----------------------------------总购买量 FlashLongWrite(0x1008,Overal,1);//------------------------------------累计量 FlashLongWrite(0x100c,MeterSt,1);//-----------------------------------表状态字 FlashLongWrite(0x1010,BaoJing,1);//-----------------------------------报警两 FlashLongWrite(0x1014,Regrate,1);//-----------------------------------囤积量 FlashLongWrite(0x1018,BuyData,1);//-----------------------------------本次购买 FlashLongWrite(0x101c,Residual,1);//----------------------------------剩余量 FlashLongWrite(0x1020,OverData,1);//----------------------------------透支量 FlashLongWrite(0x1024,ChongZhi,1);//----------------------------------充值次数和地区代码 } //--------------------------------------------------------------------------------------------------------------------------数据清除以及复制 //-----***-void RamCopy(unsigned char *DataOneDptr,unsigned char *DataTwoDptr,unsigned char DataLend)-***-----// //---功能描述:数据复制 //---输入条件:*DataOneDptr-->目的地数组的首地址,*DataTwoDptr-->数据源数组的首地址,DataLend-->需要复制的长度 //---输出项目: void RamCopy(unsigned char *DataOneDptr,unsigned char *DataTwoDptr,unsigned char DataLend) { unsigned char i; for(i=0;i<DataLend;i++) { *DataOneDptr=*DataTwoDptr; DataOneDptr++; DataTwoDptr++; } } //-----***-void RamCler(unsigned char *DataDptr,unsigned char DataLend)-***-----// //---功能描述:数据清零 //---输入条件:*DataDptr-->需要清零数组的首地址,DataLend-->需要清零的长度 //---输出项目: void RamCler(unsigned char *DataDptr,unsigned char DataLend) { unsigned char i; for(i=0;i<DataLend;i++) { *DataDptr=0; DataDptr++; } } //-----***-void ClerDataInfo(void)-***-----// //---功能描述:表计水量信息归零 //---输入条件: //---输出项目: void ClerDataInfo(void) { RamCler(MeterNum,4);//----------------------------------------------表号 RamCler(AddsBuy,4);//-----------------------------------------------累购量 RamCler(Overal,4);//------------------------------------------------累计量 RamCler(MeterSt,4);//-----------------------------------------------表状态 RamCler(BaoJing,4);//-----------------------------------------------报警量 RamCler(Regrate,4);//-----------------------------------------------囤积量 RamCler(BuyData,4);//-----------------------------------------------购买量 RamCler(Residual,4);//----------------------------------------------剩余量 RamCler(OverData,4);//----------------------------------------------透支量 RamCler(ChongZhi,4);//----------------------------------------------充值次数和地区代码 DataSave(); }