Dallas DS18B20
bit tmpreadbit()
{
unsigned int i;
bit dat;
DQ=0;i++; //i++ for delay
DQ=1;i++;i++;
dat=DQ;
i=8;
while(i>0)i--;
return (dat);
}
unsigned char read_byte()
{
unsigned char i, value = 0;
for (i=0;i<8;i++) {
value >>= 1;
if (read_bit()) value |= 0x80;
}
return value;
}
// Write a bit to the 1-Wire bus
void write_bit(unsigned char bitval)
{
DQ = 0; // Pull DQ low
_nop_(); _nop_(); // Wait 1-15 µs
DQ = bitval;
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); // Wait rest of timeslot
DQ = 1; // Release the bus
}
// Write a byte to the 1-Wire bus
void write_byte(unsigned char val)
{
unsigned char i;
for (i=0; i<8; i++) {
write_bit(val & 0x01); // LSB first
val >>= 1;
}
}
Page Source