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

#include "main.h"
//I2CHUB
//ADRESA pro èteni 0xE1
//pro zápis 0xE0


#define EEPROM_SDA  PIN_C4
#define EEPROM_SCL  PIN_C3
#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)
#use rs232(baud=9600,parity=N,xmit=PIN_C7,rcv=PIN_C6,bits=8) //rcv TXD xmit RXD

void setHUB(int d) //vstupuje 1-8
//hodnota udava jaky port bude pripojen na master I2C
//zde je nastaveno propojeni jen jedne brany, je mozne propojit i vice bran naraz
{
switch (d) {

    case 1: d=0x01;

           break;

    case 2: d=0x02;

           break;
           
    case 3: d=0x04;

           break;

    case 4: d=0x08;

           break;
    case 5: d=0x10;

           break;

    case 6: d=0x20;

           break;
    case 7: d=0x40;

           break;

    case 8: d=0x80;

           break;
   
   default:printf("chyba");

           break; }
   
   i2c_start();
   I2C_Write(0xE0);
   
   I2C_write(d);
   i2c_stop();           
           
         
   

}


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("I2CHUB  \r\n",);
printf("(c)MLAB 2013 JACHO  \r\n",);
unsigned int8 INTEP,INTEP1, NAP,NAP1, PROUD,PROUD1,KAPZ,KAPZ1,KAPC,KAPC1, STAV,STAV1;



int16 data4;
int16 data5;
int16 data6;
int16 data7;
int16 data8;
int16 data9;

byte XH;





  while(TRUE)
 { 

 


//akcelerometr
   int1 ack;
   i2c_start();            // If the write command is acknowledged,
   ack = i2c_write(0xE1);//hen the device is ready.
   i2c_stop();
   ack=!ack;
   

   
delay_ms(1000);


//printf("Stav: %d (procenta)\r\n", ack);

printf("Stavx: %X(procenta)\r\n", XH);




setHUB(3);





 
 i2c_start();
   I2C_Write(0xAA);
   I2C_write(0x0c);
  
   i2c_start();
   I2C_Write(0xAB);
   INTEP=i2c_read(0);
   i2c_stop();
   
  i2c_start();
   I2C_Write(0xAA);
   I2C_write(0x0d);
   i2c_start();
   I2C_Write(0xAB);
   INTEP1=i2c_read(0);
   i2c_stop();
   
   i2c_start();
   I2C_Write(0xAA);
   I2C_write(0x08);
   i2c_stop();
   i2c_start();
   I2C_Write(0xAB);
   NAP=i2c_read(0);
   i2c_stop();

   i2c_start();
   I2C_Write(0xAA);
   I2C_write(0x09);
   i2c_stop();
   i2c_start();
   I2C_Write(0xAB);
   NAP1=i2c_read(0);
   i2c_stop();
   
    i2c_start();
   I2C_Write(0xAA);
   I2C_write(0x0a);
   i2c_stop();
   i2c_start();
   I2C_Write(0xAB);
   PROUD=i2c_read(0);
   i2c_stop();

   i2c_start();
   I2C_Write(0xAA);
   I2C_write(0x0b);
   i2c_stop();
   i2c_start();
   I2C_Write(0xAB);
   PROUD1=i2c_read(0);
   i2c_stop();  
   
   
       i2c_start();
   I2C_Write(0xAA);
   I2C_write(0x04);
   i2c_stop();
   i2c_start();
   I2C_Write(0xAB);
   KAPZ=i2c_read(0);
   i2c_stop();

   i2c_start();
   I2C_Write(0xAA);
   I2C_write(0x05);
   i2c_stop();
   i2c_start();
   I2C_Write(0xAB);
   KAPZ1=i2c_read(0);
   i2c_stop(); 
   
   
   i2c_start();
   I2C_Write(0xAA);
   I2C_write(0x06);
   i2c_stop();
   i2c_start();
   I2C_Write(0xAB);
   KAPC=i2c_read(0);
   i2c_stop();

   i2c_start();
   I2C_Write(0xAA);
   I2C_write(0x07);
   i2c_stop();
   i2c_start();
   I2C_Write(0xAB);
   KAPC1=i2c_read(0);
   i2c_stop();
   
      i2c_start();
   I2C_Write(0xAA);
   I2C_write(0x02);
   i2c_stop();
   i2c_start();
   I2C_Write(0xAB);
   STAV=i2c_read(0);
   i2c_stop();

   i2c_start();
   I2C_Write(0xAA);
   I2C_write(0x03);
   i2c_stop();
   i2c_start();
   I2C_Write(0xAB);
   STAV1=i2c_read(0);
   i2c_stop(); 
   

data4 = (((unsigned int16) INTEP1 << 8) + INTEP );
data4 = (data4/10)-273;
data5 = (((unsigned int16) NAP1 << 8) + NAP );
data6 = (((unsigned int16) PROUD1 << 8) + PROUD );
data7 = (((unsigned int16) KAPZ1 << 8) + KAPZ );
data8 = (((unsigned int16) KAPC1 << 8) + KAPC );
data9 = (((unsigned int16) STAV1 << 8) + STAV );

 
   


printf("Teplota uvnitr io: %Ld C\r\n", data4);
printf("Napeti clanku: %Ld mV \r\n", data5);
printf("Proud: %Ld mA \r\n", data6);
printf("Kapacita zbytek: %Ld mAh\r\n", data7);
printf("Kapacita max: %Ld mAh\r\n", data8);
printf("Stav: %Ld (procenta)\r\n", data9);


delay_ms (2000);
 
 
 
 } 
}