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

📄 ddcjc.c

📁 电动车测试平台控制系统。自动测量电动车各项参数
💻 C
📖 第 1 页 / 共 3 页
字号:
				      if (ShiftIndex == 8)
		                  ShiftIndex = 4; 
					  DataIndex = CodeIndex[ShiftIndex - 4];
					  break;
				  case 8:
				      if (ShiftIndex == 8)
		                  ShiftIndex = 6; 
					  DataIndex = CodeIndex[ShiftIndex - 4];
				      break;
				  case 12:	
				      if (ShiftIndex == 8)
					      ShiftIndex ++;
				      if (ShiftIndex == 17)
					      ShiftIndex = 5;
					  DataIndex = UartBuf[ShiftIndex]; 
				      break;
				  default:
		              if (ShiftIndex == 8)
		                  ShiftIndex = 5; 
					  DataIndex = CodeIndex[ShiftIndex - 4];
					  break;
				  }
	          }
		  }
		  else
		  {
		      InitUart ();
			  Printer(0);
		  }
		  break;  
	 default:		
  	      break; 
	 }
	 return ~P2;
}

void SendLED (uchar *DataBuf)
{
     uchar data i, j;
	 uchar data mid;
     
	 _nop_ ();	 
	 
	 for (i=0; i<3; i++)
	 {
          for (j=0; j<8; j++)
          { 
		       mid = 0x80 >> j;
               if (DataBuf[i] & mid)
                   DAT_595 = HIGH;
               else
                   DAT_595 = LOW;
               _nop_ ();
               CLK_595 = LOW;
               _nop_ ();
               CLK_595 = HIGH;
               _nop_ ();
          }		  
	 }	 
	 
	 SEL_595 = (DispIndex == 8) ? LOW:HIGH;	

	 STB_595 = LOW;
     _nop_ ();
	 STB_595 = HIGH; 
	 _nop_ (); 
     DAT_595 = HIGH; //数据线保持高电平
     _nop_ ();	 	 
}     

void GetDateTime ()
{
     uchar i;
	 uchar cTemp, cBaseYear;
	 //读年月日时分秒
	 Set8583Byte (PCF8583_CTR, 0x40);
	 Get8583Data (SECOND, UartBuf, 5);
	 Set8583Byte (PCF8583_CTR, 0x00);
	 Get8583Data (BASE_YEAR, &cBaseYear, 1);
     
	 //初始化显示下标
	 UartBuf[17] = UartBuf[0] & 0x0F;		         
	 UartBuf[16] = UartBuf[0]>>4 & 0x0F;		     //秒
	 UartBuf[15] = UartBuf[1] & 0x0F; 
	 UartBuf[14] = UartBuf[1]>>4 & 0x0F;             //分 
	 UartBuf[13] = UartBuf[2] & 0x0F;
	 UartBuf[12] = UartBuf[2]>>4 & 0x0F;             //时
	  	
	 cTemp = GetDate (UartBuf[3]);				     //日  
	 UartBuf[11] = cTemp & 0x0F;
	 UartBuf[10] = cTemp>>4 & 0x0F; 

	 cTemp = GetMonth (UartBuf[4]);		             //月
	 UartBuf[9] = cTemp & 0x0F;
	 UartBuf[7] = cTemp>>4 & 0x0F;

	 cTemp = GetYear (UartBuf[3]) + cBaseYear*4;     //年 
	 UartBuf[6] = cTemp & 0x0F;
	 UartBuf[5] = (cTemp>>4) & 0x0F;  
	 UartBuf[4] = 0;
	 UartBuf[3] = 2; 
	 //换算到LED
	 for (i=3; i<18; i++)
	 {
	      if (i == 8)
		      i++;
		  DispBuf[i] = LED_CODE[UartBuf[i]];
	 }	     
}

void Display ()												        
{											   
    uchar data595[3];

	//计算显示数据
	data595[0] = 0x01 << DispIndex;
	
	if (DispIndex == 0)
	{
	    data595[0] = 0x01 << DispIndex;
		data595[2] = 0x00;//DispBuf[DispIndex];
	    data595[1] = 0x00;//DispBuf[DispIndex+9];
		SendLED (data595);
	} 
	
	data595[0] = 0x01 << DispIndex; 
	
	if (IsShine)
	{
	    if (ShiftIndex < 9)
		{
		    if (ShiftIndex == DispIndex)
			{
				if (ShineTime < 5)
				    data595[2] = 0;
				else if (ShineTime < 10)
				{
				     data595[2] = DispBuf[DispIndex];
				}
				ShineTime ++;
				ShineTime %= 10;
			}
			else
			    data595[2] = DispBuf[DispIndex];
	        data595[1] = DispBuf[DispIndex+9];	
		}
		else 
		{
		    if (ShiftIndex == DispIndex + 9)
			{
		    	if (ShineTime < 5)
				    data595[1] = 0;
				else if (ShineTime < 10)
				{
				    data595[1] = DispBuf[DispIndex+9];	
				}
				ShineTime ++;
				ShineTime %= 10;
			}
			else
				data595[1] = DispBuf[DispIndex+9];	 
	        data595[2] = DispBuf[DispIndex];	
		}	
	}
	else
	{  
	    data595[2] = DispBuf[DispIndex];
	    data595[1] = DispBuf[DispIndex+9];	 
	}
	
	//SEL_595 = (DispIndex == 8) ? LOW:HIGH;	

	SendLED (data595);

	if (DispIndex == 7)
	{
	    data595[0] = 0x01 << DispIndex;
		data595[2] = 0x00;//DispBuf[DispIndex];
	    data595[1] = 0x00;//DispBuf[DispIndex+9];
		SendLED (data595);
	}
		
    //计算下一次显示下标
	DispIndex ++;
    DispIndex = DispIndex % DISPLAY_BUF_LEN;
}

void IntToLED (uint uData, uchar uLength, uchar uDotPos, bit bIsDot, bit bClear, uchar StartIndex)   //数据转换到LED
{
     char i; 

	 for (i=uLength; i>=0; i--)
	 {
	      DispBuf[StartIndex+i] = LED_CODE[uData % 10];
	      uData /= 10;
     }

     if (bClear)
	 {
	     //对高位0消影
         while (++i < uDotPos)
	     {
			if (DispBuf[StartIndex+i] == LED_CODE[0]) 
			{
			    DispBuf[StartIndex+i] = 0x00;  
			}
			else break;	  
	     }
	 }
	 if (bIsDot)
	     DispBuf[uDotPos+StartIndex] |= 0x80;
}

void InitT2 ()     //T2复用,用于计时
{
     //初始化定时器2
	 T2CON = 0x00;
	 TH2 = TH2_VAL;
	 TL2 = TL2_VAL;
	 RCAP2H = TH2_VAL;
	 RCAP2L = TL2_VAL;	 
	 TR2 = TRUE;
	 ES = FALSE;
	 PT2 = TRUE;
	 ET2 = TRUE;
}

void InitUart ()
{
     //初始化定时器2,用于通信
	 T2CON = 0x30;	 
	 RCAP2H = 0xFF;	  //9600
	 RCAP2L = 0xDC;
	 ET2 = FALSE; 
	 TR2 = TRUE;
	 //PS = FALSE;
	 SCON = 0xC0;     //9位
	 ES = TRUE;       //中断允许位

	 PrintIndex = 0;
}

void GetVoltage () //读电压
{
     uchar i, j;
	 uint AD0; 	   //AD0数据
	 dword ADTotal = 0;

	 read2543 (0);
	 
	 for (j=0; j<8; j++)
	 {
		  AD0 = 0;
		  for (i=0; i<15; i++)
		  {
		       AD0 += read2543 (0);
		  }
		  AD0 /= 15;
		  ADTotal += AD0;
		 
	 }
	 
	 ADTotal /= 8;
			
	 Voltage = ADTotal * 375 / 256;
	
	 if (Voltage < OptVoltage)
	     bVoltAlarm = TRUE;
	 else
	     bVoltAlarm = FALSE;
							   
	 if (CheckStatus == UNDER_STATUS)
	 {
		 if (Current < 6 &&
		     !bUndered)
         {
		     MinVoltage = Voltage;
			 bUndered = TRUE;

			 if (ModeIndex == 2)
			 {
			     TRIP_PIN = FALSE;   //取消测试
	         	 EX1 = FALSE;        //停止脉冲记数
				 ET0 = FALSE;
				 ET2 = FALSE; 
				 CheckStatus = END_STATUS;

				 IntToLED (DisTheory, 3, 3, FALSE, TRUE, 0);
				 //显示欠压值
                 IntToLED (MinVoltage, 3, 1, TRUE, TRUE, 4); 
				 //显示过流电流
		         IntToLED (0, 2, 1, TRUE, TRUE, 9);
				 IntToLED (MaxRate, 2, 1, TRUE, TRUE, 12);

				 if (PrinterIndex == AUTO_PRINT)
				 {
				     InitUart ();
				     Printer(0);
				 }
			  }
		 }
	 }      
}

void GetCurrent () //读电流
{
     uchar i;
	 uint AD1 = 0; 	   //AD0数据

	 read2543 (1);
	 
	 for (i=0; i<15; i++)
	 {
	      AD1 += read2543 (1);
	 }
	 
	 AD1 /= 15;

     Current = (dword)AD1 * 750 / 1024;	
	 
	 //分段补偿
	 
	 if (Current <= 100)
	 {
	     Current =  Current + 50;
	 }
	 else if (Current < 300)
	 {
	     Current = ((Current-100) * 1940 / 200 + 1490) / 100; 
	 }
	 else if (Current < 500)
	 {
	     Current = ((Current-300) * 1920 / 200 + 3430) / 100;
	 }
	 else if (Current < 1000)
	 {
	     Current = ((Current-500) * 4810 / 500 + 5340) / 100;
	 }
	 else if (Current < 1500)
	 {
	     Current = ((Current-1000) * 4850 / 500 + 10160) / 100;
	 }
	 else if (Current < 2000)
	 {
	     Current = ((Current-1500) * 4830 / 500 + 15010) / 100;
	 }
	 else if (Current < 2500)
	 {
	     Current = ((Current-2000) * 4840 / 500 + 19840) / 100;
	 }
	 else if (Current < 2900)
	 {
	     Current = ((Current-2500) * 3880 / 400 + 24680) / 100;
	 }
	 else if (Current < 2970) 
	 {
	     Current = Current - 50;   
	 }
	 else if(Current < 2983)
	 {
	     Current = Current - 40;
	 }
	 else if(Current < 2985)
	 {
	     Current = Current - 20;
	 }
	 
	 Current /= 10;

	 if (Current > OptCurrent)
         bCurrAlarm = TRUE;
	 else
	     bCurrAlarm = FALSE;
	   
	 if (CheckStatus == OVER_STATUS)
	 {
	     if (Current > MaxCurrent)
	         MaxCurrent = Current;
	 }	    
}

void GetPrintData(uint iData, uchar uLen, uchar uDotPos, bit bVFlag)   //准备打印数据
{
     char i;
	 char j;

	 if (bVFlag)
	 {
	     for (i=uLen; i>0; i--)
		 {  
			  if (i != uDotPos)
			  {
			      
				  UartBuf[15+i] = iData % 10 + 0x30;
				  iData /= 10;
			  }
			  else
			  {
			      UartBuf[15+i] =  0x2E;  
			  } 
		 }

		 if (UartBuf[17] == 0xB0)
		 {
		     UartBuf[16] = 0xA1;
			 UartBuf[17] = 0xA1;
		 }
	 }
	 else
	 {
		 for (i=uLen; i>0; i--)
		 { 
			  j = i * 2;
			  
			  if (i != uDotPos)
			  {
			      
				  UartBuf[10+j] = iData % 10 + 0xB0;
				  UartBuf[10+j-1] = 0xA3;  	 
				  iData /= 10;
			  }
			  else
			  {
			      UartBuf[10+j] =  0xAE;
				  UartBuf[10+j-1] = 0xA3;  
			  } 
		 }

		 if (UartBuf[12] == 0xB0 && uDotPos <= uLen)
		 {
		     UartBuf[11] = 0xA1;
			 UartBuf[12] = 0xA1;
		 }
	 }
}

void PrinterByte(byte Data)     //打印机忙等待
{
     uint i, j; 
	 
	 for (i=0; i<256; i++)
	      for (j=0; j<2000; j++)
	 {
	     if (!PRT_BUSY)
		 {   
		     SBUF = Data;
		     return;
		 }
	 }

	 //打印失败,取消打印
	 ES = FALSE;
	 PT1 = FALSE;
}

void Printer (uchar uIndex)    //打印文字
{
     //uchar TimeBuf[6];
	 uchar cBaseYear, cTemp;

	 //准备打印命令
	 //打印“换行”
	 UartBuf[0] = 0x0A;

	 switch (uIndex)
	 {
	 case 0:
			 //欠压保护
			 UartBuf[1] = 39 + 0xA0;
			 UartBuf[2] = 23 + 0xA0;
			 UartBuf[3] = 49 + 0xA0;
			 UartBuf[4] = 25 + 0xA0;
			 UartBuf[5] = 17 + 0xA0;
			 UartBuf[6] = 3 + 0xA0;
			 UartBuf[7] = 27 + 0xA0;
			 UartBuf[8] = 4 + 0xA0;
			 UartBuf[9] = 0xA3;
	         UartBuf[10] = 0xBA;
			 if (ModeIndex != 1 &&
			     ModeIndex != 3)
			 { 
			     //写电压值
			     GetPrintData(MinVoltage, 5, 3, FALSE);
			     UartBuf[21] = 0x20;
			     UartBuf[22] = 0xA3;
	             UartBuf[23] =  0xD6;
			     UartLength = 24;
			 }
			 else
			     UartLength = 11; 
			 break;
	 case 1:
	         //过流保护
			 UartBuf[1] = 25 + 0xA0;
			 UartBuf[2] = 93 + 0xA0;
			 UartBuf[3] = 33 + 0xA0;
			 UartBuf[4] = 87 + 0xA0;
			 UartBuf[5] = 17 + 0xA0;
			 UartBuf[6] = 3 + 0xA0;
			 UartBuf[7] = 27 + 0xA0;
			 UartBuf[8] = 4 + 0xA0;
			 UartBuf[9] = 0xA3;
	         UartBuf[10] = 0xBA;
			 if (ModeIndex != 2 &&
			     ModeIndex != 3)
			 {
			     //写电流
			     GetPrintData(MaxCurrent, 4, 3, FALSE);
			     UartBuf[19] = 0x20;
			     UartBuf[20] = 0xA3;
	             UartBuf[21] = 0xC1;
			     UartLength = 22;
			 }
			 else
			   	 UartLength = 11;
	         break;
	 case 2:
	         //最高车速
			 UartBuf[1] = 55 + 0xA0;
			 UartBuf[2] = 78 + 0xA0;
			 UartBuf[3] = 24 + 0xA0;
			 UartBuf[4] = 63 + 0xA0;
			 UartBuf[5] = 19 + 0xA0;
			 UartBuf[6] = 21 + 0xA0;
			 UartBuf[7] = 43 + 0xA0;
			 UartBuf[8] = 57 + 0xA0;
			 UartBuf[9] = 0xA3;
	         UartBuf[10] = 0xBA;
			 //写车速
			 UartBuf[11] = 0x1B;
			 UartBuf[12] = 0x36;
			 UartBuf[13] = 0x1B;
			 UartBuf[14] = 0x56;
			 UartBuf[15] = 0x02;

			 GetPrintData(MaxRate, 4, 3, TRUE);
			 
			 UartBuf[20] = 0x20;
			 UartBuf[21] = 0x4B;
	         UartBuf[22] = 0x6D;

			 UartBuf[23] = 0x2F;
             UartBuf[24] = 0x48;

			 UartBuf[25] = 0x1B;
	         UartBuf[26] = 0x38;

			 UartBuf[27] = 0x1B;
			 UartBuf[28] = 0x56;
			 UartBuf[29] = 0x01;
			 UartLength = 30;
	         break;
	 case 3:
	         //启动时间
			 UartBuf[1] = 38 + 0xA0;
			 UartBuf[2] = 84 + 0xA0;
			 UartBuf[3] = 22 + 0xA0;
			 UartBuf[4] = 15 + 0xA0;
			 UartBuf[5] = 42 + 0xA0;
			 UartBuf[6] = 17 + 0xA0;
			 UartBuf[7] = 28 + 0xA0;
			 UartBuf[8] = 68 + 0xA0;
			 UartBuf[9] = 0xA3;
	         UartBuf[10] = 0xBA;
			 //写时间
			 GetPrintData(StartTime/2, 4, 3, FALSE);
			 UartBuf[19] = 0x20;
			 UartBuf[20] = 35 + 0xA0;
	         UartBuf[21] = 75 + 0xA0;
			 UartLength = 22;
	         break;
	 case 4:
	         //日期:
			 UartBuf[1] = 40 + 0xA0;
			 UartBuf[2] = 53 + 0xA0;
			 UartBuf[3] = 38 + 0xA0;
			 UartBuf[4] = 58 + 0xA0;
			 UartBuf[5] = 0xA3;
	         UartBuf[6] = 0xBA;
			 UartBuf[7] = 0x1B;
			 UartBuf[8] = 0x56;
			 UartBuf[9] = 0x02;

			 Get8583Data(BASE_YEAR, &cBaseYear, 1); 
	         Get8583Data (MINUTE, TimeBuf, 4);
			 
	         cTemp = GetYear (TimeBuf[2]) + cBaseYear*4;   //年
			 UartBuf[10] = ((cTemp>>4) & 0x0F) + 0x30;   
			 UartBuf[11] = (cTemp & 0x0F) + 0x30;
			 UartBuf[12] = 0x2D;
			 cTemp = GetMonth (TimeBuf[3]);         //月
			 UartBuf[13] = (cTemp>>4 & 0x0F) + 0x30;
			 UartBuf[14] = (cTemp & 0x0F) + 0x30;
			 UartBuf[15] = 0x2D;
			 cTemp = GetDate (TimeBuf[2]);          //日
			 UartBuf[16] = (cTemp>>4 & 0x0F) + 0x30;  
			 UartBuf[17] = (cTemp & 0x0F) + 0x30;
			 UartBuf[18] = 0x20;
			 UartBuf[19] = (TimeBuf[1]>>4 & 0x0F) + 0x30;  //时	 
			 UartBuf[20] = (TimeBuf[1] & 0x0F) + 0x30;
			 UartBuf[21] = 0x3A;
			 UartBuf[22] = (TimeBuf[0]>>4 & 0x0F) + 0x30;  //分	 
			 UartBuf[23] = (TimeBuf[0] & 0x0F) + 0x30;
             
			 UartBuf[24] = 0x1B;
			 UartBuf[25] = 0x56;
			 UartBuf[26] = 0x01;
			 UartLength = 27;
			 break;
	 case 5: //测试编号:
	         UartBuf[1] = 18 + 0xA0;
			 UartBuf[2] = 66 + 0xA0;
			 UartBuf[3] = 42 + 0xA0;
			 UartBuf[4] = 52 + 0xA0;
			 UartBuf[5] = 17 + 0xA0;
			 UartBuf[6] = 64 + 0xA0;
			 UartBuf[7] = 26 + 0xA0;
			 UartBuf[8] = 37 + 0xA0;
			 UartBuf[9] = 0xA3;
	         UartBuf[10] = 0xBA;

			 GetPrintData(RecordIndex, 4, 5, FALSE);
			 UartLength = 19;
	         break;
	 case 6:
			 //虚线
			 for (cTemp=1; cTemp<25; cTemp++)
			 {
				  UartBuf[cTemp] = 0xA9;	  
			 }
			 UartLength = 25;
	         break;
	 case 7:
	         //标题
			 UartBuf[1] = 21 + 0xA0;	   //电
			 UartBuf[2] = 71 + 0xA0;

			 UartBuf[3] = 22 + 0xA0;	   //动
			 UartBuf[4] = 15 + 0xA0;

			 UartBuf[5] = 55 + 0xA0;	   //自
			 UartBuf[6] = 52 + 0xA0;

			 UartBuf[7] = 48 + 0xA0;	   //行
			 UartBuf[8] = 48 + 0xA0;
										   
			 UartBuf[9] = 19 + 0xA0;	   //车
			 UartBuf[10] = 21 + 0xA0;

			 UartBuf[11] = 48 + 0xA0;	   //性
			 UartBuf[12] = 52 + 0xA0;

			 UartBuf[13] = 36 + 0xA0;	   //能
			 UartBuf[14] = 60 + 0xA0;

			 UartBuf[15] = 18 + 0xA0;	   //测
			 UartBuf[16] = 66 + 0xA0;

			 UartBuf[17] = 42 + 0xA0;	   //试
			 UartBuf[18] = 52 + 0xA0;

			 UartBuf[19] = 17 + 0xA0;	   //报

⌨️ 快捷键说明

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