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

//Meteorologicka cast
#define VERSION "0.1"
#define AUTOR "Jan Chroust"
#define DATE "15.4.2013"

#include <math.h>
#include <string.h>

#include "main.h"
#include "SHT25.h"
#include "MPL3115.h"

//set I2C
#define PIN_SDA  PIN_B0
#define PIN_SCL  PIN_B1
#use i2c(master, sda=PIN_SDA, scl=PIN_SCL)
//set RS232
#use rs232(baud=9600,parity=N,xmit=PIN_B3,rcv=PIN_B2,bits=8) //rcv TXD xmit RXD

#define  SEND_DELAY     50       // Cas mezi dvema znaky na RS232


char  VER[4]=VERSION;   // Ulozeni verze do retezce

void uvitani(void)               // uvodni zprava
{
   printf("\r\n\r\n# Meteorologicka stanice %s (C) 2013 www.mlab.cz \r\n",VERSION); 
   printf("\r\n# Autor: %s Posledni uprava: %s \r\n",AUTOR, DATE);// Welcome message
   printf("# ver poradi ");
   printf("altimet_t[°C] altimet_a[m] altimet_p[Pa] ");
   printf("sht_t[°C] sht_h[%%]  Anemo[m/s]check\r\n\r\n");
}

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);

float altimet_t;  //teplota z MPL3115
float altimet_p;  //tlak z MPL3115
float altimet_a;  //vyska z MPL3115
float sht25_t;    //teplota z SHT25
float sht25_h;    //relativni vlhkost z SHT25
 
unsigned int16 poradi=0; //cislo vzorku
sht_config = SHT25_RH12_T14 | SHT25_HEATER_OFF; //vypnuti topeni v SHT25
uvitani();


while(TRUE)
   {
   char output[12];   // vystupni zasobnik
   int8 j;           // ukazatel na retezec
   int8 check=0;     // Checksum is calculated between '$' and '*'
   unsigned int sht_config;
  
   mpl3115_setP(); //nastaveni pro tlak a teplotu
   delay_ms (500);
   altimet_t=mpl3115_T();
   altimet_p=mpl3115_P();
   
   mpl3115_setA(); //nastaveni pro vysku a teplotu
   delay_ms (500);
   altimet_a=mpl3115_A();
 

   
//SHT25_setup(sht_config),sht_config);
 
      
     
  sht25_t=SHT25_get_temp();
  sht25_h=SHT25_get_hum();
  
  
         delay_us(SEND_DELAY);
         putc('$');
         delay_us(SEND_DELAY);
         sprintf(output,"MST%s \0",VER);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%4.0Lu \0", poradi);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%3.2f \0", altimet_t );
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%5.2f \0", altimet_a);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%6.2f \0", altimet_p);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%5.2f \0", sht25_t);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%3.2f \0", sht25_h);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%5.0f \0", altimet_t);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%5.1f \0", altimet_t);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%3.1f \0", altimet_t);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"*%X\r\n\0", check);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j++]); }
         delay_us(SEND_DELAY);
  
  poradi++;
  
  
  
  
  
  
  
  
  }
  
  
}