/******************************************************************************************* 函数原形:void Uart1_Init_Rec(int pclk,int baud) 功能描述:当工作在接收模式下时,初始化UART1(485) 参 数:无 *******************************************************************************************/ void Uart1_Init_Rec(int pclk,int baud) { int i; if (pclk == 0) pclk = PCLK;
rUFCON1=0x0; //FIFO disable rUMCON1=0x0; //AFC disable
rULCON1=0x33; //8位数据,1位停止位,第9位(奇偶位)强制为1 rUCON1=0x105; //rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling rUBRDIV1=( (int)(pclk/16./baud) -1 );//bps = 38400 pISR_UART1=(unsigned)Uart1_RXInt_LT1200; rSRCPND=BIT_UART1; rINTPND=BIT_UART1; rSUBSRCPND=BIT_SUB_RXD1;//清除RXD1,ERR1中断挂起标志位
rINTMSK &=~(BIT_UART1); //去除UART中断屏蔽 rINTSUBMSK &=~(BIT_SUB_RXD1);//去除子中断屏蔽 for(i=0;i<100;i++); }
/******************************************************************************************* 函数原形:void __irq Uart0_RXInt_LT1200(void) 功能描述:LT1200 485接收中断处理程序 参 数:无 *******************************************************************************************/ void __irq Uart1_RXInt_LT1200(void) { char buffer[6]={2,4,0,0,0,0}; unsigned short JoinNum,Value,i; if (((rUTRSTAT1&0x1)>0) && (rUERSTAT1==0)) { ch=~rURXH1; if(_9bit==0) {//Parity Check as 0 if(ch==0x00) //分隔符 {//Next Command Start gState=0xfe;//ff:no active ,fe:active,fd:got id,00~f0:get how many receive buf } } else { if(gState==0xfe) {//接收的第一个字符 gReceiveID=ch; if(ch==0xff) {//接收到复位指令(FF 02 03 00) 或查询网络型号指令(FF 02 03 01 ID 00) gState=0xfc; gReceiveLen=0; } else {//接收到ID, gState--;//gState = fd } } else if(gState==0xfd)//ID XX { if(ch==00) {//ID 00 主机空闲 gState=0xfe; if(gReceiveID==h_config->NetID) //just search me to input { gMyTimeOut=0; gReceive00Time=0; //the pgm2 have searched me ,so I work properly. if(pSendHead!=pSendTail) { buffer[2]=(unsigned char)pSendHead->JoinNum; buffer[3]=(unsigned char)(pSendHead->JoinNum>>8); buffer[4]=(unsigned char)pSendHead->Value; buffer[5]=(unsigned char)(pSendHead->Value>>8); if(++pSendHead==(Sendbuf+50))pSendHead=Sendbuf; Send_485(buffer,6); } else Send_485("x2x0",2); } } else {//接收到信息,ID LEN gReceiveLen=ch; gState=0; } } else if(gState==0xfc)//接收到复位指令(FF 02 03 00) 或查询网络型号指令(FF 02 03 01 ID 00) { switch(gReceiveLen) { case 0: if(ch==0x02) gReceiveLen++;//FF 02 else gState=0xff;//错误无效 break; case 1: if(ch==0x03) gReceiveLen++;//FF 02 03 else gState=0xff; break; case 2: if(ch==0x01) gReceiveLen++;//FF 02 03 01 else if(ch==0x00) //FF 02 03 00 = 复位 { // HardReset();//接收到主机的复位指令(FF 02 03 00) // Uart_Printf("
Receive = FF 02 03 00"); // Uart_Printf("
Reset"); } //reset else gState=0xff; break; case 3: gReceiveID=ch; //FF 02 03 01 ID gReceiveLen++; break; case 4: if(ch==00) //FF 02 03 01 ID 00 {//查询网络型号指令(FF 02 03 01 ID 00) gMyTimeOut=0; gState=0xfe; //search net device if(gReceiveID==h_config->NetID) { // Delay(1); Send_485(SERIES,14); } } else gState=0xff; break; } } else if(gState!=0xff)//ID LEN Value {//接收到信息 gMyTimeOut=0;//when geting long data ,ignore the time out. ReceiveBuffer[gState++]=ch; if(gReceiveLen==gState) { // rULCON1=0x3b; //reset to get 00;//Parity forced as 0,Command end if(gReceiveID==h_config->NetID) //control me { gState=0xfe; JoinNum=ReceiveBuffer[1]; JoinNum<<=8; JoinNum+=ReceiveBuffer[0]; pTaskTail->JoinNum=JoinNum; if(JoinNum>1999) { Uart_SendByte(JoinNum); pTaskTail->Value=gReceiveLen-2; pTaskTail->pBuf=pStringBuf; for(i=0;i<gReceiveLen;i++) { *pStringBuf=ReceiveBuffer[i+2]; if(++pStringBuf==(StringBuf+1024))pStringBuf=StringBuf; } } else if(JoinNum>999) { Value=ReceiveBuffer[3]; Value<<=8; Value+=ReceiveBuffer[2]; pTaskTail->Value=Value; } else { Uart_SendByte(JoinNum); if(ReceiveBuffer[2]==0x00)Value=1; else Value=0; pTaskTail->Value=Value; } if(++pTaskTail==(Taskbuf+50))pTaskTail=Taskbuf; // TestVal[2] = executeevent.animatedTimer - TestVal[0]; } } } } } rSRCPND=BIT_UART1; rINTPND=BIT_UART1; rSUBSRCPND=BIT_SUB_RXD1;//清除RXD1,ERR1中断挂起标志位 } |