OneWire


OneWire( uint8_t pin)

构造函数

void depower(void)

断电,总线停止

uint8_t read(void)

读取一个字节数据

uint8_t read_bit(void)

读取一位数据

void read_bytes(uint8_t *buf, uint16_t count)

读取多个字节的数据

uint8_t reset(void)

总线复位

void select(const uint8_t rom[8])

选择设备

void skip(void)

跳过设备选择

void write(uint8_t v, uint8_t power = 0)

写入一个字节数据

void write_bit(uint8_t v)

写入一位的数据

void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0)

写入多个数据

应用示例:DS18S20

#include 


OneWire  ds(10);  

void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
    Serial.print("No more addresses.\n");
    ds.reset_search();
    delay(250);
    return;
  }

  Serial.print("R=");
  for( i = 0; i < 8; i++) {
    Serial.print(addr[i], HEX);
    Serial.print(" ");
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n");
      return;
  }

  if ( addr[0] != 0x10) {
      Serial.print("Device is not a DS18S20 family device.\n");
      return;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         

  delay(1000);     

  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);        

  Serial.print("P=");
  Serial.print(present,HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print( OneWire::crc8( data, 8), HEX);
  Serial.println();
}