Beispiel #1
0
void dallasWaitUntilDone(void)
{
	//timerPause(6);
	
	// wait until we recieve a one
	cli();
	while(!dallasReadBit());
	sei();
}
Beispiel #2
0
u08 dallasReadByte(void)
{
	u08 i;
	u08 byte = 0;

	cli();
	
	// read all 8 bits
	for(i=0;i<8;i++)
	{
		if (dallasReadBit())
			byte |= 0x01<<i;

		// allow a us delay between each read
		dallasDelayUs(1);
	}

	sei();

	return byte;
}
u08 dallasFindNextDevice(dallas_rom_id_T* rom_id)
{
    u08 bit;
    u08 i = 0;
    u08 bit_index = 1;
    u08 byte_index = 0;
    u08 bit_mask = 1;
    u08 discrep_marker = 0;

    // reset the CRC
    dallas_crc = 0;

    if (done_flag || dallasReset() != DALLAS_PRESENCE)
    {
        // no more devices parts detected
        return FALSE;
    }

    // send search ROM command
    dallasWriteByte(DALLAS_SEARCH_ROM);

    // loop until through all 8 ROM bytes
    while(byte_index<8)
    {
        // read line 2 times to determine status of devices
        //    00 - devices connected to bus with conflicting bits
        //    01 - all devices have a 0 in this position
        //    10 - all devices ahve a 1 in this position
        //    11 - there are no devices connected to bus
        i = 0;
        cli();
        if (dallasReadBit())
            i = 2;				// store the msb if 1
        //delay_us(120);
        delay(120);
        if (dallasReadBit())
            i |= 1;				// store the lsb if 1
        sei();

        if (i==3)
        {
            // there are no devices on the 1-wire
            break;
        }
        else
        {
            if (i>0)
            {
                // all devices coupled have 0 or 1
                // shift 1 to determine if the msb is 0 or 1
                bit = i>>1;
            }
            else
            {
                // if this discrepancy is before the last discrepancy on a
                // previous FindNextDevice then pick the same as last time
                if (bit_index<last_discrep)
                    bit = ((rom_id->byte[byte_index] & bit_mask) > 0);
                else
                    bit = (bit_index==last_discrep);

                // if 0 was picked then record position with bit mask
                if (!bit)
                    discrep_marker = bit_index;
            }

            // isolate bit in rom_id->byte[byte_index] with bit mask
            if (bit)
                rom_id->byte[byte_index] |= bit_mask;
            else
                rom_id->byte[byte_index] &= ~bit_mask;

            // ROM search write
            //cli();
            dallasWriteBit(bit);
            //sei();

            // ncrement bit index counter and shift the bit mask
            bit_index++;
            bit_mask <<= 1;

            if (!bit_mask)
            {
                // if the mask is 0 then go to new ROM
                // accumulate the CRC and incriment the byte index and bit mask
                dallasCRC(rom_id->byte[byte_index]);
                byte_index++;
                bit_mask++;
            }
        }