⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zdjjc.c

📁 织带机产量检测系统的监测终端。RS-485总线
💻 C
📖 第 1 页 / 共 2 页
字号:
	             break;
	        case 2:
	             break;
	        case 3:  //确认
				 //保存波特率
				 C24SetBuf (BAUD_RATE, &BaudIndex, 1);
	             EX1 = TRUE; //重开脉冲采样
	             displayBuf[6] = 0x08;
	             curKey = NO_KEY;
	             return;
           }
	       curKey = NO_KEY; 
	 }
}

void SetAddress (uchar uAddress)
{
	uchar idata CodeIndex[3] = {0, 0 ,0};  //段码下标,依次对应片3, 4, 5
	uchar data Index = 3;  //片选下标
	uchar i;
	uint Value = 0;

	//显示地址
	displayBuf[0] = SEVEN_BLK1[10];
	IntToLED (uAddress, 5, FALSE);

	//换算得到当前值的下标
	for (i=5; i>2; i--)
	{
		CodeIndex[i-3] = uAddress % 10 + 1;
		uAddress = uAddress / 10;
	} 

	while (TRUE)
	{
	       Delay (100, 100);
		   switch (curKey)
	       {
	        case 0:  //光标移动
	             Index ++;
			     if (Index > 5)
			         Index = 3;
			     displayBuf[6] = 0x08 >> (Index - 3);    
	             break;
	        case 1:  //数字翻动
	             displayBuf[Index] = SEVEN_BLK1[CodeIndex[Index-3]++];
	             CodeIndex[Index - 3] = CodeIndex[Index - 3] % 10;
	             break;
	        case 2:
	             break;
	        case 3:  //确认
			     //计算地址
				 for (i=0; i<3; i++)
				     Value = Value * 10 + CodeIndex[i] - 1;
				 if (Value > 0 && Value < 248)
				 {
				     //保存地址
				     Address = Value;
				     C24SetBuf (ADDRESS, &Address, 1);
				 }
				 else
				 {
				     for (i=0; i<5; i++)
					 {
				          displayBuf[i+1] = ErrorHint[i]; 
					 }
					 Delay (100, 350);
				 }	  
				 curKey = NO_KEY;
				 SetBaud ();
				 return;
           }
	       curKey = NO_KEY; 
	}

}

void KeyProc ()
{
     if (curKey !=NO_KEY)
	 {
		 Delay (100, 100);
		 switch (curKey)
		 {
		 case 0:
		        ET0 = FALSE;
		        PulseAct = 0;
		        RunStatus = TRUE; 	        
				displayBuf[6] = 0x01;
		        break;
	     case 1:
	            ET0 = FALSE;
	            PulseCount = 0; 
	            PulseTotal = 0;
		        RunStatus = FALSE;
		        displayBuf[6] = 0x02;
		        break;
		 case 2:
		        ET0 = FALSE; //关闭脉冲采样
		        EX1 = FALSE; 
		        displayBuf[6] = 0x04;
		        //进入设置状态
		        SetAddress (Address);
		        break;
		 case 3:
		        EX1 = TRUE; //重开脉冲采样
		        displayBuf[6] = 0x08;
		        break;
		 }
		 curKey = NO_KEY;
	 }
}

void SaveCurrData () reentrant
{ 
	 uchar data CurrData[10];
 	 uchar data Time[7];

	 //保存当前班数据   
	 //织带长度
	 CurrData[0] = RunLength.b[0];
	 CurrData[1] = RunLength.b[1];
	 //织带累计时间
	 CurrData[2] = RunTime.b[0];
	 CurrData[3] = RunTime.b[1];
	 //调试带长度
	 CurrData[4] = DebugLength.b[0];
	 CurrData[5] = DebugLength.b[1];
	 //调试带累计时间
	 CurrData[6] = DebugTime.b[0];
	 CurrData[7] = DebugTime.b[1];
	 //异常脉冲次数 
	 CurrData[8] = PulseCheat.b[0];
	 CurrData[9] = PulseCheat.b[1];

	 C24SetBuf(RECORD_ROOT+RecordIndex*32, CurrData, 10);

	 //累计停机次数
	 CurrData[0] = 0;
	 CurrData[1] = curMotorStopCount;
	 //累计停机时间
	 CurrData[2] = curMotorStopTimeLength.b[0];
	 CurrData[3] = curMotorStopTimeLength.b[1];
	 //经线断次数
	 CurrData[4] = JingCutCount;
	 //经线断时间
	 CurrData[5] = JingCutTime;
	 //纬线断次数
	 CurrData[6] = WeiCutCount;
	 //纬线断时间
	 CurrData[7] = WeiCutTime;
	 //底线断次数
	 CurrData[8] = DiCutCount;
	 //底线断时间
	 CurrData[9] = DiCutTime;

	 C24SetBuf(RECORD_ROOT+RecordIndex*32+10, CurrData, 10); 

	 //卷绕断次数
     CurrData[0] = JuanCutCount;
	 //卷绕断时间
	 CurrData[1] = JuanCutTime;

	 //班开始结束时间
	 C24GetBuf (GROUP_TIME+GroupIndex*4, Time, 4);
	 CurrData[2] = Time[0];
	 CurrData[3] = Time[1];
	 CurrData[4] = Time[2];
	 CurrData[5] = Time[3];
	 //当班日期
	 v_Get1302(Time); 
	 CurrData[6] = Time[3];
	 CurrData[7] = Time[4];
	 CurrData[8] = Time[5];
	 CurrData[9] = Time[6];

	 C24SetBuf(RECORD_ROOT+RecordIndex*32+20, CurrData, 10); 
}


void UartInt(void) interrupt 4
{
	 if (RI)
	 {
		 RI = FALSE;
		 IsReceive= TRUE;
		 UartBuf[Rindex] = SBUF;   //写缓冲区
		 if (Rindex == 1)
		 {
		     //读命令
		     if (UartBuf[1] == 0x80 ||
			     UartBuf[1] == 0x82 ||
				 UartBuf[1] == 0x85) 
				 UartLength = 4;      
			  
			 if (UartBuf[1] == 0x84 ||
			     UartBuf[1] == 0x87)
				 UartLength = 5;    

			 if (UartBuf[1] == 0x81 ||
			     UartBuf[1] == 0x83)
			     UartLength = 22; 

		     if (UartBuf[1] == 0x86)
			     UartLength = 11;
				 
				 
			 if (UartBuf[1] == 0x88)
			     UartLength = 6;	 
		 }

		 Rindex ++;
		
		 if (Rindex == UartLength)
		 {
			 Rindex = 0;
			 RecOver = TRUE;
		 }
	 }

	 if (TI)
	 {
		 TI = FALSE; 
		 
		 EN_SEND = TRUE;
		 
		 Tindex ++;
		 
		 if (Tindex < UartLength)
		 {
		     SBUF = UartBuf[Tindex];
			 TrasOver = FALSE;
		 }
		 else
		 {
			 EN_SEND = FALSE;
			 TrasOver = TRUE;
			 RecOver = FALSE;
			 Tindex = 0;
		 }     
	 }
}

void ModbusProcess(void)
{
     uchar data QueryIndex;
     uchar idata *pBuf;
     uchar idata Time[7];
	 uchar data RxAddress;
	 WordType CRC;

	 REN = FALSE;
	 if (UartBuf[0] == Address || 
	     UartBuf[0] == 0 ||
		 UartBuf[0] == 0xFF)  //比较地址
	 {
		 //计算校验码
		 CRC.w = CRC16(UartBuf, UartLength-2);
		 if (CRC.b[0] == UartBuf[UartLength-2] &&		  //比较校验码
		     CRC.b[1] == UartBuf[UartLength-1])
	     {
		     RxAddress = UartBuf[0];  //保存旧地址 
			 UartBuf[0] = Address;    //写地址

			 Tindex = 0;
			 
			 //判断命令号
			 switch (UartBuf[1])
			 {
			 case 0x80:
			       pBuf = UartBuf+2;
			      //读交班时间
				  C24GetBuf (GROUP_TIME, pBuf, 17);
				  UartLength = 22;
				  //写班号
				  UartBuf[19] = GroupIndex;
			      break;
			 case 0x81:
			      pBuf = UartBuf+2; 
			      //写交班时间
			      C24SetBuf (GROUP_TIME, pBuf, 17);
			      UartLength = 4;
			      break;
			 case 0x82:
			      pBuf = UartBuf+2;
			      //读当前设备各数据
				  C24GetBuf (TERM_H, pBuf, 18);
				  UartBuf[6] = 0x00;
				  if (!motorStatus)
				      UartBuf[6] |= 0x01;
				  if (RunStatus)
				      UartBuf[6] |= 0x02;
				  UartLength = 22;
			      break;
		     case 0x83:
		          pBuf = UartBuf+4; 
		          //写当前设备各数据
		          C24SetBuf (KVALUE_H, pBuf, 2);   //写K值
				  pBuf = pBuf+3; 
				  C24SetBuf (BAUD_RATE, pBuf, 1);   //写波特率
				  pBuf = pBuf+1;
				  C24SetBuf (ADDRESS, pBuf, 5);   //写地址等
		          UartLength = 4;
		          GetEEPromData ();  //重新读数据
				  InitBaud(); 
			      break;
			 case 0x84:
			      if (UartBuf[2] > 15)
			          goto MODBUSOVER;
			      
			      pBuf = UartBuf+3;

				  if (UartBuf[2] == 0)  //当前班记录
				  {
					  //送当前班数据   
	                  //织带长度
	                  UartBuf[3] = RunLength.b[0];
	                  UartBuf[4] = RunLength.b[1];
                 	  //织带累计时间
	                  UartBuf[5] = RunTime.b[0];
	                  UartBuf[6] = RunTime.b[1];
	                  //调试带长度
	                  UartBuf[7] = DebugLength.b[0];
	                  UartBuf[8] = DebugLength.b[1];
	                  //调试带累计时间
	                  UartBuf[9] = DebugTime.b[0];
	                  UartBuf[10] = DebugTime.b[1];
	                  //异常脉冲次数 
	                  UartBuf[11] = PulseCheat.b[0];
	                  UartBuf[12] = PulseCheat.b[1];
	                  //累计停机次数
	                  UartBuf[13] = 0;
	                  UartBuf[14] = curMotorStopCount;
	                  //累计停机时间
	                  UartBuf[15] = curMotorStopTimeLength.b[0];
	                  UartBuf[16] = curMotorStopTimeLength.b[1];
					  //经线断次数
					  UartBuf[17] = JingCutCount;
					  //经线断时间
					  UartBuf[18] = JingCutTime;
					  //纬线断次数
					  UartBuf[19] = WeiCutCount;
					  //纬线断时间
					  UartBuf[20] = WeiCutTime;
					  //底线断次数
					  UartBuf[21] = DiCutCount;
					  //底线断时间
					  UartBuf[22] = DiCutTime;
					  //卷绕断次数
					  UartBuf[23] = JuanCutCount;
					  //卷绕断时间
					  UartBuf[24] = JuanCutTime;
	                  //班开始结束时间
	                  C24GetBuf (GROUP_TIME+GroupIndex*4, Time, 4);
	                  UartBuf[25] = Time[0];
	                  UartBuf[26] = Time[1];
	                  UartBuf[27] = Time[2];
	                  UartBuf[28] = Time[3];
	                  //当班日期
	                  v_Get1302(Time); 
	                  UartBuf[29] = Time[3];
	                  UartBuf[30] = Time[4];
	                  UartBuf[31] = Time[5];
	                  UartBuf[32] = Time[6];
	                  QueryIndex = RecordIndex;
				  }
				  else
				  {
			          if (RecordIndex < UartBuf[2])
			              QueryIndex = RecordIndex + RECORDCOUNT - UartBuf[2]; 
			          else
			              QueryIndex = RecordIndex - UartBuf[2]; 

			          //读指定班数据
				      C24GetBuf (RECORD_ROOT + QueryIndex*32, pBuf, 30);	
			      } 
				  UartBuf[2] = QueryIndex;
				  UartLength = 35;
			      break;
			 case 0x85:
			      pBuf = UartBuf+2;
			      //读当前设备时间
				  v_Get1302 (pBuf);
				  UartLength = 11;
			      break;
			 case 0x86:
			      pBuf = UartBuf+2; 
			      //写当前设备时间
				  v_Set1302 (pBuf);
				  UartLength = 4;
			      break;
			 case 0x87:
			      BaudIndex = UartBuf[2];
				  C24SetBuf (BAUD_RATE, &BaudIndex, 1);
				  InitBaud(); 
				  UartLength = 4;
				  break;
		     case 0x88:
			      pBuf = UartBuf+2;
			      uKValue.b[0] = UartBuf[2];
				  uKValue.b[1] = UartBuf[3];
				  C24SetBuf (KVALUE_H, pBuf, 2);
				  UartLength = 4;
				  break;
			 default:
			      goto MODBUSOVER;
			 }

			 if (RxAddress != 0 &&
			     UartBuf[0] == Address) 
			 {
			     //计算CRC校验码		   
			     CRC.w = CRC16(UartBuf, UartLength-2);
			     UartBuf[UartLength-2] = CRC.b[0];
			     UartBuf[UartLength-1] = CRC.b[1];
			     //发送一字节,触发连续发送中断
				 EN_SEND = TRUE;
			     SBUF = UartBuf[0];
			 }
		 }
	 }
MODBUSOVER:
     REN = TRUE;	
	 RecOver = FALSE;
}

void CutCheck (void)	 //断线检测
{
     if (RunStatus)
	 {
	     if (JingStatus && (!JING_PIN))
	     {
	         JingCutCount ++;
	     }
	 
	     if (WeiStatus && (!WEI_PIN))
	     {
	         WeiCutCount ++;
	     }
	 
	     if (DiStatus && (!DIXI_PIN))
	     {
	         DiCutCount ++;
	     }
	 
	     if (JuanStatus && (!JUAN_PIN))
	     {
	         JuanCutCount ++;
	     }

	     if (!JING_PIN)
	     {
	         displayBuf[6] |= 0x08;
	     }

	     if (!WEI_PIN)
	     {
	         displayBuf[6] |= 0x10;
	     }
		
	     if (!DIXI_PIN)
	     {
	         displayBuf[6] |= 0x20;
	     }

	     if (!JUAN_PIN)
	     {
	         displayBuf[6] |= 0x40;
	     }

	     JingStatus = JING_PIN;
	     WeiStatus = WEI_PIN;
	     DiStatus = DIXI_PIN;
	     JuanStatus = JUAN_PIN;	 
	 }
}

uchar BCDToBin (uchar uBCD)
{
     uchar uBin;
	 
	 uBin = ((uBCD & 0xF0) >> 4) * 10 + (uBCD & 0x0F);
	 return uBin;
}

void CountTime (void)
{
	 uchar idata Time[7];
	 uchar SubMin;
	 //读第一次时间
	 if (!TimeStatus)
	 {
	     v_Get1302 (Time);
		 FirstMin = BCDToBin(Time[1]);
		 TimeStatus = TRUE;  
	 }
	 else
	 {
	     v_Get1302 (Time);
		 NextMin = BCDToBin(Time[1]);

		 if (NextMin >= FirstMin)
		     SubMin = NextMin - FirstMin;
		 else
			 SubMin = 60 - FirstMin + NextMin;

		 if (SubMin != 0)
		 {
		     if (RunStatus)
			 {
	             RunTime.w += SubMin;
				 
				 if (MOTOR)
				     curMotorStopTimeLength.w += SubMin;
			 }
	         else
	             DebugTime.w += SubMin;
				 
			 if (!JING_PIN)
	         {
		         JingCutTime += SubMin;
				 if (JingCutCount == 0)
				     JingCutCount = 1;
	         }

	         if (!WEI_PIN)
	         {
	             WeiCutTime += SubMin;	
				 if (WeiCutCount == 0)
				     WeiCutCount = 1;
	         }
		
		     if (!DIXI_PIN)
	         {
	             DiCutTime += SubMin;
				 if (DiCutCount == 0)
				     DiCutCount = 1;
	         }

	         if (!JUAN_PIN)
	         {
	             JuanCutTime += SubMin;
				 if (JuanCutCount == 0)
				     JuanCutCount = 1;
	         }	
			    
			 TimeStatus = FALSE;
		 }
		
	 }	
}

void main ()
{
	 uchar idata TimeBuf[7];
	 uchar data i, j;

	 RstStatus = RST_PIN;
	
	 InitParam ();
	 
	 while (TRUE)
	 {	  
	      if (IsReceive)
          {
	          for (i=0; i<TIMEOUT; i++)
	               for (j=0; j<TIMEOUT; j++)
	               {
	                    if (RecOver)
			            { 
			                ModbusProcess();
			       	        break;
		       	        }  
		          }
		      IsReceive = FALSE;
		      Rindex = 0; 	   	    
          }
		  
		  if (KeyFlag)
	      {
	          KeyProc();
	          KeyFlag = FALSE;
          } 
	      
	      if (GroupSaveCount >= 100)
	      {
	          //换班
	          GroupSaveCount = 0;
	          v_Get1302 (TimeBuf);
	          ChangeGroup(TimeBuf[2], TimeBuf[1]);	  	  
          } 

		  CutCheck (); 
		  CountTime (); 
     }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -