Data_Coolant.c 12.9 KB
Newer Older
1 2
#include "Data_Coolant.h"
#include "Components.h"
3
#include "Hysteresis.h"
4
#define FAST_TIME 15 //20*20 400ms变化一格
5
#define NORMAL_TIME 500 //500*20 10s变化一格
6
#define Disconnected_TIME 1 //掉线时间
7 8 9 10 11
#pragma ghs section  bss = ".myNonInitArea"
//_Data_Cng Data_Cng_Percent;
//_Data_Cng Data_Cng_Pressure;
Hys_uint8_t u8HysRamData[HYS_RAM_DATA_LEN];
#pragma ghs section  bss = default
12

13 14
DataCoolantTempStruct DataCoolantTemp ;
DataCoolantTempDisStruct DataCoolantTemp_Dis;
15

16
DataCoolantTempSamplerStruct DataResCoolantTempSampler ;
17

18 19 20 21
 uint8_t u8SelfCheckTime;
 uint8_t u8SelfCheckTime_Finish;
#define SelfCheckTime 60
uint16_t TEMP_SPEED;
22 23
uint8_t  Lost_Flag;
uint16_t Lost_Time;
24 25 26
uint16_t time_temp;
uint16_t time_power_on = 0;
uint16_t time_lost = 0;
27

28 29 30 31 32 33 34 35 36 37 38 39
const HYS_Seg_Table_st_t stHYSSegTableCng[9] =
{
	{0,529},		    
	{550, 609},   		
	{630, 689}, 		
	{710, 859}, 		
	{880, 939}, 		
	{960, 1019},
    {1040, 1099},
    {1120, 1179},
    {1200,0xFFFF}, 		
};
40

41 42 43 44 45 46 47
/*为了过QAC用的,实际完全不会使用*/
const HYS_Seg_Table_st_t stHYSSegTablePWM[3] =
{
	{0U, 40U},
	{50U, 60U},
	{70U, 100U},
};
48

49 50
const HYS_Attribute_st_t stHysAttribute[HYS_NUM_MAX] =
{
51
	{stHYSSegTableCng, 0U, 9U, 1U},
52 53
	{stHYSSegTablePWM, 0U, 3U, 1U},			/*为了过QAC用的,实际完全不会使用*/
};
54

55
uint16_t  Coolant_Res[Coolant_LEN] =
56
{
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    3000,
    1540,
    1510,
    1130,
    1100,
    990,
    960,
    850,
    820,
    680,
	650,
    500,
	470,
    410,
	380,
    230,
	200,
};
75

76
uint16_t  Coolant_Temp[Coolant_LEN] =
77
{
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    0,
    52,
    55,
    60,
	63,
    68,
    71,
    85,
    88,
    93,
    96,
    101,
    104,
    109,
    112,
    117,
    120,
} ;

/*
*  DataCoolantTemp.Temp_Value    当前温度值
*  DataCoolantTemp.u8Res_Valid   AD采集的数据是否有效
*  DataCoolantTemp.u8Res_ADValid AD采集是否有效
*  DataCoolantTemp.TEMPMode      此时水温表所在状态
*  DataCoolantTemp_Dis.u8_CurSeg 当前格数
*  DataCoolantTemp.TEMPSetp      目标个数
*/
void TEMP_KL30_Init ( void )
{
    DataCoolantTemp.Temp_Value = 0; 
    DataCoolantTemp.u8Can_Valid = 0;
    DataCoolantTemp.u16Can_Actual = 0;
    DataCoolantTemp.u8Res_Valid = 0 ;
    DataCoolantTemp.u16Res_ADValue  = 0;
    DataCoolantTemp.u8Res_ADValid  = 0;

    DataResCoolantTempSampler.Cnt = 0; 
    DataCoolantTemp.TEMPMode = 0;
    DataCoolantTemp_Dis.u8_CurSeg = 0;
    DataCoolantTemp.TEMPSetp = 0;
118
    DataCoolantTemp.u8_ResStatus = 0;
119
    DataCoolantTemp.u8_Last_ResStatus = 0;
120 121 122 123 124 125 126
    TEMP_SPEED = 0;
    time_power_on = 0;
    time_lost = 0;
    time_temp = 0;
    HYS_Init_st_t stHYSInit;
	stHYSInit.u8HYSNum = HYS_NUM_MAX;
    HYS_KL30_Init(&u8HysRamData[0], stHysAttribute, &stHYSInit);
127 128
}

129
void TEMP_KL15_Init ( void )
130 131
{

132 133 134 135 136 137
    DataCoolantTemp.Temp_Value = 0; 
    DataCoolantTemp.u8Can_Valid = 0;
    DataCoolantTemp.u16Can_Actual = 0;
    DataCoolantTemp.u8Res_Valid = 0 ;
    DataCoolantTemp.u16Res_ADValue  = 0;
    DataCoolantTemp.u8Res_ADValid  = 0;
138
    DataCoolantTemp.u8_ResStatus = 0;
139
    DataCoolantTemp.u8_Last_ResStatus = 0;
140 141 142 143 144 145 146 147 148 149 150 151 152 153
    DataCoolantTemp_Dis.u8_CurSeg = 0;
    DataResCoolantTempSampler.Cnt = 0;  
    DataCoolantTemp.TEMPMode = 0;
    TEMP_SPEED = 0;
    time_power_on = 0;
    time_lost = 0;
    time_temp = 0;
}

void Data_Coolant_Temp_Processing_Service ( void )
{
    uint16_t  Coolant_Temperature;
    uint32_t Temp;
    uint8_t  i;
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    Coolant_Temperature = Get_CAN_CH0_ID_101_Sig_ECU_Engine_Temperature();
    //can
    if (SYS_OPR_STAT_IGN_ON )
    {   
        //can
        if( (CAN_MSG_Status(&CAN_CH0_CanMsgOp, CAN_CH0_ID_CH0_101_Msg_Count) == CAN_SIG_LOST))
        {
            DataCoolantTemp.u8Can_Valid = 0;
            DataCoolantTemp.u16Can_Actual = 0;
        }
        else
        {
            if(Coolant_Temperature >= 0x6338)
            {
                DataCoolantTemp.u8Can_Valid = 0;
                DataCoolantTemp.u16Can_Actual = 0;
            }
            else
            {
                DataCoolantTemp.u8Can_Valid = 1;
                if(Coolant_Temperature >= 2730)
                {
                    Coolant_Temperature -= 2730; 
                }
                else if(Coolant_Temperature <= 2430)
                {
                    Coolant_Temperature =0; 
182
                    DataCoolantTemp.u8Can_Valid = 1; 
183 184 185 186 187 188 189 190 191 192
                }
                else
                {
                    Coolant_Temperature =0; 
                }
                DataCoolantTemp.u16Can_Actual = Coolant_Temperature;
            }
        }
    }
    else
193
    {
194 195
        DataCoolantTemp.u8Can_Valid = 0;
        DataCoolantTemp.u16Can_Actual = 0;  
196
    }
197 198
    //Res
    if(ADC_Read_Signal_Valid(ADC_CH_COOLANT_TEMP1_R) == 0)
199
    {
200 201 202 203 204 205 206 207 208
        DataCoolantTemp.u8Res_ADValid = 0;
        DataResCoolantTempSampler.Cnt = 0;
        DataCoolantTemp.u16Res_ADValue = 65535;
        DataCoolantTemp.u8Res_Flashtimer = 0;
    }
    else
    {
        DataCoolantTemp.u8Res_Flashtimer ++;
        if(DataCoolantTemp.u8Res_Flashtimer >= 5)
209
        {
210 211
            DataCoolantTemp.u8Res_Flashtimer = 5;
            if(DataCoolantTemp.u8Res_ADValid == 0)
212
            {
213 214 215 216 217 218 219 220
                DataCoolantTemp.u16Res_ADValue = ADC_Read_Signal(ADC_CH_COOLANT_TEMP1_R);
                DataCoolantTemp.u8Res_ADValid = 1;
            }
            else
            {
                DataResCoolantTempSampler.Buffer[DataResCoolantTempSampler.Cnt] = ADC_Read_Signal(ADC_CH_COOLANT_TEMP1_R);
                i = DataResCoolantTempSampler.Cnt;
                while ((i > 0) && (DataResCoolantTempSampler.Buffer[i] < DataResCoolantTempSampler.Buffer[i - 1]))
221
                {
222 223 224 225
                    Temp = DataResCoolantTempSampler.Buffer[i];
                    DataResCoolantTempSampler.Buffer[i] = DataResCoolantTempSampler.Buffer[i - 1];
                    DataResCoolantTempSampler.Buffer[i - 1] = Temp;
                    i--;
226
                }
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

                DataResCoolantTempSampler.Cnt++;
                if (DataResCoolantTempSampler.Cnt >= 5)
                {
                    DataResCoolantTempSampler.Cnt = 0;
                    Temp = (DataResCoolantTempSampler.Buffer[1]+DataResCoolantTempSampler.Buffer[2]+DataResCoolantTempSampler.Buffer[3])/3;
                     DataCoolantTemp.u16Res_ADValue = (uint16_t)(Temp);
                } 
            } 
        }
    }
    
    if((ADC_Read_Signal_Valid(ADC_CH_COOLANT_TEMP1_R) == 0)||(DataCoolantTemp.u8Res_Flashtimer < 5))
    {
        DataCoolantTemp.u8Res_Valid = 0 ;  
        DataCoolantTemp.u16Res_Actual = 0; 
        DataResCoolantTempSampler.ResOpen_ActTimer = 10;  
        DataResCoolantTempSampler.ResOpen_InActTimer = 25;  
    }
    else
    {
        if((DataCoolantTemp.u8Res_ADValid)&&(DataCoolantTemp.u16Res_ADValue >= 3000))
        {            
            if(DataResCoolantTempSampler.ResOpen_ActTimer < 10) 
            {
                DataResCoolantTempSampler.ResOpen_ActTimer++;   
            }  
254 255
            else
            {
256
                DataCoolantTemp.u8_Last_ResStatus = 3;//当前状态为开路
257 258 259 260 261 262 263 264 265 266
                DataCoolantTemp.u8Res_Valid = 0 ;  
                DataCoolantTemp.u16Res_Actual = 0 ; 
                DataResCoolantTempSampler.ResOpen_InActTimer = 0;  
            }   
        }
        else
        {  
            if(DataResCoolantTempSampler.ResOpen_InActTimer < 5)
            {
                DataResCoolantTempSampler.ResOpen_InActTimer++;
267
            }
268
            else
269
            {
270 271 272
                DataCoolantTemp.u8Res_Valid = 1 ; 
                DataResCoolantTempSampler.ResOpen_ActTimer = 0;
                if(DataCoolantTemp.u16Res_ADValue < 3000)
273
                {
274 275 276 277 278
                    DataCoolantTemp.u16Res_Actual = Get_Coolant_Res_To_Temp(DataCoolantTemp.u16Res_ADValue);
                }    
            }  
        }  
    }
279 280
    if(DataCoolantTemp.u8Can_Valid || DataCoolantTemp.u8Res_Valid )
    {
281
        DataCoolantTemp.u8_Last_ResStatus = 1;//当前状态正常
282 283
    }
    if (DataCoolantTemp.u8Can_Valid == 1)
284 285 286
    {
        DataCoolantTemp.Temp_Value = DataCoolantTemp.u16Can_Actual;
    }
287
    else if( DataCoolantTemp.u8Res_Valid == 1)
288 289
    {
        DataCoolantTemp.Temp_Value = DataCoolantTemp.u16Res_Actual;
290 291
        if(DataCoolantTemp.u16Res_ADValue < 40)
        {
292 293
            DataCoolantTemp.u8_Last_ResStatus = 2;//当前状态为短路
            DataCoolantTemp.TEMPMode = 2;
294
        }
295 296 297
    }
    else
    {
298
        if(DataCoolantTemp.u8_Last_ResStatus != 3)
299
        {
300
            DataCoolantTemp.u8_Last_ResStatus = 0;
301
        }
302 303 304 305
        DataCoolantTemp.Temp_Value = 0;
    }
    TEMP_Gauges_Cal();
    time_power_on++;
306
    TEMP_SPEED = NORMAL_TIME;
307 308 309 310 311 312 313
    if(time_power_on >= 100)//上电延时获取数据
    {
        time_power_on = 100;

        if(DataCoolantTemp.u8Can_Valid||DataCoolantTemp.u8Res_Valid)
        {
            time_lost = 0;
314
            if(DataCoolantTemp.TEMPMode ==3)//掉线
315
            {
316 317 318 319 320 321
                if(DataCoolantTemp.u8_ResStatus != 3)
                {
                    Lost_Flag = 1;
                    time_temp = 0;
                }

322
            }
323
            else if (DataCoolantTemp.TEMPMode == 2)
324
            {
325 326 327 328 329
                if(DataCoolantTemp.u8_ResStatus != 2)
                {
                    Lost_Flag = 1;
                    time_temp = 0;
                }
330
            }
331
            else if(DataCoolantTemp.TEMPMode == 0)//自检
332
            {
333
                DataCoolantTemp_Dis.u8_CurSeg = DataCoolantTemp.TEMPSetp;
334
            }
335

336
            DataCoolantTemp.TEMPMode = 1; 
337
        }
338
        else 
339
        {
340 341
            time_lost++;
            if(time_lost >= Disconnected_TIME)
342
            {
343 344 345 346
                time_lost = Disconnected_TIME;
                if(DataCoolantTemp.TEMPMode != 3)
                {
                    DataCoolantTemp.TEMPMode = 3;
347 348
                    time_temp = 0;
                    Lost_Flag = 1;
349 350
                }
                DataCoolantTemp.TEMPSetp = 0;
351 352 353
            }
        }
    }
354 355 356 357 358
    // if(DataCoolantTemp.u8_ResStatus==0)
    // {
    //     DataCoolantTemp.u8_ResStatus = DataCoolantTemp.u8_Last_ResStatus;
    // }
    if(DataCoolantTemp.u8_ResStatus != DataCoolantTemp.u8_Last_ResStatus)
359
    {
360 361
        Lost_Time++;
        if(Lost_Time > 150)
362
        {
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
            DataCoolantTemp.u8_ResStatus = DataCoolantTemp.u8_Last_ResStatus;
            Lost_Time = 0;
            Lost_Flag = 0;
            DataCoolantTemp_Dis.u8_CurSeg = DataCoolantTemp.TEMPSetp;
        }       
    }
    // if(Lost_Flag)
    // {
    //     Lost_Time++;
    //     if(Lost_Time > 150)
    //     {
    //         DataCoolantTemp.u8_ResStatus = DataCoolantTemp.u8_Last_ResStatus;
    //         Lost_Time = 0;
    //         Lost_Flag = 0;
    //         DataCoolantTemp_Dis.u8_CurSeg = DataCoolantTemp.TEMPSetp;
    //     }
    // }
380

381 382
    time_temp ++;
		
383 384

    if(time_temp>=TEMP_SPEED)
385
    {
386 387
        time_temp = 0;
        if(DataCoolantTemp.TEMPSetp > DataCoolantTemp_Dis.u8_CurSeg)
388
        {
389
            DataCoolantTemp_Dis.u8_CurSeg++;
390
        }
391
        else if(DataCoolantTemp.TEMPSetp < DataCoolantTemp_Dis.u8_CurSeg)
392
        {
393
            DataCoolantTemp_Dis.u8_CurSeg--;
394
        }
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    }        
}
//硬线数据
uint16_t Get_Coolant_Res_To_Temp(uint16_t Temp_Res)
{
    uint16_t TempValue = 0;

     TempValue = Get_Res_To_Temp(Coolant_Res, Coolant_Temp, Coolant_LEN, Temp_Res) ;   

    return TempValue*10;
}


void TEMP_Gauges_Cal(void)//计算目标格数
{

    DataCoolantTemp.TEMPSetp = 0;
    HYS_Physical_Quantity_Input(HYS_TEMP, DataCoolantTemp.Temp_Value);
    DataCoolantTemp.TEMPSetp =  HYS_Get_Seg(HYS_TEMP);
    if(DataCoolantTemp.TEMPSetp > 9 )
    {
        DataCoolantTemp.TEMPSetp = 0;
417
    }
418
    DataCoolantTemp.TEMPSetp = DataCoolantTemp.TEMPSetp - 1;
419 420
}

421
uint16_t Get_Res_To_Temp(uint16_t *ResBoard, uint16_t *TempBoard, uint8_t Len, uint16_t input)
422
{
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	uint8_t i = 0;
	uint16_t ret = 0;
	uint32_t temp = 0;
	if(input >= ResBoard[0])
	{
		ret = TempBoard[0];
	}
	else if(input <= ResBoard[Len - 1])
	{
		ret = TempBoard[Len - 1];
	}
	else
	{
        for(i = 0; i < (Len - 1); i++)
		{
			if((input < ResBoard[i]) && (input >= ResBoard[i + 1]))
			{
				temp = TempBoard[i + 1] - TempBoard[i];
				temp *= (ResBoard[i] - input);
				temp /= (ResBoard[i] - ResBoard[i + 1]);
				temp += TempBoard[i];
				ret = (uint16_t)temp;
				break;
			}
		}
	}
449

450 451 452 453 454 455 456 457 458 459 460 461
	return ret;
}

void Common_SelfCheckTime_Init(void)
{
    u8SelfCheckTime = 0;
	u8SelfCheckTime_Finish = 0;
}

void Common_SelfCheckTime(void)
{
    if (SYS_OPR_STAT_IGN_ON)
462
    {
463
        if (u8SelfCheckTime < SelfCheckTime)
464
        {
465 466
            u8SelfCheckTime++;
			u8SelfCheckTime_Finish = 0;
467
        }
468
        else
469
        {
470 471
            u8SelfCheckTime = SelfCheckTime;
			u8SelfCheckTime_Finish = 1;
472 473
        }
    }
474 475 476 477 478
    else
    {
        u8SelfCheckTime = SelfCheckTime;
		u8SelfCheckTime_Finish = 1;
    }
479 480 481
}


482
uint8_t GET_DataCoolantTempSegDisp(void)
483
{
484
   return DataCoolantTemp_Dis.u8_CurSeg;
485 486
}

487
uint16_t GET_DataCollantTempSegValid(void) 
488
{
489
   return DataCoolantTemp.u16Res_ADValue;//DataCoolantTemp_Dis.Dis_Valid;
490
}
491 492 493 494
uint16_t GET_DataCollantTempSegStatus(void) 
{
   return DataCoolantTemp.u8_ResStatus;//DataCoolantTemp_Dis.Dis_Valid;
}