-
-
Save idreamsi/d13ec7f99af320fea98272963c346aa8 to your computer and use it in GitHub Desktop.
wiolink grove driver for ds18b20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* grove_ds18b20.cpp | |
* | |
* Copyright (c) 2012 seeed technology inc. | |
* Website : www.seeed.cc | |
* Author : Kai | |
* | |
* The MIT License (MIT) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
#include "grove_ds18b20.h" | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
#ifndef ENABLE_DEBUG_ON_UART1 | |
#define ENABLE_DEBUG_ON_UART1 1 | |
#endif | |
#define ONE_WIRE_BUS 12 // DS18B20 on Digital Port 1 = pin 12 look at http://www.seeedstudio.com/wiki/Wio_Link | |
OneWire oneWire(ONE_WIRE_BUS); | |
DallasTemperature DS18B20(&oneWire); | |
GroveDs18b20::GroveDs18b20(int pin) | |
{ | |
#ifdef ENABLE_DEBUG_ON_UART1 | |
Serial1.println("Hello ds18b20!!!"); | |
byte i; | |
byte addr[8]; | |
if ( !oneWire.search(addr)) { | |
Serial1.print("no addresses found\n"); | |
oneWire.reset_search(); | |
return; | |
} | |
Serial1.print("R="); | |
for( i = 0; i < 8; i++) { | |
Serial1.print(addr[i], HEX); | |
Serial1.print(" "); | |
} | |
if ( OneWire::crc8( addr, 7) != addr[7]) { | |
Serial1.print("bad CRC!\n"); | |
return; | |
} | |
if ( addr[0] == 0x10) { | |
Serial1.print("device is DS18S20 family\n"); | |
} | |
else if ( addr[0] == 0x28) { | |
Serial1.print("device is DS18B20 family\n"); | |
} | |
else { | |
Serial1.print("device not recognized : 0x"); | |
Serial1.println(addr[0],HEX); | |
return; | |
} | |
#endif | |
DS18B20.begin();/* Inizialisieren der Dallas Temperature library */ | |
} | |
/* read count of devices */ | |
bool GroveDs18b20::read_device_count(uint8_t *count) | |
{ | |
*count = DS18B20.getDeviceCount(); | |
return true; | |
} | |
/* read temperature of first device in Celsius */ | |
bool GroveDs18b20::read_temperature_C(uint8_t index, float *temperature) | |
{ | |
DS18B20.requestTemperatures(); // Temp abfragen | |
if (index < 1 || index > DS18B20.getDeviceCount()) | |
{ | |
error_desc = "there is no device with this device number"; | |
return false; | |
} | |
*temperature = DS18B20.getTempCByIndex(index - 1); | |
return true; | |
} | |
/* read temperature of first device in Fahrenheit */ | |
bool GroveDs18b20::read_temperature_F(uint8_t index, float *temperature) | |
{ | |
DS18B20.requestTemperatures(); // Temp abfragen | |
if (index < 1 || index > DS18B20.getDeviceCount()) | |
{ | |
error_desc = "there is no device with this device number"; | |
return false; | |
} | |
*temperature = DS18B20.getTempFByIndex(index - 1); | |
return true; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* grove_ds18b20.h | |
* | |
* Copyright (c) 2012 seeed technology inc. | |
* Website : www.seeed.cc | |
* Author : Kai | |
* | |
* The MIT License (MIT) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
#ifndef __GROVE_DS18B20_H__ | |
#define __GROVE_DS18B20_H__ | |
#include "suli2.h" | |
//GROVE_NAME "Grove_ds18b20" | |
//SKU 0497dd7c-2faf-4ccb-900d-9a8f3b099fed | |
//IF_TYPE GPIO | |
//IMAGE_URL http://i.imgur.com/McTQZjy.jpg | |
class GroveDs18b20 | |
{ | |
public: | |
GroveDs18b20(int pin); | |
/** | |
* get the number of devices at the bus | |
* | |
* @param device_count - count of devices | |
* | |
* @return bool | |
*/ | |
bool read_device_count(uint8_t *device_count); | |
/** | |
* read the temperature of device with number device_index | |
* | |
* @param device_index - number of device to read from (1~n) | |
* @param celsius_degree - unit: Celsius degree | |
* | |
* @return bool | |
*/ | |
bool read_temperature_C(uint8_t device_index, float *celsius_degree); | |
/** | |
* read the temperature of device with number device_index | |
* | |
* @param device_count - number of device to read from (1~n) | |
* @param fahrenheit_degree - unit: Fahrenheit degree | |
* | |
* @return bool | |
*/ | |
bool read_temperature_F(uint8_t device_index, float *fahrenheit_degree); | |
char *get_last_error() { return error_desc; }; | |
private: | |
char *error_desc; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment