#include "includes.h" 

  
  
//*****************************************************
// rozliseni			9 bit	10 bit	11 bit	12 bit
// Conversion Time (ms)	93.75	187.5	375		750
//*****************************************************

//#define TEMPERATURE_RESOLUTION_9BIT			
//#define TEMPERATURE_RESOLUTION_10BIT			
#define TEMPERATURE_RESOLUTION_11BIT		
//#define TEMPERATURE_RESOLUTION_12BIT		


// mereni ONEWIRE je nutne povilit pomoci globalniho  define USE_ONEWIRE

      
// maskovani nepouzitych bitu podle zvoleneho rozliseni
#ifdef TEMPERATURE_RESOLUTION_9BIT
  #define TEMP_MASK		0xF8
#endif

#ifdef TEMPERATURE_RESOLUTION_10BIT
  #define TEMP_MASK		0xFC
#endif

#ifdef TEMPERATURE_RESOLUTION_11BIT
  #define TEMP_MASK		0xFE
#endif

#ifdef TEMPERATURE_RESOLUTION_12BIT
  #define TEMP_MASK		0xFF
#endif


  tUChar ow_buff[9];
  tUChar LowByte;
  tUChar HighByte;
  tUInt  TReading;
  tUChar SignBit;
 
  tUInt Tc_100;
  tUInt Whole;
  tUInt Fract;


  
//****************************************************************************
// cteni teplomeru a konverze vysledku - vraci zmerenou teplotu
// ve formatu 2'nd complement se znamenkem, x10 (napr. 1250=12.5 st.C)
//****************************************************************************
tUInt ds1820_read(void) 
{ 
#ifdef USE_ONEWIRE

  tUInt status; 
  tUChar i;
  tUInt temp;
  tUInt temp16;
  
  TReading = 0xffff;		 	// defalt hodnota, kterou vraci v pripade chyby

  
// nacist scratchpad
  status = OW_Reset(); 
  if (status == OW_RESET_OK)
   {
     OW_WriteByte(ONEWIRE_CMD_SKIP_ROM); 
     OW_WriteByte(ONEWIRE_CMD_READ_SCRATCHPAD); 
	   
     for (i=0; i<9; i++)
      {
	    ow_buff[i]= OW_ReadByte(); 		// teplota MSB bajt
        ClearWDT();
	  }
  
     // kontrola CRC nacteneho paketu - pokud souhlasi provest konverzi teploty
     if ( OW_CRC(&ow_buff[0], 8) == ow_buff[8])
      {
        LED_GRN_ON;		
	    LowByte = ow_buff[0] & TEMP_MASK;
  	    HighByte = ow_buff[1];
        TReading = (HighByte << 8) + LowByte;		// cele 16-bit vyjadreni nactene teploty
     
	    temp16 = TReading;							// zalohovat pro dalsi zpracovani
	    if (TReading & 0x8000)						// zaporna teplota?
         {
           SignBit = 1;
           TReading = (TReading ^ 0xffff) + 1; 		// 2's comp
         }
	    else
	     {
	       SignBit = 0;
	     }
			 
	    ClearWDT();
        Tc_100 = mul32(TReading, 6);				// prevod na stupne
		temp = TReading>>2;	
	    Tc_100 += temp;
	 
	    ClearWDT();
	    Whole = div32(Tc_100, 100);  				// cele stupne
        Fract = Tc_100 % 100;						// setiny stupne
     
	    // konverze teploty ve stupnich do 2's comp formatu	
	    TReading = Tc_100;							// zpracovat jiz konvertovanou teplotu ve stupnich
        if (SignBit) 								// je to zaporna teplota
         {
          TReading = (TReading ^ 0xffff) - 1; 		// prevest na 2's comp format
	      asm ("nop");
         }
	  	 
    	  //temp_lo * 0.625 = temp_lo * 625 / 1000 = temp_lo * 640 / 1024
	      //Tc_100 = mul32(TReading, 640);
	      //Tc_100 = div32(Tc_100, 1024);
  	 }

  }		// konec prvni RESET OK


   
  // pred ukoncenim rutiny spustit nove mereni teploty - viz. doba konverze podle rozliseni
  ClearWDT();
  status = OW_Reset(); 
  if (status == OW_RESET_OK)
   {
	 OW_WriteByte(ONEWIRE_CMD_SKIP_ROM); 
     OW_WriteByte(ONEWIRE_CMD_CONVERT_T); 
   }

  ClearWDT();
  TReading = div32(TReading, 10); 		// zaokrouhlit na jedno desetine misto

  if ((appCB.sensor.first_sample) && (TReading == 0x352))
   {
     appCB.sensor.first_sample = 0;
	 TReading = 0xffff;				// toto mereni zahodit
   }
  
  return(TReading); 
#endif

} 


//****************************************************************************
// nastaveni teplomeru pro 10-bit rozliseni
//****************************************************************************
tUInt DS18B20_Init(void)
{
#ifdef USE_ONEWIRE
  signed int temperature; 
  tUInt status;
  

  status = OW_Reset(); 
  if (status == OW_RESET_OK)
   {
     OW_WriteByte(ONEWIRE_CMD_SKIP_ROM); 
     OW_WriteByte(ONEWIRE_CMD_WRITE_SCRATCHPAD); 
  
     OW_WriteByte(40); 							// horni limita alarmu
     OW_WriteByte(5); 							// spodni limita alarmu

     #ifdef TEMPERATURE_RESOLUTION_12BIT
      OW_WriteByte(DS18B20_RESOLUTION_12BIT); 	// konfiguracni registr - nastaveni rozliseni
     #endif
  
  	 #ifdef TEMPERATURE_RESOLUTION_11BIT
   	  OW_WriteByte(DS18B20_RESOLUTION_11BIT); 	// konfiguracni registr - nastaveni rozliseni
  	 #endif
  
     #ifdef TEMPERATURE_RESOLUTION_10BIT
      OW_WriteByte(DS18B20_RESOLUTION_10BIT); 	// konfiguracni registr - nastaveni rozliseni
     #endif
  
     #ifdef TEMPERATURE_RESOLUTION_9BIT
      OW_WriteByte(DS18B20_RESOLUTION_9BIT); 	// konfiguracni registr - nastaveni rozliseni
     #endif
 
    // nacist scratchpad - default hodnota po zapnuti 85 stupnu C, tak tuto hodnotu vycist
    status = OW_Reset(); 
    if (status == OW_RESET_OK)
	 {
	   OW_WriteByte(ONEWIRE_CMD_SKIP_ROM); 
       OW_WriteByte(ONEWIRE_CMD_READ_SCRATCHPAD); 
	   return (NO_ERROR);
	 }
	 
  }		// dallas nenacten
   
   return (OW_OPEN);		// nic nenalezeno
#endif    
}




