#ifndef ir 
#define ir 


#define IR ((PIND & 0b00000100)>>2)	// the infrared receiver has to be connected to an interrupt-pin!

//makra
#define setb(port,pin) port |= 1<<pin 		//nastav bit
#define clrb(port,pin) port &= ~(1<<pin) 	//nuluj bit
#define negb(port,pin) port ^= 1<<pin 		//neguj bit


typedef struct 
{
unsigned char data[2];
unsigned char state;
} rc5_struct;
rc5_struct rc5;

	unsigned char togg=0;	//togle bit zmena pri kazdom stlaceni tl
	unsigned char toggold=0;	//togle bit zmena pri kazdom stlaceni tl


void WriteTimer1(unsigned int d)
	{
	TCNT1=d;
	return;
	}

unsigned int ReadTimer1 (void)
	{
	unsigned int d;
	d=TCNT1;
	return d;
	}

//;************************  IR rc5  ***************************
void get_RC5(void)
{
   unsigned int tmp=0;
   unsigned char t,i;
   setb(PORTC,0);
   clrb(PORTC,0);

	WriteTimer1(0);
	while ((ReadTimer1())<471);//298 431


	WriteTimer1(0);
	tmp|=IR;

	while(!(IR));
   	if (((ReadTimer1())<400) || ((ReadTimer1())>605)) //4000--6050

	return;// 0;// a=0;   // no RC5 code, abort decoding

		while ((ReadTimer1())<961); //881
		WriteTimer1(0);
		tmp<<=1;
		tmp|=IR;


		while ((ReadTimer1())<905);//8809
		WriteTimer1(0);
		tmp<<=1;
		tmp|=IR;



   if (tmp!=0b010) return;// 0;   // no RC5 code, abort decoding



		while ((ReadTimer1())<961);//8810
		WriteTimer1(0);
		tmp<<=1;
		tmp|=IR;
		t=IR;
		
 
    for (i=0;i<11;i++)//11
		{
		while ((ReadTimer1())<1882);//17682
		clrb(PORTC,0);
		WriteTimer1(0);
		tmp<<=1;
		tmp|=IR;
	  	setb(PORTC,0);
		
		}

   			rc5.data[0]=tmp&0b111111;  		// command code
   			rc5.data[1]=(tmp>>6)&0b11111; 	// device address
   			togg=(tmp>>11)&0b1; 	// device address



   	//		togg=(tmp>>11)&0b1; 

	GICR = (0 << INT0);//zakazat ptrerusenie od int0
   			rc5.state = 1;

   return;// 1;
}

#endif 
