#include <avr/io.h>
#include <math.h>
#include <avr/interrupt.h>
#include "header.h"
//#include <avr/pgmspace.h>
#include <ctype.h>
#include <avr/sfr_defs.h>


// New from atmega16lcd
//#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
//#include <math.h>



// Routine definition
void InitPorts(void);
void USART_Init(unsigned	int	ubrr);
void Timers_Init(void);
//void external_interrupts_init(void);
//void adc_init(void);



// Global variables

volatile static u8 TX_read;
volatile static u8 RX_write;


static u8	send_data=1;	//send_data=0;
static u8	send_rx=0;		//Mila otan sou zhth8ei
static u8	new_rx=0;
static u8 temp;
// Accelerometers
//#include "files/adc_file.c"					//ADC kai Accelerometers Variables

#include "files/interrupt_routines.c"
#include "files/servos.h"								// Se poies 8eseis frenaroun ta servo

//--------------------------------main-----------------------------------
int main(void)
{
	sei();
	//adc_init();
	InitPorts();
	//external_interrupts_init();

	USART_Init(MYUBRR);	//115.2K
	Timers_Init();	

	while(1){
		
		if ((send_data==1)&&(send_rx==1)){
			send_data=0;				
			send_rx=30;
			TX_read=1;
			previous_tx_char='a';	
			UDR=special_char;				//Trigger transmition by sending the special character
		}

		//if(!(ADCSRA&0b01000000))			// An oloklhrw8hke to Conversion  xekina ena neo, To ADSC phge sto 0
		//	adc_routine();				// Jekina neo Conversion

		//----------Wheel Speed count Start-----------------------------
		temp=PINB;
		if (temp&0b00000001){							// An einai high koita to previous state
			if ((previous_state&0b00000001)==0)			// An isxyei exei allaxei to state tou PB0
				front_left_ticks++;
		}

		if (temp&0b00000010){							// An einai high koita to previous state
			if ((previous_state&0b00000010)==0)			// An isxyei exei allaxei to state tou PB1
				 front_right_ticks++;
			
		}
		if (temp&0b00000100){							// An einai high koita to previous state
			if ((previous_state&0b00000100)==0)			// An isxyei exei allaxei to state tou PB2
				rear_left_ticks++;
		}

		if (temp&0b00001000){
			if((previous_state&0b00001000)==0)			// An einai high koita to previous state
				rear_right_ticks++;						// An isxyei exei allaxei to state tou PB3
			
		}
		if (temp&0b00010000){
			if((previous_state&0b00010000)==0)			// An einai high koita to previous state
				rpm_ticks++;							// An isxyei exei allaxei to state tou PB3
			
		}
		previous_state=temp;	
		//----------Wheel Speed count End-----------------------------
			
	}

}
//--------------------------------main-----------------------------------




void InitPorts(void){	


	DDRB=0;
	DDRC=0;

	DDRD|=(0<<PD0)|(1<<PD1);			// PD0 RXD in, PD1 TXDoutcv
	DDRD|=(0<<PD6);						
	DDRD|=(0<<PD7);						

}

void Timers_Init(void)
{
	
	// Timer Counter_0 8bit
	TCCR0|=(0<<CS02)|(0<<CS01)|(1<<CS00);		//No prescaling Timer prescaler T_overflow=16us
	TIMSK|=(1<<TOIE0);							//Timer/Counter 0, overflow enable
	

	// Timer Counter_2 8bit
	TCCR2|=(0<<CS22)|(1<<CS21)|(0<<CS20);		//Timer prescaler=8 T_overflow=128 us
	TIMSK|=(1<<TOIE2);							//Timer/Counter 2, overflow enable

}

void	USART_Init(unsigned	int	ubrr)
{
	//Set Baud rate here
	UBRRH=(unsigned char)(ubrr>>8);
	UBRRL=(unsigned char)ubrr;
	// Enable receiver and trasmitter and receiver and transmitter Interrupts
	UCSRB=(1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(1<<TXCIE);
	//Set frame format; 8 data, 1 stop bit sel 164
	UCSRC=(1<<URSEL)|(0<<USBS)|(1<<UCSZ1)|(1<<UCSZ0);
	//PORTB=~ubrr;

	TX_read=1;
	RX_write=0;
}


/*
void adc_init(void){
	ADMUX&=~((1<<REFS0)|(1<<REFS1));			// REFS0=0 kai REFS1=0	ARef,Internal Vref off(sel 215)
	//ADMUX|=((1<<REFS0)|(1<<REFS1));				// REFS0=1 kai REFS1=1	ARef,Internal Vref on(sel 215)
	ADMUX&=~(1<<ADLAR);							// Left Adjust result off	sel 206 ATmega8
	
	ADMUX&=ADC0_and;
	ADMUX|=ADC0_or;								// Start from ADC0 port

	ADCSRA|=(1<<ADIE)|(1<<ADEN);				// Interrupt Enable, ADC Enable
	//ADCSRA|=(0<<ADPS2)|(1<<ADPS1)|(0<<ADPS0);	// ADC prescaler=4 sel 218

	ADCSRA|=(1<<ADPS2)|(1<<ADPS1)|(0<<ADPS0);	// ADC prescaler=64 sel 218

	ADCSRA|=(1<<ADSC);							// Start Conversion
}
*/


/*
void external_interrupts_init(void)
{
	GICR|=(1<<INT0)	;							//Enable external Interrupt 0
	MCUCR|=(0<<ISC01)|(1<<ISC00);				//Any logical change on Interrupt 0 generates Interrupt sel 67

	GICR|=(1<<INT1)	;							//Enable external Interrupt 1
	MCUCR|=(0<<ISC11)|(1<<ISC10);				//Any logical change on Interrupt 0 generates Interrupt sel 67

}
*/

