#ifndef _AVR_SYSTIME_COM_H_
#define _AVR_SYSTIME_COM_H_ 1

// /////////////////////////////////////////////////////////////
// pozor 8bit systime tzn. nutno pretypovat v dotazu na hodiny
volatile uint8_t systime8=0x00;
volatile uint32_t systime32=0x00;

// /////////////////////////////////////////////////////////////
volatile uint8_t TEST_last_service = 0;
volatile uint8_t TEST_service_period = 5;

volatile uint8_t usart_last_service = 0;
volatile uint8_t usart_service_period = 1;

volatile uint32_t dc_last_service = 0;
volatile uint32_t dc_service_period = 300;
volatile uint8_t sleep = 0;

volatile uint32_t off_last_service = 0;
volatile uint32_t off_service_period = 30;

void systime_init(void)
{
	PRR0&=~(1<<PRTIM2);	//povoleni casovace 2
	ASSR&=~(1<<EXCLK);	//zaprnuti externiho krystalu
	ASSR|=(1<<AS2);	//zvoleni krystalu tzn. pri 32768hz dojde k preteceni za 7,8125ms
	TIMSK2|=(1<<TOIE2);			//povolení preteceni
	TCCR2B|=(1<<CS22)|(1<<CS20);	// 1s perioda
	while(ASSR&(1<<TCN2UB)); // dokud se casovac nespusti kvuli asynchr. modu
}


ISR(TIMER2_OVF_vect) // kazdych 1s
{
	systime8++;
	systime32++;
}


/// funkce pro zjisteni aktualni hodnoty casove promenne
uint8_t get_systime(void)
{
	volatile uint8_t timex;
	cli();
	timex=systime8;
	sei();
	return(timex);
}

uint32_t get_systime32(void)
{
	volatile uint32_t timex;
	cli();
	timex=systime32;
	sei();
	return(timex);
}


#endif /* _AVR_SYSTIME_COM_H_ */
