#include <main.h>
#define EEPROM_SDA  PIN_B0
#define EEPROM_SCL  PIN_B1
#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)
#use rs232(baud=9600,parity=N,xmit=PIN_B3,rcv=PIN_B2,bits=8) //rcv TXD xmit RXD
#include <math.h>
#include <HMC5883L.h>








float azimut (void) //vypocet aktualni vysky panelu
{
signed int16 x,y,z;

x= mag_readX();  
y= mag_readY();
z= mag_readZ();


float a, b;
a=(float)y/x;
b=atan(a);
b = (b/3.14)*180;
b=abs(b);
   

   

   if(x==0) //osetreni proti deleni 0
   { 
   printf("Elektronický kompas0\r\n",);
      if(y>0)
         { 
         b=90;
         }
      else
         {
         b=270;
         }
   }  
   else
      {
      if(x>0)
       {
       if(y>=0)
       {
         b=180+b;
         printf("180+b\r\n",);
       }
       else
       {
         b=180-b;
         printf("180-b\r\n",);
       }
       }
      else
       {
       if(y>=0)
       {
         b=360-b;
         printf("360-b\r\n",);
       }
       else
       {
         b=b;
         printf("b\r\n",);
       }
       }
         
      }   

      return b;
   
}

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard

printf("Elektronický kompas\r\n",);
float az,k=0;
set_mag();
signed int16 X,Y,Z;
while(true)
{  
az=azimut();
   
   


printf("Elektronický kompas \r\n",);
printf("Simple Thermomether %10.2f \r\n", az);

//X=mag_readX();
//Y=mag_readY();
//Z=mag_readZ();
printf("Simple Thermometherx %Ld \r\n", X);
printf("Simple Thermomethery %Ld \r\n", Y);
printf("Simple Thermometherz %Ld \r\n", Z);
Delay_ms(2000); 

 } 
}