/*
 * TCwPWM_drv.h
 *
 * Created: 20.4.2016 15:19:33
 *  Author: Chenda
 */ 


#ifndef TCWPWM_DRV_H_
#define TCWPWM_DRV_H_

// Timer/Counter 4
#define mTimer4Mode_CTC()		TCCR4A&=~(3<<WGM40);TCCR4B&=~(3<<WGM42);TCCR4B|=(1<<WGM42)		//  pocita do hodnoty OCRnA registru a pak se vynuluje
#define mTimer4Mode_NORM()		TCCR4A&=~(3<<WGM40);TCCR4B&=~(3<<WGM42)							//  pocita do hodnoty 0xFFFF a pak se nuluje
#define mTimer4Mode_FPWM_ICR()	TCCR4A&=~(3<<WGM40);TCCR4B&=~(3<<WGM42);TCCR4B|=(3<<WGM42);TCCR4A|=(1<<WGM41)		
#define mTimer4Stop()			TCCR4B&=~(7<<CS40)
#define mTimer4Run_Clk_1()		TCCR4B|=(0<<CS42)|(0<<CS41)|(1<<CS40)
#define mTimer4Run_Clk_8()		TCCR4B|=(0<<CS42)|(1<<CS41)|(0<<CS40)
#define mTimer4Run_Clk_64()		TCCR4B|=(0<<CS42)|(1<<CS41)|(1<<CS40)
#define mTimer4Run_Clk_256()	TCCR4B|=(1<<CS42)|(0<<CS41)|(0<<CS40)
#define mTimer4Run_Clk_1024()	TCCR4B|=(1<<CS42)|(0<<CS41)|(1<<CS40)
#define mTimer4Clear()			TCNT4H=0;TCNT4L=0												//  pri zapisu do TCNTn se nejdrive zapisuje do TCNTnH a az pote do TCNTnL, pri cteni naopak
#define mTimer4IntOvf_Enab()	TIMSK4|=(1<<TOIE4)												//  for normal mode
#define mTimer4IntOvf_Clear()	TIFR4|=(1<<TOV4)												//  for normal mode
#define mTimer4IntOvf_Dis()		TIMSK4&=~(1<<TOIE4)												//  for normal mode
#define mTimer4InpCapRef_Upd(x) ICR4H=((uint8_t)(x>>8));ICR4L=((uint8_t)x)
#define mTimer4IntIcr_Enab()	TIMSK4|=(1<<ICIE4)
#define mTimer4IntIcr_Clear()	TIFR4|=(1<<ICF4)
#define mTimer4IntIcr_Dis()		TIMSK4&=~(1<<ICIE4)
#define mTimer4CompRegA_Upd(x)	OCR4AH=((uint8_t)(x>>8));OCR4AL=((uint8_t)x)
#define mTimer4IntOcrA_Enab()	TIMSK4|=(1<<OCIE4A)
#define mTimer4IntOcrA_Clear()	TIFR4|=(1<<OCF4A)
#define mTimer4IntOcrA_Dis()	TIMSK4&=~(1<<OCIE4A)
#define mTimer4CompRegB_Upd(x)	OCR4BH=((uint8_t)(x>>8));OCR4BL=((uint8_t)x)
#define mTimer4IntOcrB_Enab()	TIMSK4|=(1<<OCIE4B)
#define mTimer4IntOcrB_Clear()	TIFR4|=(1<<OCF4B)
#define mTimer4IntOcrB_Dis()	TIMSK4&=~(1<<OCIE4B)

// Timer/Counter 0
#define mTimer0Mode_CTC()		TCCR0A&=~(3<<WGM00);TCCR0B&=~(3<<WGM02);TCCR0B|=(1<<WGM02)		//  pocita do hodnoty OCRnA registru a pak se vynuluje
#define mTimer0Mode_NORM()		TCCR0A&=~(3<<WGM00);TCCR0B&=~(3<<WGM02)							//  pocita do hodnoty 0xFFFF a pak se nuluje
#define mTimer0Mode_FPWM_ICR()	TCCR0A&=~(3<<WGM00);TCCR0B&=~(3<<WGM02);TCCR0B|=(3<<WGM02);TCCR0A|=(1<<WGM01)
#define mTimer0Stop()			TCCR0B&=~(7<<CS00)
#define mTimer0Run_Clk_1()		TCCR0B|=(0<<CS02)|(0<<CS01)|(1<<CS00)
#define mTimer0Run_Clk_8()		TCCR0B|=(0<<CS02)|(1<<CS01)|(0<<CS00)
#define mTimer0Run_Clk_64()		TCCR0B|=(0<<CS02)|(1<<CS01)|(1<<CS00)
#define mTimer0Run_Clk_256()	TCCR0B|=(1<<CS02)|(0<<CS01)|(0<<CS00)
#define mTimer0Run_Clk_1024()	TCCR0B|=(1<<CS02)|(0<<CS01)|(1<<CS00)
#define mTimer0Clear()			TCNT0=0												
#define mTimer0IntOvf_Enab()	TIMSK0|=(1<<TOIE0)												//  for normal mode
#define mTimer0IntOvf_Clear()	TIFR0|=(1<<TOV0)												//  for normal mode
#define mTimer0IntOvf_Dis()		TIMSK0&=~(1<<TOIE0)												//  for normal mode
#define mTimer0CompRegA_Upd(x)	OCR0A=((uint8_t)x)
#define mTimer0IntOcrA_Enab()	TIMSK0|=(1<<OCIE0A)
#define mTimer0IntOcrA_Clear()	TIFR0|=(1<<OCF0A)
#define mTimer0IntOcrA_Dis()	TIMSK0&=~(1<<OCIE0A)
#define mTimer0CompRegB_Upd(x)	OCR0B=((uint8_t)x)
#define mTimer0IntOcrB_Enab()	TIMSK0|=(1<<OCIE0B)
#define mTimer0IntOcrB_Clear()	TIFR0|=(1<<OCF0B)
#define mTimer0IntOcrB_Dis()	TIMSK0&=~(1<<OCIE0B)

#endif /* TCWPWM_DRV_H_ */