#define F_CPU 8000000UL
#include <avr/io.h>
#include <string.h>
#include <avr/sleep.h>  
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

//*******************************************************************

#define setb(port,pin)  port |= 1<<pin    
#define clrb(port,pin)  port &= ~(1<<pin) 
#define negb(port,pin)  port ^= 1<<pin    

//********************* init PORT & USART ***************************

void PORT_init(void)
{
	DDRA  = 0x38;
	setb (PORTA,5);
	
	DDRB  = 0B10110000;  	   
  	setb (PORTB,6);
	setb (PORTB,4);
	
  	DDRC  = 0B00000111;        
  
  	DDRD  = 0B00000010;        
  	PORTD = 0x00;  	   
}

void USART_init (void)
{
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
} 

//********************** A/D prevodnik ******************************
unsigned int read_adc(unsigned char kanal)
{
	ADMUX=kanal; // určí převáděný kanál
	ADCSRA|=0x40; // odstartuje převod
	while ((ADCSRA & 0x10)==0);
	ADCSRA|=0x10;
	return ADCW;
}

//********************* UART posli znak: ****************************
void u_putc( char data )
{
	while ( !( UCSRA & (1<<UDRE)) )     
	;
	UDR = data;                        
}

//******************** UART posli string: ***************************
void u_puts( char *text )
{
	unsigned char i=0,temp;
		
		do
		{
			temp = text[i];
			if(temp==0) break;
			u_putc(temp);
			i++;
		}
		while(temp>0);
}

//********************** Akcelerometr -> Atmega16 *******************
void SPI_MasterInitDIN(void) 
{	
	//Registr SPCR=SPIE/SPE/DORD/MSTR/CPOL/CPHA/SPR1/SPR0
	SPCR = 0b01011000;
}

//********************** Akcelerometr <- Atmega16 *******************

void SPI_MasterInitDOUT(void)
{
	//Registr SPCR=SPIE/SPE/DORD/MSTR/CPOL/CPHA/SPR1/SPR0
	SPCR = 0b01011100;
}

//***************** Odslani kontrolniho registru ********************

void SPI_MasterTransmit(unsigned char cData)
{
	
	SPDR = cData;
	while(!(SPSR & (1<<SPIF)))
	;
}
//****************** Prijeti dat z akcelerometru ********************
unsigned int SPI_MasterReceive(void)

{
	SPDR=0b11111111;
	while(!(SPSR & (1<<SPIF)))
	;
	return SPDR;
} 

//**************************  MAIN  *********************************
int main (void)
{
	unsigned int vysledek_x, vysledek_y, vysledek_z;
	float realne_x, realne_y,realne_z;
	float realne_gx, realne_gy,realne_gz;
    char retezec[255];
	unsigned int byteH, byteL;
	float acc;	

	PORT_init();
	USART_init();
		
	ACSR=0x80;
	SFIOR=0x00;
	ADMUX=0x00;
	ADCSRA=0x87;
	
	//	 	
	while (1)
	{

//************************ MMA7260Q *********************************
	
	//	Vypocet napeti na osach
    vysledek_x = read_adc(0);
	vysledek_y = read_adc(1);
	vysledek_z = read_adc(2);
    realne_x = (float) 3.00 * vysledek_x;
    realne_x = (float) realne_x / 1024;
	realne_y = (float) 3.00 * vysledek_y;
    realne_y = (float) realne_y / 1024;
	realne_z = (float) 3.00 * vysledek_z;
    realne_z = (float) realne_z / 1024;
	//	Vypocet g na osach
	realne_gx = realne_x - 1.65;
	realne_gx = realne_gx * 1.25;
	realne_gy = realne_y - 1.65;
	realne_gy = realne_gy* 1.25;
	realne_gz = realne_z - 1.65;
	realne_gz = realne_gz * 1.25;
	
	sprintf(retezec, "Akcelerometr MMA7260Q: \n");
	u_puts(retezec); 
	//	Vypsani g na osach
	sprintf(retezec, "gx = %4.2fg ",realne_gx);
    u_puts(retezec);
	sprintf(retezec, "	gy= %4.2fg ",realne_gy);
    u_puts(retezec);
	sprintf(retezec, "	gz = %4.2fg \n \n",realne_gz);
    u_puts(retezec); 
	
//************************ ADIS16006 X-osa **************************
	sprintf(retezec, "Akcelerometr ADIS16006: \n");
	u_puts(retezec); 

	SPI_MasterInitDOUT();
	_delay_us(10);
	setb(PORTC,0);
	SPI_MasterTransmit(0b00000100);
	clrb(PORTC,0);
		
	_delay_us(10);	

	SPI_MasterInitDIN();
	_delay_us(10);
	setb(PORTC,0);
	byteH = SPI_MasterReceive();
	byteL = SPI_MasterReceive();
	clrb(PORTC,0);
	

	acc=(byteH*256)+byteL;
	acc=(acc-2048)*0.00390625;
	sprintf(retezec, "gx= %4.2fg ",acc);
	u_puts(retezec);
	
		
//************************ ADIS16006 Y-osa **************************
	SPI_MasterInitDOUT();
	_delay_us(10);
	setb(PORTC,0);
	SPI_MasterTransmit(0b00001100);
	clrb(PORTC,0);	
	
	_delay_us(10);	

	SPI_MasterInitDIN();
	_delay_us(10);
	setb(PORTC,0);
	byteH = SPI_MasterReceive();
	byteL = SPI_MasterReceive();
	clrb(PORTC,0);
	

	acc=(byteH*256)+byteL;
	acc=(acc-2048)*0.00390625;
	sprintf(retezec, " gy= %4.2fg\n \n",acc);
	u_puts(retezec);
	
	 
	_delay_ms(1000);
	};
}
	
//**************************** KONEC ********************************

