#include "C:\Users\Honza\Documents\pic\uhel_sl\main.h"



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

#define AK_W  0x38 //adresa akcelerometru zápis
#define AK_R  0x39 //adresa akcelerometru ètení
#define AK_XH 0x01 //osa X LSB
#define AK_XL 0x02 //osa X MSB
#define AK_YH 0x03 //osa Y LSB
#define AK_YL 0x04 //osa Y MSB
#define AK_ZH 0x05 //osa Z LSB
#define AK_ZL 0x06 //osa Z MSB


//pøipojení motorù
//AIN1 - pro vysku slunce
#define AIN1 PIN_D0 
#define AIN2 PIN_D1 
//motor A -cerveny vodic na AOUT2
//motor A -modry vodic na Aout1

signed int16 X, Y, Z,AX, AY, AZ;

void setAK (void) //nastaveni akcelerometru
{
   i2c_start();
   I2C_Write(AK_W);
   I2C_write(0x2A);
   I2C_write(0x01); //nastaví aktivní stav
 
   i2c_stop(); 
}

int16 akcele (int8 H, int8 L) //vycitani hodnot z akcelerometru
{
unsigned int8 XL=0,XH=0;
signed int16 x;

   i2c_start();
   I2C_Write(AK_W);
   I2C_write(H);
   i2c_start();
   I2C_Write(AK_R);
   XH=i2c_read(0);
   i2c_stop(); 

   i2c_start();
   I2C_Write(AK_W);
   I2C_write(L);
   i2c_start();
   I2C_Write(AK_R);
   XL=i2c_read(0);
   i2c_stop();
   
   x = (((unsigned int16) XH << 8) + XL ); //prevod na 16bit hodnotu
   x=x/4;
   
   return x;
}

float vyska_sl (void) //vypocet aktualni vysky panelu
{
X= akcele (AK_XH, AK_XL);  
Y= akcele (AK_YH, AK_YL); 
Z= akcele (AK_ZH, AK_ZL); 

AX=abs(X);
AY=abs(Y)+250;
AZ=abs(Z)+250;

float a, b;
a=(float)Y/Z;
b=atan(a);
b = (b/3.14)*180;
b=abs(b);
   
if(((AX>AY) || (AX>AZ))) //indikace prevraceni panelu
   { 
   printf("Prevracený panel)\r\n", );
   }
else
   {
   if(Z==0) //osetreni proti deleni 0
   { 
      if(Y>0)
         { 
         b=180;
         }
      else
         {
         b=0;
         }
   }  
   else
      {
      if(Z>0)
       {
       if(Y>=0)
       {
         b=90+b;
       }
       else
       {
         b=90-b;
       }
       }
      else
       {
       if(Y>=0)
       {
         b=180-b;
       }
       else
       {
         b=270+b;
       }
       }
         
      }   
   
   
   
  }
   //printf("uhel namìreny %10.2f \r\n", b);
      return b;
   
}

void motorA (int8 H) //pro ovladani prvniho motoru nastaveni vysky
{
switch(H){
   case 1:  //reverzní chod
   output_low (AIN2);
   output_high (AIN1);  
   break;

case 2: //dopøedu
   output_low (AIN1);
   output_high (AIN2);   
   break;

default:
   output_low (AIN2);
   output_low (AIN1);
         }
}

void uhel (int8 H) //slouzi pro nastaveni nove vysky panelu
{
float a;
int16 b,c;

a=vyska_sl();
b= (int16) a;
//printf("auhel pro mereni: %Ld(procenta)\r\n", b);
if(b>350)
   {
   b=0;
   }
else
   {

   }  
c=abs(H-b); 

while(c>2) //maximalni odchylka uhlu, aby nebylo potreba panelem hybat
   {
   while(H!=b) //probiha dokud se uhel panelu nerovna zadanemu na cele stupne
      {
//printf("aktualni uhel: %Ld(procenta)\r\n", b);
//printf("zadane nastavení: %d(procenta)\r\n", H);
      if(H>b)
         {
         motorA(2);
      //printf("zmotor2í: \r\n", );
         }
      else
      {
      motorA(1);
      }

      delay_ms (50); //cas sepnuti motoru

      motorA(3); //vypne motor
      delay_ms (50); //doma na ustaleni panelu pred merenim
      a=vyska_sl();
      b= (int16) a;
      //printf("auhel pro mereni: %Ld(procenta)\r\n", b);
      if(b>350) //osetreni pro uhel 0. Zabezpeci ze neprejde stav z 0 na 359 kdy by se solar zacal tocit na druhou stranu
         {
         b=0;
         }
      else
         {
         b=b;
         }
      c=abs(H-b);
      }
   }
motorA(3); //vypne motor
printf("Podaøené nastavení: %Ld\r\n", 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
   
   setup_adc_ports(PIN_A0); //piny pro A/D RA0

printf("Akcelerometr:  \r\n",);

motorA(3);

float a, b;
unsigned int value1=10;


setAK(); //nastaveni akcelerometru
  
  while(TRUE)
 { 

X= akcele (AK_XH, AK_XL);  
Y= akcele (AK_YH, AK_YL); 
Z= akcele (AK_ZH, AK_ZL); 

printf("Stav X: %Ld(procenta)\r\n", X);
printf("Stav Y: %Ld(procenta)\r\n", Y);
printf("Stav Z: %Ld(procenta)\r\n", Z);


 

        
      
        set_adc_channel(0); //nastavi AD na kanál 0 (RA0)
         read_adc(ADC_START_ONLY);              // Spustime A/D prevod
       Delay_ms(1);  
          while(!adc_done());                    // Cekame na dokonceni prevodu
         value1=read_adc();            // Precteme hodnotu z prevodniku
         uhel (value1);
         printf("zadaný uhel %d \r\n", value1);
      
  



delay_ms (2000);

  
}
}