#include "lab.h"
#include "main.h"
#include "onewire.h"
#include "math.h"
#include "img.h"
#include "process2.h"

uint8_t LCDNoAutoUpdate	= 0;

uint32_t RC5_sticky;

uint8_t STOP = 0;

uint16_t Power_Rect 	= 790;
uint16_t Power;
uint32_t Triac_Power_sum;
//float 	 Corect_Power = 3.11;
float 	 Corect_Power = 1;

float sum_PWM_Print=0;//!!!!!!!!!!

uint16_t sum_PWM[30000];

uint8_t hour_set = 0x00;
uint8_t min_set;
uint8_t sec_set;
uint8_t hour_golov_timer = 10;
uint8_t type_selection_golov = 0;

uint8_t hour_delta  = 0;
uint8_t min_delta   = 0;
uint8_t sec_delta   = 0;

uint32_t sec_lcd = 0; 
uint8_t  sec_set  	= 0;
uint32_t sec_start = 0; 

uint32_t glob_interval;
uint32_t glob_interval2;

uint32_t glob_interval_set = 90; 
uint32_t glob_interval_def;
uint32_t glob_interval_def_set = 30;
uint32_t counter; 

uint32_t var_cache_LCD[100];
uint32_t var_cache_WEB[100];
WEB_struct WEB_Arr[50];

uint8_t LCD_Update = 0; 
uint8_t First_WEB = 0;

float Tdef         	= 0;
float T_Att_Dim		 	= 0;
float T_Att_Aqua   	= 0;
float Tn           	= 0;
float T_3cm      		= 0;

float T_10cm      	= 0;
float T_Par      	  = 0;
float T_NBK_Set     = 93.00;
float T_10cm_Maxx    = 0;

float Tkyb         	= 0;

float KI_Motor        = 0;
float KP_Motor        = 0;
float error_I_Motor;
float error_P_Motor;

float	KI_Power 				= 0.015;
float KP_Power        = 2;
float error_I_Power;
float error_P_Power;

float KI_Dimrot = 0.02;//0.03
float KP_Dimrot = 1.2;//1.15
float error_P_Dimrot;
float error_I_Dimrot = 150;

uint8_t nDTdef;
uint8_t nDTkyb;
uint8_t nDTattention;



typedef struct Temp{	
	 float *OW_t;
   uint8_t id[8];
	 uint8_t number;
}temp;

temp T_d_Att_Dim_OW ={&T_Att_Dim,	{0x28,0x27,0x3D,0x10,0x05,0x00,0x00,0x48},0}; 
temp Tdef_OW 				={&Tdef,			{0x28,0x90,0x17,0xF4,0x03,0x00,0x00,0x19},0}; 
temp T_Att_Dim_OW 	={&T_Att_Dim,	{0x28,0x75,0xDF,0xAA,0x03,0x00,0x00,0x0A},0}; 
temp T_Att_Aqua_OW 	={&T_Att_Aqua,{0x28,0xF1,0x01,0xAB,0x03,0x00,0x00,0x4F},0}; 
temp Tn_OW 					={&Tn,				{0x28,0xBC,0xE2,0xAA,0x03,0x00,0x00,0x6B},0}; 
temp T_10cm_OW 			={&T_10cm,		{0x28,0xFF,0x61,0x19,0x63,0x04,0x00,0x45},0}; 
temp T_Par_OW 			={&T_Par,			{0x28,0x1F,0x17,0xF4,0x03,0x00,0x00,0xD7},0}; 
temp Tkyb_OW 				={&Tkyb,			{0x28,0xFF,0xAD,0x0F,0x63,0x04,0x00,0x77},0}; 

typedef struct end_CD{	
	uint16_t x; 
	uint16_t y;
	char *str;
}End_LCD;

End_LCD End_LCD_Print[4]; 

float Tattention    		  	= 50;

float Tdef_set_dist      		= 95.00;
float Tkyb_set_dist_END  		= 98.8;

float Tdef_set_rect_default = 74.00;
float Tdef_set_rect_hol     = 75.84;
float Tdef_set_rect_podghol = 76.38;
float Tdef_set_rect_hvst    = 77.0;

float Theating_elements_Att = 100.3;
uint32_t ADC_Conc_spirt_Att	= 2500;

float Alcohol            		= 0;

float Corect_Alcohol_NBK        = 3.54;
float Corect_Alcohol_Kyb        = 5.95;

uint16_t NO_Att = 0;

uint8_t TIMER_250;
uint8_t Timer_flag;

uint8_t OCR_CEL_Start = 85;
uint8_t OCR_CEL_End   = 10;
float Tkyb_set_rect_End  		= 92;
float Tkyb_set_rect_Start		= 86;

uint8_t OCR_PWM      = 0;
uint8_t OCR_PWM_hist = 0;

uint8_t OCR_CEL      = 0;
uint8_t OCR_Motor       = 0;

uint8_t body_head       = 0;
uint8_t stop_start      = 0;
uint8_t auto_hand       = 0;
uint8_t mode_proc       = 0;
uint8_t mode_proc_hist  = 0;
uint8_t mode            = 0;

uint8_t END             = 0;
uint8_t Lock_Menu       = 0;
uint8_t mode_setting  	= 0;
uint8_t current_mode   	= 255;

uint8_t EditMenu 			= 255;
uint8_t EditMenu_hist = 255;
uint8_t Disable_LCD = 0;

uint16_t ADC_Press_Set = 170;
uint16_t ADC_Press;
uint32_t ADC_Press_long;
uint16_t ADC_Conc_spirt;

uint8_t Water_start       = 0;
uint8_t Water_Attention2  = 0;

static void TIMER_change();
static void RC5_val(RC5Frame_TypeDef);

static void general_process();
static void distillation_process();
static void rectification_process();
static void NBK_process();
static void change_mode(uint8_t mode, uint8_t update_lcd);
static float Get_Alcohol(float f_Temperature);
static float Get_Alcohol_Par(float f_Temperature);//!!!!!
static void Attention();
static void ReadTemperature();
static void Key(uint8_t key);
static void WaterControl();
static uint8_t sensor_test();

static void Change_OCR_PWM(int16_t OCR_PWM_Change);
static void Change_OCR_CEL(int16_t OCR_CEL_Change);
static void Change_MOTOR_CEL(int16_t OCR_CEL_Change);

static void SET_OCR_PWM(uint8_t OCR_PWM_SET);
static void SET_OCR_CEL(uint8_t OCR_CEL_SET);
static void SET_OCR_MOTOR(uint32_t OCR_MOTOR_SET);

static void Update_OCR(void);
static void Timer_Update(void);
static void Hot_STOP(uint8_t mode, uint32_t frequence);
static void Save();
static void Load();
static void RC5();
static void PID_NBK(float error);
static void PID_Power();

const uint32_t arr_spirt[125][3] = {
{10000,0,0},{9981,32,318},{9962,65,769},{9943,97,950},{9924,129,1257},{9905,160,1558},{9886,191,1848},{9867,223,2124},{9848,255,2378},{9829,287,2612},{9810,318,2798},{9757,380,3146},
{9706,439,3476},{9654,500,3768},{9602,561,4040},{9550,621,4296},{9498,682,4538},{9446,740,4643},{9394,800,4952},{9342,858,5133},{9290,917,5287},{9185,1058,5640},{9105,1200,5905},{9047,1336,6708},
{8990,1473,6301},{8945,1605,6470},{8892,1737,6621},{8850,1863,6737},{8813,1991,6851},{8776,2117,6954},{8745,2237,7051},{8710,2359,7141},{8677,2476,7227},{8646,2593,7303},{8620,2707,7379},
{8596,2820,7444},{8573,2931,7508},{8550,3039,7568},{8528,3146,7622},{8510,3251,7671},{8490,3354,7716},{8472,3456,7757},{8455,3556,7790},{8436,3654,7818},{8420,3751,7847},{8416,3845,7862},
{8392,3938,7900},{8378,4031,7925},{8365,4120,7946},{8350,4209,7663},{8337,4296,7983},{8325,4381,8000},{8312,4466,8020},{8300,4550,8037},{8290,4631,8054},{8278,4791,8086},{8255,4944,8116},
{8243,5093,8145},{8228,5238,8170},{8216,5378,8199},{8205,5515,8224},{8194,5647,8252},{8185,5775,8279},{8175,5899,8307},{8164,6020,8333},{8155,6137,8357},{8145,6251,8381},{8134,6362,8402},
{8125,6470,8425},{8115,6575,8448},{8105,6677,8468},{8095,6777,8489},{8087,6874,8511},{8075,6968,8531},{8066,7060,8550},{8057,7149,8568},{8048,7236,8587},{8039,7321,8606},{8029,7404,8628},
{8025,7485,8646},{8018,7564,8667},{8010,7641,8665},{8003,7716,8707},{7996,7790,8727},{7989,7861,8747},{7984,7932,8770},{7977,8000,8793},{7975,8067,8815},{7971,8132,8839},{7965,8196,8864},
{7960,8258,8887},{7955,8318,8911},{7950,8378,8937},{7944,8436,8962},{7940,8493,8981},{7935,8549,9012},{7930,8604,9039},{7927,8657,9063},{7920,8709,9087},{7915,8760,9111},{7910,8810,9135},
{7905,8859,9158},{7900,8906,9781},{7895,8954,9204},{7889,8999,9225},{7886,9043,9250},{7880,9087,9274},{7876,9131,9298},{7871,9173,9320},{7866,9213,9343},{7860,9254,9366},{7857,9293,9388},
{7853,9332,9412},{7850,9370,9435},{7847,9407,9462},{7844,9442,9486},{7840,9478,9513},{7837,9512,9541},{7834,9547,9750},{7831,9579,9599},{7828,9612,9626},{7824,9644,9654},{7821,9676,9678},
{7818,9706,9707},{7815,9717,9717}};

uint8_t size_mas;

Menu const *MenuTime;

Mode_proc_lcd start_menu[] = {
	{"Пуск"},{"Стоп"}};

Mode_proc_lcd NBK_menu[] = {
	{"Нагр"},{"Реж "},{"Вiдб"},};

Mode_proc_lcd rect_menu[] = {
	{"Нагр"},{"Реж "},{"Гол "},{"Тiло"},{"Хв-т"},{"Прод"}};

Mode_proc_lcd mode_menu[] = {
	{"Дист"},{"Рект"},{"НБК "},};

Mode_proc_lcd auto_menu[] = {{"Авто"},{"Ручн"}}; 

Mode_proc_lcd type_selection_gol_menu[] = {{"Пара"},{"Рiдн"}}; 

uint8_t show_def_00[7]     = {1,1,0,0,0,0};
uint8_t show_def_Gol[7]    = {0,0,1,0,0,0};
uint8_t show_def_Rect[7]   = {0,0,0,1,0,0};
uint8_t show_def_Hvost[7]  = {0,0,0,0,1,0};

uint8_t show_Tn[7]     = {0,1,1,1,1,1,1};
uint8_t show_Tv[7]     = {1,0,0,0,0,1,1};

Menu const MenuVarNBK[] = {
	{&auto_menu,&auto_hand,2,5,3,"",1,1,0,1},{&NBK_menu,&mode,2,5,33,"",1,1,0,2},{&mode_menu,&mode_proc,3,5,63,"",1,1,1,2},{&start_menu,&stop_start,2,5,93,"",1,1,0,1},{&NO_Att,&NO_Att,16,5,123,"!%-3.0f",0,1,NULL,NULL,NULL,COLOR_BACKGRAUND,COLOR_RED},	
	{&ADC_Conc_spirt,&ADC_Conc_spirt,32,5,153,"%-4.0f",0,1,0},
	{&T_Par,&T_NBK_Set,0,99,3,"%2.2f",1,0.06,0,120,NULL,NULL,0x8000},
	{&Alcohol,&Alcohol,0,99,27,"%%%2.1f",0,0,NULL,NULL,NULL,NULL,0x8000},
	{&T_10cm,&T_10cm_Maxx,0,100,110,"%2.2f",1,0.06,0,120,NULL,NULL,0x8000},
	{&Tkyb,&Tkyb,0,98,200,"%2.2f",0,0.06,0,120,NULL,NULL,0x8000},{&hour_delta,&hour_golov_timer,8,5,187,"%02.0f",0},{&min_delta,&min_delta,8,40,185,":%02.0f",0},{&hour,&hour_set,8,5,215,"%02.0f",0,1,0,24},{&min,&min_set,8,40,215,":%02.0f",0,1,0,60},
	{&Power,&Triac_Power_Val,16,190,200,"%4.0f",1,1,0,1100,NULL,0xFD50},
	{&OCR_Motor,&OCR_Motor,8,270,70,"%-3.0f",1,1,0,MAX_MOTOR,NULL,NULL,0x18B4},{&ADC_Press,&ADC_Press,16,270,117,"%-3.0f",0,1,0,400,NULL,COLOR_BACKGRAUND,0x04A0},
	{NULL,NULL,0,0,0,0,0,0},
};

Menu const MenuVarRect[] = {
	{&auto_menu,&auto_hand,2,5,3,"",1,1,0,1},{&rect_menu,&mode,2,5,33,"",1,1,0,5},{&mode_menu,&mode_proc,3,5,63,"",1,1,1,2},{&start_menu,&stop_start,2,5,93,"",1,1,0,1},{&NO_Att,&NO_Att,16,5,123,"!%-3.0f",0,1,NULL,NULL,NULL,COLOR_BACKGRAUND,COLOR_RED},	
	{&ADC_Conc_spirt,&ADC_Conc_spirt,32,5,153,"%-4.0f",0,1,0},
	{&Tdef,&Tdef_set_rect_default,0,99,3,"%2.2f",1,0.06,0,120,show_def_00,NULL,0x8000},
	{&Tdef,&Tdef_set_rect_hol,0,99,3,"%2.2f",1,0.06,0,120,show_def_Gol,NULL,0x8000},
	{&Tdef,&Tdef_set_rect_default,0,99,3,"%2.2f",1,0.06,0,120,show_def_Rect,NULL,0x8000},
	{&Tdef,&Tdef_set_rect_hvst,0,99,3,"%2.2f",1,0.06,0,120,show_def_Hvost,NULL,0x8000},
	//{&KP_Dimrot,&KP_Dimrot,0,80,30,"%1.2f",1,0.01,0,9,NULL,NULL,0x8000},
	{&KP_Power,&KP_Power,0,80,30,"%1.2f",1,0.01,0,8,NULL,NULL,0x8000},
	{&OCR_CEL,&OCR_CEL,8,140,53,"%2.0f",1,1,0,100,NULL,NULL,0x18B4},
	//{&KI_Dimrot,&KI_Dimrot,0,80,93,"%1.3f",1,0.001,0,2,NULL,NULL,0x8000},	
	{&KI_Power,&KI_Power,0,80,93,"%1.3f",1,0.001,0,2,NULL,NULL,0x8000},	
	{&Tn,&Tkyb_set_rect_Start,0,99,120,"%2.2f",1,0.06,0,120,NULL,NULL,0x8000},
	{&Tkyb,&Tkyb_set_rect_End,0,98,200,"%2.2f",1,0.06,0,120,NULL,NULL,0x8000},{&hour_delta,&hour_golov_timer,8,5,187,"%02.0f",0},{&min_delta,&min_delta,8,40,187,":%02.0f",0},{&hour,&hour_set,8,5,215,"%02.0f",0,1,0,24},{&min,&min_set,8,40,215,":%02.0f",0,1,0,60},
	{&Alcohol,&Alcohol,0,185,160,"%%%2.1f",0,1,NULL,NULL,NULL,0xFD50},{&Power,&Triac_Power_Val,16,190,200,"%4.0f",1,1,0,1100,NULL,0xFD50},
	{&OCR_PWM,&OCR_PWM,8,270,5,"%-3.0f",1,1,0,200,NULL,NULL,0x18B4},{&OCR_Motor,&OCR_Motor,8,270,60,"%-3.0f",1,1,0,MAX_MOTOR,NULL,NULL,0x18B4},{&ADC_Press,&ADC_Press_Set,16,270,117,"%-3.0f",1,1,0,400,NULL,COLOR_BACKGRAUND,0x04A0},
	{NULL,NULL,0,0,0,0,0,0},
};

Menu const MenuSettingRect[] = {
	{&Tdef_OW,&Tdef_OW.number,1,3,0,"Тдеф[%d]=%2.2f",1,NULL,1,OW_DEVICE_COUNT},
	{&Tn_OW,&Tn_OW.number,1,3,25,"Тниз.кол.[%d]=%2.2f",1,NULL,1,OW_DEVICE_COUNT},
	{&Tkyb_OW,&Tkyb_OW.number,1,3,50,"Ткуб[%d]=%2.2f",1,NULL,1,OW_DEVICE_COUNT}, {&Corect_Alcohol_Kyb,&Corect_Alcohol_Kyb,0,3,100,"Кор.алк.=%2.1f",1,0.1,0,100,NULL,NULL},
	{&T_Att_Dim_OW,&T_Att_Dim_OW.number,1,3,75,"!Тдеф[%d]=%2.2f",1,NULL,1,OW_DEVICE_COUNT},
	{&T_Att_Aqua_OW,&T_Att_Aqua_OW.number,1,3,100,"Тспирт[%d]=%2.2f",1,NULL,1,OW_DEVICE_COUNT},
	{&Tattention,&Tattention,0,3,125,"%2.2f",1,0.06,0,120,NULL,NULL,0x8000},
	{&NO_Att,&NO_Att,1,150,123,"!%-3.0f",1,1,NULL,NULL,NULL,COLOR_BACKGRAUND,COLOR_RED},
	{&type_selection_gol_menu,&type_selection_golov,2,3,175,"",1,1,0,1},
	{NULL,NULL,0,0,0,0,0,0},
};

Menu const MenuSettingNBK[] = {
	{&T_Par_OW,&T_Par_OW.number,1,3,0,"Тпари[%d]=%2.2f",1,NULL,1,OW_DEVICE_COUNT},
	{&T_10cm_OW,&T_10cm_OW.number,1,3,25,"Т10 см.[%d]=%2.2f",1,NULL,1,OW_DEVICE_COUNT},
	{&Tkyb_OW,&Tkyb_OW.number,1,3,50,"Ткуб[%d]=%2.2f",1,NULL,1,OW_DEVICE_COUNT},
	{&Corect_Alcohol_NBK,&Corect_Alcohol_NBK,0,3,75,"Кор.алк.=%2.1f",1,0.1,0,100,NULL,NULL},
	{&Corect_Power,&Corect_Power,0,3,100,"Кор.потужн.=%2.2f",1,0.01,0,100,NULL,NULL},
	{NULL,NULL,0,0,0,0,0,0},
};
	
Menu const MenuVarDist[] = {
	{&stop_start,&stop_start,2,1,1,"",1,1,0,1},{&mode,&mode,2,1,6,"",1,1,0,3},{&mode_proc,&mode_proc,2,1,11,"",1,1,1,2},{&auto_hand,&auto_hand,2,1,16,"",1,1,0,1},{&Lock_Menu,&Lock_Menu,8,1,21,"%-1.0f",0,1,0,2},{&NO_Att,&NO_Att,16,1,23,"!%-3.0f",0,0,1},	
	{&OCR_PWM,&OCR_PWM,8,2,1,"Ai%-3.0f",1,1,0,200},{&ADC_Press,&ADC_Press,32,2,8,"P%-3.0f",0,0,1},
	{&Tkyb,&Tkyb,0,4,1,"K%2.2f",1,0.06,0,120},{&Alcohol,&Alcohol,0,4,8,"Ar%2.2f",0,0,1},
	{NULL,NULL,0,0,0,0,0,0},
};



void main2(void)
{	
	OCR_Motor = 0;
	sum_PWM[0] = 0xFFFE;//!!!!!
	
	End_LCD_Print[0].str = NULL;	End_LCD_Print[1].str = NULL;	End_LCD_Print[2].str = NULL;	End_LCD_Print[3].str = NULL;
	
	
	Update_OCR();
		
	Load();
	change_mode(0,1);
	
	sec_lcd = 0;	 		

	
  while(1)
  {		
		RC5();		
    ethernetif_input(&gnetif);
    sys_check_timeouts();		
    if (TIMER_250) TIMER_change();					
    if (Timer_flag) Timer_Update();    
    //		#ifdef USE_DHCP
    //				DHCP_Periodic_Handle(&gnetif);
    //		#endif 		
  }   
}


void PrintToLCD(uint8_t full_update, uint8_t WEB){	
  uint16_t i = 0, n = 0, m = 0, WEB_i = 1;  
  uint8_t* show, tmp_uint8_t; 
  uint32_t Triac_Power_tmp, tmp_uint32;
  float tmp_float, float_Power;
	uint32_t* var_cache;
	
	static uint8_t LCD_EditMenu_hist = 255;
	
	if(!STOP){
			if(full_update == 1){
				
				Update_Time(0);
				
				Triac_Power_sum = sum_ADC/number_ADC;
				
				if(Triac_Power_sum < 70){
					Triac_Power_sum = 0;
				}
				uint64_t tmp_PWM = 0;
				
				float tmp_PWM2=0;
				
				if(sum_PWM[0] != 0xFFFE){
					uint32_t ii=0;
					for(ii = 0; ii < 30000; ii++){
						if(sum_PWM[ii] == 0xFFFF){
							sum_PWM[ii] 		= Triac_Power_sum;
							tmp_PWM2 			  = tmp_PWM + Triac_Power_sum;
							sum_PWM_Print   = tmp_PWM2/(ii+1);
							break;
						}
						else{
							tmp_PWM = tmp_PWM + sum_PWM[ii];
						 }
						}
									
				//!!!!sprintf(Buffer_LCD,"%-4.2f",sum_PWM_Print);
				//!!!!TM_ILI9341_Puts(80, 150, Buffer_LCD, &TM_Font_11x18, COLOR_BLACK,COLOR_BACKGRAUND);							

				}	
				//float_Power = 0.0039*(float)Triac_Power_sum*(float)Triac_Power_sum-0.4042*(float)Triac_Power_sum+42.22;
				float_Power = Triac_Power_sum;
				Power = (uint16_t)(float_Power*Corect_Power);

				//sprintf(Buffer_LCD,"%d",Err_Power);		
				//TM_ILI9341_Puts(80, 150, Buffer_LCD, &TM_Font_11x18, COLOR_BLACK,COLOR_BACKGRAUND);		
				
				sprintf(Buffer_LCD,"%4.0f",error_I_Power);	
				TM_ILI9341_Puts(100, 150, Buffer_LCD, &TM_Font_11x18, COLOR_BLACK,COLOR_BACKGRAUND);	
				
				number_ADC 	= 0;
				sum_ADC 		= 0;
			}
			else{
				i = EditMenu; 
			}
			
		if (WEB == 1){
				var_cache = var_cache_WEB;
				for (i=0; i<200; i++){
					if(WEB_Arr[i].edit == 1){
						WEB_Arr[i].edit = 0;
					}
					else{
						break;
					}
				}
				sprintf(WEB_Arr[0].str,"%d", mode_proc);	
				WEB_Arr[0].edit = 1;
		}
		else{
			var_cache = var_cache_LCD;
		}
		
			while(MenuTime[i].var != NULL) {  			
				
				show = MenuTime[i].show;
				
				if(show != 0){
					if(show[mode] == 0){
						i += 1;
						continue;
					}
				}     
				
				memset(Buffer_LCD,0, sizeof(Buffer_LCD));  
				
				if((MenuTime[i].type == 2)|(MenuTime[i].type == 3)){
					tmp_uint8_t = *(uint8_t *)(MenuTime[i].var_host);
					tmp_uint32 = (uint32_t)*(uint8_t *)(MenuTime[i].var_host);
					sprintf(Buffer_LCD,"%s", ((Mode_proc_lcd *)(MenuTime[i].var))[tmp_uint8_t].name);	
				}	
				else if(MenuTime[i].type == 1){		
				 tmp_uint32 = (uint32_t)*(uint8_t *)(MenuTime[i].var_host);	

				if(EditMenu == i){				
					 for (m=0; m<8; m++){	
						((temp *)(MenuTime[i].var))->id[m] = OW_SEARCH_BUFFER[tmp_uint32][m];
					 }
				 }			 
				 tmp_float 	= Get_Var(((temp *)(MenuTime[i].var))->OW_t, 0);	
				 
				 sprintf(Buffer_LCD,MenuTime[i].pref, tmp_uint32, tmp_float);			 
				}
				else{
					if(EditMenu == i){
						tmp_float 	= Get_Var(MenuTime[i].var_host, MenuTime[i].type);
						tmp_uint32 	= Get_uint32(MenuTime[i].var_host, MenuTime[i].type);	
					}
					else{
						tmp_float 	= Get_Var(MenuTime[i].var, MenuTime[i].type);
						tmp_uint32 	= Get_uint32(MenuTime[i].var, MenuTime[i].type);	
					}
					
					if((tmp_float >= 100)&(MenuTime[i].type == 0)){
						sprintf(Buffer_LCD,"K%3.1f", tmp_float); 
					}
					else{
						sprintf(Buffer_LCD,MenuTime[i].pref, tmp_float); 
					}
				}	
				
			uint32_t background;
			uint32_t foreground = COLOR_BLACK;
		if(EditMenu == i){ 	
			background = COLOR_YELLOW;						
			} 
		 else{
				 if(MenuTime[i].background != 0){
						background = MenuTime[i].background;
					}
				else{		
						background = COLOR_BACKGRAUND;
				 }	
				 if(MenuTime[i].foreground != 0){
						foreground = MenuTime[i].foreground;
				 } 
		} 
		
		if(WEB == 1){
				memcpy(WEB_Arr[WEB_i].str, Buffer_LCD, sizeof(Buffer_LCD));
				WEB_Arr[WEB_i].x = 2 * MenuTime[i].x;		WEB_Arr[WEB_i].y = 2 * MenuTime[i].y;	
				RGB565ToRGB888(background, WEB_Arr[WEB_i].background);	
				RGB565ToRGB888(foreground, WEB_Arr[WEB_i].foreground);
				WEB_Arr[WEB_i].edit = 1;
				WEB_i += 1;
			}
		else{		
			if((LCD_Update==0)|(var_cache[i] != tmp_uint32)|((EditMenu == i)&(EditMenu!=LCD_EditMenu_hist))|((LCD_EditMenu_hist==i)&(i!=EditMenu))){					 
						TM_ILI9341_Puts(MenuTime[i].x,  MenuTime[i].y, Buffer_LCD, &UB_Font_Font_16x27,foreground, background);					 					 
						var_cache[i] = tmp_uint32; 		
					} 	
			}

		if (full_update == 0){
			break;
		}
			i +=1;
		}
}
	else{
		for(i=0; i<4; i++){
				if (WEB == 1){
					memcpy(WEB_Arr[WEB_i].str, Buffer_LCD, sizeof(Buffer_LCD));
					WEB_Arr[WEB_i].x = MenuTime[i].x;		WEB_Arr[WEB_i].y = MenuTime[i].y;	
					
					RGB565ToRGB888(COLOR_RED, WEB_Arr[WEB_i].background);	
					RGB565ToRGB888(COLOR_BLACK, WEB_Arr[WEB_i].foreground);

					WEB_Arr[WEB_i].edit = 1;
					WEB_i += 1;
				}
				else{
					if(End_LCD_Print[i].str!=NULL){
						TM_ILI9341_Puts(End_LCD_Print[i].x, End_LCD_Print[i].y, End_LCD_Print[i].str, &UB_Font_Font_16x27, COLOR_BLACK,COLOR_RED);
					}
				}	
			}
		}
		
		LCD_Update = 1;

		LCD_EditMenu_hist = EditMenu;
}

void RC5(){	
  uint32_t tmp;
  
  if (RC5_FrameReceived == YES)
  { 
		RC5_Frame = RC5_Decode();
		Key(RC5_Frame.Command);		
		if(EditMenu != 255){
			if((RC5_Frame.Command == Key_Up)|(RC5_Frame.Command == Key_Down)){
				RC5_val(RC5_Frame);									
			}
			else{
				PrintToLCD(1,0);
			}
		}
		LCDNoAutoUpdate = 1;
	}
}

void RC5_val(RC5Frame_TypeDef RC5Frame){  
  float f_multiplicity, f_change, tmp_float,tmp_float2,f_MIN,f_MAX;
  
	if (EditMenu != 255){ 
		RC5_sticky += 1;
		
		__HAL_TIM_SetCompare(&TimHandlePWM_LED_ILI,TIM_CHANNEL_1,70);
		sec_lcd = 0;
		
		if(RC5_Frame.Command == Key_Up){
			f_multiplicity = MenuTime[EditMenu].multiplicity;
		}
		else if(RC5_Frame.Command == Key_Down) {
			f_multiplicity = -MenuTime[EditMenu].multiplicity;
		}	
		
		f_MIN = MenuTime[EditMenu].MIN;
		f_MAX = MenuTime[EditMenu].MAX;
		
		if(MenuTime[EditMenu].type == 0){
			tmp_float = *(float *)(MenuTime[EditMenu].var_host);
		}
		else if((MenuTime[EditMenu].type == 1)|(MenuTime[EditMenu].type == 2)|(MenuTime[EditMenu].type == 8)|(MenuTime[EditMenu].type == 3)){
			tmp_float = (float)*(uint8_t *)(MenuTime[EditMenu].var_host);
		}
		else if(MenuTime[EditMenu].type == 16){
			tmp_float = (float)*(uint16_t *)(MenuTime[EditMenu].var_host);
		}
		else if(MenuTime[EditMenu].type == 32){
			tmp_float = (float)*(uint32_t *)(MenuTime[EditMenu].var_host);
		}
		
		f_change = 1;
		if((RC5_sticky > 20)&(RC5_sticky < 70)){
			f_change = 10;
		}
		else if(RC5_sticky > 70){	
			f_change = 30;
		}	
		
		tmp_float2 = tmp_float + f_multiplicity * f_change;
		
		if((tmp_float2 <= f_MIN)|(tmp_float2 >= f_MAX)){
			f_change = 1;
		}	
		
		f_multiplicity *= f_change;
		tmp_float += f_multiplicity;
		
		if(MenuTime[EditMenu].type == 0){
			if((tmp_float >= f_MIN)|(tmp_float <= f_MAX)){
				*(float*)MenuTime[EditMenu].var_host = tmp_float;
			}	
		}
		else if((MenuTime[EditMenu].type == 1)|(MenuTime[EditMenu].type == 3)|(MenuTime[EditMenu].type == 8)){
			if((tmp_float >= f_MIN)&(tmp_float <= f_MAX)){
				*(uint8_t*)MenuTime[EditMenu].var_host = (uint8_t)tmp_float;
			}
		}
		else if(MenuTime[EditMenu].type == 16){
			if((tmp_float >= f_MIN)&(tmp_float <= f_MAX)){
				*(uint16_t*)MenuTime[EditMenu].var_host = (uint16_t)tmp_float;
			}
		}
		else if(MenuTime[EditMenu].type == 32){
			if((tmp_float >= f_MIN)&(tmp_float <= f_MAX)){
				*(uint32_t*)MenuTime[EditMenu].var_host = (uint32_t)tmp_float;
			}
		}	
		else if(MenuTime[EditMenu].type == 2){	
		uint8_t i; 
				
			if((tmp_float >= f_MIN)&(tmp_float <= f_MAX)){
				*(uint8_t*)MenuTime[EditMenu].var_host = (uint8_t)tmp_float;
			}
		}
		
		Update_OCR();

		if(MenuTime[EditMenu].type == 3){
			if(mode_proc_hist != mode_proc){
				change_mode(0,1);
			}
		}
		else{
			PrintToLCD(0,0);
		}
		
	} 
}

void TIM6_DAC_IRQHandler(void)
{  
	
  HAL_TIM_IRQHandler(&TimHandle6);
  
  Timer_flag = 0;	
  TIMER_250 = 1;	
  
}


void ReadTemperature()
{   	
	Tkyb  		 = Read_ds18b20(Tkyb_OW.id, &hi2c1, Tkyb);
	
  if (mode_proc == 0){
    T_Att_Dim  = Read_ds18b20(T_d_Att_Dim_OW.id, &hi2c1, T_Att_Dim);
    T_Att_Aqua = T_Att_Dim;
		Alcohol 	 = Get_Alcohol(Tkyb) + Corect_Alcohol_Kyb;	
  }
  else if (mode_proc == 1){
    Tdef  			= Read_ds18b20(Tdef_OW.id, &hi2c1, Tdef);
    T_Att_Aqua  = Read_ds18b20(T_Att_Aqua_OW.id, &hi2c1, T_Att_Aqua);
    Tn  				= Read_ds18b20(Tn_OW.id, &hi2c1, Tn);
    T_Att_Dim  	= Read_ds18b20(T_Att_Dim_OW.id, &hi2c1, T_Att_Dim) ;
		Alcohol 		= Get_Alcohol(Tkyb);	
  }		
  else if (mode_proc == 2){		
    T_10cm				= Read_ds18b20(T_10cm_OW.id, &hi2c1, T_10cm);
    T_Par 				= Read_ds18b20(T_Par_OW.id, &hi2c1, T_Par);
		Alcohol 			= Get_Alcohol_Par(T_Par);	
		
		if(T_10cm > T_10cm_Maxx){
				T_10cm_Maxx = T_10cm;
			}
  }
  
  ConvertTemperature_ds18b20(&hi2c1);	
	
}


void TIMER_change()
{   //250 ms, 4f
  static uint32_t RC5_sticky_hist;
  static uint8_t sec1 = 0; 
  
  static uint8_t sec_tmp = 0;

  #if !DEBUG_PROC
  HAL_IWDG_Refresh(&Hiwdg); 
#endif /* DEBUG_PROC */
	
  if (sec_tmp < 3){
    sec_tmp += 1;
  }
  else{
    sec_tmp  = 0;
  }
  
  if(RC5_sticky_hist == RC5_sticky){
    RC5_sticky = 0;
  }
  
  RC5_sticky_hist = RC5_sticky;
  
  TIMER_250 = 0; 
  
	if(LCDNoAutoUpdate == 1){
		LCDNoAutoUpdate = 0;
	}
	else{
		PrintToLCD(1,0);
	}
  
  if(stop_start){
    HAL_GPIO_WritePin(COOLER_PORT, COOLER, GPIO_PIN_SET);
  }
  else{
    HAL_GPIO_WritePin(COOLER_PORT, COOLER, GPIO_PIN_RESET);
  }
  
  if ((counter == 2)|(counter == 4)){
    
    if (counter == 4)
    {
      sec1 = 1;
      counter = 0;
    }      
  }  
  
  if (sec1) { 
		
		time_loc += 1;
		
    if(NO_Att > 0){
      NO_Att -=1;
    }
    
    ReadTemperature(); 
    ConvertTemperature_ds18b20(&hi2c1);
    
    general_process();   
    
    if (__HAL_TIM_GetCompare(&TimHandlePWM_LED_ILI,TIM_CHANNEL_1) != 0) 
    {
      sec_lcd += 1;
      
      if (sec_lcd >= ENABLE_LCD_TIME)
      {
        sec_lcd = 0;
        if( mode_setting == 0){ 
          EditMenu 			= 255; 
          EditMenu_hist = 255;
        }	
				__HAL_TIM_SetCompare(&TimHandlePWM_LED_ILI,TIM_CHANNEL_1,99);
      }
    }
#if !DEBUG_PROC
    Attention(); 
#endif /* DEBUG_PROC */
  }  
  
  counter += 1;
  sec1 = 0;
  
}


void general_process()
{  
  if(stop_start){
    
    if(mode != current_mode)Update_Time(1);
    
    if (mode_proc == 0){
      distillation_process();
    }
    else if(mode_proc == 1){
      rectification_process();
    }  
    else if(mode_proc == 2){
      NBK_process();
    }  
  }
  else{
    Triac_Power_Val = 0;
		time_loc 				= 0;
  } 
  
  // WaterControl();
}

void Def_OSR(float Tdef_set_tmp)
{ 	
  float ErrorTPlus = 0, ErrorT_Def = 0;
  int8_t set = 0;
	float Dimrot_Val_f;
	int8_t Dimrot_Val_d;	
	
//----P--------	
			ErrorT_Def = Tdef - Tdef_set_tmp;
			
			error_P_Dimrot = ErrorT_Def * KP_Dimrot;
			
			//Dimrot_Val_f = error_I_Dimrot + error_P_Dimrot;
	
	  	//Dimrot_Val_d = (uint8_t)Dimrot_Val_f;	
			
			//SET_OCR_PWM(Dimrot_Val_d);
	
//----I--------	
			ErrorT_Def = Tdef - Tdef_set_tmp;
			error_I_Dimrot = error_I_Dimrot+ErrorT_Def*KI_Dimrot;				
			
			if(error_I_Dimrot > 200)error_I_Dimrot = 200;
			else if(error_I_Dimrot < 0)error_I_Dimrot = 40;
			
			//error_P_Dimrot = ErrorT_Def * KP_Dimrot;
			
			Dimrot_Val_f = error_I_Dimrot + error_P_Dimrot;
	
	  	Dimrot_Val_d = (uint8_t)Dimrot_Val_f;	
			
			SET_OCR_PWM(Dimrot_Val_d);
}


void distillation_process()
{    
  //ClearBit(BEPPER_PORT,BEPPER);  
  Tattention = 55.00;

  
  if (mode == 0){  
    if (current_mode != 10){
      current_mode = 10;
      glob_interval = 0;
      SET_OCR_PWM(0);
      Triac_Power_Val = 1000;
      //SetBit(BEPPER_PORT,BEPPER);  
    }		
    if(Tkyb > 88){
      mode = 1;
    }
  } 
  else if(mode == 1){
    
    if (current_mode != 11){
      current_mode = 11;
      glob_interval = 0;
      Triac_Power_Val = Power_Rect;
      //SetBit(BEPPER_PORT,BEPPER); 
    }
    
    if (!auto_hand)
    {  
      SET_OCR_PWM(10);
      
      if (glob_interval >= 400){
        mode = 2;
      }
    }
    
    glob_interval += 1;
  }
  else if(mode == 2){
    
    if (current_mode != 12){
      current_mode = 12;
      SET_OCR_PWM(30);
      glob_interval = 0;
      Triac_Power_Val = 1000;
      //SetBit(BEPPER_PORT,BEPPER); 
    }
    
    if (glob_interval >= 90){
      glob_interval = 0;
      if (!auto_hand){
        Change_OCR_PWM(1);
      } 
    }   
    
    if(Tkyb >= Tkyb_set_dist_END){
      mode = 3;
    }
    
    glob_interval += 1;
  } 
  else if(mode == 3){
    
    if (current_mode != 13){
      current_mode = 13;
      SET_OCR_PWM(0);
      glob_interval = 0;
      //SetBit(BEPPER_PORT,BEPPER); 
    }
    
    SET_OCR_PWM(0);
    
    if(glob_interval > 100){
      Hot_STOP(2, 200);
    }
    else{
      glob_interval += 1;
    }   
  }
}

void NBK_process()
{  
  static float i_sum, T_Par_Modee = 0, T_Par_END;
	static int8_t NO_Stable_Mode = 0; 
	
  if (mode == 0){  
    if (current_mode != 0){
      current_mode 		= 0;
      glob_interval 	= 0;
      Triac_Power_Val = 1000;
      SET_OCR_MOTOR(0); 
    }
    if (!auto_hand){ 
			  if(T_10cm > 96){
					SET_OCR_MOTOR(MAX_MOTOR); 
						
					if (glob_interval < 10){
						glob_interval += 1;
					}
					else{
							mode = 1;	
					}							
				}				
    } 
  }
		else if(mode == 1){ 
			if (current_mode != 1){
				current_mode 			= 1;
				glob_interval 		= 0;
				Triac_Power_Val 	= 1000;
				SET_OCR_MOTOR(20);		
		} 
		
		float delta = T_Par_Modee - T_Par;	
		if(T_Par > 80){		
			if ((delta > 1)|(delta < -1)){
					NO_Stable_Mode = 1;
				}	
				else{
					if(NO_Stable_Mode == 0){
						mode = 2;	
					}
				}
				
			if (glob_interval > 160){
				T_Par_Modee = T_Par;
				glob_interval = 0;
				NO_Stable_Mode = 0;	
			} 
			else{
				glob_interval += 1;
			}
		}
	}
  else if(mode == 2){ 
    if (current_mode != 3){
      current_mode 			= 3;
      glob_interval 		= 0;
			glob_interval2		= 0;
      Triac_Power_Val = 1000;
      SET_OCR_MOTOR(20);
      KI_Motor = 0.3;
      KP_Motor = 3;
			error_I_Motor = 20;	
			T_Par_END = T_Par;			
    }   
    if (!auto_hand){
      
      float error = T_Par - T_NBK_Set;	
      PID_NBK(error);
			
			if (glob_interval > 120){
				glob_interval = 0;
				float diff = T_10cm_Maxx - T_10cm;

				if(diff < 0.6){
					T_NBK_Set -=0.1;
				}			
				else{
					if(diff > 0.8){
						T_NBK_Set +=0.1;
					}
				}	
			} 
			else{
				glob_interval += 1;
			}
			
		if (glob_interval2 > 60){
			
		float delta = T_Par - T_Par_END;		
		if(delta > 2){
				if(T_Par > 97){
				 Hot_STOP(2, 200);
				}
			}			
			T_Par_END = T_Par;
			glob_interval2 = 0;
			}
		else{
			glob_interval2 +=1;
		}		
			//PID_Power();    
    }
  }
}


void rectification_process()
{ 
  float Tave_Tn_high;
  uint8_t change_value = 0;
	static uint8_t OCR_PWM_Hist = 0;
	
	if (auto_hand){
			error_I_Power = 750;
	}


  if (mode == 0){
    //-------Нагрев--------
    
    if(current_mode != 0){
      current_mode = 0;   
      SET_OCR_CEL(0);
      SET_OCR_PWM(0);
      Triac_Power_Val = 1000;
			Tattention = 45; 
    }
    
    if (!auto_hand){    
      if(ADC_Press > 100){
        mode = 1;
        SET_OCR_PWM(150);
      }
    }
  } 
  else if(mode == 1){
    //-------Работа на себя--------         
    if(current_mode != 1){
      current_mode = 1;
      glob_interval = 0;
      SET_OCR_CEL(0);
			SET_OCR_PWM(150);
      Triac_Power_Val = Power_Rect; 
			Tattention = 45; 
			error_I_Dimrot = 150;
			error_I_Power = Power_Rect;	
    }
    
		if (!auto_hand){
      if (glob_interval2 >= 300){
				Def_OSR(Tdef_set_rect_default); 
			}{glob_interval2 += 1;}			
    } 
			
    if (glob_interval >= 1800){
      mode = 2;
      glob_interval = 0;
    }glob_interval += 1;}
	
  //else if((mode == 2)|(mode == 3)|(mode == 5)){
	//mode = 2  -  Holov
	//mode = 3  -  Telo
	//mode = 4  -  Xvost
	//mode = 5  -  Prod
	
	else if(mode == 2){
	static	float Tdef_set_rect_par = 0;
    //------par--------
    if(mode == 2){
      if(current_mode != 2){
        current_mode = 2;
        glob_interval = 0;		
        SET_OCR_CEL(0);
        //Triac_Power_Val = Power_Rect;  
				Tattention = 50;				
      }
    }
   
    if (!auto_hand)
			{ 	
				PID_Power();				 
				Def_OSR(Tdef_set_rect_hol); 
			}
			
	} 
	
  else if(mode == 3){
    //-------Отбор тела--------
    if(current_mode != 3){
      current_mode = 3;
      glob_interval = 0;
      glob_interval_set = 240; 
      SET_OCR_CEL(85);
      Triac_Power_Val = Power_Rect;  
			error_I_Power 	= Power_Rect;		
			Tattention = 45; 			
    }  
    
    if (!auto_hand)
    {  
			
			Def_OSR(Tdef_set_rect_default); 
			
      if(Tkyb > Tkyb_set_rect_End){
        SET_OCR_CEL(0);
        mode = 4; 
        return;
      }
      
			PID_Power();
			
      if(Tkyb >= Tkyb_set_rect_Start){
       
				uint8_t PWM_TMP  = (uint8_t)(Interpolation((uint32_t)100*Tkyb_set_rect_Start,(uint32_t)OCR_CEL_Start,(uint32_t)100*Tkyb_set_rect_End,(uint32_t)OCR_CEL_End,(uint32_t)100*Tkyb));			
        SET_OCR_CEL(PWM_TMP); 
             
      }
      else{
        
        SET_OCR_CEL(OCR_CEL); 
        
      }   
    }
  }
	
	//Hvost
	 else if(mode == 4){
		 
      if(current_mode != 4){
        current_mode = 4;
        glob_interval = 0;
        SET_OCR_CEL(0);
				if(OCR_PWM_Hist == 0) {
					 SET_OCR_PWM(100);
				}
				else{
					SET_OCR_PWM(OCR_PWM_Hist);
				}
        Triac_Power_Val = Power_Rect; 
				Tattention = 55;
      }
			
		if (!auto_hand){ 
				if(Tkyb < 98){			
				//!!!!PID_Power();
				}		
				else{
				 Triac_Power_Val = Power_Rect; 
				}		
				
				Def_OSR(Tdef_set_rect_hvst);
				
			}		
    } 
	 
  //Продувка
  else if(mode == 5){
    if(current_mode != 5){
      current_mode = 5;
      glob_interval = 0;
			OCR_PWM_Hist = __HAL_TIM_GetCompare(&TimHandlePWM_Dimrot,TIM_CHANNEL_1);
      SET_OCR_PWM(0);
      SET_OCR_CEL(0);
			Triac_Power_Val = 1000; 
			Tattention = 80; 
    }
    
    if (!auto_hand)
    {  
      SET_OCR_PWM(0);
      SET_OCR_CEL(0);
    }
    
    if (glob_interval >= 0){
      Hot_STOP(2, 200);
    }
    else{glob_interval += 1;}   
  } 
	
}

void PID_NBK(float error)
{ 	
  float OCR_Motor_f;
  static uint8_t sec4 = 0;
  
  sec4 += 1;
  
  if (sec4 < 11){
    sec4 += 1;
  }
  else{
		
    sec4  = 0;
    
    error_I_Motor = error_I_Motor + error*KI_Motor;				
    
    if(error_I_Motor > 255)error_I_Motor = 255;
    else if(error_I_Motor < 20)error_I_Motor = 20;
    
    error_P_Motor = error * KP_Motor;
    
    OCR_Motor_f = error_I_Motor + error_P_Motor;
    
    uint32_t OCR_Motor_t = (uint32_t)OCR_Motor_f;	
    
    SET_OCR_MOTOR(OCR_Motor_t);	 		
    
  }
  
}


void PID_Power()
{ 
	float Triac_Power_Val_f;
	
		float error_tmp = (float)ADC_Press_Set - (float)ADC_Press; 

		error_I_Power = error_I_Power+error_tmp*KI_Power;				
		
		if(error_I_Power > 1000)error_I_Power = 1000;
		else if(error_I_Power < 600)error_I_Power = 600;
		
		error_P_Power = error_tmp * KP_Power;
		
		Triac_Power_Val_f = error_I_Power + error_P_Power;

	  Triac_Power_Val = (uint32_t)Triac_Power_Val_f;			
}

void load_OW(){
	uint8_t i = 0;	
		if(memcmp(Tdef_OW.id, OW_SEARCH_BUFFER[i], sizeof(Tdef_OW.id)) == 0){Tkyb_OW.number = i;}
		if(memcmp(T_Att_Dim_OW.id, OW_SEARCH_BUFFER[i], sizeof(Tdef_OW.id)) == 0){Tkyb_OW.number = i;}
		if(memcmp(T_Att_Aqua_OW.id, OW_SEARCH_BUFFER[i], sizeof(Tdef_OW.id)) == 0){Tkyb_OW.number = i;}
		if(memcmp(Tn_OW.id, OW_SEARCH_BUFFER[i], sizeof(Tdef_OW.id)) == 0){Tkyb_OW.number = i;}
		if(memcmp(T_10cm_OW.id, OW_SEARCH_BUFFER[i], sizeof(Tdef_OW.id)) == 0){Tkyb_OW.number = i;}
		if(memcmp(T_Par_OW.id, OW_SEARCH_BUFFER[i], sizeof(Tdef_OW.id)) == 0){Tkyb_OW.number = i;}
		if(memcmp(Tkyb_OW.id, OW_SEARCH_BUFFER[i], sizeof(Tdef_OW.id)) == 0){Tkyb_OW.number = i;}		
	}
			
void change_mode(uint8_t mode_set, uint8_t update_lcd)
{  
	mode_proc_hist = mode_proc;
	LCD_Update = 0;
	
  if(mode_proc == 0){
    MenuTime = &MenuVarDist[0];
  }
  else if(mode_proc == 1){ 
		if (mode_set == 1){
			TM_ILI9341_Fill(COLOR_BACKGRAUND);//140/240/240
			MenuTime = &MenuSettingRect[0];
			load_OW();
		}
		else{	
			MenuTime = &MenuVarRect[0];
			GFX_Rect();
		}	
	}
  else if(mode_proc == 2){
		if (mode_set == 1){
			 TM_ILI9341_Fill(COLOR_BACKGRAUND);//140/240/240
				MenuTime = &MenuSettingNBK[0];
				load_OW();
			}
		else{	
			MenuTime = &MenuVarNBK[0];
			GFX_NBK();
		}
  } 
	
	uint8_t i; 
	for(i=0; i<255; i++){
		 if (MenuTime[i].var == NULL){
			 size_mas = i - 1;
			 break;
		 }
	}	  
	
	LCD_Update = 0; 
	//if(update_lcd == 1){
		PrintToLCD(1,0);	
	//}
}

float Get_Alcohol(float f_Temperature){
  
  uint8_t i               							= 0; 
  float	tmp_ratio, Alc 	= 0;   
  uint32_t tmp_T       							= 0;

  tmp_ratio     = 1.25 - (ADC_Press * 0.0026);
	uint32_t tmp_Temperature = 100*(tmp_ratio + f_Temperature);
  
  if(tmp_Temperature > 10000){
    Alcohol = 0;
  }
  else{
    if(tmp_Temperature > 7815){
      
      while (arr_spirt[i][0] > tmp_Temperature){
        i += 1;
      }  
		Alc = ((float)Interpolation(arr_spirt[i][0], arr_spirt[i][1],arr_spirt[i-1][0],arr_spirt[i-1][1],tmp_Temperature))/100;	
			
    }  
  }
	
	return Alc;
	
}

float Get_Alcohol_Par(float Temperature){
  uint8_t i 						= 0;
  float Alc 					 	= 0;
	
 uint32_t tmp_Temperature = 100*Temperature;
  
  if(tmp_Temperature > 7815){
    
    while (arr_spirt[i][0] > tmp_Temperature){
      i += 1;
    }
    
		Alc = ((float)Interpolation(arr_spirt[i][0], arr_spirt[i][2],arr_spirt[i-1][0],arr_spirt[i-1][2],tmp_Temperature))/100;		
		 
  }
  
	Alc = Alc - Corect_Alcohol_NBK;
	
	if(Alc < 0){
		Alc = 0;
	}
	
  return Alc;
}


void Attention(){
	
uint8_t Error 												= 0; 	
static uint8_t NumberAttention 				= 0;
static uint8_t NumberAttentionSensor 	= 0;
	
if(NO_Att == 0){
	if(!STOP){	
		if(!mode_setting){

				if(mode_proc == 1){
					if (Tdef == 0){sprintf(Buffer_Att,"-деф.\n");Error = 1;}
					if (Tn == 0){sprintf(Buffer_Att, "%s-низ. колони\n", Buffer_Att); Error = 1;}
					if (Tkyb == 0){sprintf(Buffer_Att, "%s-куба\n", Buffer_Att);Error = 1;}
					if (T_Att_Dim == 0){sprintf(Buffer_Att, "%s-захист ТСА\n", Buffer_Att);Error = 1;}
					if (T_Att_Aqua == 0){sprintf(Buffer_Att, "%s-захист хол. спирту\n", Buffer_Att);Error = 1;}
					}
					if (ADC_Press < 5){sprintf(Buffer_Att, "%s-тиску\n", Buffer_Att);Error = 1;}
				
				if(Error){NumberAttentionSensor+=1;}
				else{NumberAttentionSensor = 0;}
				if(NumberAttentionSensor > 7){
					
					End_LCD_Print[0].x 		= 50; End_LCD_Print[0].y = 10;
					End_LCD_Print[0].str 	= "!!! УВАГА !!!";
					
					End_LCD_Print[1].x 		= 10; End_LCD_Print[1].y = 38;
					End_LCD_Print[1].str 	= "Не знайденi датчики:";
					
					End_LCD_Print[2].x 		= 20; End_LCD_Print[2].y = 70;
					End_LCD_Print[2].str 	= Buffer_Att;
				
					Hot_STOP(0, 1500);
				}
			}	


				Error = 0;

				if (Tkyb > Theating_elements_Att){sprintf(Buffer_Att,"Перегрiв ТЕНу\n   Р=%2.2f",Tkyb);Error = 1;}
				if (ADC_Conc_spirt > ADC_Conc_spirt_Att){sprintf(Buffer_Att,"Пари спирту в примiщенi");Error = 1;}
				if(!HAL_GPIO_ReadPin(FLOOD_PORT, FLOOD)){sprintf(Buffer_Att,"Потоп");Error = 1;}
				
				if(mode_proc == 1){
					if (T_Att_Dim > Tattention){sprintf(Buffer_Att,"Прорив пари через ТСА");Error = 1;}
					if (T_Att_Aqua > Tattention){sprintf(Buffer_Att,"Прорив пари через холодильник");Error = 1;}
				}
			
				if(mode_proc != 2){
						if((ADC_Press > 250)&(ADC_Press < 990)){sprintf(Buffer_Att,"Перевищення тиску\n   Р=%d",ADC_Press);Error = 1;} 			
				}
				else{
					if((ADC_Press > 310)&(ADC_Press < 990)){sprintf(Buffer_Att,"Перевищення тиску\n   Р=%d",ADC_Press);Error = 1;} 
				}
				
				if(Error){NumberAttention+=1;}
				else{NumberAttention = 0;}
				if(NumberAttention > 2){
					
					End_LCD_Print[0].x 		= 50; End_LCD_Print[0].y = 20;
					End_LCD_Print[0].str 	= "!!! УВАГА !!!";
					
					End_LCD_Print[1].x 		= 20; End_LCD_Print[1].y = 60;
					End_LCD_Print[1].str 	= Buffer_Att;
					
					Hot_STOP(1, 1500);
				}
		}	
	}	
}

void Hot_STOP(uint8_t mode, uint32_t frequence){
  uint32_t Delay = 0;
  
	STOP = 1;
	
	__HAL_TIM_SetCompare(&TimHandlePWM_Buzzer,TIM_CHANNEL_4,frequence);
	Triac_Power_Val = 0;
	stop_start 			= 0;

	SET_OCR_PWM(0);
	SET_OCR_CEL(0);
	SET_OCR_MOTOR(0);
  
	HAL_NVIC_DisableIRQ(TIM8_BRK_TIM12_IRQn);
	HAL_NVIC_DisableIRQ(DMA2_Stream0_IRQn);
	HAL_NVIC_DisableIRQ(EXTI3_IRQn);

	HAL_GPIO_WritePin(TRIAC_PORT, TRIAC, GPIO_PIN_RESET);
	
	sum_ADC = 0;

	 if(mode == 2){ 
			End_LCD_Print[0].x 		= 20; End_LCD_Print[0].y = 40;
			End_LCD_Print[0].str 	= "!!! Процес завершено !!!";		 
  }	
	 
		TM_ILI9341_Fill(COLOR_YELLOW);//140/240/240
}

void Edit_Menu(uint8_t Mode_menu_var){
  
  uint8_t i;
  
  hour_set = hour;
  min_set = min;
  sec_set = sec;
	
  if((EditMenu != 255)&(Mode_menu_var == 1)){
    EditMenu = 255;
  }
  else{
    if(Mode_menu_var == 2){
      for (i=EditMenu-1; i<=255; i--){		
				if(i>100){
					i = size_mas;
				}
        if(MenuTime[i].edit){           
          uint8_t* show = (uint8_t*)(MenuTime[i].show);
          if(show != 0){
            if(show[mode] == 0){
              continue;
            }
          }
          EditMenu 			= i;
          EditMenu_hist = EditMenu;
          break;
        }
      }
    }	
    else if((Mode_menu_var == 3)|(Mode_menu_var == 1)){
      for (i=EditMenu+1; i<100; i++){		
        if (MenuTime[i].var == NULL){
          EditMenu 			= 0;
          EditMenu_hist = EditMenu;
					 break;
        }
        if (MenuTime[i].edit){           
          uint8_t* show = (uint8_t*)(MenuTime[i].show);
          if(show != 0){
            if(show[mode] == 0){
              continue;
            }
          }
          EditMenu 			= i;
          EditMenu_hist = EditMenu;
          break;
        }
      }
    }	
  }
  PrintToLCD(1,0);
}

void Key(uint8_t key)
{  		
	__HAL_TIM_SetCompare(&TimHandlePWM_LED_ILI,TIM_CHANNEL_1,70);
	sec_lcd = 0;
	
   if (key == Key_Menu){ 

      if (EditMenu != 255){
        EditMenu = 255;
      }
      else{
        if (EditMenu_hist == 255){
          Edit_Menu(1);
        }
        else{
					
          EditMenu = EditMenu_hist;
        }
      }
    } 
  if (EditMenu != 255){    
    //------------------2------------     
    if(key == Key_Start){ 
			
		 uint32_t i;
			for(i = 0; i < 30000; i++){
					sum_PWM[i] = 0xFFFF;
			}
//      if(!stop_start){
//        stop_start = 1;
//      }
//      else{
//        stop_start = 0;
//      }
//      
//      EditMenu = 255; 
    }
    //------------------4------------    
    else if (key == Key_No_At){ 
      NO_Att 			= 500;
      n_RC5_TRIAC = 600;
			error_I_Power = Power_Rect;
    }
    //------------------6-----------       
    else if (key == Key_At){ 
      EditMenu = 255; 
      stop_start = 0; 
      if(mode_proc == 1)
      {
        SET_OCR_PWM(200);
        SET_OCR_CEL(0);
      }
    }
    //------------------7------------    
    else if (key == Key_Esc){
//      if (mode_setting){
//        mode_setting = 0; 
//        change_mode(1);
//      }
//      EditMenu = 255; 
    }   
    //------------------9------------  
//    else if (key == 44){ 		
//		  error_I_Dimrot = 100;
//    } 
    else if (key == Key_SAVE){ 
      Save();	
    }
		 //------------------11------------  
		else if (key == Key_Set){ 
			if(mode_setting == 1){
				mode_setting = 0;	
				change_mode(0,1);
//				if(mode_proc == 0){
//					MenuTime = &MenuVarDist[0];
//				}
//				else if(mode_proc == 1){ 
//					MenuTime = &MenuVarRect[0];
//					GFX_Rect();
//					PrintToLCD(1,0);
//						
//				}
//				else if(mode_proc == 2){
//						MenuTime = &MenuVarNBK[0];
//						GFX_NBK();
//						PrintToLCD(1,0);					
//				} 								
			}
			
			else{
					mode_setting = 1;
					TM_ILI9341_Fill(COLOR_BACKGRAUND);//140/240/240		                          			
					if(mode_proc == 0){
						//!!!!!!!!MenuTime = &MenuVarDist[0];
					}
					else if(mode_proc == 1){ 
							TM_ILI9341_Fill(COLOR_BACKGRAUND);//140/240/240
							MenuTime = &MenuSettingRect[0];
					}
					else if(mode_proc == 2){
							 TM_ILI9341_Fill(COLOR_BACKGRAUND);//140/240/240
								MenuTime = &MenuSettingNBK[0];
					} 
					PrintToLCD(1,0);
			}
			//change_mode(0,1); 
    } 
    //------------------10------------  
    else if (key == Key_VOL_M){ 
      Edit_Menu(2);
    }
    else if (key == Key_VOL_P){ 
      Edit_Menu(3);
    }
  }
}

void WaterControl()
{ 
  uint8_t timeuot = 60;
  
  if(Tkyb > 8000){
    timeuot = 0;
  }
  
  if(Water_start < timeuot){
    
    OCR_PWM_hist = 0;
    Water_start += 1;
    SET_OCR_PWM(200); 
    if (Water_start == timeuot){
      SET_OCR_PWM(0);  
    }
  }
  else{
    if (mode_proc == 0){
      if(!stop_start){
        SET_OCR_PWM(0);
      }
    }
    else if (mode_proc == 1){
      if(!stop_start){
        SET_OCR_PWM(0);
      }
      else{
        if(mode == 0){
          SET_OCR_PWM(0);
        }
        else if(mode > 0){
          if(!OCR_PWM_hist){
            OCR_PWM_hist = 1;
            SET_OCR_PWM(180);
          }
        }
      }
    }   
  }
}

void SET_OCR_PWM(uint8_t OCR_PWM_SET){
  if(OCR_PWM_SET < 201){
    __HAL_TIM_SetCompare(&TimHandlePWM_Dimrot,TIM_CHANNEL_1,40*OCR_PWM_SET);
    OCR_PWM = OCR_PWM_SET;
  }
}

void SET_OCR_CEL(uint8_t OCR_CEL_SET){
  if(OCR_CEL_SET < 101){
    __HAL_TIM_SetCompare(&TimHandlePWM_Spirt,TIM_CHANNEL_4,80*OCR_CEL_SET);
    OCR_CEL = OCR_CEL_SET;
  }
}

void SET_OCR_MOTOR(uint32_t OCR_MOTOR_SET){
  if(OCR_MOTOR_SET > MAX_MOTOR){
		OCR_MOTOR_SET = MAX_MOTOR;
  }
	  __HAL_TIM_SetCompare(&TimHandlePWM_Motor,TIM_CHANNEL_2,OCR_MOTOR_SET);
    OCR_Motor = OCR_MOTOR_SET;
}

void Change_OCR_PWM(int16_t OCR_PWM_Change)
{int16_t OCR_PWM_TMP = 0;

OCR_PWM_TMP = OCR_PWM + OCR_PWM_Change;

if(OCR_PWM_TMP < 0){
  OCR_PWM = 0;
}
else if(OCR_PWM_TMP > 200){
  OCR_PWM = 200;
}
else{
  OCR_PWM += OCR_PWM_Change;
}
__HAL_TIM_SetCompare(&TimHandlePWM_Dimrot,TIM_CHANNEL_1,40*OCR_PWM);
}


void Change_OCR_CEL(int16_t OCR_CEL_Change)
{ int16_t OCR_CEL_TMP = 0;

OCR_CEL_TMP = OCR_CEL + OCR_CEL_Change;

if(OCR_CEL_TMP < 0){
  OCR_CEL = 0;
}
else if( OCR_CEL_TMP > 100){
  OCR_CEL = 100;
}
else{
  OCR_CEL += OCR_CEL_Change;
}
__HAL_TIM_SetCompare(&TimHandlePWM_Spirt,TIM_CHANNEL_4,80*OCR_CEL);
}

void Change_OCR_MOTOR(int32_t OCR_MOTOR_Change)
{ int16_t OCR_MOTOR_TMP = 0;

OCR_MOTOR_TMP = OCR_CEL + OCR_MOTOR_Change;

if(OCR_MOTOR_TMP < 0){
  OCR_CEL = 0;
}
else if( OCR_MOTOR_TMP > MAX_MOTOR){
  OCR_CEL = MAX_MOTOR;
}
else{
  OCR_Motor += OCR_MOTOR_Change;
}
__HAL_TIM_SetCompare(&TimHandlePWM_Motor,TIM_CHANNEL_4,OCR_Motor);

}

void Update_OCR(void){
	static int32_t PWM;
	
  //DIMROT
  PWM = __HAL_TIM_GetCompare(&TimHandlePWM_Dimrot,TIM_CHANNEL_1);
  
  if(PWM != OCR_PWM*40){
    __HAL_TIM_SetCompare(&TimHandlePWM_Dimrot,TIM_CHANNEL_1,40*OCR_PWM);
  }
  PWM = __HAL_TIM_GetCompare(&TimHandlePWM_Dimrot,TIM_CHANNEL_1);
  
  //Spirt	
  PWM = 0;		
  PWM = __HAL_TIM_GetCompare(&TimHandlePWM_Spirt,TIM_CHANNEL_4);
  
  if(PWM != OCR_CEL*80){
    __HAL_TIM_SetCompare(&TimHandlePWM_Spirt,TIM_CHANNEL_4,80*OCR_CEL);
  }
  
  //Motor
  PWM = 0;		
  PWM = __HAL_TIM_GetCompare(&TimHandlePWM_Motor,TIM_CHANNEL_2);
  
  if(PWM != OCR_Motor){
    __HAL_TIM_SetCompare(&TimHandlePWM_Motor,TIM_CHANNEL_2,OCR_Motor);
  }
  
}

void Timer_Update(void){
}	

void Get_Buffer_Telnet(uint8_t Header, char *Buffer_Telnet){
  uint32_t number_ADC;
	uint32_t sum_ADC;
  if(Header == 1){
    if(mode_proc == 1){
			sprintf(Buffer_Telnet, "Time	Tdef	Def_PWM	mode	Alc_PWM	ADC_Press	Triac_Power_Val	auto_hand	Power	error_I_Power	KPPower	error_P_Power");
			//sprintf(Buffer_Telnet, "Time	Sec	Tkyb	Alcohol	Tn	Tdef	Def_PWM	ADC_Press	Alc_PWM	OCR_Motor	Power	Triac_Power_Val	mode	auto_hand	KI_Dimrot	error_I_Dimrot	KP_Dimrot	error_P_Dimrot");
     //sprintf(Buffer_Telnet, "Time	Alcohol	Tkyb	Tn	Tdef	ADC_Press	OCR_PWM	OCR_CEL	OCR_Motor	Power	Triac_Power_Val	mode");
    }
    else if(mode_proc == 2){
			sprintf(Buffer_Telnet, "Time	Sec	Tkyb	T_10cm	T_10cm_Maxx	T_Par	Alcohol	OCR_Motor	T_NBK_Set	ADC_Press	Power	Triac_Power_Val	mode");
      //sprintf(Buffer_Telnet, "Time	Tkyb	T_10cm	T_10cm_Maxx	T_Par	Alcohol	T_NBK_Set	OCR_Motor	ADC_Press	Power	Triac_Power_Val	mode");
    }
  }
  else{
		
    Update_Time(0);
    if(mode_proc == 1){ 
			sprintf(Buffer_Telnet, "%02d:%02d:%02d	%2.2f	%d	%d	%d	%d	%d	%d	%d	%2.f	%2.2f	%2.2f",
			hour_delta,min_delta,sec_delta,Tdef,OCR_PWM,mode,OCR_CEL,ADC_Press,Triac_Power_Val,auto_hand,Power,error_I_Power,KP_Power,error_P_Power);	
			//sprintf(Buffer_Telnet, "%02d:%02d:%02d	%lld	%2.2f	%2.2f	%2.2f	%2.2f	%d	%d	%d	%d	%d	%d	%d	%d	%2.2f	%2.2f	%2.2f	%2.2f",
			//hour_delta,min_delta,sec_delta,time,Tkyb,Alcohol,Tn,Tdef,OCR_PWM,ADC_Press,OCR_CEL,OCR_Motor,Power,Triac_Power_Val,mode,auto_hand,KI_Dimrot,error_I_Dimrot,KP_Dimrot,error_P_Dimrot);						
      //sprintf(Buffer_Telnet, "%02d:%02d:%02d	%2.2f	%2.2f	%2.2f	%2.2f	%d	%d	%d	%d	%d	%d	%d",hour_delta,min_delta,sec_delta,Alcohol,Tkyb,Tn,Tdef,ADC_Press,OCR_PWM,OCR_CEL,OCR_Motor,Power,Triac_Power_Val,mode);
    }
    else if(mode_proc == 2){
			sprintf(Buffer_Telnet, "%02d:%02d:%02d	%2.2f	%2.2f	%2.2f	%2.2f	%2.2f	%d	%2.2f	%d	%d	%d	%d",hour_delta,min_delta,sec_delta,Tkyb,T_10cm,T_10cm_Maxx,T_Par,Alcohol,OCR_Motor,T_NBK_Set,ADC_Press,Triac_Power_Val,Power,mode);	
			//sprintf(Buffer_Telnet, "%02d:%02d:%02d	%2.2f	%2.2f	%2.2f	%2.2f	%2.2f	%2.2f	%d	%d	%d	%d	%d",Hour,Min,Sec,Tkyb,T_10cm,T_10cm_Maxx,T_Par,Alcohol,T_NBK_Set,OCR_Motor,ADC_Press,Triac_Power_Val,Power,mode);	
    }
  }
}	


void Save(){

	RTC_TimeTypeDef  stimestructure;
	uint16_t save_setting[50];
	
	uint8_t rr=0;
	
		typedef struct {
		uint8_t	address[8];
	} Device;
	
	 Device device_id[10];

	if ((strcmp(MenuTime[EditMenu].pref, "%2.3f") == 0)|(strcmp(MenuTime[EditMenu].pref, ":%02.0f") == 0)|(strcmp(MenuTime[EditMenu].pref, "%02.0f") == 0)){

		stimestructure.Hours 		= hour_set;
		stimestructure.Minutes 	= min_set;
		stimestructure.Seconds 	= sec_set;
    stimestructure.TimeFormat = RTC_HOURFORMAT12_AM;
    stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
    stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;

		if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,RTC_FORMAT_BIN) != HAL_OK){rr = 1;}
	}
	else{

//		if(mode == 1){
//				memcpy(device_id[0].address, T_d_Att_Dim_OW.id, 10 * 8 * sizeof(uint8_t));
//				memcpy(device_id[1].address, Tdef_OW.id, 10 * 8 * sizeof(uint8_t));
//				memcpy(device_id[2].address, Tn_OW.id, 10 * 8 * sizeof(uint8_t));
//				memcpy(device_id[3].address, T_Att_Dim_OW.id, 10 * 8 * sizeof(uint8_t));
//				memcpy(device_id[4].address, T_Att_Aqua_OW.id, 10 * 8 * sizeof(uint8_t));
//				memcpy(device_id[5].address, T_10cm_OW.id, 10 * 8 * sizeof(uint8_t));
//				memcpy(device_id[6].address, T_Par_OW.id, 10 * 8 * sizeof(uint8_t));
//				memcpy(device_id[7].address, Tkyb_OW.id, 10 * 8 * sizeof(uint8_t));	
//				BSP_EEPROM_WriteBuffer(&save_setting, 2*1024, 10 * 10 * 8 * sizeof(uint16_t));	
//			
//				save_setting[0] 	= type_selection_golov;	
//				BSP_EEPROM_WriteBuffer(&save_setting, 64, 10 * sizeof(uint16_t));				
//			}
//			else{		
				save_setting[0] 	= Triac_Power_Val;				
//				save_setting[1] 	= (uint16_t)(100*Tdef_set_dist);
//				save_setting[2] 	= (uint16_t)(100*Tkyb_set_dist_END); 
//				save_setting[3] 	= (uint16_t)(100*Tdef_set_rect_hol);
//				save_setting[4] 	= (uint16_t)(100*Tdef_set_rect_podghol);
//				save_setting[5] 	= (uint16_t)(100*Tdef_set_rect_hvst);
//				save_setting[6] 	= (uint16_t)(100*Tdef_set_rect_default);
//				save_setting[7] 	= (uint16_t)(100*T_NBK_Set);	
//				save_setting[8] 	= ADC_Press_Set;
				BSP_EEPROM_WriteBuffer(&save_setting, 0, 10 * sizeof(uint16_t));	
//			}	
		}	
	
			TM_ILI9341_Fill(COLOR_BACKGRAUND);
			TM_ILI9341_Puts(120, 120, "SAVE", &UB_Font_Font_16x27, COLOR_BLACK,COLOR_YELLOW);	
		
			HAL_Delay(700);
		  change_mode(0,0);

}

void Load(){ 
	
	  uint16_t load_setting[50];		
		uint8_t T_clear_m[8]  = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
		uint8_t i;
							
		stop_start = 1;
		auto_hand = 0;
		mode_proc = 1;
		mode = 0;   
		
	typedef struct {
		uint8_t	address[8];
	} Device;
	
	 Device device_id[10];
	

//	BSP_EEPROM_ReadBuffer((uint8_t*)&device_id, 2*1024, 10 * 10 * 8 * sizeof(uint16_t));	
//	
//	if(memcmp(Tkyb_OW.id, device_id[7].address, sizeof(Tkyb_OW.id)) == 0){
//		memcpy(T_d_Att_Dim_OW.id, device_id[0].address, 10 * 8 * sizeof(uint8_t));
//		memcpy(Tdef_OW.id, 				device_id[1].address, 10 * 8 * sizeof(uint8_t));
//		memcpy(Tn_OW.id, 					device_id[2].address, 10 * 8 * sizeof(uint8_t));
//		memcpy(T_Att_Dim_OW.id, 	device_id[3].address, 10 * 8 * sizeof(uint8_t));
//		memcpy(T_Att_Aqua_OW.id, 	device_id[4].address, 10 * 8 * sizeof(uint8_t));
//		memcpy(T_10cm_OW.id, 			device_id[5].address, 10 * 8 * sizeof(uint8_t));
//		memcpy(T_Par_OW.id, 			device_id[6].address, 10 * 8 * sizeof(uint8_t));
//		memcpy(Tkyb_OW.id, 				device_id[7].address, 10 * 8 * sizeof(uint8_t));	
//	}
	
		for(i = 0; i < OW_DEVICE_COUNT; i++){
			if(memcmp(Tdef_OW.id, OW_SEARCH_BUFFER[i], sizeof(Tdef_OW.id)) == 0){
				mode_proc = 1;
			}	
			
			if(memcmp(T_10cm_OW.id, OW_SEARCH_BUFFER[i], sizeof(Tdef_OW.id)) == 0){
				mode_proc = 2;
			}		
				
		}		

		BSP_EEPROM_ReadBuffer((uint8_t*)&load_setting, 0, 10 * sizeof(uint16_t));
		
		uint8_t No_Empty=0;
		for(i = 0; i < 10; i++){
			if((load_setting[i])!=0){
				No_Empty = 1;
				break;
			}
		}
		
		if(No_Empty == 1){
  	 Power_Rect							=  load_setting[0];
//		 Tdef_set_dist					=	(float)(load_setting[1]/100);
//		 Tkyb_set_dist_END			=	(float)(load_setting[2]/100);
//		 Tdef_set_rect_hol			=	(float)(load_setting[3]/100);	
//		 Tdef_set_rect_podghol	=	(float)(load_setting[4]/100);
//		 Tdef_set_rect_hvst			=	(float)(load_setting[5]/100);
//		 Tdef_set_rect_default 	=	(float)(load_setting[6]/100);
//		 T_NBK_Set							=	(float)(load_setting[7]/100);
//		ADC_Press_Set						=  load_setting[8];
		
		}	
		
		error_I_Power = 750;
		error_I_Dimrot = 150;
}


