#include <GLOBAL.h>
#include <stdlib.h>
#include <stdbool.h>
#include <msp430f5438a.h>
///------------------并口操作
#define AleDirOut P7DIR |= BIT7
#define AleHig P7OUT |= BIT7
#define AleLow P7OUT &=~ BIT7
#define CsDirOut P7DIR |= BIT6
#define CsHig P7OUT |= BIT6
#define CsLow P7OUT &=~ BIT6
#define WdDirOut P7DIR |= BIT5
#define WdHig P7OUT |= BIT5
#define WdLow P7OUT &=~ BIT5
#define RdDirOut P7DIR |= BIT4
#define RdHig P7OUT |= BIT4
#define RdLow P7OUT &=~ BIT4
//---数据总线
#define D0DirOut P6DIR |= BIT0//---强制输出
#define D0OutHig P6OUT |= BIT0
#define D0OutLow P6OUT &=~ BIT0
#define D0DirIns P6OUT &= ~BIT0//---被动输出
#define D0InsHig P6DIR &= ~BIT0
#define D0InsLow P6DIR |= BIT0
#define D0InsSt P6IN & BIT0
#define D1DirOut P6DIR |= BIT1
#define D1OutHig P6OUT |= BIT1
#define D1OutLow P6OUT &=~ BIT1
#define D1DirIns P6OUT &= ~BIT1//---被动输出
#define D1InsHig P6DIR &= ~BIT1
#define D1InsLow P6DIR |= BIT1
#define D1InsSt P6IN & BIT1
#define D2DirOut P6DIR |= BIT2
#define D2OutHig P6OUT |= BIT2
#define D2OutLow P6OUT &=~ BIT2
#define D2DirIns P6OUT &= ~BIT2
#define D2InsHig P6DIR &= ~BIT2
#define D2InsLow P6DIR |= BIT2
#define D2InsSt P6IN & BIT2
#define D3DirOut P6DIR |= BIT3
#define D3OutHig P6OUT |= BIT3
#define D3OutLow P6OUT &=~ BIT3
#define D3DirIns P6OUT &= ~BIT3
#define D3InsHig P6DIR &= ~BIT3
#define D3InsLow P6DIR |= BIT3
#define D3InsSt P6IN & BIT3
#define D4DirOut P6DIR |= BIT4
#define D4OutHig P6OUT |= BIT4
#define D4OutLow P6OUT &=~ BIT4
#define D4DirIns P6OUT &= ~BIT4
#define D4InsHig P6DIR &= ~BIT4
#define D4InsLow P6DIR |= BIT4
#define D4InsSt P6IN & BIT4
#define D5DirOut P6DIR |= BIT5
#define D5OutHig P6OUT |= BIT5
#define D5OutLow P6OUT &=~ BIT5
#define D5DirIns P6OUT &= ~BIT5
#define D5InsHig P6DIR &= ~BIT5
#define D5InsLow P6DIR |= BIT5
#define D5InsSt P6IN & BIT5
#define D6DirOut P6DIR |= BIT6
#define D6OutHig P6OUT |= BIT6
#define D6OutLow P6OUT &=~ BIT6
#define D6DirIns P6OUT &= ~BIT6
#define D6InsHig P6DIR &= ~BIT6
#define D6InsLow P6DIR |= BIT6
#define D6InsSt P6IN & BIT6
#define D7DirOut P6DIR |= BIT7
#define D7OutHig P6OUT |= BIT7
#define D7OutLow P6OUT &=~ BIT7
#define D7DirIns P6OUT &= ~BIT7
#define D7InsHig P6DIR &= ~BIT7
#define D7InsLow P6DIR |= BIT7
#define D7InsSt P6IN & BIT7
//---地址总线
#define A00DirOut P2DIR |= BIT1
#define A00Hig P2OUT |= BIT1
#define A00Low P2OUT &=~ BIT1
#define A01DirOut P2DIR |= BIT2
#define A01Hig P2OUT |= BIT2
#define A01Low P2OUT &=~ BIT2
#define A02DirOut P2DIR |= BIT3
#define A02Hig P2OUT |= BIT3
#define A02Low P2OUT &=~ BIT3
#define A03DirOut P2DIR |= BIT4
#define A03Hig P2OUT |= BIT4
#define A03Low P2OUT &=~ BIT4
#define A04DirOut P2DIR |= BIT5
#define A04Hig P2OUT |= BIT5
#define A04Low P2OUT &=~ BIT5
#define A05DirOut P2DIR |= BIT6
#define A05Hig P2OUT |= BIT6
#define A05Low P2OUT &=~ BIT6
#define A06DirOut P2DIR |= BIT7
#define A06Hig P2OUT |= BIT7
#define A06Low P2OUT &=~ BIT7
#define A07DirOut P3DIR |= BIT0
#define A07Hig P3OUT |= BIT0
#define A07Low P3OUT &=~ BIT0
#define A08DirOut P3DIR |= BIT1
#define A08Hig P3OUT |= BIT1
#define A08Low P3OUT &=~ BIT1
#define A09DirOut P3DIR |= BIT2
#define A09Hig P3OUT |= BIT2
#define A09Low P3OUT &=~ BIT2
#define A10DirOut P3DIR |= BIT3
#define A10Hig P3OUT |= BIT3
#define A10Low P3OUT &=~ BIT3
#define A11DirOut P3DIR |= BIT6
#define A11Hig P3OUT |= BIT6
#define A11Low P3OUT &=~ BIT6
#define A12DirOut P3DIR |= BIT7
#define A12Hig P3OUT |= BIT7
#define A12Low P3OUT &=~ BIT7
#define A13DirOut P4DIR |= BIT0
#define A13Hig P4OUT |= BIT0
#define A13Low P4OUT &=~ BIT0
#define A14DirOut P4DIR |= BIT1
#define A14Hig P4OUT |= BIT1
#define A14Low P4OUT &=~ BIT1
#define A15DirOut P4DIR |= BIT2
#define A15Hig P4OUT |= BIT2
#define A15Low P4OUT &=~ BIT2
#define A16DirOut P4DIR |= BIT3
#define A16Hig P4OUT |= BIT3
#define A16Low P4OUT &=~ BIT3
#define A17DirOut P4DIR |= BIT4
#define A17Hig P4OUT |= BIT4
#define A17Low P4OUT &=~ BIT4
#define A18DirOut P4DIR |= BIT5
#define A18Hig P4OUT |= BIT5
#define A18Low P4OUT &=~ BIT5
#define A19DirOut P4DIR |= BIT6
#define A19Hig P4OUT |= BIT6
#define A19Low P4OUT &=~ BIT6
#define A20DirOut P4DIR |= BIT7
#define A20Hig P4OUT |= BIT7
#define A20Low P4OUT &=~ BIT7
unsigned char RamDelayData=5;
void RamDelay(unsigned char Delay)
{
while(Delay--);
}
//-----***-void RamCler(unsigned char *DataDptr,unsigned char DataLend)-***-----//
//---功能描述:数据清零
//---输入条件:*DataDptr-->需要清零数组的首地址,DataLend-->需要清零的长度
//---输出项目:
void RamCler(unsigned char *DataDptr,unsigned char DataLenth)
{
while(DataLenth--)
{
*DataDptr=0;
DataDptr++;
}
}
void RamInit(void)
{
AleDirOut;
AleHig;
WdDirOut;
WdHig;
RdDirOut;
RdHig;
A00DirOut;
A01DirOut;
A02DirOut;
A03DirOut;
A04DirOut;
A05DirOut;
A06DirOut;
A07DirOut;
A08DirOut;
A09DirOut;
A10DirOut;
A11DirOut;
A12DirOut;
A13DirOut;
A14DirOut;
A15DirOut;
A16DirOut;
A17DirOut;
A18DirOut;
A19DirOut;
A20DirOut;
A00Hig;
A01Hig;
A02Hig;
A03Hig;
A04Hig;
A05Hig;
A06Hig;
A07Hig;
A08Hig;
A09Hig;
A10Hig;
A11Hig;
A12Hig;
A13Hig;
A14Hig;
A15Hig;
A16Hig;
A17Hig;
A18Hig;
A19Hig;
A20Hig;
}
void RamAleClr(unsigned long RamAdd)
{
unsigned char i;
i=RamAdd/0x7cfff;
AleHig;
RamDelay(RamDelayData);
if(i==0){A00Low;}else{A00Hig;}
if(i==1){A01Low;}else{A01Hig;}
if(i==2){A02Low;}else{A02Hig;}
if(i==3){A03Low;}else{A03Hig;}
if(i==4){A04Low;}else{A04Hig;}
if(i==5){A05Low;}else{A05Hig;}
if(i==6){A06Low;}else{A06Hig;}
if(i==7){A07Low;}else{A07Hig;}
AleLow;
RamDelay(RamDelayData);
}
void RamAleSet(void)
{
AleHig;
RamDelay(RamDelayData);
A00Hig;
A01Hig;
A02Hig;
A03Hig;
A04Hig;
A05Hig;
A06Hig;
A07Hig;
RamDelay(RamDelayData);
AleLow;
RamDelay(RamDelayData);
}
void RamAddSet(unsigned long RamAdd)
{
if(RamAdd&0x00000001){A00Hig;}else{A00Low;}
if(RamAdd&0x00000002){A01Hig;}else{A01Low;}
if(RamAdd&0x00000004){A02Hig;}else{A02Low;}
if(RamAdd&0x00000008){A03Hig;}else{A03Low;}
if(RamAdd&0x00000010){A04Hig;}else{A04Low;}
if(RamAdd&0x00000020){A05Hig;}else{A05Low;}
if(RamAdd&0x00000040){A06Hig;}else{A06Low;}
if(RamAdd&0x00000080){A07Hig;}else{A07Low;}
if(RamAdd&0x00000100){A08Hig;}else{A08Low;}
if(RamAdd&0x00000200){A09Hig;}else{A09Low;}
if(RamAdd&0x00000400){A10Hig;}else{A10Low;}
if(RamAdd&0x00000800){A11Hig;}else{A11Low;}
if(RamAdd&0x00001000){A12Hig;}else{A12Low;}
if(RamAdd&0x00002000){A13Hig;}else{A13Low;}
if(RamAdd&0x00004000){A14Hig;}else{A14Low;}
if(RamAdd&0x00008000){A15Hig;}else{A15Low;}
if(RamAdd&0x00010000){A16Hig;}else{A16Low;}
if(RamAdd&0x00020000){A17Hig;}else{A17Low;}
if(RamAdd&0x00040000){A18Hig;}else{A18Low;}
if(RamAdd&0x00080000){A19Hig;}else{A19Low;}
if(RamAdd&0x00100000){A20Hig;}else{A20Low;}
}
void RamCmdRst(void)
{
AleDirOut;
AleHig;
CsDirOut;
CsHig;
WdDirOut;
WdHig;
RdDirOut;
RdHig;
A00DirOut;
A01DirOut;
A02DirOut;
A03DirOut;
A04DirOut;
A05DirOut;
A06DirOut;
A07DirOut;
A08DirOut;
A09DirOut;
A10DirOut;
A11DirOut;
A12DirOut;
A13DirOut;
A14DirOut;
A15DirOut;
A16DirOut;
A17DirOut;
A18DirOut;
A19DirOut;
A20DirOut;
A00Hig;
A01Hig;
A02Hig;
A03Hig;
A04Hig;
A05Hig;
A06Hig;
A07Hig;
A08Hig;
A09Hig;
A10Hig;
A11Hig;
A12Hig;
A13Hig;
A14Hig;
A15Hig;
A16Hig;
A17Hig;
A18Hig;
A19Hig;
A20Hig;
RamDelay(RamDelayData);
}
void RamCmdSet(bool CmdSt,bool BitSt)
{
WdDirOut;
RdDirOut;
if(CmdSt)//---写命令
{
RdHig;
RamDelay(RamDelayData);
if(BitSt)
{
WdHig;
}
else
{
WdLow;
}
}
else//--------读命令
{
WdHig;
RamDelay(RamDelayData);
if(BitSt)
{
RdHig;
}
else
{
RdLow;
}
}
RamDelay(RamDelayData);
}
unsigned char RecRam(void)
{
unsigned char RecData;
RecData=0;
//---被动拉高,输入状态
D0DirIns;
D1DirIns;
D2DirIns;
D3DirIns;
D4DirIns;
D5DirIns;
D6DirIns;
D7DirIns;
D0InsHig;
D1InsHig;
D2InsHig;
D3InsHig;
D4InsHig;
D5InsHig;
D6InsHig;
D7InsHig;
RamDelay(RamDelayData);
if(D0InsSt){RecData|=0x01; }else{RecData&=0xfe;}
if(D1InsSt){RecData|=0x02; }else{RecData&=0xfd;}
if(D2InsSt){RecData|=0x04; }else{RecData&=0xfb;}
if(D3InsSt){RecData|=0x08; }else{RecData&=0xf7;}
if(D4InsSt){RecData|=0x10; }else{RecData&=0xef;}
if(D5InsSt){RecData|=0x20; }else{RecData&=0xdf;}
if(D6InsSt){RecData|=0x40; }else{RecData&=0xbf;}
if(D7InsSt){RecData|=0x80; }else{RecData&=0x7f;}
return RecData;
}
unsigned char RamRead(unsigned long RamAdd)
{
unsigned char RecData;
_DINT();
RamAleClr(RamAdd);//---选择CS
RamAddSet(RamAdd);//---发送字节地址
RamCmdSet(0,0);//---读开始
RecData=RecRam();
RamCmdSet(0,1);//---读结束
RamAleSet();//---选择CS
//RamCmdRst();
_EINT();
return RecData;
}
//-----***-void ExitRamRead(unsigned char *BufDptr,unsigned long RamAdd,unsigned char DataLenth)-***-----//
//---功能描述:外部RAM读取
//---输入条件:RamAdd-->外部RAM首字节地址,DataLenth--->读取的字节的长度
//---输出项目:*BufDptr-->数据存放的BUF指针,
void ExitRamRead(unsigned char *BufDptr,unsigned long RamAdd,unsigned int DataLenth)
{
RamInit();
while(DataLenth--)
{
*BufDptr = RamRead(RamAdd);
BufDptr++;
RamAdd++;
}
//RamInit();
}
void WriteRam(unsigned char RamData)
{
//---强制拉高,输出状态
D0DirOut;
D1DirOut;
D2DirOut;
D3DirOut;
D4DirOut;
D5DirOut;
D6DirOut;
D7DirOut;
RamDelay(RamDelayData);
if(RamData&0x01){D0OutHig;}else{D0OutLow;}
if(RamData&0x02){D1OutHig;}else{D1OutLow;}
if(RamData&0x04){D2OutHig;}else{D2OutLow;}
if(RamData&0x08){D3OutHig;}else{D3OutLow;}
if(RamData&0x10){D4OutHig;}else{D4OutLow;}
if(RamData&0x20){D5OutHig;}else{D5OutLow;}
if(RamData&0x40){D6OutHig;}else{D6OutLow;}
if(RamData&0x80){D7OutHig;}else{D7OutLow;}
}
void RamWrite(unsigned char RamData,unsigned long RamAdd)
{
_DINT();
RamAleClr(RamAdd);
RamAddSet(RamAdd);
RamCmdSet(1,0);
WriteRam(RamData);
RamCmdSet(1,1);
RamAleSet();//---
//RamCmdRst();
_EINT();
}
//-----***-void ExitRamRead(unsigned char *BufDptr,unsigned long RamAdd,unsigned char DataLenth)-***-----//
//---功能描述:外部RAM读取
//---输入条件:*BufDptr-->被写数据的BUF指针,RamAdd-->外部RAM首字节地址,DataLenth--->读取的字节的长度
//---输出项目:
void ExitRamWrite(unsigned char *BufDptr,unsigned long RamAdd,unsigned int DataLenth)
{
RamInit();
while(DataLenth--)
{
RamWrite(*BufDptr,RamAdd);
RamAdd++;
BufDptr++;
}
}
void FreezeDataPick(unsigned long ByteAddress)
{
unsigned int i,Length,DataChkLen;
unsigned long FreezeDataDtpr;
Length=0;
i=0;
while(MeterSum--)
{
DataChkLen=i*58;
i++;
FreezeDataDtpr=ByteAddress;
ExitRamRead(&MeterInfo[Length],FreezeDataDtpr,7);//---
Length+=7;
MeterInfo[Length]=0xa1;
Length+=1;
FreezeDataDtpr=ByteAddress+0x07;
ExitRamRead(&MeterInfo[Length],FreezeDataDtpr,4);//---购买
Length+=4;
MeterInfo[Length]=0xa2;
Length+=1;
FreezeDataDtpr=ByteAddress+0x0b;
ExitRamRead(&MeterInfo[Length],FreezeDataDtpr,4);//---剩余
Length+=4;
MeterInfo[Length]=0xa3;
Length+=1;
FreezeDataDtpr=ByteAddress+0x0f;
ExitRamRead(&MeterInfo[Length],FreezeDataDtpr,4);//---累计
Length+=4;
MeterInfo[Length]=0xa4;
Length+=1;
FreezeDataDtpr=ByteAddress+0x13;
ExitRamRead(&MeterInfo[Length],FreezeDataDtpr,4);//---温度
Length+=4;
MeterInfo[Length]=0xa5;
Length+=1;
FreezeDataDtpr=ByteAddress+0x4c;
ExitRamRead(&MeterInfo[Length],FreezeDataDtpr,4);//---状态
Length+=4;
MeterInfo[Length]=0xa6;
Length+=1;
FreezeDataDtpr=ByteAddress+0x17;
ExitRamRead(&MeterInfo[Length],FreezeDataDtpr,4);//---
Length+=4;
MeterInfo[Length]=0xa7;
Length+=1;
FreezeDataDtpr=ByteAddress+0x1b;
ExitRamRead(&MeterInfo[Length],FreezeDataDtpr,4);//---
Length+=4;
MeterInfo[Length]=0xa8;
Length+=1;
FreezeDataDtpr=ByteAddress+0x1f;
ExitRamRead(&MeterInfo[Length],FreezeDataDtpr,4);//---
Length+=4;
MeterInfo[Length]=0xa9;
Length+=1;
FreezeDataDtpr=ByteAddress+0x48;
ExitRamRead(&MeterInfo[Length],FreezeDataDtpr,4);//---
Length+=4;
MeterInfo[Length]=0xaa;
Length+=1;
FreezeDataDtpr=ByteAddress+0x23;
ExitRamRead(&MeterInfo[Length],FreezeDataDtpr,4);//---
Length+=4;
MeterInfo[Length]=EleChk(&MeterInfo[DataChkLen],57);
Length+=1;
ByteAddress+=80;
}
}
void ConnFreezeDataSave(unsigned long ByteAddress)
{
unsigned int Length;
unsigned long FreezeDataDtpr;
Length=1;
FreezeDataDtpr=ByteAddress+0x07;
ExitRamWrite(&MeterInfo[Length],FreezeDataDtpr,4);//---购买
Length=6;
FreezeDataDtpr=ByteAddress+0x0f;
ExitRamWrite(&MeterInfo[Length],FreezeDataDtpr,4);//---累计
Length=11;
FreezeDataDtpr=ByteAddress+0x0b;
ExitRamWrite(&MeterInfo[Length],FreezeDataDtpr,4);//---剩余
Length=16;
FreezeDataDtpr=ByteAddress+0x13;
ExitRamWrite(&MeterInfo[Length],FreezeDataDtpr,4);//---温度
Length=21;
FreezeDataDtpr=ByteAddress+0x4c;
ExitRamWrite(&MeterInfo[Length],FreezeDataDtpr,4);//---状态
FreezeDataDtpr=ByteAddress+0x48;
ExitRamWrite(&Timer[2],FreezeDataDtpr,4);//---冻结时间
}
void MeterFreezeDataSave(unsigned long ByteAddress)
{
unsigned int Length;
unsigned long FreezeDataDtpr;
Length=1;
FreezeDataDtpr=ByteAddress+0x38;
ExitRamWrite(&MeterInfo[Length],FreezeDataDtpr,4);//---累积热量
Length=11;
FreezeDataDtpr=ByteAddress+0x17;
ExitRamWrite(&MeterInfo[Length],FreezeDataDtpr,4);//---当前流速
Length=16;
FreezeDataDtpr=ByteAddress+0x23;
ExitRamWrite(&MeterInfo[Length],FreezeDataDtpr,4);//---累计流量
Length=20;
FreezeDataDtpr=ByteAddress+0x1b;
ExitRamWrite(&MeterInfo[Length],FreezeDataDtpr,2);//---供水回水温度
Length=23;
FreezeDataDtpr=ByteAddress+0x1d;
ExitRamWrite(&MeterInfo[Length],FreezeDataDtpr,2);//---供水回水温度
Length=26;
FreezeDataDtpr=ByteAddress+0x1f;
ExitRamWrite(&MeterInfo[Length],FreezeDataDtpr,3);//---累计工作时间
FreezeDataDtpr=ByteAddress+0x48;
ExitRamWrite(&Timer[2],FreezeDataDtpr,4);//---冻结时间
}
unsigned int HourDataPick(unsigned char *BufDptr)
{
unsigned char i;
unsigned int RecData;
unsigned long ReadAddress;
MeterNo=*(BufDptr+1) + (*(BufDptr+2) << 8);
MeterSum=*(BufDptr+0);
if(MeterSum>8)
{
MeterSum=8;
}
RecData=MeterSum*58;
ReadAddress=0;
i=BcdToHex(*(BufDptr+3));
while(i>0)
{
ReadAddress+=5120;
i--;
}
ReadAddress+=MeterHourStrAdd;
MeterNo-=1;
ReadAddress+=MeterNo*80;
FreezeDataPick(ReadAddress);
return RecData;
}
unsigned int DayDataPick(unsigned char *BufDptr)
{
unsigned int RecData;
unsigned long i,ReadAddress,SaveNo,ReadDayNo,CurrentDayNo;
SaveNo=0;
IicMuchByteRead(0x11,GloBuf,2);
SaveNo=GloBuf[0] + (GloBuf[1] << 8);
if(SaveNo>732)
{
SaveNo=0;
}
CurrentDayNo=NumerOfDays(BcdToHex(Timer[5]),BcdToHex(Timer[4]),BcdToHex(Timer[3]));
ReadDayNo=NumerOfDays(BcdToHex(*(BufDptr+6)),BcdToHex(*(BufDptr+5)),BcdToHex(*(BufDptr+4)));
if(CurrentDayNo>ReadDayNo)
{
i=CurrentDayNo-ReadDayNo;
if(SaveNo>i)
{
SaveNo=CurrentDayNo-ReadDayNo;
}
}
MeterNo=*(BufDptr+1) + (*(BufDptr+2) << 8);
MeterSum=*(BufDptr+0);
if(MeterSum>8)
{
MeterSum=8;
}
RecData=MeterSum*58;
ReadAddress=MeterDayStrAdd;
while(SaveNo>0)
{
ReadAddress+=5120;
SaveNo--;
}
MeterNo-=1;
ReadAddress+=MeterNo*80;
FreezeDataPick(ReadAddress);
if(RecData>1000)
{
RecData=0;
}
return RecData;
}
unsigned int MonthDataPick(unsigned char *BufDptr)
{
unsigned int RecData;
unsigned long i,ReadAddress,SaveNo,ReadDayNo,CurrentDayNo;
SaveNo=0;
IicMuchByteRead(0x14,GloBuf,2);
SaveNo=GloBuf[0];
if(SaveNo>48)
{
SaveNo=0;
}
CurrentDayNo=BcdToHex(Timer[5])*12;
CurrentDayNo+=BcdToHex(Timer[4]);
ReadDayNo=BcdToHex(*(BufDptr+6))*12;
ReadDayNo+=BcdToHex(*(BufDptr+5));
if(CurrentDayNo>ReadDayNo)
{
i=CurrentDayNo-ReadDayNo;
if(SaveNo>i)
{
SaveNo=CurrentDayNo-ReadDayNo;
}
}
MeterNo=*(BufDptr+1) + (*(BufDptr+2) << 8);
MeterSum=*(BufDptr+0);
if(MeterSum>8)
{
MeterSum=8;
}
RecData=MeterSum*58;
ReadAddress=MeterMonStrAdd;
while(SaveNo>0)
{
ReadAddress+=5120;
SaveNo--;
}
MeterNo-=1;
ReadAddress+=MeterNo*80;
FreezeDataPick(ReadAddress);
if(RecData>1000)
{
RecData=0;
}
return RecData;;
}
|
|