#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    

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

void PORT_init(void)
{
	DDRA  = 0x38;
	setb (PORTA,5);
	
	DDRB  = 0x00;  	   
  	PORTB = 0xFF;

  	DDRC  = 0x00;        
  	PORTC = 0xFF;        
  
  	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)) )     // Wait for empty transmit buffer
	;
	UDR = data;                         // Put data into buffer, sends the 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);
}

//**************************  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];

	
	PORT_init();
	USART_init();
	
	ACSR=0x80;
	SFIOR=0x00;
	ADMUX=0x00;
	ADCSRA=0x87;
	
	//	MMA7260Q - Akcelerometr 	
	while (1)
	{
	//	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;
	//	Vypsani napeti na osach	
    sprintf(retezec, "x = %4.3f ",realne_x);
    u_puts(retezec);
	sprintf(retezec, "	y = %4.3f ", realne_y);
    u_puts(retezec);
	sprintf(retezec, "	z = %4.3f \n", realne_z);
    u_puts(retezec);
	//	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;
	//	Vypsani g na osach
	sprintf(retezec, "gx = %4.2f ",realne_gx);
    u_puts(retezec);
	sprintf(retezec, "	gy= %4.2f ",realne_gy);
    u_puts(retezec);
	sprintf(retezec, "	gz = %4.2f \n",realne_gz);
    u_puts(retezec); 
	 
	_delay_ms(100);
	};
}
	
//*********************************************************************
