#include "msp430x14x.h"
#define uchar unsigned char
#define uint unsigned int
/****************************************************************************/
#define P3_0_WDI 0x01 //WDT
#define P3_1_BEEP 0x02 //蜂鸣
#define P3_2_RTS1 0x04 //RS485A控制
#define P3_3_RTS 0x08 //RS485B控制
#define P5_5_DAT 0x20 //LED DAT BC7281B接口
#define P5_6_KEY 0x40 //LED KEY
#define P5_7_CLK 0x80 //LED CLK
/****************************************************************************/
uchar CAddress; //仪表地址
uchar Cbps=3,Cbpss=4;
uchar Cdata8=0,Cdata8s=0;
uchar Cmima[4];//密码
typedef enum
{
SYS_Idle,//空闲
SYS_Txd,//发送数据
SYS_Twait1,//发送等待
SYS_Twait2, //发送等待
SYS_Rwait,//接收等待
}SYS_STATE;
SYS_STATE systemState;
uchar Rxok=0;
uchar Cflag,Crep=0,Cdscon=0;
uint Csearch[16];
uint nHmax[16]; //20mA液位值
uint fXiuZheng[16];//修正系数
uchar CregArry[32];//液位值
uchar Cmnumber=12;
uchar Ckey_number;
uchar FlashWord[10];
uchar CDispBuff[12];
uint *pq=(uint *)0xfd00;//指向Flash
unsigned char aRxBuff0[16]; //接收数据缓冲区
unsigned char NRxBuff0=0;
unsigned char aTxBuff0[128]; //发送数据缓冲区
unsigned char NTxBuff0=0;
unsigned char commandPending0=0,Ntxd0=0;
uint timeoutCounter0=0;
uint RXcrc0;
uint UstarReg0,UregNum0;
uint txwait0;
uchar bUartRxErr0;
uchar Couttime,Couttime1,CdelayT,CdelayT1;
unsigned char aRxBuff1[16]; //接收数据缓冲区
unsigned char NRxBuff1=0;
unsigned char aTxBuff1[32]; //发送数据缓冲区
unsigned char NTxBuff1=0;
unsigned char commandPending1=0,Ntxd1=0;
uint timeoutCounter1=0;
uint RXcrc1;
uint UstarReg1,UregNum1;
uint txwait1;
uchar bUartRxErr1;
unsigned char *puchMsg ; /* 要进行CRC校验的消息 */
unsigned int CRC16(uchar *puchMsg, uchar usDataLen)
{
unsigned char uchCRCHi = 0xFF ; /* 高CRC字节初始化 */
unsigned char uchCRCLo = 0xFF ; /* 低CRC 字节初始化 */
unsigned uIndex ; /* CRC循环中的索引 */
while (usDataLen--) /* 传输消息缓冲区 */
{
uIndex = uchCRCHi ^ *puchMsg++ ; /* 计算CRC */
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
uchCRCLo = auchCRCLo[uIndex] ;
}
return (uchCRCHi << 8 | uchCRCLo) ;
}
void UpdateStateMachine(void)
{
static unsigned char CAdesm=1;
static unsigned int ia;
switch (systemState)
{
case SYS_Idle:
P3OUT|=P3_2_RTS1;
aTxBuff1[0]=CAdesm;//地址
aTxBuff1[1]=0x03;//功能代码
aTxBuff1[2]=0x00;//起始地址
aTxBuff1[3]=0x00;//起始地址
aTxBuff1[4]=0x00;//寄存器个数
aTxBuff1[5]=0x01;//寄存器个数
puchMsg=aTxBuff1;
RXcrc1=CRC16(puchMsg, 6);//字符串校验
aTxBuff1[6]=RXcrc1>>8;//功能代码
aTxBuff1[7]=RXcrc1&0x00ff;
NTxBuff1=0;
systemState=SYS_Txd;
break;
case SYS_Txd:
if (NTxBuff1<8)
{
TXBUF1=aTxBuff1[NTxBuff1];
NTxBuff1++;
systemState=SYS_Twait1;
}
else
{
ia=0;
systemState=SYS_Twait2;
}
break;
case SYS_Twait1:
if (IFG2&UTXIFG1)//查询是否发送完毕
{
systemState=SYS_Txd;
}
break;
case SYS_Twait2:
ia++;
if (ia>=CdelayT1)
{
CAdesm++;
if (CAdesm>Cmnumber)
CAdesm=1;
systemState=SYS_Rwait;
P3OUT&=~P3_2_RTS1;
Rxok=0;
ia=0;
}
break;
case SYS_Rwait:
ia++;
if ((ia>=2500)||((Rxok==1)&&(ia>=80)))
{
systemState=SYS_Idle;
}
break;
default :
systemState=SYS_Idle;
break;
}
}
void HandleUART0(void)
{
uint i;
if (NRxBuff0>0)
{
if(timeoutCounter0++ > Couttime)//接收超时
{
NRxBuff0=0;
for (i=0;i<10;i++)
{
aRxBuff0[i]=0;
}
}
}
else
{
timeoutCounter0 = 0;// Don't let timeoutCounter slowly grow over many commands.
}
if (commandPending0 == 0)//收态
{
if (NRxBuff0>=8)
{
if ((aRxBuff0[3]==0x04)&&(aRxBuff0[1]==0x06))
{
if (NRxBuff0>=10)
{
puchMsg=aRxBuff0;
RXcrc0=CRC16(puchMsg, NRxBuff0-2);//字符串校验
i=aRxBuff0[NRxBuff0-2]*0x100+aRxBuff0[NRxBuff0-1];
NRxBuff0=0;
if (RXcrc0==i)//通过CRC校验
{
if (aRxBuff0[0]==CAddress)
{
commandPending0 = 1;
}
}
}
}
else
{
puchMsg=aRxBuff0;
RXcrc0=CRC16(puchMsg, NRxBuff0-2);//字符串校验
i=aRxBuff0[NRxBuff0-2]*0x100+aRxBuff0[NRxBuff0-1];
NRxBuff0=0;
if (RXcrc0==i)//通过CRC校验
{
if (aRxBuff0[0]==CAddress)
{
commandPending0 = 1;
}
}
else
{
commandPending0 = 0;
NRxBuff0=0;
for (i=0;i<10;i++)
{
aRxBuff0[i]=0;
}
}
}
}
}
else if (commandPending0 == 1)//命令处理
{
switch(aRxBuff0[1])
{
case 3:
UstarReg0=aRxBuff0[2]*0x100+aRxBuff0[3];
UregNum0=(aRxBuff0[4]*0x100+aRxBuff0[5])<<1;
aTxBuff0[0]=CAddress;//地址
aTxBuff0[1]=0x03;//功能代码
aTxBuff0[2]=UregNum0;
for (i=0;i<UregNum0;i++)
{
aTxBuff0[i+3]=CregArry[(UstarReg0<<1)+i];
}
puchMsg=aTxBuff0;
RXcrc0=CRC16(puchMsg, UregNum0+3);//CRC计算
aTxBuff0[UregNum0+3]=(RXcrc0&0xff00)>>8; //CRC高字节
aTxBuff0[UregNum0+4]=RXcrc0&0x00ff;//CRC低字节
P3OUT|=P3_3_RTS;
Ntxd0=UregNum0+5;
NTxBuff0=0;
commandPending0 = 2;
//SendUart0(UregNum+5);//发送
break;
case 6:
UstarReg0=aRxBuff0[2]*0x100+aRxBuff0[3];
switch(UstarReg0)
{
case 0://初值
break;
case 1://地址
break;
case 2://20mA
break;
case 3://调相
break;
case 4://修正系数
break;
default :
break;
}
commandPending0 = 0;
break;
default :
commandPending0 = 0;
break;
}//功能
}
else if (commandPending0 == 2)//发态
{
TXBUF0=aTxBuff0[NTxBuff0];
NTxBuff0++;
if (NTxBuff0>=Ntxd0)
{
commandPending0 = 4;
txwait0=CdelayT;
}
else
{
commandPending0 = 3;
}
}
else if(commandPending0 == 3)//发送等待状态
{
if (IFG1&UTXIFG0)//查询是否发送完毕
{
commandPending0 =2;
}
}
else if(commandPending0 == 4)
{
if(--txwait0 == 0)
{
commandPending0 = 0;
P3OUT&=~P3_3_RTS;
}
}
}
void HandleUART1(void)
{
uint i;
if (NRxBuff1>0)
{
if(timeoutCounter1++ > Couttime1)//接收超时
{
NRxBuff1=0;
for (i=0;i<10;i++)
{
aRxBuff1[i]=0;
}
}
}
else
{
timeoutCounter1 = 0;// Don't let timeoutCounter slowly grow over many commands.
}
if (commandPending1 == 0)//收态
{
if (NRxBuff1>=7)
{
puchMsg=aRxBuff1;
RXcrc1=CRC16(puchMsg, NRxBuff1-2);//字符串校验
i=aRxBuff1[NRxBuff1-2]*0x100+aRxBuff1[NRxBuff1-1];
NRxBuff1=0;
if (RXcrc1==i)//通过CRC校验
{
commandPending1 = 1;
}
else
{
commandPend