Hi.
I've been porting the onewire lib to work in Due.
I've it working well in my program, but would be nice if some more people would test it also.
Library is attached.
I've built a test sketch with the function I'm using to read the temperature. Sketch as follows:
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <OneWire.h>
OneWire ds(54); // data bus on pin A0
//DS18B20
float celsius;
unsigned int raw_temp;
boolean conversioninprogress=false;
byte data[12];
byte addr[8];
unsigned long tempconvmillis;
unsigned long waitforesultmillis;
float read_temp()
{
if (!conversioninprogress)
{
ds.reset();
delay(1);
ds.skip();
delay(1);
ds.write(0x44,0); // start conversion, with parasite power off at the end
conversioninprogress=true;
waitforesultmillis=millis();
}
if (((millis()-waitforesultmillis) > 1000) && conversioninprogress)
// maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
{
byte i;
byte present = 0;
present = ds.reset();
delay(1);
ds.skip();
delay(1);
ds.write(0xBE,0); // Read Scratchpad
delay(1);
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
// convert the data to actual temperature
raw_temp = (data[1] << 8) | data[0];
unsigned char t_mask[4] = {0x7, 0x3, 0x1, 0x0};
byte cfg = (data[4] & 0x60) >> 5;
raw_temp &= ~t_mask[cfg];
conversioninprogress=false;
celsius = (float)raw_temp / 16.0;
}
tempconvmillis=millis();
return celsius;
}
void setup()
{
Serial.begin(115200);
}
void loop()
{
if ((millis()-tempconvmillis) > 2000)
Serial.println(read_temp());
}
Regards,
Joao
OneWire_working_due.zip (13.9 KB)