#include <16F628A.h>      //#include <16F88.h> org.
#fuses XT,NOWDT,NOLVP,PUT,NOPROTECT,NOBROWNOUT //,NOWRT,CCPB3 
#use delay(clock=4000000) 
#use rs232(baud=9600,xmit=PIN_b2,rcv=PIN_B1,ERRORS) 

#define DQ PIN_A0      // One Wire Bus pin assignment 

#include "onewire.c" 

void main(void) 
{ 
   int8 i; 
   signed int16 temperature;
   signed int temp,d2,d1,d0;
   int8 scratch[9];        
   
   
    
   output_float(DQ);       // Set as input. 4k7 pullup on bus. 
   output_high(pin_a3);
   output_high(pin_a4);
   while(1) 
   { 
      ow_reset(); 
      write_byte(0xCC); // Skip Rom command 
      write_byte(0x44); // Temperature Convert command 
      output_float(DQ); 
      delay_ms(750);    // Max. time for conversion is 750mS 
      ow_reset(); 
      write_byte(0xCC); // Skip Rom command 
      write_byte(0xBE); // Read scratch pad command 
    
      dowcrc = 0;        
    
      // Get the data bytes 
      for (i=0;i<=7;i++) 
      { 
          scratch[i] = read_byte(); 
          ow_crc(scratch[i]);          
      } 
    
      scratch[8] = read_byte();   // Get crc byte 
      ow_reset(); 
      
      // If the received crc is same as calculated then data is valid. 
      // Scale and round to nearest degree C. 
      // Scaling is 0.0625 (1/16) deg.C/bit with default 12 bit resolution. 
      // Round by adding half denominator for positive temperatures and 
      // subtracting half denominator for negative temperatures. 
                
      if (scratch[8] == dowcrc)  
      { 
         temperature = (signed int16) make16(scratch[1],scratch[0]); 
          
         if (temperature >= 0) 
            temperature = (temperature + 8)/16; 
        
         else 
            temperature = (temperature - 8)/16; 
            
         // printf("TEMP= %4Ld C \n\r",temperature);
         
         d2=temperature/100;
         temp=temperature-d2*100;
         d1=temp/10;
         d0=temp%10;
         
         output_low(pin_a3);
         output_b(d0*0x10);
         output_high(pin_a3);
         output_low(pin_a4);
         output_b(d1*0x10);
         output_high(pin_a4);
         }      
      
   } 
} 
