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)
sensors.requestTemperatures()in your main loop. Your reset cause 28 is probably caused by a pointer pointing to an uninitialized object. Are you sure the temperature-IC is ready? - BMelis