I am starting a temperature measurement (sensors.requestTemperatures) in a timer interrupt and I need a way to know whether it is ready. I tried using the sensors.isConversionAvailable method, but it throws an exception. I looked at the source of the isConversionAvailable method and tried to call the readScratchPad method myself, and an exception was thrown there.
Full code:
#include <DallasTemperature.h>
#include <OneWire.h>
#define TIMER_SECONDS 1
#define SENSOR_PIN 5
OneWire sensorWire(SENSOR_PIN);
DallasTemperature sensors(&sensorWire);
void setup() {
Serial.begin(115200);
Serial.println("Initializing...");
sensors.begin();
sensors.setResolution(10);
sensors.setWaitForConversion(false);
timer1_isr_init();
timer1_attachInterrupt([] {
sensors.requestTemperatures();
});
timer1_enable(TIM_DIV265, TIM_EDGE, TIM_LOOP);
timer1_write(F_CPU / 256 * TIMER_SECONDS);
Serial.println("Initialization complete");
}
void loop() {
Serial.println("Started the loop method");
//When this block is uncommented, the second exception is thrown
/*uint8_t number;
sensors.readScratchPad(0, &number);
Serial.print("Scratch pad number: ");
Serial.println(number);*/
if (sensors.isConversionAvailable(0)) {
Serial.print("Temperature: ");
Serial.println(sensors.getTempCByIndex(0));
} else {
Serial.println("The conversion is unavailable");
}
Serial.println("Finished the loop method");
}
Exception (isConversionAvailable):
Initializing...
Initialization complete
Started the loop method
Exception (28):
epc1=0x402025af epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
ctx: cont
sp: 3ffef230 end: 3ffef460 offset: 01a0
>>>stack>>>
3ffef3d0: 00000000 3ffee354 00000000 402025ad
3ffef3e0: 3ffef420 00000000 3ffee338 40201e57
3ffef3f0: 00000000 00000017 3ffef4a0 40202bcd
3ffef400: 3ffe8588 00000000 3ffef4a0 3ffee42c
3ffef410: 3fffdad0 3ffee338 3ffef4a0 40202144
3ffef420: 40201052 88011627 3ffef4a0 40202c1c
3ffef430: 3fffdad0 00000000 3ffef4a0 40201d0e
3ffef440: feefeffe 00000000 3ffee424 40202888
3ffef450: feefeffe feefeffe 3ffee440 40100114
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,6)
Exception (readScratchPad):
Initializing...
Initialization complete
Started the loop method
Exception (28):
epc1=0x402025cf epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
ctx: cont
sp: 3ffef250 end: 3ffef470 offset: 01a0
>>>stack>>>
3ffef3f0: 00000000 3ffee364 00000000 402025cd
3ffef400: 3ffef440 00000000 3ffee348 40201e77
3ffef410: 00000000 3ffee348 3ffef4b0 40202c18
3ffef420: 3ffe8438 00000000 3ffef4b0 3ffee43c
3ffef430: 3fffdad0 3ffee348 3ffef4b0 40201d14
3ffef440: 3fffdad0 3ffee348 3ffef4b0 40201cd0
3ffef450: feefeffe 00000000 3ffee434 402028a8
3ffef460: feefeffe feefeffe 3ffee450 40100114
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,6)
Looking at the function's code (a few comments omitted):
bool DallasTemperature::readScratchPad(const uint8_t* deviceAddress, uint8_t* scratchPad){
// send the reset command and fail fast
int b = _wire->reset();
if (b == 0) return false;
_wire->select(deviceAddress);
_wire->write(READSCRATCH);
for(uint8_t i = 0; i < 9; i++){
scratchPad[i] = _wire->read();
}
b = _wire->reset();
return (b == 1);
}
You call the function with
sensors.readScratchPad(0, &number);
Then deviceAddress = NULL
. So then there is the call _wire->select(deviceAddress)
. The _wire
object is of type OneWire*
. The function select
is implemented as follows:
void OneWire::select(const uint8_t rom[8])
{
uint8_t i;
write(0x55); // Choose ROM
for (i = 0; i < 8; i++) write(rom[i]);
}
And in rom[i]
you have a dereference on the NULL
pointer you gave it initially. I.e.: Crash.
Looking at another function in the lib shows how to obtain the a valid deviceAddress
pointer.
float DallasTemperature::getTempCByIndex(uint8_t deviceIndex){
DeviceAddress deviceAddress;
if (!getAddress(deviceAddress, deviceIndex)){
return DEVICE_DISCONNECTED_C;
}
return getTempC((uint8_t*)deviceAddress);
}
So, if you want the address of your first sensor, you can get it by doing:
DeviceAddress deviceAddress;
if (!sensors.getAddress(deviceAddress, 0)){
Serial.println("No device address found for index 0!!!");
return;
}
//use deviceAddr in the next call
ScratchPad readScratchPad; //this is a typedef to uint8_t[9]
sensors.readScratchPad( (uint8_t*)deviceAddress, readScratchPad);
//do whatever you like with the values in the readScratchPad array..
For further references look at the library files at https://github.com/milesburton/Arduino-Temperature-Control-Library and https://github.com/PaulStoffregen/OneWire .