#include <mega16.h>
#include <delay.h>
#include <1wire.h>
#include <ds18b20.h>
#include <alcd.h>
#include <stdio.h>
#include <string.h>






#define		ADC1		3
#define		ADC2		4


#define		IMP			PINB.2


#define		PORT1		PORTC.3
//#define		PORT2		PORTC.2


#define		FAN1		PORTC.1
#define		FAN2		PORTC.0


#define		FOTOB1		PIND.6
#define		FOTOB2		PINC.6
//#define		FOTOB3		PINB.6



//#define		POWER1		PORTC.5
#define		POWER1		PORTC.2
#define		POWER2		PORTC.4















// maximum number of DS1820 devices
// connected to the 1 Wire bus
#define MAX_DS1820 4
// number of DS1820 devices
// connected to the 1 Wire bus
unsigned char ds1820_devices;
// DS1820 devices ROM code storage area,
// 9 bytes are used for each device
// (see the w1_search function description in the help)
unsigned char ds1820_rom_codes[MAX_DS1820+1][9];
signed char ds1820_temp[MAX_DS1820+1];



unsigned char index_temp_svetlo;
unsigned char index_temp_okoli;






//global
unsigned char lcd_buffer[33];

bit _provoz;
bit _provoz_svetlo;
bit _fan_1;
bit _fan_2;
bit step_hladina;

bit _vent_01;
bit _vent_02;

bit _vent_foto_cerpadlo;
bit _vent_foto_12;

bit _teplomer;


unsigned char menu_lvl, menu_2, menu_1;


unsigned int step_provoz;
unsigned int step_svetlo;
unsigned char step_ventil;

unsigned char provoz_stage;

unsigned int prutok_100ul=0;


unsigned int temp;



















#define VENT_CERPADLO		0			//cas 0.1s
#define VENT_230			1			//cas 0.1s
#define VENT_DELAY			2			//cas 0.1s
#define VENT_REPEAT			3			//cas 1s vent1+vent2+2xdelay
#define VENT_CHOD			4			//0=prutokomer+cas,1=fotocidlo+cas,2=cas interval,3=vse

#define SVETLO_CHOD			5			//0=off,1=defined,2=custom
#define SVETLO_INTENZITA	6			//0-100%
#define SVETLO_CUSTOM		7			//x hodin / 24h T
#define SVETLO_PRE			8			//0=24/0,1=18/6, 2=12/12
#define SVETLO_LIM			9			//teplota na snizeni intenzity
#define SVETLO_MAX			10			//teplota pro vypnuti

#define FAN_1				11			//0=off,1=on,2=auto
#define FAN_2				12			//0=off,1=on,2=auto
#define FAN_1_OT			13			//40-100%
#define FAN_2_OT			14			//40-100%
#define FAN_1_DH			15			//minimalni teplota
#define FAN_1_SH			16			//optimalni teplota
#define FAN_1_HH			17			//maximalni teplota
#define FAN_2_DH			18			//minimalni teplota
#define FAN_2_SH			19			//optimalni teplota
#define FAN_2_HH			20			//maximalni teplota

#define VENT_12				21			//cas 0.1s


#define HLADINA_LIM			22			//lim. hladina/vypnuti,error
#define HLADINA_MM			23
#define HLADINA_D1H			24
#define HLADINA_D2H			25
#define HLADINA_D1L			26
#define HLADINA_D2L			27

#define VENT_MLH			28			//mnozstvi vody pro zavreni ventilu
#define VENT_MLL			29			//mnozstvi vody pro zavreni ventilu

#define TCS_H				30			//tep. cidlo pro svetlo
#define TCS_L				31			//tep. cidlo pro svetlo

#define TCP_H				32			//tep. cidlo pro prostredi
#define TCP_L				33			//tep. cidlo pro prostredi

#define PRUTOK_NAS			34			//0 pulz=2500/litr		1 pulz=10000/litr



#define CRC_H				35
#define CRC_L				36

#define AMOUNT				37
unsigned char gm[AMOUNT];
eeprom unsigned char gme[AMOUNT];


unsigned char *text_1[4]={"Prutok + Cas","Fotobrana + Cas","Pouze Cas","Vsechny kontroly"};
unsigned char *text_2[3]={"OFF","Predefinovano","Rucni nastaveni"};
flash unsigned char text_3[3]={24,18,12};
unsigned char *text_4[3]={"OFF","ON","Auto"};






flash unsigned char menu_pol[]={7,4,7,6,11,2,4,4,4};






























flash unsigned char TabCRCHi[]=
{
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,	0x81, 0x40,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x01, 0xC0,	0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,	0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,	0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,	0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x01, 0xC0,	0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,	0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,	0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
};

flash unsigned char TabCRCLo[]=
{
	0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04,
	0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,	0x08, 0xC8,
	0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
	0x14, 0xD4,	0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10,
	0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,	0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
	0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,	0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
	0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,	0xEC, 0x2C,
	0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0,
	0xA0, 0x60,	0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
	0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,	0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
	0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,	0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C,
	0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,	0x70, 0xB0,
	0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54,
	0x9C, 0x5C,	0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98,
	0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,	0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
	0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,	0x43, 0x83, 0x41, 0x81, 0x80, 0x40
};




unsigned char CRCHi;
unsigned char CRCLo;

void CALC_CRC(unsigned char *vstup, unsigned char pocet)
{
	unsigned int index;
	CRCHi=0xFF;
	CRCLo=0xFF;

	while(pocet-->2)
	{
		index=CRCHi^*vstup++;
		
		CRCHi=CRCLo^TabCRCHi[index];
		CRCLo=TabCRCLo[index];
	}
}







#define ALARM_LIMIT				0
#define PUSH_FAIL				1
#define LONG_PUSH_LIMIT			2
#define AUTOREPPEAT_FREQ		3
#define FAST_AUTOREPPEAT_FREQ	4
#define FAST_AUTOREPPEAT_LIMIT	5
#define PULL_FAIL				6


#define RAIN_M		7
#define RAIN_A		5
#define RAIN_B		6



flash unsigned int rain_key_time[]={10,1,750,100,50,50,1};
flash unsigned char rain_map=(1<<RAIN_A)|(1<<RAIN_B)|(1<<RAIN_M);


unsigned char *rain_instant="LR";
unsigned char *rain_loop="";
unsigned char *rain_advanced="M";


bit KEY_SET;
bit KEY_SET_0;
bit KEY_SET_1;
bit KEY_SET_2;



unsigned int rain_key_delay;
unsigned int rain_alarm_delay;
unsigned char rain_stage;







void enable_alarm()
{
	rain_alarm_delay=0;
	TCCR0=0x1B;
}
void disable_alarm()
{
	TCCR0=0x0B;
}





unsigned char key_read(unsigned char type)
{
	unsigned char rain_symbol=PINA&rain_map;
	unsigned char x;
	unsigned char rain_counter=0;
	
	static unsigned char rain_push_count=0;
	static unsigned char rain_counter_h=0;
	
	static unsigned int rain_key_delay_h=0;
	static unsigned char rain_stage_h=0;
	static unsigned char rain_symbol_h=0;
	static unsigned char rain_rotation=0;
	

	//
	{
		if((~rain_symbol&(1<<RAIN_M))==(1<<RAIN_M)) rain_symbol='M';

		
		
		
		else if((~rain_symbol&rain_map)==((1<<RAIN_A)|(1<<RAIN_B)))
		{
			rain_rotation='U';
			rain_symbol=rain_map;
		}
		else if((~rain_symbol&rain_map)==((0<<RAIN_A)|(0<<RAIN_B)))
		{
			rain_rotation='D';
			rain_symbol=rain_map;
		}
		
		
		
		
		else if((~rain_symbol&rain_map)==(1<<RAIN_A) && rain_rotation=='U') rain_symbol='L';
		else if((~rain_symbol&rain_map)==(1<<RAIN_B) && rain_rotation=='U') rain_symbol='R';
		
		else if((~rain_symbol&rain_map)==(1<<RAIN_A) && rain_rotation=='D') rain_symbol='R';
		else if((~rain_symbol&rain_map)==(1<<RAIN_B) && rain_rotation=='D') rain_symbol='L';
		
		
		
		else rain_symbol=rain_map;
	}

	
	for(x=0;x<8;x++) if((PIND&rain_map)&(1<<x)) rain_counter++;
	
	

	
	
	
	
	if(!type) return rain_symbol;
	else if(rain_stage==6)
	{
		if(KEY_SET_0)
		{
			if(rain_counter_h>rain_counter || rain_symbol==rain_map) rain_stage=0;
			
			if(rain_symbol==rain_map && rain_stage_h==2 && !strchr(rain_instant,rain_symbol_h) && !KEY_SET_1)
			{
				enable_alarm();
				
				return rain_symbol_h;
			}
		}
		else if(rain_symbol==rain_symbol_h)
		{
			rain_stage=rain_stage_h;
			rain_key_delay=rain_key_delay_h;
		}
	}
	else if(rain_symbol!=rain_symbol_h && rain_stage)
	{
		rain_stage_h=rain_stage;
		rain_stage=6;
		
		rain_key_delay_h=rain_key_delay;
		rain_key_delay=0;
		KEY_SET_0=0;
		
		return 0;
	}
	else if(rain_symbol!=rain_map && !rain_stage)
	{
		KEY_SET_0=0;
		KEY_SET_1=0;
		KEY_SET_2=0;
		

		rain_key_delay=0;
		rain_push_count=0;
		
		rain_counter_h=rain_counter;
		rain_symbol_h=rain_symbol;
		
		rain_stage++;
	}
	
	

	
	
	
	

	if(KEY_SET_0 && rain_symbol)
	{
		KEY_SET_0=0;
		rain_key_delay=0;
		rain_push_count++;
		

		switch(rain_stage)
		{
			case 3: if(rain_push_count>rain_key_time[FAST_AUTOREPPEAT_LIMIT]) rain_stage++;
			case 4: return rain_symbol;
			case 2:
			{
				if(strchr(rain_loop,rain_symbol))
				{
					KEY_SET_1=1;
					rain_stage++;
				}
				
				else if(strchr(rain_advanced,rain_symbol) && !KEY_SET_2)
				{
					if(!KEY_SET_1) KEY_SET_1=1;
					else KEY_SET_2=1;
					
					
					enable_alarm();
					
					return rain_symbol;
				}
			}
			break;
			
			case 1:
			{
				rain_stage++;
				
				if(strchr(rain_instant,rain_symbol))
				{
					enable_alarm();
					
					return rain_symbol;
				}
			}
		}
	}
	
	return 0;
}







void test_teplot_fan(unsigned char c)
{
	if(c=='-')
	{
		if(gm[FAN_1_SH]>=gm[FAN_1_HH]) gm[FAN_1_SH]=gm[FAN_1_HH]-1;
		if(gm[FAN_1_DH]>=gm[FAN_1_SH]) gm[FAN_1_DH]=gm[FAN_1_SH]-1;
		
		if(gm[FAN_2_SH]>=gm[FAN_2_HH]) gm[FAN_2_SH]=gm[FAN_2_HH]-1;
		if(gm[FAN_2_DH]>=gm[FAN_2_SH]) gm[FAN_2_DH]=gm[FAN_2_SH]-1;
	}
	if(c=='+')
	{
		if(gm[FAN_1_DH]>=gm[FAN_1_SH]) gm[FAN_1_SH]=gm[FAN_1_DH]+1;
		if(gm[FAN_1_SH]>=gm[FAN_1_HH]) gm[FAN_1_HH]=gm[FAN_1_SH]+1;
		
		if(gm[FAN_2_DH]>=gm[FAN_2_SH]) gm[FAN_2_SH]=gm[FAN_2_DH]+1;
		if(gm[FAN_2_SH]>=gm[FAN_2_HH]) gm[FAN_2_HH]=gm[FAN_2_SH]+1;
	}
}




void test_teplot_svetlo(unsigned char c)
{
	if(c=='+') if(gm[SVETLO_LIM]>=gm[SVETLO_MAX]) gm[SVETLO_MAX]=gm[SVETLO_LIM]+1;
	if(c=='-') if(gm[SVETLO_LIM]>=gm[SVETLO_MAX]) gm[SVETLO_LIM]=gm[SVETLO_MAX]-1;
}
















#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif

#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif

#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)




interrupt [USART_RXC] void usart_rx_isr(void)
{
	char status,data;
	status=UCSRA;
	data=UDR;
	
	if((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
	{
	}
}


interrupt [USART_TXC] void usart_tx_isr(void)
{
}









#define ADC_VREF_TYPE 0xC0
unsigned int read_adc(unsigned char adc_input)
{
	ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
	delay_us(10);
	ADCSRA|=0x40;

	while((ADCSRA & 0x10)==0);
	ADCSRA|=0x10;
	return ADCW;
}


























unsigned int fan_sens_1;
unsigned int fan_sens_2;


// FAN sens oc1B
interrupt [EXT_INT0] void ext_int0_isr(void)
{
	fan_sens_1++;
}

// FAN sens oc1A
interrupt [EXT_INT1] void ext_int1_isr(void)
{
	fan_sens_2++;
}

// prutokomer
interrupt [EXT_INT2] void ext_int2_isr(void)
{
	if(gm[PRUTOK_NAS])	prutok_100ul++;
	else				prutok_100ul+=4;
	
	
	temp=((unsigned int)gm[VENT_MLH]<<8)+gm[VENT_MLL];
	
	if(prutok_100ul>=temp && temp && (!gm[VENT_CHOD]||gm[VENT_CHOD]==3) && provoz_stage==1)
	{
		step_provoz=0;
		POWER1=1;
		provoz_stage++;
		
		if(!menu_lvl) prutok_100ul=0;
	}
}









// 10s
void timer_10000()
{
	if(_provoz) step_svetlo++;
	
	_teplomer=1;
}

// 100ms
void timer_100()
{
	static unsigned char fan_step=0;
	static unsigned char step_4=0;
	
	if(++step_4>=100)
	{
		timer_10000();
		step_4=0;
	}
	
	if(++fan_step>=10)
	{
		fan_step=0;
		
		if(!menu_2 && !menu_lvl && menu_1==2)
		{
			lcd_gotoxy(7,0);
			sprintf(lcd_buffer,"%5u",fan_sens_1*30);
			lcd_puts(lcd_buffer);
		}
		if(menu_2==3 && menu_1==3)
		{
			lcd_gotoxy(7,1);
			sprintf(lcd_buffer,"%5u",fan_sens_1*30);
			lcd_puts(lcd_buffer);
		}
		if((!menu_2 && !menu_lvl && menu_1==2) || (menu_2==8 && menu_1==3))
		{
			lcd_gotoxy(7,1);
			sprintf(lcd_buffer,"%5u",fan_sens_2*30);
			lcd_puts(lcd_buffer);
		}
		fan_sens_1=0;
		fan_sens_2=0;
		
		if(menu_2==2 && menu_1==6)
		{
			lcd_gotoxy(0,1);
			sprintf(lcd_buffer,"%8u.%u ml",prutok_100ul/10,prutok_100ul%10);
			lcd_puts(lcd_buffer);
		}
	}
	
	step_hladina=1;
	
	
	
	step_ventil++;
	if(step_ventil>gm[VENT_12]) PORT1=0;
}

// 10ms
void timer_10()
{
	if(_provoz || _vent_01 || _vent_02)
	{
		step_provoz++;
		
		if(provoz_stage==0)
		{
			if(!FOTOB1) _vent_foto_cerpadlo=0;
			else _vent_foto_cerpadlo=1;
			
			step_provoz=0;
			POWER1=0;
			provoz_stage++;
		}
		else if(step_provoz>=(unsigned int)gm[VENT_CERPADLO]*10 && provoz_stage==1)
		{
			step_provoz=0;
			POWER1=1;
			provoz_stage++;
			_vent_01=0;
		}
		else if(step_provoz>=(unsigned int)gm[VENT_DELAY]*10 && provoz_stage==2)
		{
			step_provoz=0;
			POWER1=1;
			POWER2=0;
			provoz_stage++;
		}
		else if(step_provoz>=(unsigned int)gm[VENT_230]*10 && provoz_stage==3)
		{
			step_provoz=0;
			POWER2=1;
			provoz_stage++;
			_vent_02=0;
		}
		else if(step_provoz>=(unsigned int)gm[VENT_DELAY]*10 && provoz_stage==4)
		{
			POWER2=1;
			step_provoz=0;
			provoz_stage++;
		}
		else if(step_provoz>=(unsigned int)gm[VENT_REPEAT]*100 && provoz_stage==5)
		{
			if(!FOTOB1) _vent_foto_cerpadlo=0;
			else _vent_foto_cerpadlo=1;
			
			POWER2=1;
			step_provoz=0;
			POWER1=0;
			provoz_stage=1;
		}
	}
	else if(!menu_lvl)
	{
		POWER1=1;
		POWER2=1;
	}
}


// 1ms
void timer_1()
{
	static unsigned char step_2=0;
	static unsigned char step_3=0;
	
	if(++step_2>=10)
	{
		timer_10();
		step_2=0;
	}
	
	if(++step_3>=100)
	{
		timer_100();
		step_3=0;
	}
	
	
	KEY_SET=1;
	
	if(rain_key_delay>=rain_key_time[rain_stage]) KEY_SET_0=1;
	else rain_key_delay++;

	if(rain_alarm_delay>=rain_key_time[ALARM_LIMIT]) disable_alarm();
	else rain_alarm_delay++;
}




// 40us
//interrupt [TIM1_COMPA] void timer1_compa_isr(void)
interrupt [TIM1_CAPT] void timer1_capt_isr(void)
{
	static unsigned char step_1=0;
	
	if(++step_1>=25)
	{
		timer_1();
		step_1=0;
	}
}


























unsigned char svetlo_nastaveni(unsigned char intens)
{
	if(!gm[SVETLO_CHOD] || !intens)
	{
		TCCR2=0x4E;
		_provoz_svetlo=0;
	}
	else _provoz_svetlo=1;
	
	
	OCR2=((unsigned int)0xFF*intens)/100;	
	
	
	if(gm[SVETLO_CHOD]==1) return text_3[gm[SVETLO_PRE]];
	else if(gm[SVETLO_CHOD]==2) return gm[SVETLO_CUSTOM];
}



void ventilator_nastaveni(unsigned char fan1, unsigned char fan2)
{
	float pom=0x01B9;
	
	OCR1A=(unsigned int)(pom*fan2/100);
	
	OCR1B=(unsigned int)(pom*fan1/100);
	
	
	
	
	
	if(!gm[FAN_1] || !fan1) FAN1=0;
	else FAN1=1;
	
	if(!gm[FAN_2] || !fan2) FAN2=0;
	else FAN2=1;
}








void zapnout()
{
	_provoz=1;
	
	svetlo_nastaveni(gm[SVETLO_INTENZITA]);
	TCCR2=0x6E;
	
	ventilator_nastaveni(gm[FAN_1_OT],gm[FAN_2_OT]);
}

void vypnout()
{
	_provoz=0;
	
	POWER1=1;
	POWER2=1;
	
	PORT1=0;

	
	
	
	TCCR2=0x4E;
	svetlo_nastaveni(0);
	ventilator_nastaveni(0,0);
}







unsigned char najdi_cidlo(unsigned char high, unsigned low)
{
	unsigned char x;
	
	
	
	for(x=0;x<ds1820_devices;x++)
	{
		CALC_CRC(ds1820_rom_codes[x],10);
		
		if(high==CRCHi && low==CRCLo) return x;
	}
	
	return MAX_DS1820;
}









void save_int()
{
}

void save()
{
	unsigned char x;
	
	save_int();
	
	
	
	
	
	CALC_CRC(ds1820_rom_codes[index_temp_svetlo],10);
	gm[TCS_H]=CRCHi;
	gm[TCS_L]=CRCLo;
	
	CALC_CRC(ds1820_rom_codes[index_temp_okoli],10);
	gm[TCP_H]=CRCHi;
	gm[TCP_L]=CRCLo;
	
	
	
	
	
	
	
	
	CALC_CRC(gm,AMOUNT);
	gm[CRC_H]=CRCHi;
	gm[CRC_L]=CRCLo;
	
	for(x=0;x<AMOUNT;x++) gme[x]=gm[x];
	
	while(1);
}

void load_int()
{
	
}

unsigned char load()
{
	unsigned char x;
	
	for(x=0;x<AMOUNT;x++) gm[x]=gme[x];
	
	load_int();
	
	CALC_CRC(gm,AMOUNT);
	if(gm[CRC_H]!=CRCHi || gm[CRC_L]!=CRCLo) return 1;
	else return 0;
}

void load_default()
{
	gm[VENT_CERPADLO]=50;
	gm[VENT_230]=5;
	gm[VENT_DELAY]=10;
	gm[VENT_REPEAT]=10;
	gm[VENT_CHOD]=3;
	
	gm[SVETLO_CHOD]=1;
	gm[SVETLO_INTENZITA]=5;
	gm[SVETLO_CUSTOM]=12;
	gm[SVETLO_PRE]=2;
	gm[SVETLO_LIM]=45;
	gm[SVETLO_MAX]=60;
	
	gm[FAN_1]=2;
	gm[FAN_2]=2;
	gm[FAN_1_OT]=50;
	gm[FAN_2_OT]=50;
	gm[FAN_1_DH]=20;
	gm[FAN_1_SH]=25;
	gm[FAN_1_HH]=30;
	gm[FAN_2_DH]=20;
	gm[FAN_2_SH]=30;
	gm[FAN_2_HH]=40;
	
	gm[VENT_12]=20;
	
	gm[HLADINA_LIM]=10;
	gm[HLADINA_MM]=250;	//*2=500mm max
	gm[HLADINA_D1H]=0;
	gm[HLADINA_D1L]=0;
	gm[HLADINA_D2H]=1023>>8;
	gm[HLADINA_D2L]=(unsigned char)1023;
	
	gm[VENT_MLH]=0;
	gm[VENT_MLL]=100;
	
	gm[TCS_H]=0;
	gm[TCS_L]=0;
	gm[TCP_H]=0;
	gm[TCP_L]=0;
	
	gm[PRUTOK_NAS]=1;
	
	
	
	
	
	
	
	
	load_int();
	
	CALC_CRC(gm,AMOUNT);
	gm[CRC_H]=CRCHi;
	gm[CRC_L]=CRCLo;
}




















void main(void)
{
	unsigned char key='M';
	unsigned char temp1;
	unsigned char temp2;
	

	unsigned int hladina_mm=0;

	unsigned int hladina_d=0;
	unsigned int hladina_dd=0;
	
	unsigned char svetlo;
	
	unsigned char counter_hladina=0;
	unsigned char x;
	
	
	// Input/Output Ports initialization
	// Port A initialization
	// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=Out 
	// State7=P State6=P State5=P State4=T State3=T State2=1 State1=1 State0=1 
	PORTA=0xE7;
	DDRA=0x07;

	// Port B initialization
	// Func7=Out Func6=In Func5=Out Func4=Out Func3=Out Func2=In Func1=Out Func0=Out 
	// State7=1 State6=P State5=1 State4=1 State3=0 State2=P State1=1 State0=1 
	PORTB=0xF7;
	DDRB=0xBB;

	// Port C initialization
	// Func7=In Func6=In Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
	// State7=P State6=P State5=1 State4=1 State3=0 State2=0 State1=0 State0=0 
	PORTC=0xF0;
	DDRC=0x3F;


	
	
	// Port D initialization
	// Func7=Out Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=Out Func0=In 
	// State7=0 State6=P State5=0 State4=0 State3=P State2=P State1=1 State0=P 
	PORTD=0x4F;
	DDRD=0xB2;


	// PWM
	// Timer/Counter 0 initialization
	// Clock source: System Clock
	// Clock value: 43.200 kHz
	// Mode: Fast PWM top=0xFF
	// OC0 output: Non-Inverted PWM
	//TCCR0=0x6C;
	//TCNT0=0x00;
	//OCR0=0x00;

	// BUZZER 4khz
	// Timer/Counter 0 initialization
	// Clock source: System Clock
	// Clock value: 172.800 kHz
	// Mode: CTC top=OCR0
	// OC0 output: Toggle on compare match
	//TCCR0=0x1B;		//0x0B	OC0 output: Disconnected
	//TCNT0=0x00;
	//OCR0=0x2B;

	// Timer/Counter 0 initialization
	// Clock source: System Clock
	// Clock value: Timer 0 Stopped
	// Mode: Normal top=0xFF
	// OC0 output: Disconnected
	TCCR0=0x0B;
	TCNT0=0x00;
	OCR0=0x2B;



	// Timer/Counter 1 initialization
	// Clock source: System Clock
	// Clock value: 11059.200 kHz
	// Mode: Fast PWM top=ICR1
	// OC1A output: Non-Inv.
	// OC1B output: Non-Inv.
	// Noise Canceler: Off
	// Input Capture on Falling Edge
	// Timer1 Overflow Interrupt: Off
	// Input Capture Interrupt: Off
	// Compare A Match Interrupt: Off
	// Compare B Match Interrupt: Off
	TCCR1A=0xA2;
	TCCR1B=0x19;
	TCNT1H=0x00;
	TCNT1L=0x00;
	ICR1H=0x01;
	ICR1L=0xB9;
	OCR1AH=0x00;
	OCR1AL=0x00;
	OCR1BH=0x00;
	OCR1BL=0xDC;

	// Timer/Counter 2 initialization
	// Clock source: System Clock
	// Clock value: 43.200 kHz
	// Mode: Fast PWM top=0xFF
	// OC2 output: Disconnected
	ASSR=0x00;
	TCCR2=0x4E;			//0x6E		OC2 output: Non-Inverted PWM
	TCNT2=0x00;
	OCR2=0x00;

	// External Interrupt(s) initialization
	// INT0: On
	// INT0 Mode: Falling Edge
	// INT1: On
	// INT1 Mode: Falling Edge
	// INT2: On
	// INT2 Mode: Falling Edge
	GICR|=0xE0;
	MCUCR=0x0A;
	MCUCSR=0x00;
	GIFR=0xE0;

	// Timer(s)/Counter(s) Interrupt(s) initialization
	TIMSK=0x20;

	// USART initialization
	// Communication Parameters: 8 Data, 1 Stop, No Parity
	// USART Receiver: On
	// USART Transmitter: On
	// USART Mode: Asynchronous
	// USART Baud Rate: 9600
	UCSRA=0x00;
	UCSRB=0xD8;
	UCSRC=0x86;
	UBRRH=0x00;
	UBRRL=0x47;

	// Analog Comparator initialization
	// Analog Comparator: Off
	// Analog Comparator Input Capture by Timer/Counter 1: Off
	ACSR=0x80;
	SFIOR=0x00;

	// ADC initialization
	// ADC Clock frequency: 172.800 kHz
	// ADC Voltage Reference: Int., cap. on AREF
	// ADC Auto Trigger Source: ADC Stopped
	ADMUX=ADC_VREF_TYPE & 0xff;
	ADCSRA=0x86;

	// SPI initialization
	// SPI disabled
	SPCR=0x00;

	// TWI initialization
	// TWI disabled
	TWCR=0x00;

	// 1 Wire Bus initialization
	// 1 Wire Data port: PORTC
	// 1 Wire Data bit: 7
	// Note: 1 Wire port settings must be specified in the
	// Project|Configure|C Compiler|Libraries|1 Wire IDE menu.
	w1_init();

	// Determine the number of DS1820 devices
	// connected to the 1 Wire bus
	//ds1820_devices=w1_search(0xf0,ds1820_rom_codes);

	// Alphanumeric LCD initialization
	// Connections specified in the
	// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
	// RS - PORTA Bit 2
	// RD - PORTB Bit 7
	// EN - PORTA Bit 1
	// D4 - PORTB Bit 5
	// D5 - PORTA Bit 0
	// D6 - PORTB Bit 4
	// D7 - PORTB Bit 1
	// Characters/line: 16
	lcd_init(16);

	// Watchdog Timer initialization
	// Watchdog Timer Prescaler: OSC/2048k
	#pragma optsize-
	WDTCR=0x1F;
	WDTCR=0x0F;
	#ifdef _OPTIMIZE_SIZE_
	#pragma optsize+
	#endif

	// Global enable interrupts
	#asm("sei")

	

	
	
	
	
	lcd_clear();
	lcd_putsf("    M.Vrabec    ");
	lcd_putsf("  RJPAPS v1.3   ");
	
	
	
	
	
	
	
	if(load() || key_read(0)=='M')
	{
		load_default();
		
		lcd_clear();
		lcd_putsf("    Restart     ");
		
		delay_ms(1000);
		
		save();
	}
	
	delay_ms(2000);
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	#asm("cli")
	ds1820_devices=w1_search(0xf0,ds1820_rom_codes);
	if(ds1820_devices)
		for(x=0;x<ds1820_devices;x++)
			ds18b20_init(ds1820_rom_codes[x],0,100,DS18B20_9BIT_RES);
	#asm("sei")
	
	
	
	
	
	
	
	
	
	
	
	
	
	svetlo=svetlo_nastaveni(0);
	ventilator_nastaveni(0,0);
	
	index_temp_svetlo=najdi_cidlo(gm[TCS_H],gm[TCS_L]);
	index_temp_okoli=najdi_cidlo(gm[TCP_H],gm[TCP_L]);
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	while(1)
	{
		#asm("wdr")
		
		
		


		
		
		if(_provoz_svetlo)
		{
			if(step_svetlo>=((unsigned int)24*360) && TCCR2!=0x6E)
			{
				TCCR2=0x6E;
				step_svetlo=0;
			}
			if(step_svetlo>=((unsigned int)svetlo*360) && TCCR2!=0x4E) TCCR2=0x4E;
		}
		
		
		
		if(!FOTOB1 && _vent_foto_cerpadlo)
		{
			if((gm[VENT_CHOD]==1 || gm[VENT_CHOD]==3) && provoz_stage==1)
			{
				step_provoz=0;
				POWER1=1;
				provoz_stage++;
			}
		}
		
		if(!FOTOB2 && _vent_foto_12)
		{
			PORT1=0;
		}

		
		
		
		
		
		if(step_hladina)
		{
			step_hladina=0;
			
			hladina_d+=read_adc(ADC1);
			counter_hladina++;
			
			if(counter_hladina>=10)
			{
				hladina_d/=counter_hladina;
				counter_hladina=0;
				
				if(hladina_d>(((unsigned int)gm[HLADINA_D1H]<<8)+gm[HLADINA_D1L]))
					hladina_dd=hladina_d-(((unsigned int)gm[HLADINA_D1H]<<8)+gm[HLADINA_D1L]);
				else hladina_dd=0;
				hladina_d=0;
				
				
				
				hladina_mm=(unsigned int)((float)hladina_dd*gm[HLADINA_MM]*2/(((unsigned int)gm[HLADINA_D2H]<<8)+gm[HLADINA_D2L]));

				
				
				
				
				
				
				if(menu_lvl && menu_1==5 && (menu_2==3 || menu_2==2))
				{
					sprintf(lcd_buffer,"%4u",hladina_dd);
					lcd_gotoxy(8,1);
					lcd_puts(lcd_buffer);
				}
				if(menu_1==4 && !menu_lvl)
				{
					sprintf(lcd_buffer,"%10u",hladina_mm);
					lcd_gotoxy(0,1);
					lcd_puts(lcd_buffer);
				}
			}
		}

		
		
		
		
		
		if(_teplomer)
		{
			_teplomer=0;
			
			#asm("cli")
			
			for(x=0;x<ds1820_devices;x++) ds1820_temp[x]=(signed char)(ds18b20_temperature(ds1820_rom_codes[x]));
			
			#asm("sei")
			
			
			
			
			
			
			
			
			
			if(gm[FAN_1]==2)
			{
				if(ds1820_temp[index_temp_okoli]>gm[FAN_1_HH])
				{
					_fan_1=0;
					temp1=100;
				}
				else if(ds1820_temp[index_temp_okoli]<gm[FAN_1_DH])
				{
					_fan_1=1;
					temp1=0;
				}
				else if(ds1820_temp[index_temp_okoli]>gm[FAN_1_SH])
				{
					if(_fan_1)		temp1=80;
					else			temp1=70;
				}
				else if(ds1820_temp[index_temp_okoli]<gm[FAN_1_SH])
				{
					if(_fan_1)		temp1=60;
					else			temp1=50;
				}
			}
			else temp1=gm[FAN_1_OT];
			
			if(gm[FAN_2]==2)
			{
				if(ds1820_temp[index_temp_okoli]>gm[FAN_2_HH])
				{
					_fan_2=0;
					temp2=100;
				}
				else if(ds1820_temp[index_temp_okoli]<gm[FAN_2_DH])
				{
					_fan_2=1;
					temp2=0;
				}
				else if(ds1820_temp[index_temp_okoli]>gm[FAN_2_SH])
				{
					if(_fan_2)		temp2=80;
					else			temp2=70;
				}
				else if(ds1820_temp[index_temp_okoli]<gm[FAN_2_SH])
				{
					if(_fan_2)		temp2=60;
					else			temp2=50;
				}
			}
			else temp2=gm[FAN_2_OT];
			
			ventilator_nastaveni(temp1,temp2);
			
			
			
			
			
			
			
			
			
			
			
			//svetlo rizeni
			{
				if(ds1820_temp[index_temp_svetlo]>gm[SVETLO_MAX])		svetlo_nastaveni(0);
				else if(ds1820_temp[index_temp_svetlo]>gm[SVETLO_LIM])	svetlo_nastaveni(gm[SVETLO_INTENZITA]/2);
				else 													svetlo_nastaveni(gm[SVETLO_INTENZITA]);
			}
			
			
			
			
			
			
			
			
			
			if(menu_1==7 && menu_2==3)
			{
				sprintf(lcd_buffer,"%5u",ds1820_temp[index_temp_svetlo]);
				lcd_gotoxy(8,1);
				lcd_puts(lcd_buffer);
			}
			else if(menu_1==7 && menu_2==4)
			{
				sprintf(lcd_buffer,"%5u",ds1820_temp[index_temp_okoli]);
				lcd_gotoxy(8,1);
				lcd_puts(lcd_buffer);
			}
			else if(menu_2==0 && menu_1==3)
			{
				sprintf(lcd_buffer,"Svetlo:%5u \xDFC Okoli:%6u \xDFC",ds1820_temp[index_temp_svetlo],ds1820_temp[index_temp_okoli]);
				lcd_clear();
				lcd_puts(lcd_buffer);
			}
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		if(key)
		{
			switch(key)
			{
				case 'M':
				{
					if(KEY_SET_2)
					{
						lcd_clear();
						lcd_putsf("     Saved      ");
						
						delay_ms(1000);
						
						save();
					}
					else if(KEY_SET_1)
					{
						if(menu_lvl && menu_2==2 && menu_1==1)
						{
							_vent_01=1;
							step_provoz=0;
							provoz_stage=1;
							POWER1=0;
						}
						else if(menu_lvl && menu_2==3 && menu_1==1)
						{
							_vent_02=1;
							step_provoz=0;
							provoz_stage=3;
							POWER2=0;
						}
						else if(menu_lvl && menu_2==2 && menu_1==5)
						{
							gm[HLADINA_D1H]=hladina_dd>>8;
							gm[HLADINA_D1L]=hladina_dd;
						}
						else if(menu_lvl && menu_2==3 && menu_1==5)
						{
							gm[HLADINA_D2H]=hladina_dd>>8;
							gm[HLADINA_D2L]=hladina_dd;
						}
						else if(menu_lvl && menu_2==2 && menu_1==4)
						{
							PORT1=1;
							step_ventil=0;
							
							if(FOTOB2) _vent_foto_12=1;
							else _vent_foto_12=0;
						}
						else if(menu_lvl && menu_2==2 && menu_1==6)
						{
							gm[VENT_MLH]=prutok_100ul>>8;
							gm[VENT_MLL]=(unsigned char)prutok_100ul;
						}
						else if(menu_lvl && menu_2==3 && menu_1==6)
						{
							prutok_100ul=0;
						}
						else if(menu_lvl && menu_2==2 && menu_1==7)
						{
							#asm("cli")
							ds1820_devices=w1_search(0xf0,ds1820_rom_codes);
							
							if(ds1820_devices)
								for(x=0;x<ds1820_devices;x++)
									ds18b20_init(ds1820_rom_codes[x],0,100,DS18B20_9BIT_RES);
								
								
							#asm("sei")
							
							index_temp_svetlo=najdi_cidlo(gm[TCS_H],gm[TCS_L]);
							index_temp_okoli=najdi_cidlo(gm[TCP_H],gm[TCP_L]);
						}
						
						

						
						
						
						if(!menu_lvl)
						{
							_provoz=0;
							menu_lvl=1;
							menu_1=1;
							
							POWER1=1;
							POWER2=1;
						}
						else if(!menu_2) menu_2=1;
						else if(menu_lvl && menu_2==1) menu_2=0;
					}
					else
					{
						if(!menu_lvl)
						{
							if(menu_1<menu_pol[1]) menu_1++;
							else menu_1=1;
						}
						else if(menu_2)
						{
							if(menu_2<menu_pol[menu_1+1]) menu_2++;
							else menu_2=1;
						}
						else
						{
							if(menu_1<menu_pol[0]) menu_1++;
							else menu_1=1;
						}
						
						
						
						
						
						
						
						
						if(menu_1==1 && menu_2==7 && (gm[VENT_CHOD]==0 || gm[VENT_CHOD]==3))
							menu_2=1;
							
						if(menu_1==3)
						{
							if(gm[FAN_1]==0 && menu_2==3) menu_2=7;
							if(gm[FAN_2]==0 && menu_2==8) menu_2=1;
							
							if(gm[FAN_1]==1 && menu_2==4) menu_2=7;
							if(gm[FAN_2]==1 && menu_2==9) menu_2=1;
							
							if(gm[FAN_1]==2 && menu_2==3) menu_2=4;
							if(gm[FAN_2]==2 && menu_2==8) menu_2=9;
						}
						
						
						
						
						
						
					}
					
					
					if(_provoz) temp=13;
					else temp=0;
					
					
					if(!menu_lvl)
					{
						if(menu_1==1)
						{
							if(_provoz)		sprintf(lcd_buffer,"     RJPAPS         Zapnuto <<<");
							else			sprintf(lcd_buffer,"     RJPAPS     >>> Vypnuto    ");
						}
						else if(menu_1==2)
						{
							sprintf(lcd_buffer,"Fan 1:%6u rpmFan 2:%6u rpm",0,0);
						}
						else if(menu_1==3)
						{
							sprintf(lcd_buffer,"Svetlo:%5u \xDFC Okoli:%6u \xDFC",ds1820_temp[index_temp_svetlo],ds1820_temp[index_temp_okoli]);
						}
						else if(menu_1==4)
						{
							sprintf(lcd_buffer,"Hladina Nadrze: %10u mm",hladina_mm);
						}
					}
					else
					{
						if(menu_2==1)
						{
							sprintf(lcd_buffer," Navrat do menu ");
						}
						else if(menu_2)
						{
							switch(menu_1)
							{
								case 1:
								{
									if(menu_2==2)		sprintf(lcd_buffer,"Inter. Cerpadlo:%8u.%u s",gm[VENT_CERPADLO]/10,gm[VENT_CERPADLO]%10);
									else if(menu_2==3)	sprintf(lcd_buffer,"Interval Vent:  %8u.%u s",gm[VENT_230]/10,gm[VENT_230]%10);
									else if(menu_2==4)	sprintf(lcd_buffer,"Spozdeni Vent:  %8u.%u s",gm[VENT_DELAY]/10,gm[VENT_DELAY]%10);
									else if(menu_2==5)
									{
										temp=((unsigned int)gm[VENT_REPEAT]*10)+(2*gm[VENT_DELAY])+gm[VENT_CERPADLO]+gm[VENT_230];
										sprintf(lcd_buffer,"Rychlost Vent:  %8u.%u s",temp/10,temp%10);
									}
									else if(menu_2==6)	sprintf(lcd_buffer,"Rizeni chodu:   %16s",text_1[gm[VENT_CHOD]]);
									else if(menu_2==7)
									{
										temp=((unsigned int)gm[VENT_MLH]<<8)+gm[VENT_MLL];
										sprintf(lcd_buffer,"Mnozstvi vody:  %8u.%u ml",temp/10,temp%10);
									}
								}break;
								
								case 2:
								{
									if(menu_2==2)		sprintf(lcd_buffer,"Chod svetla:    %16s",text_2[gm[SVETLO_CHOD]]);
									else if(menu_2==3)
									{
										if(gm[SVETLO_CHOD]==1) temp=text_3[gm[SVETLO_PRE]];
										if(gm[SVETLO_CHOD]==2) temp=gm[SVETLO_CUSTOM];
										
										sprintf(lcd_buffer,"Interval:       %02uh ON / %02uh OFF",temp,24-temp);
									}
									else if(menu_2==4)	sprintf(lcd_buffer,"Intenzita:      %10u %%",gm[SVETLO_INTENZITA]);
									else if(menu_2==5)	sprintf(lcd_buffer,"Limitni teplota:%10u \xDFC",gm[SVETLO_LIM]);
									else if(menu_2==6)	sprintf(lcd_buffer,"Max. teplota:   %10u \xDFC",gm[SVETLO_MAX]);
								}break;
								
								case 3:
								{
									if(menu_2==2)		sprintf(lcd_buffer,"Ventilator 1:   %16s",text_4[gm[FAN_1]]);
									else if(menu_2==3)	sprintf(lcd_buffer,"Rychlost 1:     %3u %% /%5u rpm",gm[FAN_1_OT],fan_sens_1*60);
									else if(menu_2==4)	sprintf(lcd_buffer,"Dolni hranice:  %10u \xDFC",gm[FAN_1_DH]);
									else if(menu_2==5)	sprintf(lcd_buffer,"Stredni hranice:%10u \xDFC",gm[FAN_1_SH]);
									else if(menu_2==6)	sprintf(lcd_buffer,"Horni hranice:  %10u \xDFC",gm[FAN_1_HH]);
									
									else if(menu_2==7)	sprintf(lcd_buffer,"Ventilator 2:   %16s",text_4[gm[FAN_2]]);
									else if(menu_2==8)	sprintf(lcd_buffer,"Rychlost 2:     %3u %% /%5u rpm",gm[FAN_2_OT],fan_sens_2*60);
									else if(menu_2==9)	sprintf(lcd_buffer,"Dolni hranice:  %10u \xDFC",gm[FAN_2_DH]);
									else if(menu_2==10)	sprintf(lcd_buffer,"Stredni hranice:%10u \xDFC",gm[FAN_2_SH]);
									else if(menu_2==11)	sprintf(lcd_buffer,"Horni hranice:  %10u \xDFC",gm[FAN_2_HH]);
								}break;
								
								case 4:
								{
									if(menu_2==2)		sprintf(lcd_buffer,"Interval Vent12:%8u.%u s",gm[VENT_12]/10,gm[VENT_12]%10);
								}break;
								
								case 5:
								{
									if(menu_2==2)
									{
										sprintf(lcd_buffer,"Hladina Min:    %3u mm /%4u dig",0,hladina_dd);
									}
									else if(menu_2==3)
									{
										sprintf(lcd_buffer,"Hladina Manual: %3u mm /%4u dig",(unsigned int)gm[HLADINA_MM]*2,hladina_dd);
									}										
									else if(menu_2==4)	sprintf(lcd_buffer,"Limitni hladina:%10u mm",gm[HLADINA_LIM]);
								}break;
								
								case 6:
								{
									if(menu_2==2)		sprintf(lcd_buffer,"Prutokomer:     %8u.%u ml",prutok_100ul/10,prutok_100ul%10);
									else if(menu_2==3)	sprintf(lcd_buffer,"Prutokomer:           Reset     ");
									else if(menu_2==4)
									{
										if(!gm[PRUTOK_NAS])	sprintf(lcd_buffer,"Prutokomer:       2500 imp/litr ");
										else 				sprintf(lcd_buffer,"Prutokomer:      10000 imp/litr ");
									}
								}break;
								
								case 7:
								{
									if(menu_2==2)		sprintf(lcd_buffer,"Teplotni cidla: %2u ks / REFRESH ",ds1820_devices);
									else if(menu_2==3)
									{
										CALC_CRC(ds1820_rom_codes[index_temp_svetlo],10);
										sprintf(lcd_buffer,"Cidlo svetla:   [%02X][%02X]%5u \xDFC",CRCHi,CRCLo,ds1820_temp[index_temp_svetlo]);
									}
									else if(menu_2==4)
									{
										CALC_CRC(ds1820_rom_codes[index_temp_okoli],10);
										sprintf(lcd_buffer,"Cidlo prostredi:[%02X][%02X]%5u \xDFC",CRCHi,CRCLo,ds1820_temp[index_temp_okoli]);
									}
								}break;
							}
						}
						else
						{
							if(menu_1==1)		sprintf(lcd_buffer,"Provoz jednotky %2u/%u  +------   ",menu_1,menu_pol[0]);
							else if(menu_1==2)	sprintf(lcd_buffer,"Nastaveni svetla%2u/%u  -+-----   ",menu_1,menu_pol[0]);
							else if(menu_1==3)	sprintf(lcd_buffer,"Chod ventilatoru%2u/%u  --+----   ",menu_1,menu_pol[0]);
							else if(menu_1==4)	sprintf(lcd_buffer,"Provoz davkovani%2u/%u  ---+---   ",menu_1,menu_pol[0]);
							else if(menu_1==5)	sprintf(lcd_buffer,"Mereni hladiny  %2u/%u  ----+--   ",menu_1,menu_pol[0]);
							else if(menu_1==6)	sprintf(lcd_buffer,"Stav prutokomeru%2u/%u  -----+-   ",menu_1,menu_pol[0]);
							else if(menu_1==7)	sprintf(lcd_buffer,"Teplotni cidla  %2u/%u  ------+   ",menu_1,menu_pol[0]);
						}
					}
					
					lcd_clear();
					lcd_puts(lcd_buffer);
				}break;
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				case 'R':
				{
					if(KEY_SET_2)
					{
					}
					if(KEY_SET_1)
					{
					}
					else
					{
						if(!menu_lvl)
						{
							if(menu_1==1)
							{
								lcd_gotoxy(0,1);
								lcd_putsf("                ");
								if(temp<13) temp++;
								
								if(_provoz)
								{
									if(temp==13)
									{
										zapnout();
										
										lcd_gotoxy(0,1);
										lcd_putsf("     Zapnuto <<<");
									}
									else
									{
										lcd_gotoxy(temp,1);
										lcd_putsf("<<<");
									}
								}
								else
								{
									if(temp==13)
									{
										zapnout();
										
										lcd_gotoxy(0,1);
										lcd_putsf("     Zapnuto <<<");
									}
									else if(temp)
									{
										lcd_gotoxy(temp,1);
										lcd_putsf(">>>");
									}
								}
							}
						}
						else
						{
							if(menu_2)
							{
								switch(menu_1)
								{
									case 1:
									{
										if(menu_2==2)
										{
											if(gm[VENT_CERPADLO]<100) gm[VENT_CERPADLO]++;
											
											sprintf(lcd_buffer,"%8u.%u",gm[VENT_CERPADLO]/10,gm[VENT_CERPADLO]%10);
										}
										else if(menu_2==3)
										{
											if(gm[VENT_230]<100) gm[VENT_230]++;
											
											sprintf(lcd_buffer,"%8u.%u",gm[VENT_230]/10,gm[VENT_230]%10);
										}
										else if(menu_2==4)
										{
											if(gm[VENT_DELAY]<100) gm[VENT_DELAY]++;
											
											sprintf(lcd_buffer,"%8u.%u",gm[VENT_DELAY]/10,gm[VENT_DELAY]%10);
										}
										else if(menu_2==5)
										{
											if(gm[VENT_REPEAT]<250) gm[VENT_REPEAT]++;
											
											temp=((unsigned int)gm[VENT_REPEAT]*10)+(2*gm[VENT_DELAY])+gm[VENT_CERPADLO]+gm[VENT_230];
											
											sprintf(lcd_buffer,"%8u.%u",temp/10,temp%10);
										}
										else if(menu_2==6)
										{
											if(gm[VENT_CHOD]<3) gm[VENT_CHOD]++;
											
											sprintf(lcd_buffer,"%16s",text_1[gm[VENT_CHOD]]);
										}
										else if(menu_2==7)
										{
											temp=((unsigned int)gm[VENT_MLH]<<8)+gm[VENT_MLL];
											
											if(temp<20000) temp++;
											
											sprintf(lcd_buffer,"%8u.%u",temp/10,temp%10);
											
											gm[VENT_MLH]=temp>>8;
											gm[VENT_MLL]=(unsigned char)temp;
										}
									}break;
									
									case 2:
									{
										if(menu_2==2)
										{
											if(gm[SVETLO_CHOD]<2) gm[SVETLO_CHOD]++;
											
											sprintf(lcd_buffer,"%16s",text_2[gm[SVETLO_CHOD]]);
										}
										else if(menu_2==3)
										{
											if(gm[SVETLO_CHOD]==1)
											{
												if(gm[SVETLO_PRE]<2) gm[SVETLO_PRE]++;
												temp=text_3[gm[SVETLO_PRE]];
											}
											if(gm[SVETLO_CHOD]==2)
											{
												if(gm[SVETLO_CUSTOM]<24) gm[SVETLO_CUSTOM]++;
												temp=gm[SVETLO_CUSTOM];
											}
											
											sprintf(lcd_buffer,"%02uh ON / %02u",temp,24-temp);
										}
										else if(menu_2==4)
										{
											if(gm[SVETLO_INTENZITA]<100) gm[SVETLO_INTENZITA]++;
											
											sprintf(lcd_buffer,"%10u",gm[SVETLO_INTENZITA]);
											
											TCCR2=0x6E;
										}
										else if(menu_2==5)
										{
											if(gm[SVETLO_LIM]<150) gm[SVETLO_LIM]++;
											
											sprintf(lcd_buffer,"%10u",gm[SVETLO_LIM]);
										}
										else if(menu_2==6)
										{
											if(gm[SVETLO_MAX]<150) gm[SVETLO_MAX]++;
											
											sprintf(lcd_buffer,"%10u",gm[SVETLO_MAX]);
										}
										
										svetlo=svetlo_nastaveni(gm[SVETLO_INTENZITA]);
										test_teplot_svetlo('+');
									}break;
									
									case 3:
									{
										if(menu_2==2)
										{
											if(gm[FAN_1]<2) gm[FAN_1]++;
											
											sprintf(lcd_buffer,"%16s",text_4[gm[FAN_1]]);
										}
										else if(menu_2==3)
										{
											if(gm[FAN_1_OT]<100) gm[FAN_1_OT]++;
											
											sprintf(lcd_buffer,"%3u %% /%5u rpm",gm[FAN_1_OT],fan_sens_1*60);
										}
										else if(menu_2==4)
										{
											if(gm[FAN_1_DH]<150) gm[FAN_1_DH]++;
											
											sprintf(lcd_buffer,"%10u",gm[FAN_1_DH]);
										}
										else if(menu_2==5)
										{
											if(gm[FAN_1_SH]<150) gm[FAN_1_SH]++;
											
											sprintf(lcd_buffer,"%10u",gm[FAN_1_SH]);
										}
										else if(menu_2==6)
										{
											if(gm[FAN_1_HH]<150) gm[FAN_1_HH]++;
											
											sprintf(lcd_buffer,"%10u",gm[FAN_1_HH]);
										}
										else if(menu_2==7)
										{
											if(gm[FAN_2]<2) gm[FAN_2]++;
											
											sprintf(lcd_buffer,"%16s",text_4[gm[FAN_2]]);
										}
										else if(menu_2==8)
										{
											if(gm[FAN_2_OT]<100) gm[FAN_2_OT]++;
											
											sprintf(lcd_buffer,"%3u %% /%5u rpm",gm[FAN_2_OT],fan_sens_2*60);
										}
										else if(menu_2==9)
										{
											if(gm[FAN_2_DH]<150) gm[FAN_2_DH]++;
											
											sprintf(lcd_buffer,"%10u",gm[FAN_2_DH]);
										}
										else if(menu_2==10)
										{
											if(gm[FAN_2_SH]<150) gm[FAN_2_SH]++;
											
											sprintf(lcd_buffer,"%10u",gm[FAN_2_SH]);
										}
										else if(menu_2==11)
										{
											if(gm[FAN_2_HH]<150) gm[FAN_2_HH]++;
											
											sprintf(lcd_buffer,"%10u",gm[FAN_2_HH]);
										}
										
										ventilator_nastaveni(gm[FAN_1_OT],gm[FAN_2_OT]);
										test_teplot_fan('+');
									}break;
									
									case 4:
									{
										if(menu_2==2)
										{
											if(gm[VENT_12]<100) gm[VENT_12]++;
											
											sprintf(lcd_buffer,"%8u.%u",gm[VENT_12]/10,gm[VENT_12]%10);
										}
									}break;
									
									case 5:
									{
										if(menu_2==3)
										{
											if(gm[HLADINA_MM]<250) gm[HLADINA_MM]++;
											
											sprintf(lcd_buffer,"%3u",(unsigned int)gm[HLADINA_MM]*2);
										}
										else if(menu_2==4)
										{
											if(gm[HLADINA_LIM]<250) gm[HLADINA_LIM]++;
											
											sprintf(lcd_buffer,"%10u",gm[HLADINA_LIM]);
										}
										else lcd_buffer[0]=0;
									}break;
									
									case 6:
									{
										if(menu_2==4)
										{
											gm[PRUTOK_NAS]=1;
											sprintf(lcd_buffer," 10000");
										}
										else lcd_buffer[0]=0;
									}break;
									
									case 7:
									{
										if(menu_2==3)
										{
											if(index_temp_svetlo<MAX_DS1820) index_temp_svetlo++;
											
											CALC_CRC(ds1820_rom_codes[index_temp_svetlo],10);
											sprintf(lcd_buffer,"[%02X][%02X]%5u \xDFC",CRCHi,CRCLo,ds1820_temp[index_temp_svetlo]);
										}
										else if(menu_2==4)
										{
											if(index_temp_okoli<MAX_DS1820) index_temp_okoli++;
											
											CALC_CRC(ds1820_rom_codes[index_temp_okoli],10);
											sprintf(lcd_buffer,"[%02X][%02X]%5u \xDFC",CRCHi,CRCLo,ds1820_temp[index_temp_okoli]);
										}
										else lcd_buffer[0]=0;
									}break;
								}
								
								lcd_gotoxy(0,1);
								lcd_puts(lcd_buffer);
							}
						}
					}
				}break;
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				case 'L':
				{
					if(KEY_SET_2)
					{
					}
					if(KEY_SET_1)
					{
					}
					else
					{
						if(!menu_lvl)
						{
							if(menu_1==1)
							{
								lcd_gotoxy(0,1);
								lcd_putsf("                ");
								if(temp) temp--;
								
								if(_provoz)
								{
									if(!temp)
									{
										vypnout();
										
										lcd_gotoxy(0,1);
										lcd_putsf(">>> Vypnuto     ");
									}
									else
									{
										lcd_gotoxy(temp,1);
										lcd_putsf("<<<");
									}
								}
								else
								{
									if(!temp)
									{
										vypnout();
										
										lcd_gotoxy(0,1);
										lcd_putsf(">>> Vypnuto     ");
									}
									else
									{
										lcd_gotoxy(temp,1);
										lcd_putsf(">>>");
									}
								}
							}
						}
						else
						{
							if(menu_2)
							{
								switch(menu_1)
								{
									case 1:
									{
										if(menu_2==2)
										{
											if(gm[VENT_CERPADLO]>1) gm[VENT_CERPADLO]--;
											
											sprintf(lcd_buffer,"%8u.%u",gm[VENT_CERPADLO]/10,gm[VENT_CERPADLO]%10);
										}
										else if(menu_2==3)
										{
											if(gm[VENT_230]>1) gm[VENT_230]--;
											
											sprintf(lcd_buffer,"%8u.%u",gm[VENT_230]/10,gm[VENT_230]%10);
										}
										else if(menu_2==4)
										{
											if(gm[VENT_DELAY]>1) gm[VENT_DELAY]--;
											
											sprintf(lcd_buffer,"%8u.%u",gm[VENT_DELAY]/10,gm[VENT_DELAY]%10);
										}
										else if(menu_2==5)
										{
											if(gm[VENT_REPEAT]) gm[VENT_REPEAT]--;
											
											temp=((unsigned int)gm[VENT_REPEAT]*10)+(2*gm[VENT_DELAY])+gm[VENT_CERPADLO]+gm[VENT_230];
											
											sprintf(lcd_buffer,"%8u.%u",temp/10,temp%10);
										}
										else if(menu_2==6)
										{
											if(gm[VENT_CHOD]) gm[VENT_CHOD]--;
											
											sprintf(lcd_buffer,"%16s",text_1[gm[VENT_CHOD]]);
										}
										else if(menu_2==7)
										{
											temp=((unsigned int)gm[VENT_MLH]<<8)+gm[VENT_MLL];
											
											if(temp) temp--;
											
											sprintf(lcd_buffer,"%8u.%u",temp/10,temp%10);
											
											gm[VENT_MLH]=temp>>8;
											gm[VENT_MLL]=(unsigned char)temp;
										}
									}break;
									
									case 2:
									{
										if(menu_2==2)
										{
											if(gm[SVETLO_CHOD]) gm[SVETLO_CHOD]--;
											
											sprintf(lcd_buffer,"%16s",text_2[gm[SVETLO_CHOD]]);
										}
										else if(menu_2==3)
										{
											if(gm[SVETLO_CHOD]==1)
											{
												if(gm[SVETLO_PRE]) gm[SVETLO_PRE]--;
												temp=text_3[gm[SVETLO_PRE]];
											}
											if(gm[SVETLO_CHOD]==2)
											{
												if(gm[SVETLO_CUSTOM]) gm[SVETLO_CUSTOM]--;
												temp=gm[SVETLO_CUSTOM];
											}
											
											sprintf(lcd_buffer,"%02uh ON / %02u",temp,24-temp);
										}
										else if(menu_2==4)
										{
											if(gm[SVETLO_INTENZITA]>1) gm[SVETLO_INTENZITA]--;
											
											sprintf(lcd_buffer,"%10u",gm[SVETLO_INTENZITA]);
										}
										else if(menu_2==5)
										{
											if(gm[SVETLO_LIM]) gm[SVETLO_LIM]--;
											
											sprintf(lcd_buffer,"%10u",gm[SVETLO_LIM]);
										}
										else if(menu_2==6)
										{
											if(gm[SVETLO_MAX]) gm[SVETLO_MAX]--;
											
											sprintf(lcd_buffer,"%10u",gm[SVETLO_MAX]);
										}
										
										test_teplot_svetlo('-');
										svetlo=svetlo_nastaveni(gm[SVETLO_INTENZITA]);
									}break;
									
									case 3:
									{
										if(menu_2==2)
										{
											if(gm[FAN_1]) gm[FAN_1]--;
											
											sprintf(lcd_buffer,"%16s",text_4[gm[FAN_1]]);
										}
										else if(menu_2==3)
										{
											if(gm[FAN_1_OT]>40) gm[FAN_1_OT]--;
											
											sprintf(lcd_buffer,"%3u %% /%5u rpm",gm[FAN_1_OT],fan_sens_1*60);
										}
										else if(menu_2==4)
										{
											if(gm[FAN_1_DH]) gm[FAN_1_DH]--;
											
											sprintf(lcd_buffer,"%10u",gm[FAN_1_DH]);
										}
										else if(menu_2==5)
										{
											if(gm[FAN_1_SH]) gm[FAN_1_SH]--;
											
											sprintf(lcd_buffer,"%10u",gm[FAN_1_SH]);
										}
										else if(menu_2==6)
										{
											if(gm[FAN_1_HH]) gm[FAN_1_HH]--;
											
											sprintf(lcd_buffer,"%10u",gm[FAN_1_HH]);
										}
										else if(menu_2==7)
										{
											if(gm[FAN_2]) gm[FAN_2]--;
											
											sprintf(lcd_buffer,"%16s",text_4[gm[FAN_2]]);
										}
										else if(menu_2==8)
										{
											if(gm[FAN_2_OT]>40) gm[FAN_2_OT]--;
											
											sprintf(lcd_buffer,"%3u %% /%5u rpm",gm[FAN_2_OT],fan_sens_2*60);
										}
										else if(menu_2==9)
										{
											if(gm[FAN_2_DH]) gm[FAN_2_DH]--;
											
											sprintf(lcd_buffer,"%10u",gm[FAN_2_DH]);
										}
										else if(menu_2==10)
										{
											if(gm[FAN_2_SH]) gm[FAN_2_SH]--;
											
											sprintf(lcd_buffer,"%10u",gm[FAN_2_SH]);
										}
										else if(menu_2==11)
										{
											if(gm[FAN_2_HH]) gm[FAN_2_HH]--;
											
											sprintf(lcd_buffer,"%10u",gm[FAN_2_HH]);
										}
										
										ventilator_nastaveni(gm[FAN_1_OT],gm[FAN_2_OT]);
										test_teplot_fan('-');
									}break;
									
									case 4:
									{
										if(menu_2==2)
										{
											if(gm[VENT_12]>1) gm[VENT_12]--;
											
											sprintf(lcd_buffer,"%8u.%u",gm[VENT_12]/10,gm[VENT_12]%10);
										}
									}break;
									
									case 5:
									{
										if(menu_2==3)
										{
											if(gm[HLADINA_MM]) gm[HLADINA_MM]--;
											
											sprintf(lcd_buffer,"%3u",(unsigned int)gm[HLADINA_MM]*2);
										}
										else if(menu_2==4)
										{
											if(gm[HLADINA_LIM]) gm[HLADINA_LIM]--;
											
											sprintf(lcd_buffer,"%10u",gm[HLADINA_LIM]);
										}
										else lcd_buffer[0]=0;
									}break;
									
									case 6:
									{
										if(menu_2==4)
										{
											gm[PRUTOK_NAS]=0;
											sprintf(lcd_buffer,"  2500");
										}
										else lcd_buffer[0]=0;
									}break;
									
									case 7:
									{
										if(menu_2==3)
										{
											if(index_temp_svetlo) index_temp_svetlo--;
											
											CALC_CRC(ds1820_rom_codes[index_temp_svetlo],10);
											sprintf(lcd_buffer,"[%02X][%02X]%5u \xDFC",CRCHi,CRCLo,ds1820_temp[index_temp_svetlo]);
										}
										else if(menu_2==4)
										{
											if(index_temp_okoli) index_temp_okoli--;
											
											CALC_CRC(ds1820_rom_codes[index_temp_okoli],10);
											sprintf(lcd_buffer,"[%02X][%02X]%5u \xDFC",CRCHi,CRCLo,ds1820_temp[index_temp_okoli]);
										}
										else lcd_buffer[0]=0;
									}break;
								}
								
								lcd_gotoxy(0,1);
								lcd_puts(lcd_buffer);
							}
						}
					}
					
				}break;
			}
		}
		
		if(KEY_SET)
		{
			KEY_SET=0;
			key=key_read(1);
		}
		else key=0;
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}