Sub_Uart.c

#include "Sub_Sys.h"
/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
 Sys_Uart_Struct Uart1_Val;

//串口初始化
void Sub_Uart_Init(void) 
{
;
}
//串口通讯处理
void Sub_Uart1_Comm(void)
{
	
  static uint16_t Sub_Uart1_Comm_Cnt = 0;
	static uint32_t Comm_Error_Cnt = 0;//xg-230814N  uint16_t
	static uint16_t Rx_Data_Cnt = 0;
	static uint16_t Fault_Record_Reset_Cnt = 0;
	uint16_t Compressor_Run_Speed_Val;
	uint16_t CrcValue_Tmp;
	
	//发送读取数据协议
	if(Sub_Uart1_Comm_Cnt == 0)
	{
		RS485_TX;
		
	  Uart1_Val.Send_Data[0] = 0x01;
		Uart1_Val.Send_Data[1] = 0x03;
		Uart1_Val.Send_Data[2] = 0x07;
		Uart1_Val.Send_Data[3] = 0xD0;
		Uart1_Val.Send_Data[4] = 0x00;
		Uart1_Val.Send_Data[5] = 0x0B;
		Uart1_Val.Send_Data[6] = 0x04;
		Uart1_Val.Send_Data[7] = 0x80;
		
		Uart1_Val.Send_Data_Cnt = 1;
		Uart1_Val.Send_Data_Length = 8;
		Uart1_Val.Rev_Data_Cnt = 0;
		
		USART2_Send_Data(Uart1_Val.Send_Data_Length);
	}
	//发送写入数据协议
	else if(Sub_Uart1_Comm_Cnt == 500)
	{
		RS485_TX;
		
	  Uart1_Val.Send_Data[0] = 0x01;
		Uart1_Val.Send_Data[1] = 0x10;
		Uart1_Val.Send_Data[2] = 0x03;
		Uart1_Val.Send_Data[3] = 0xE8;
		Uart1_Val.Send_Data[4] = 0x00;
		Uart1_Val.Send_Data[5] = 0x06;
		Uart1_Val.Send_Data[6] = 0x0C;
		//故障记录清零
		Uart1_Val.Send_Data[7] = 0x00;
		Uart1_Val.Send_Data[8] = Sys_Run.Fault_Record_Reset;
		//运行使能
		Uart1_Val.Send_Data[9] = 0x00;
		Uart1_Val.Send_Data[10] = Sys_Run.Compressor_Run_Enable;
		//转速给定
		Compressor_Run_Speed_Val = Sys_Run.Compressor_Run_Speed*60;
		//最低运行频率
		if(Sys_Run.Compressor_Run_Speed < MenuF_Val.MenuF_Struct.MenuF_P_Compressor_Min_Speed)
		{
		  Compressor_Run_Speed_Val = MenuF_Val.MenuF_Struct.MenuF_P_Compressor_Min_Speed*60;
		}
		
		Uart1_Val.Send_Data[11] = Compressor_Run_Speed_Val>>8;
		Uart1_Val.Send_Data[12] = Compressor_Run_Speed_Val&0x00ff;
		//运行方向
		Uart1_Val.Send_Data[13] = 0;
		Uart1_Val.Send_Data[14] = 0;
		//外风机转速
		Uart1_Val.Send_Data[15] = 0;
		Uart1_Val.Send_Data[16] = Sys_Run.OUT_Door_Fan_Run_Enable;
		//内风机转速
		Uart1_Val.Send_Data[17] = 0;
		Uart1_Val.Send_Data[18] = Sys_Run.InDoor_Fan_Run_Enable;
		//校验
		CrcValue_Tmp = CrcValueByteCalc(Uart1_Val.Send_Data,19);
		Uart1_Val.Send_Data[19] = CrcValue_Tmp&0x00ff;
		Uart1_Val.Send_Data[20] = CrcValue_Tmp>>8;
		
		Uart1_Val.Send_Data_Cnt = 1;
		Uart1_Val.Send_Data_Length = 21;
    Uart1_Val.Rev_Data_Cnt = 0;
		USART2_Send_Data(Uart1_Val.Send_Data_Length);
	}
	//故障复位清零
	if(Sys_Run.Fault_Record_Reset == 1)
	{
	  if(Fault_Record_Reset_Cnt++ > 1500)
		{
			Sys_Run.Fault_Record_Reset = 0;
		  Fault_Record_Reset_Cnt = 0;
		}
	}
	else
	{
	  Fault_Record_Reset_Cnt = 0;
	}
	
	//通讯超时
	if(Comm_Error_Cnt < 90000)//180S  //xg-230814N 15000)//30S
	{
	  Comm_Error_Cnt++;
		
		if((0 == (Comm_Error_Cnt%2500)))//通讯中断后初始化 5S一次
		{
			USART2_Configuration();
			Uart1_Val.RS485_OK = 0;
		}
	}
	else
	{
		Uart1_Val.RS485_Error = 1;
	  Comm_Error_Cnt = 0;
		Uart1_Val.RS485_OK = 0;
	}
	//接受数据超时
	if(Uart1_Val.Rev_Data_Start)
	{
	  if(Rx_Data_Cnt < 150)
	  {
	    Rx_Data_Cnt++;
	  }
	  else
	  {
	    Uart1_Val.Rev_Data_Cnt = 0;
		  Uart1_Val.Rev_Data_Done = 0;
		  Uart1_Val.Rev_Data_Length = 8;
		  Uart1_Val.Rev_Data_Start = 0;
			Rx_Data_Cnt = 0;
	  }	  
	}
	else
	{
	  Rx_Data_Cnt = 0;
	}

	//读取数据成功
	if(Uart1_Val.Rev_Data_Done == 1)
	{
	  CrcValue_Tmp = CrcValueByteCalc(Uart1_Val.Rev_Data,Uart1_Val.Rev_Data_Length-2);
		
		if(((CrcValue_Tmp&0x00ff) == Uart1_Val.Rev_Data[Uart1_Val.Rev_Data_Length-2])\
			&& ((CrcValue_Tmp>>8) == Uart1_Val.Rev_Data[Uart1_Val.Rev_Data_Length-1]))
	  {
		  if(Comm_RX_Read_Cmd == Uart1_Val.Rev_Data[1])
			{
			  MenuC_Val.MenuC_Struct.MenuC_Compressor_Speed = (uint16_t)Uart1_Val.Rev_Data[3]*256+Uart1_Val.Rev_Data[4];//压缩机转速
				
				MenuC_Val.MenuC_Struct.MenuC_Error1 = (uint16_t)Uart1_Val.Rev_Data[5]*256+Uart1_Val.Rev_Data[6];//故障信息1
				
				MenuC_Val.MenuC_Struct.MenuC_Error2 = (uint16_t)Uart1_Val.Rev_Data[7]*256+Uart1_Val.Rev_Data[8];//故障信息2
				
				MenuC_Val.MenuC_Struct.MenuC_Warning = (uint16_t)Uart1_Val.Rev_Data[11]*256+Uart1_Val.Rev_Data[12];//警告信息
				
				MenuC_Val.MenuC_Struct.MenuC_Udc = (uint16_t)Uart1_Val.Rev_Data[13]*256+Uart1_Val.Rev_Data[14];//直流电压
				
				MenuC_Val.MenuC_Struct.MenuC_Compressor_Current = (uint16_t)Uart1_Val.Rev_Data[15]*256+Uart1_Val.Rev_Data[16];//相电流
				
				MenuC_Val.MenuC_Struct.MenuC_Defrost_Temp = ((uint16_t)Uart1_Val.Rev_Data[17]*256+Uart1_Val.Rev_Data[18])*10;//除霜温度
				
				MenuC_Val.MenuC_Struct.MenuC_Mos_Temp = (uint16_t)Uart1_Val.Rev_Data[19]*256+Uart1_Val.Rev_Data[20];//MOS温度
				
				MenuC_Val.MenuC_Struct.MenuC_Board_Temp = (uint16_t)Uart1_Val.Rev_Data[21]*256+Uart1_Val.Rev_Data[22];//柜内温度
			}
			Uart1_Val.RS485_OK = 1;
			Comm_Error_Cnt = 0;
			Uart1_Val.RS485_Error = 0;
		}
		else if(Comm_RX_Write_Multiple_CMD == Uart1_Val.Rev_Data[1])
		{
		  Comm_Error_Cnt = 0;
		}
		Uart1_Val.Rev_Data_Cnt = 0;
		Uart1_Val.Rev_Data_Done = 0;
		Uart1_Val.Rev_Data_Length = 8;
		Uart1_Val.Rev_Data_Start = 0;
		Rx_Data_Cnt = 0;
	}
	
	if(Sub_Uart1_Comm_Cnt < 1000)
	{
	  Sub_Uart1_Comm_Cnt++;
	}
	else
	{
	  Sub_Uart1_Comm_Cnt = 0;
	}
	
}
//UART1中断函数
void Uart1_IRQHandler(void)
{
	;
} 
//=====================================================================
//
// crc校验
// 注意,*data为字节,而非字
//
//=====================================================================
uint16_t CrcValueByteCalc(const uint8_t *data, uint16_t length)
{
    uint16_t crcValue = 0xFFFF;
    int i;
    while (length--)
    {
      crcValue ^= *data++;
			
      for (i = 8 - 1; i >= 0; i--)
      {
        if (crcValue & 0x0001)
        {
          crcValue = (crcValue >> 1) ^ 0xa001;
        }
        else
        {
          crcValue = crcValue >> 1;
        }
      }
    }
    return (crcValue);
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值