bobbycar/controller_teensy/include/temperature.h

106 lines
3.2 KiB
C

#ifndef _TEMPERATURE_H_
#define _TEMPERATURE_H_
#include <OneWire.h>
#include <DallasTemperature.h>
//#define SEARCH_DEVICES
DeviceAddress thermometerFront={0x28,0xFF,0x64,0x0E,0x77,0xB0,0xAB,0x4B}; //IC with one marking 28FF640E77B0AB4B
float temp_Front;
DeviceAddress thermometerRear={0x28,0xFF,0x64,0x0E,0x76,0x5D,0x86,0xC2}; //IC with two markings
float temp_Rear;
DeviceAddress thermometerAir={0x28,0xFF,0x64,0x0E,0x74,0x7E,0xFE,0x23}; //IC with three markings 28FF640E747EFE23
float temp_Air;
#define ONE_WIRE_BUS 23 //GPIO pin
#define TEMPERATURE_PRECISION 12 //max is 12
#define READINTERVAL_DS18B20 5000 //ms
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void initTemperature();
bool temperatureLoop();
void printAddress(DeviceAddress deviceAddress);
void initTemperature() {
sensors.begin();
//Just search for devices. Only needed when connecting a new sensor to find the address
#ifdef SEARCH_DEVICES
Serial.print("Locating 1Wire devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
oneWire.reset_search();
for (uint8_t i=0;i<sensors.getDeviceCount();i++){
DeviceAddress _addr;
if (!oneWire.search(_addr)) {
Serial.print("Error: Device not found");
}else{
Serial.print("Found device. Address:");
printAddress(_addr);
}
Serial.println();
}
delay(1000);
#endif
//sensors.setResolution(thermometerESCFront, TEMPERATURE_PRECISION);
}
bool temperatureLoop(unsigned long loopmillis) {
static unsigned long last_read_ds18b20;
static bool flag_requestTemperatures=false;
if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20) {
if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20*4) { //timeout
Serial.println("Warn: Request Temperatures Timeout!");
flag_requestTemperatures=false;
last_read_ds18b20=loopmillis; //set time to lastread to wait for another timeout
return 0;
}
if (!flag_requestTemperatures) {
//in synchronous mode requestTemperatures takes ~34ms if sensors are available. if sensors not available it takes about 729ms
sensors.setWaitForConversion(false); //make async
//in asynchronous mode it takes ~2ms if sensors are available. if not it takes ~1ms
sensors.requestTemperatures();
sensors.setWaitForConversion(true);
flag_requestTemperatures=true;
}
if (sensors.isConversionComplete()) {
flag_requestTemperatures=false;
last_read_ds18b20=loopmillis;
temp_Front= sensors.getTempC(thermometerFront); //This takes ~12.5ms
temp_Rear= sensors.getTempC(thermometerRear);
temp_Air= sensors.getTempC(thermometerAir);
}
}
return 1;
}
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
// zero pad the address if necessary
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
#endif