#include <avr/io.h>
//**********pocitani casu-pro nekonecnou ridicci smycku**************
ISR(TIMER0_OVF_vect )
	{cas_tmr++;//if(cas_tmr)cas_tmr2++;
	}

void Timer0_init(void)				//timer pro casovou smycku
	{TCCR0=_BV(CS00)|_BV(CS01);//prescaler 64  pro 16MHz~1.024ms
	//TCCR0=_BV(CS01);//prescaler 8
	//TCCR0=_BV(CS00);
	TIMSK|=_BV(TOIE0);
	}

void USART_Init( unsigned int baud )
	{
	UCSRC = (1<<URSEL)|(3<<UCSZ0);
	/* Set baud rate */
	UBRRH = (unsigned char)(baud>>8);
	UBRRL = (unsigned char)baud;
	/* Enable Receiver and Transmitter */
	UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE);
	/* Set frame format: 8data, 1stop bit */

	//UCSRA|=(1<<U2X);
	//TIMSK = 0x05;
	}


void ADC_Init(){
	//ADCSRA=_BV(ADPS2)|_BV(ADEN);       //2MHz
	ADCSRA=_BV(ADPS0)|_BV(ADPS1)|_BV(ADPS2)|_BV(ADEN);  //16MHz
	ADMUX=_BV(ADLAR);
	}

void ADC_Init10b(){//Free running
	//ADCSRA=_BV(ADPS2)|_BV(ADEN);       //2MHz
	ADCSRA=_BV(ADPS0)|_BV(ADPS1)|_BV(ADPS2)|_BV(ADEN)|_BV(ADFR);  //16MHz
	ADMUX&=~_BV(MUX0);
	ADCSRA|=_BV(ADSC);//Free running
	}


void PWM_Init(){
//Fast PWM, 8-bit WGM10 WGM 12
	TCCR1A=(1<<WGM10)|(1<<COM1A1);//Clear OC1A/OC1B on Compare Match, set OC1A/OC1B at TOP
	TCCR1B=(1<<WGM12)|(1<<CS11)|(0<<CS10);// 8kHz clkI/O/8 (From prescaler)
	DDRB|=2;
	OCR1AL=3;
	OCR1AH=0;
	}

void PWM_InA_B_Init(){
//Fast PWM, 8-bit WGM10 WGM 12
	TCCR1A=(1<<WGM10)|(1<<COM1A1);//Clear OC1A/OC1B on Compare Match, set OC1A/OC1B at TOP
	TCCR1B=(1<<WGM12)|(1<<CS11)|(0<<CS10);// 8kHz clkI/O/8 (From prescaler)
	DDRB|=_BV(PIN0)|_BV(PIN1)|_BV(PIN2);
	OCR1AL=3;
	OCR1AH=0;
	}

/*void PWM_Init1(){               //na infra signal
	TCCR1A=(0<<WGM10)|(1<<COM1A0);
	TCCR1B=(1<<WGM12)|(1<<CS10)|(0<<CS12);
	DDRB|=2;
	OCR1A=27;
	}*/
void PWMInfra_Init(){               //na infra signal
	TCCR1A=(0<<WGM10)|(1<<COM1A0);
	TCCR1B=(1<<WGM12)|(1<<CS10)|(0<<CS12);
	DDRB|=2;
	OCR1A=210;
	}

void PWM_PWM_Init(){
	TCCR1A=(1<<WGM10)|(1<<COM1A1)|(1<<COM1B1);
	TCCR1B=(1<<WGM12)|(1<<CS11)|(0<<CS10); //8kHz
	//TCCR1B=(1<<WGM12)|(1<<CS12)|(1<<CS10);	//1kHz
	DDRB|=_BV(PIN1)|_BV(PIN2);
	OCR1AL=0;OCR1BL=0;
	OCR1AH=0;OCR1BH=0;
	}

void ZMustek_Init(void)
{DDRB|=_BV(PIN2)|_BV(PIN1)|_BV(PIN0);
}

