#include <avr/io.h>				// definice periferii procesoru
#include <avr/interrupt.h>		// definice funkci pro rizeni preruseni

#include "ks0108.h"				//knihovna ovladani LCD
#include "arial_bold_14.h"		//jednotlive fonty pisma

#include <util/delay.h>			//knihovna funkce cekani
#define F_CPU 6000000UL  		//krystal 6 MHz

unsigned char preruseni=0, j=0;	//preruseni => priznak preruseni, j => rizeni casu vypisu na LCD

//============================================= >> PRERUSENI KOMPARATOREM <<

ISR(ANA_COMP_vect)
{
	preruseni=1;			//nastaveni priznaku preruseni
}

//============================================= >> MAIN <<

int main(void) 
{
	char vypis_T[10];
	unsigned int vypis=0;
	
	DDRA = 0b11111000;		//pripojeny touch
	DDRD = 0xFF; 			//vystupni - ridici signaly pro LCD
	DDRC = 0xFF;			//vystupni - datove signaly pro LCD
	DDRC = 0x00;

//--------------------------------------------- >> Nastaveni LCD displeje<<

	for(volatile uint16_t i=0; i<15000; i++);		//cekani na nabehnuti LCD
	
	ks0108Init(0);		// inicializace LCD
	
	ks0108SelectFont(Arial_Bold_14, ks0108ReadFontData, BLACK);		//vyber fontu

//--------------------------------------------- >> Nastaveni zakladniho komparatoru<<

	Touch_init();	

//--------------------------------------------- >> 16b T/C 1 <<

	// nastaveno mode 12 - CTC: pri rovnosti s ICR1 dojde k smazani casovace
	TCCR1B	|= ( 1 << WGM13 ) | ( 1 << WGM12 );
	TCCR1A &= ~ ( 1 << WGM11 );
	TCCR1A &= ~ ( 1 << WGM10 );

	ICR1	= 46875;// hranice do ktere se ma citat
	
	TCCR1B	|= ( 1 << CS11 ) | ( 1 << CS10 ); 
	TCCR1B &= ~ ( 1 << CS12 );		// preddelicka clk / 64

	TIMSK	|= ( 1 << TICIE1 );		// povoleni preruseni od casovace

//--------------------------------------------- >> DALSI NASTAVENI <<

	sei();						//globalni povoleni preruseni

//--------------------------------------------- >> VLASTNI PROGRAM <<

	for(;;)
		{
		if (j>0)		//cekani dokud nepretece casovac = dobe 500ms
			{
			ks0108ClearScreen();		//mazani LCD	
			j=0;		//nulovani promenne na rizani casu vypisu
			}
		
		if(preruseni==1)			//pokud nastalo preruseni komparatorem
			{
			cli();					//globalni zakazani preruseni

			vypis=F_stlaceno();		//funkce vracici cislo stlacene plosky

			preruseni=0;			//vynulovani priznaku preruseni
			
			sei();					//globalni povoleni preruseni
			}//if(preruseni==1)

		ks0108GotoXY(40,40);		//pozice vypisu	
		itoa(vypis, vypis_T, 10);	//pretypovani cisla na retezec
		ks0108Puts(vypis_T);		//vypis cisla stlacene plosky
		}//for(;;)



}//int main(void) 

//============================================= >> PRERUSENI CITACEM CASOVACEM <<

SIGNAL ( SIG_INPUT_CAPTURE1 )	//preruseni od casovace (kazdych 500ms)
  {
	j++;		//citani poctu preruseni
  }
