Exemplo n.º 1
0
uint16_t DS1820_GetTemp()
{
	uint16_t ret_val_u16 = 0;
	DS1820_Reset();
	DS1820_WriteByte(DS1820_SKIP_ROM);
	DS1820_WriteByte(DS1820_CONVERT_T);
	_delay_ms(2000);
	DS1820_Reset();
	DS1820_WriteByte(DS1820_SKIP_ROM);
	DS1820_WriteByte(DS1820_READ_SCRATCHPAD);

	ret_val_u16  = DS1820_ReadByte();
	ret_val_u16 |= (DS1820_ReadByte()<<8);

	return ret_val_u16;
}
uint8_t DS1820_FindNextDevice( uint8_t pin )
{
    uint8_t idxByte;
    uint8_t state;
    uint8_t mask = 1;
    uint8_t bitpos = 1;
    uint8_t nDiscrepancyMarker = 0;
    uint8_t bit_b;
    uint8_t bStatus;
    uint8_t next_b = FALSE;

    // initialise ROM address 
    memset( romAddrOneWire, 0, 8 );

    bStatus = DS1820_Reset( pin );      // reset the 1-wire 

    if ( bStatus || bDoneFlagOneWire ) {       // no device found 
        nLastDiscrepancyOnewire = 0;           // reset the search 
        return FALSE;
    }

    // send search ROM command 
    DS1820_WriteByte( pin, DS1820_CMD_SEARCHROM );

    idxByte = 0;
    do {
        state = 0;

        // read bit 
        if ( DS1820_ReadBit( pin ) != 0 ) {
            state = 2;
        }
        __delay_us(120);

        // read bit complement
        if ( DS1820_ReadBit( pin ) != 0 ) {
            state |= 1;
        }
        __delay_us( 120 );

        // description for values of state: 
        // 00    There are devices connected to the bus which have conflicting 
        //       bits in the current ROM code bit position. 
        // 01    All devices connected to the bus have a 0 in this bit position. 
        // 10    All devices connected to the bus have a 1 in this bit position. 
        // 11    There are no devices connected to the 1-wire bus. 

        // if there are no devices on the bus 
        if ( state == 3 ) {
            break;
        }
        else {
            // devices have the same logical value at this position 
            if ( state > 0 ) {
                // get bit value 
                bit_b = (uint8_t)(state >> 1);
            }
            // devices have conflicting bits in the current ROM code 
            else {
                // if there was a conflict on the last iteration 
                if ( bitpos < nLastDiscrepancyOnewire ) {
                    // take same bit as in last iteration 
                    bit_b = ( ( romAddrOneWire[ idxByte ] & mask ) > 0 );
                }
                else {
                    bit_b = ( bitpos == nLastDiscrepancyOnewire );
                }

                if ( bit_b == 0 ) {
                    nDiscrepancyMarker = bitpos;
                }
            }

            // store bit in ROM address 
            if ( bit_b != 0 ) {
               romAddrOneWire[ idxByte ] |= mask;
            }
            else {
               romAddrOneWire[ idxByte ] &= ~mask;
            }

            DS1820_WriteBit( pin, bit_b );

            // increment bit position 
            bitpos++;

            // calculate next mask value 
            mask = mask << 1;

            // check if this byte has finished 
            if ( 0 == mask ) {
                idxByte++;      // advance to next byte of ROM mask 
                mask = 1;       // update mask 
            }
        }