#include"common.h"
#include "drv_adc.h"
#include "measure_temprature.h"

// 温度作为索引
#define T_END							(85u+40u+1u)//Maximum number of elements in the temperature table
#define B_VALUE
#ifndef B_VALUE
static const unsigned short T_PCB_Table[T_END] = 
{
	0xF30B,		//-40[degrees C]
	0xF25C,		//-39[degrees C]
	0xF1A4,		//-38[degrees C]
	0xF0E6,		//-37[degrees C]
	0xF021,		//-36[degrees C]
	0xEF54,		//-35[degrees C]
	0xEE7E,		//-34[degrees C]
	0xEDA1,		//-33[degrees C]
	0xECBD,		//-32[degrees C]
	0xEBCD,		//-31[degrees C]
	0xEAD7,		//-30[degrees C]
	0xE9D1,		//-29[degrees C]
	0xE8C7,		//-28[degrees C]
	0xE7AF,		//-27[degrees C]
	0xE68F,		//-26[degrees C]
	0xE565,		//-25[degrees C]
	0xE432,		//-24[degrees C]
	0xE2F6,		//-23[degrees C]
	0xE1B0,		//-22[degrees C]
	0xE061,		//-21[degrees C]
	0xDF07,		//-20[degrees C]
	0xDD9E,		//-19[degrees C]
	0xDC29,		//-18[degrees C]
	0xDAAC,		//-17[degrees C]
	0xD924,		//-16[degrees C]
	0xD793,		//-15[degrees C]
	0xD5F7,		//-14[degrees C]
	0xD452,		//-13[degrees C]
	0xD2A4,		//-12[degrees C]
	0xD0EB,		//-11[degrees C]
	0xCF29,		//-10[degrees C]
	0xCD54,		//-9[degrees C]
	0xCB76,		//-8[degrees C]
	0xC98D,		//-7[degrees C]
	0xC79D,		//-6[degrees C]
	0xC5A3,		//-5[degrees C]
	0xC3A2,		//-4[degrees C]
	0xC197,		//-3[degrees C]
	0xBF85,		//-2[degrees C]
	0xBD6D,		//-1[degrees C]
	0xBB49,		//0[degrees C]
	0xB919,		//1[degrees C]
	0xB6E0,		//2[degrees C]
	0xB4A4,		//3[degrees C]
	0xB261,		//4[degrees C]
	0xB015,		//5[degrees C]
	0xADC6,		//6[degrees C]
	0xAB78,		//7[degrees C]
	0xA91F,		//8[degrees C]
	0xA6C3,		//9[degrees C]
	0xA467,		//10[degrees C]
	0xA1FB,		//11[degrees C]
	0x9F93,		//12[degrees C]
	0x9D1F,		//13[degrees C]
	0x9AB2,		//14[degrees C]
	0x9847,		//15[degrees C]
	0x95D4,		//16[degrees C]
	0x9366,		//17[degrees C]
	0x90F3,		//18[degrees C]
	0x8E7D,		//19[degrees C]
	0x8C14,		//20[degrees C]
	0x899D,		//21[degrees C]
	0x8736,		//22[degrees C]
	0x84C6,		//23[degrees C]
	0x825B,		//24[degrees C]
	0x7FF8,		//25[degrees C]
	0x7D92,		//26[degrees C]
	0x7B32,		//27[degrees C]
	0x78D6,		//28[degrees C]
	0x7680,		//29[degrees C]
	0x742E,		//30[degrees C]
	0x71DC,		//31[degrees C]
	0x6F92,		//32[degrees C]
	0x6D4D,		//33[degrees C]
	0x6B11,		//34[degrees C]
	0x68DA,		//35[degrees C]
	0x66AC,		//36[degrees C]
	0x6483,		//37[degrees C]
	0x6264,		//38[degrees C]
	0x604C,		//39[degrees C]
	0x5E3A,		//40[degrees C]
	0x5C2E,		//41[degrees C]
	0x5A27,		//42[degrees C]
	0x582A,		//43[degrees C]
	0x5638,		//44[degrees C]
	0x544B,		//45[degrees C]
	0x5269,		//46[degrees C]
	0x508E,		//47[degrees C]
	0x4EBB,		//48[degrees C]
	0x4CF3,		//49[degrees C]
	0x4B31,		//50[degrees C]
	0x4977,		//51[degrees C]
	0x47C2,		//52[degrees C]
	0x4616,		//53[degrees C]
	0x4474,		//54[degrees C]
	0x42DC,		//55[degrees C]
	0x414C,		//56[degrees C]
	0x3FC3,		//57[degrees C]
	0x3E44,		//58[degrees C]
	0x3CCE,		//59[degrees C]
	0x3B5D,		//60[degrees C]
	0x39F3,		//61[degrees C]
	0x3894,		//62[degrees C]
	0x3738,		//63[degrees C]
	0x35E7,		//64[degrees C]
	0x349E,		//65[degrees C]
	0x335E,		//66[degrees C]
	0x3222,		//67[degrees C]
	0x30EF,		//68[degrees C]
	0x2FC6,		//69[degrees C]
	0x2EA2,		//70[degrees C]
	0x2D84,		//71[degrees C]
	0x2C6B,		//72[degrees C]
	0x2B59,		//73[degrees C]
	0x2A4D,		//74[degrees C]
	0x294C,		//75[degrees C]
	0x284D,		//76[degrees C]
	0x2756,		//77[degrees C]
	0x2665,		//78[degrees C]
	0x257C,		//79[degrees C]
	0x2496,		//80[degrees C]
	0x23B8,		//81[degrees C]
	0x22DD,		//82[degrees C]
	0x2205,		//83[degrees C]
	0x213A,		//84[degrees C]
	0x206E		//85[degrees C]
};
#else
static const unsigned short T_PCB_Table[T_END] = 
{
	0xF620,		//-40[degrees C]
	0xF582,		//-39[degrees C]
	0xF4DC,		//-38[degrees C]
	0xF42E,		//-37[degrees C]
	0xF378,		//-36[degrees C]
	0xF2B9,		//-35[degrees C]
	0xF1F1,		//-34[degrees C]
	0xF120,		//-33[degrees C]
	0xF045,		//-32[degrees C]
	0xEF61,		//-31[degrees C]
	0xEE73,		//-30[degrees C]
	0xED7A,		//-29[degrees C]
	0xEC77,		//-28[degrees C]
	0xEB69,		//-27[degrees C]
	0xEA51,		//-26[degrees C]
	0xE92D,		//-25[degrees C]
	0xE7FE,		//-24[degrees C]
	0xE6C4,		//-23[degrees C]
	0xE57E,		//-22[degrees C]
	0xE42C,		//-21[degrees C]
	0xE2CF,		//-20[degrees C]
	0xE166,		//-19[degrees C]
	0xDFF0,		//-18[degrees C]
	0xDE6F,		//-17[degrees C]
	0xDCE2,		//-16[degrees C]
	0xDB48,		//-15[degrees C]
	0xD9A3,		//-14[degrees C]
	0xD7F1,		//-13[degrees C]
	0xD634,		//-12[degrees C]
	0xD46A,		//-11[degrees C]
	0xD295,		//-10[degrees C]
	0xD0B4,		//-9[degrees C]
	0xCEC8,		//-8[degrees C]
	0xCCD1,		//-7[degrees C]
	0xCACF,		//-6[degrees C]
	0xC8C1,		//-5[degrees C]
	0xC6AA,		//-4[degrees C]
	0xC488,		//-3[degrees C]
	0xC25D,		//-2[degrees C]
	0xC028,		//-1[degrees C]
	0xBDE9,		//0[degrees C]
	0xBBA3,		//1[degrees C]
	0xB954,		//2[degrees C]
	0xB6FD,		//3[degrees C]
	0xB49F,		//4[degrees C]
	0xB23A,		//5[degrees C]
	0xAFCF,		//6[degrees C]
	0xAD5E,		//7[degrees C]
	0xAAE8,		//8[degrees C]
	0xA86D,		//9[degrees C]
	0xA5EE,		//10[degrees C]
	0xA36B,		//11[degrees C]
	0xA0E5,		//12[degrees C]
	0x9E5D,		//13[degrees C]
	0x9BD3,		//14[degrees C]
	0x9947,		//15[degrees C]
	0x96BB,		//16[degrees C]
	0x942E,		//17[degrees C]
	0x91A2,		//18[degrees C]
	0x8F17,		//19[degrees C]
	0x8C8C,		//20[degrees C]
	0x8A04,		//21[degrees C]
	0x877E,		//22[degrees C]
	0x84FB,		//23[degrees C]
	0x827C,		//24[degrees C]
	0x8000,		//25[degrees C]
	0x7D88,		//26[degrees C]
	0x7B16,		//27[degrees C]
	0x78A8,		//28[degrees C]
	0x763F,		//29[degrees C]
	0x73DD,		//30[degrees C]
	0x7181,		//31[degrees C]
	0x6F2B,		//32[degrees C]
	0x6CDC,		//33[degrees C]
	0x6A94,		//34[degrees C]
	0x6853,		//35[degrees C]
	0x661A,		//36[degrees C]
	0x63E8,		//37[degrees C]
	0x61BF,		//38[degrees C]
	0x5F9E,		//39[degrees C]
	0x5D85,		//40[degrees C]
	0x5B74,		//41[degrees C]
	0x596C,		//42[degrees C]
	0x576D,		//43[degrees C]
	0x5576,		//44[degrees C]
	0x5389,		//45[degrees C]
	0x51A4,		//46[degrees C]
	0x4FC8,		//47[degrees C]
	0x4DF4,		//48[degrees C]
	0x4C2A,		//49[degrees C]
	0x4A69,		//50[degrees C]
	0x48B0,		//51[degrees C]
	0x4701,		//52[degrees C]
	0x455A,		//53[degrees C]
	0x43BC,		//54[degrees C]
	0x4227,		//55[degrees C]
	0x409A,		//56[degrees C]
	0x3F16,		//57[degrees C]
	0x3D9A,		//58[degrees C]
	0x3C27,		//59[degrees C]
	0x3ABC,		//60[degrees C]
	0x3959,		//61[degrees C]
	0x37FE,		//62[degrees C]
	0x36AC,		//63[degrees C]
	0x3561,		//64[degrees C]
	0x341D,		//65[degrees C]
	0x32E2,		//66[degrees C]
	0x31AD,		//67[degrees C]
	0x3080,		//68[degrees C]
	0x2F5B,		//69[degrees C]
	0x2E3C,		//70[degrees C]
	0x2D24,		//71[degrees C]
	0x2C13,		//72[degrees C]
	0x2B09,		//73[degrees C]
	0x2A05,		//74[degrees C]
	0x2908,		//75[degrees C]
	0x2811,		//76[degrees C]
	0x2720,		//77[degrees C]
	0x2635,		//78[degrees C]
	0x2550,		//79[degrees C]
	0x2471,		//80[degrees C]
	0x2397,		//81[degrees C]
	0x22C2,		//82[degrees C]
	0x21F4,		//83[degrees C]
	0x212A,		//84[degrees C]
	0x2065		//85[degrees C]
};
#endif

DELAY_COMMON measure_temp;

int8_t ctr_temperature[THER_NUM_CON];

//const static unsigned char p_TempeNum= THER_NUM_CON;
uint8_t temp_index = 0;

void getTemperature(void)
{
	unsigned short count,adc_temp = 0;
	uint8_t ch_no = 0;
	
	Mea_Temp_Enable(1);
	temp_index = (++temp_index)%THER_NUM_CON;
	switch(temp_index)
	{
		case 0:
			ch_no = adc_temperatrue_0;
			break;
		case 1:
			ch_no = adc_temperatrue_1;
			break;
		default:
			temp_index = 0;
			ch_no = adc_temperatrue_0;
			break;	
	}

	
	// get adc value
	if(adc_getResult_6t(ch_no, &adc_temp) != 1)
	{
		goto error;
	}

	Mea_Temp_Enable(0);
	adc_temp <<= 4;

	// find 温度通过adc值查表
	for(count=(T_END-1u);count>0u;count--)
	{
		if(adc_temp <= T_PCB_Table[count] )
		{
			ctr_temperature[temp_index] = (int8_t)count - (int8_t)40;

			//adjust-start
			//if(thNo == 0)
			//res->Tadr -= 2;
			//adjust-end
				
			
			return;
		}
	}
	
error:	
	ctr_temperature[temp_index] = 0u;	
	
	return;
	
}
void Measure_Temprature_Initial(void)
{
	
	_Measure_Temprature_Initial();
	
	//
	measure_temp.set = 1;
	measure_temp.count = 0;

	memset(&ctr_temperature,0x00,sizeof(ctr_temperature));


	for(uint8_t i = 0; i < THER_NUM_CON;i++)
		getTemperature();
}





