/*****************-DS1820-*********************************/
#define DQ             LATCbits.LATC4
#define DQ_TRIS        TRISCbits.TRISC4   
/*****************-DS1820-*********************************/

unsigned int iobyte=0;

void ow_reset(void){
	unsigned int pdbyte;
	DQ_TRIS = 0;                        // Set DQ pin as output
	DQ = 1;                                  // Start with the line high
	pdbyte = 0x00;                        // Clear the PD byte
	DelayUs( 1 );                      // Drive Low for 1us
	DQ = 0;
	DelayUs( 500 );                  // Drive Low for 500us
	DQ = 1;
	DelayUs( 700 );                  // Release line and wait 70us for PD Pulse
	
	DQ_TRIS = 1;                        // Set DQ pin as input
	DelayUs( 1 );                      // Wait 1us
	if( DQ == 0) {
	    pdbyte = 0x01;                    // Set PDBYTE to 1 if get a PD Pulse
	}
	DelayUs( 400 );                  // Wait 400us after PD Pulse
}

void dsrxbyte(void){
	unsigned int  x, count, temp;
    count = 0x08;                          //Set count = 8
    for( x=0; x<8 ;x++ ){
        DQ_TRIS = 0;                        // Set DQ pin as output
        DelayUs( 1 );                      // Wait 1us
        DQ = 0;                                 // Bring DQ low for 6us
        DelayUs( 6 );
        DQ = 1;                                 // Bring DQ low for 4us
        DelayUs( 4 );
        DQ_TRIS = 1;                        // Set DQ pin as input
        DelayUs( 1 );                      // Wait 1us
        if( DQ == 1 ){
            iobyte = iobyte | ( 2^x );          // Logig OR
        }else{
            temp = ~ ( 2^x );
            iobyte = iobyte & temp;          // Logig AND
        }
    }
	return iobyte;
}

void dstxbyte(void){
    unsigned int x, temp, count = 8;
    for( x=0; x<8 ;x++ ){
        DQ_TRIS = 0;                        // Set DQ pin as output
        DelayUs( 1 );                      // Wait 1us
        DQ = 0;                                 // Set DQ pin low
        DelayUs( 3 );                     // Wait 3us
        temp = iobyte & ( 2^x );           // Logig AND
        if( temp == ( 2^x ) ){
            DQ = 1;                             // Set DQ pin high
        }
        DelayUs( 60 );                   // Wait 60us
        DQ = 1;                                 // Set DQ pin high
        DelayUs( 2 );                     // Wait 2us
    }
}

	//DS18B20 variables
    int srchrom = 0xF0;
    int rdrom = 0x33;
    int mtchrom = 0x55;
    int skprom = 0xCC;
    int picmsb=0, piclsb=0;
    int temperature=0;

		ow_reset( );
        iobyte = skprom;
        dstxbyte( );
        iobyte = 0x69;
        dstxbyte( );
        iobyte = 0x0E;
        dstxbyte( );
        dsrxbyte( );
        iobyte = picmsb;
        dsrxbyte( );
        iobyte = piclsb;
        ow_reset( );
		
		temperature = temperature | piclsb;
	    szam = temperature | (picmsb << 8);