Added new telstick windows drivers
Former-commit-id: 797b455c0a2b358e691a198017acea4eb5c02f73
This commit is contained in:
parent
36d0826a33
commit
af01fb9ae9
22 changed files with 2886 additions and 13 deletions
80
arduino/PowerTransmitter/BH1750FVI.cpp
Executable file
80
arduino/PowerTransmitter/BH1750FVI.cpp
Executable file
|
|
@ -0,0 +1,80 @@
|
|||
#include "BH1750FVI.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
BH1750FVI::BH1750FVI(){
|
||||
|
||||
}
|
||||
|
||||
void BH1750FVI::begin(void){
|
||||
Wire.begin();
|
||||
I2CWriteTo(Power_On ); //Turn it On
|
||||
pinMode(AddrPin,OUTPUT);
|
||||
digitalWrite(AddrPin,HIGH);
|
||||
|
||||
}
|
||||
void BH1750FVI::Sleep(void){
|
||||
I2CWriteTo(Power_Down ); //Turn it off , Reset operator won't work in this mode
|
||||
}
|
||||
void BH1750FVI::Reset(void){
|
||||
I2CWriteTo(Power_On ); //Turn it on again
|
||||
I2CWriteTo(reset ); //Reset
|
||||
|
||||
}
|
||||
void BH1750FVI::SetAddress(uint8_t add){
|
||||
switch (add){
|
||||
case Device_Address_L:
|
||||
address_value=Device_Address_L;
|
||||
digitalWrite(AddrPin,LOW);
|
||||
state=false;
|
||||
break;
|
||||
case Device_Address_H:
|
||||
address_value=Device_Address_H;
|
||||
digitalWrite(AddrPin,HIGH);
|
||||
state=true;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
void BH1750FVI::SetMode(uint8_t MODE){
|
||||
switch(MODE){
|
||||
case Continuous_H_resolution_Mode:
|
||||
break;
|
||||
case Continuous_H_resolution_Mode2:
|
||||
break;
|
||||
case Continuous_L_resolution_Mode:
|
||||
break;
|
||||
case OneTime_H_resolution_Mode:
|
||||
break;
|
||||
case OneTime_H_resolution_Mode2:
|
||||
break;
|
||||
case OneTime_L_resolution_Mode:
|
||||
break;
|
||||
}
|
||||
delay(10);
|
||||
I2CWriteTo(MODE);
|
||||
}
|
||||
|
||||
uint16_t BH1750FVI::GetLightIntensity(void){
|
||||
uint16_t Intensity_value;
|
||||
if(state ==true){
|
||||
Wire.beginTransmission(Device_Address_H);
|
||||
Wire.requestFrom(Device_Address_H, 2);
|
||||
}
|
||||
if(state ==false){
|
||||
Wire.beginTransmission(Device_Address_L);
|
||||
Wire.requestFrom(Device_Address_L, 2);
|
||||
}
|
||||
Intensity_value = Wire.read();
|
||||
Intensity_value <<= 8;
|
||||
Intensity_value |= Wire.read();
|
||||
Wire.endTransmission();
|
||||
Intensity_value=Intensity_value/1.2;
|
||||
return Intensity_value;
|
||||
|
||||
}
|
||||
|
||||
void BH1750FVI::I2CWriteTo(uint8_t DataToSend){
|
||||
Wire.beginTransmission(address_value);
|
||||
Wire.write(DataToSend);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
68
arduino/PowerTransmitter/BH1750FVI.h
Executable file
68
arduino/PowerTransmitter/BH1750FVI.h
Executable file
|
|
@ -0,0 +1,68 @@
|
|||
|
||||
|
||||
/* This library for Digital Light sensor BH1750FVI
|
||||
|
||||
use I2C Communication protocal , SDA,SCL Are required
|
||||
|
||||
to interface with this sensor
|
||||
|
||||
pin configuration :
|
||||
|
||||
VCC >>> 3.3V
|
||||
SDA >>> A4
|
||||
SCL >>> A5
|
||||
ADDR >> A3 "Optional"
|
||||
GND >>> gnd
|
||||
|
||||
written By : Mohannad Rawashdeh
|
||||
www.genotronex.com
|
||||
*/
|
||||
|
||||
#ifndef BH1750FVI_h
|
||||
#define BH1750FVI_h
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#include "Wire.h"
|
||||
|
||||
#define Device_Address_L 0x23 // Device address when address pin LOW
|
||||
#define Device_Address_H 0x5C // Device address when address pin LOW
|
||||
//all command here taken from Data sheet OPECODE Table page 5
|
||||
#define Power_Down 0x00
|
||||
|
||||
#define Power_On 0x01
|
||||
|
||||
#define reset 0x07
|
||||
|
||||
#define Continuous_H_resolution_Mode 0x10
|
||||
|
||||
#define Continuous_H_resolution_Mode2 0x11
|
||||
|
||||
#define Continuous_L_resolution_Mode 0x13
|
||||
|
||||
#define OneTime_H_resolution_Mode 0x20
|
||||
|
||||
#define OneTime_H_resolution_Mode2 0x21
|
||||
|
||||
#define OneTime_L_resolution_Mode 0x23//As well as address value
|
||||
|
||||
#define AddrPin 17 // Address pin enable
|
||||
class BH1750FVI {
|
||||
public:
|
||||
BH1750FVI();
|
||||
void begin(void);
|
||||
void Sleep(void);
|
||||
void SetMode(uint8_t MODE);
|
||||
void Reset(void);
|
||||
void SetAddress(uint8_t add);
|
||||
uint16_t GetLightIntensity(void);
|
||||
|
||||
private:
|
||||
void I2CWriteTo(uint8_t DataToSend);
|
||||
byte address_value;
|
||||
boolean state;
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
294
arduino/PowerTransmitter/PowerTransmitter.ino
Executable file
294
arduino/PowerTransmitter/PowerTransmitter.ino
Executable file
|
|
@ -0,0 +1,294 @@
|
|||
/*
|
||||
* Protocol: Oregon V2.1
|
||||
* Emulating sensor: THGR2228N
|
||||
*/
|
||||
|
||||
#include <Wire.h>
|
||||
#include "BH1750FVI.h"
|
||||
|
||||
BH1750FVI LightSensor;
|
||||
const byte TX_PIN = 10;
|
||||
const byte LED_PIN = 13;
|
||||
const unsigned long TIME = 512;
|
||||
const unsigned long TWOTIME = TIME*2;
|
||||
#define SEND_HIGH() digitalWrite(TX_PIN, HIGH)
|
||||
#define SEND_LOW() digitalWrite(TX_PIN, LOW)
|
||||
byte OregonMessageBuffer[9];
|
||||
unsigned long previousTime = 0;
|
||||
unsigned long currentTime = millis();
|
||||
int impulseCount = 0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
|
||||
pinMode(TX_PIN, OUTPUT);
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
SEND_LOW();
|
||||
byte ID[] = { 0x1A,0x2D }; //temperature/humidity sensor (THGR2228N)
|
||||
setType(OregonMessageBuffer, ID);
|
||||
setChannel(OregonMessageBuffer, 0x20);
|
||||
|
||||
LightSensor.begin();
|
||||
LightSensor.SetAddress(Device_Address_H);
|
||||
LightSensor.SetMode(Continuous_H_resolution_Mode);
|
||||
Serial.print("Started");
|
||||
}
|
||||
|
||||
|
||||
boolean light = false;
|
||||
void loop()
|
||||
{
|
||||
currentTime = millis();
|
||||
uint16_t lux = LightSensor.GetLightIntensity();
|
||||
if(lux > 100 && !light){
|
||||
light = true;
|
||||
impulseCount++;
|
||||
}else if(lux < 100){
|
||||
light = false;
|
||||
}
|
||||
if(currentTime - previousTime > 5000) {
|
||||
previousTime = currentTime;
|
||||
Serial.print("total impulses = ");
|
||||
Serial.println(impulseCount);
|
||||
send433(impulseCount,0,0xBA);
|
||||
impulseCount = 0;
|
||||
delay(500);
|
||||
}
|
||||
}
|
||||
|
||||
void send433(float temperature, byte humidity, byte Identitet)
|
||||
{
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
setId(OregonMessageBuffer, Identitet); //set id of the sensor, BB=187
|
||||
setBatteryLevel(OregonMessageBuffer, 1); // 0 : low, 1 : high
|
||||
setTemperature(OregonMessageBuffer, temperature); //org setTemperature(OregonMessageBuffer, 55.5);
|
||||
setHumidity(OregonMessageBuffer, humidity);
|
||||
calculateAndSetChecksum(OregonMessageBuffer);
|
||||
|
||||
// Show the Oregon Message
|
||||
for (byte i = 0; i < sizeof(OregonMessageBuffer); ++i) {
|
||||
Serial.print(OregonMessageBuffer[i] >> 4, HEX);
|
||||
Serial.print(OregonMessageBuffer[i] & 0x0F, HEX);
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// Send the Message over RF
|
||||
sendOregon(OregonMessageBuffer, sizeof(OregonMessageBuffer));
|
||||
// Send a "pause"
|
||||
SEND_LOW();
|
||||
delayMicroseconds(TWOTIME*8);
|
||||
// Send a copie of the first message. The v2.1 protocol send the message two time
|
||||
sendOregon(OregonMessageBuffer, sizeof(OregonMessageBuffer));
|
||||
SEND_LOW();
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
}
|
||||
|
||||
inline void setId(byte *data, byte ID)
|
||||
{
|
||||
data[3] = ID;
|
||||
}
|
||||
|
||||
void setBatteryLevel(byte *data, byte level)
|
||||
{
|
||||
if(!level) data[4] = 0x0C;
|
||||
else data[4] = 0x00;
|
||||
}
|
||||
|
||||
void setTemperature(byte *data, float temp)
|
||||
{
|
||||
// Set temperature sign
|
||||
if(temp < 0)
|
||||
{
|
||||
data[6] = 0x08;
|
||||
temp *= -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
data[6] = 0x00;
|
||||
}
|
||||
|
||||
// Determine decimal and float part
|
||||
int tempInt = (int)temp;
|
||||
int td = (int)(tempInt / 10);
|
||||
int tf = (int)round((float)((float)tempInt/10 - (float)td) * 10);
|
||||
|
||||
int tempFloat = (int)round((float)(temp - (float)tempInt) * 10);
|
||||
|
||||
// Set temperature decimal part
|
||||
data[5] = (td << 4);
|
||||
data[5] |= tf;
|
||||
|
||||
// Set temperature float part
|
||||
data[4] |= (tempFloat << 4);
|
||||
}
|
||||
|
||||
void setHumidity(byte* data, byte hum)
|
||||
{
|
||||
data[7] = (hum/10);
|
||||
data[6] |= (hum - data[7]*10) << 4;
|
||||
}
|
||||
|
||||
void calculateAndSetChecksum(byte* data)
|
||||
{
|
||||
int sum = 0;
|
||||
for(byte i = 0; i<8;i++)
|
||||
{
|
||||
sum += (data[i]&0xF0) >> 4;
|
||||
sum += (data[i]&0xF);
|
||||
}
|
||||
data[8] = ((sum - 0xa) & 0xFF);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//*********************************************************************************************************
|
||||
|
||||
|
||||
/**
|
||||
* \brief Send logical "0" over RF
|
||||
* \details azero bit be represented by an off-to-on transition
|
||||
* \ of the RF signal at the middle of a clock period.
|
||||
* \ Remenber, the Oregon v2.1 protocol add an inverted bit first
|
||||
*/
|
||||
inline void sendZero(void)
|
||||
{
|
||||
SEND_HIGH();
|
||||
delayMicroseconds(TIME);
|
||||
SEND_LOW();
|
||||
delayMicroseconds(TWOTIME);
|
||||
SEND_HIGH();
|
||||
delayMicroseconds(TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send logical "1" over RF
|
||||
* \details a one bit be represented by an on-to-off transition
|
||||
* \ of the RF signal at the middle of a clock period.
|
||||
* \ Remenber, the Oregon v2.1 protocol add an inverted bit first
|
||||
*/
|
||||
inline void sendOne(void)
|
||||
{
|
||||
SEND_LOW();
|
||||
delayMicroseconds(TIME);
|
||||
SEND_HIGH();
|
||||
delayMicroseconds(TWOTIME);
|
||||
SEND_LOW();
|
||||
delayMicroseconds(TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send a bits quarter (4 bits = MSB from 8 bits value) over RF
|
||||
* \param data Data to send
|
||||
*/
|
||||
inline void sendQuarterMSB(const byte data)
|
||||
{
|
||||
(bitRead(data, 4)) ? sendOne() : sendZero();
|
||||
(bitRead(data, 5)) ? sendOne() : sendZero();
|
||||
(bitRead(data, 6)) ? sendOne() : sendZero();
|
||||
(bitRead(data, 7)) ? sendOne() : sendZero();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send a bits quarter (4 bits = LSB from 8 bits value) over RF
|
||||
* \param data Data to send
|
||||
*/
|
||||
inline void sendQuarterLSB(const byte data)
|
||||
{
|
||||
(bitRead(data, 0)) ? sendOne() : sendZero();
|
||||
(bitRead(data, 1)) ? sendOne() : sendZero();
|
||||
(bitRead(data, 2)) ? sendOne() : sendZero();
|
||||
(bitRead(data, 3)) ? sendOne() : sendZero();
|
||||
}
|
||||
|
||||
/******************************************************************/
|
||||
/******************************************************************/
|
||||
/******************************************************************/
|
||||
|
||||
/**
|
||||
* \brief Send a buffer over RF
|
||||
* \param data Data to send
|
||||
* \param size size of data to send
|
||||
*/
|
||||
void sendData(byte *data, byte size)
|
||||
{
|
||||
for(byte i = 0; i < size; ++i)
|
||||
{
|
||||
sendQuarterLSB(data[i]);
|
||||
sendQuarterMSB(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send an Oregon message
|
||||
* \param data The Oregon message
|
||||
*/
|
||||
void sendOregon(byte *data, byte size)
|
||||
{
|
||||
sendPreamble();
|
||||
//sendSync();
|
||||
sendData(data, size);
|
||||
sendPostamble();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send preamble
|
||||
* \details The preamble consists of 16 "1" bits
|
||||
*/
|
||||
inline void sendPreamble(void)
|
||||
{
|
||||
byte PREAMBLE[]={
|
||||
0xFF,0xFF };
|
||||
sendData(PREAMBLE, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send postamble
|
||||
* \details The postamble consists of 8 "0" bits
|
||||
*/
|
||||
inline void sendPostamble(void)
|
||||
{
|
||||
byte POSTAMBLE[]={
|
||||
0x00 };
|
||||
sendData(POSTAMBLE, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send sync nibble
|
||||
* \details The sync is 0xA. It is not use in this version since the sync nibble
|
||||
* \ is include in the Oregon message to send.
|
||||
*/
|
||||
inline void sendSync(void)
|
||||
{
|
||||
sendQuarterLSB(0xA);
|
||||
}
|
||||
|
||||
/******************************************************************/
|
||||
/******************************************************************/
|
||||
/******************************************************************/
|
||||
|
||||
/**
|
||||
* \brief Set the sensor type
|
||||
* \param data Oregon message
|
||||
* \param type Sensor type
|
||||
*/
|
||||
inline void setType(byte *data, byte* type)
|
||||
{
|
||||
data[0] = type[0];
|
||||
data[1] = type[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Set the sensor channel
|
||||
* \param data Oregon message
|
||||
* \param channel Sensor channel (0x10, 0x20, 0x30)
|
||||
*/
|
||||
inline void setChannel(byte *data, byte channel)
|
||||
{
|
||||
data[2] = channel;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
289
arduino/ThermometerTransmitter/ThermometerTransmitter.ino
Executable file
289
arduino/ThermometerTransmitter/ThermometerTransmitter.ino
Executable file
|
|
@ -0,0 +1,289 @@
|
|||
/*
|
||||
* Protocol: Oregon V2.1
|
||||
* Emulating sensor: THGR2228N
|
||||
*/
|
||||
|
||||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
|
||||
#define TEMP_SENSOR_PIN 9
|
||||
OneWire oneWire(TEMP_SENSOR_PIN);
|
||||
DallasTemperature tempSensors(&oneWire);
|
||||
|
||||
const byte TX_PIN = 10;
|
||||
const byte LED_PIN = 13;
|
||||
const unsigned long TIME = 512;
|
||||
const unsigned long TWOTIME = TIME*2;
|
||||
#define SEND_HIGH() digitalWrite(TX_PIN, HIGH)
|
||||
#define SEND_LOW() digitalWrite(TX_PIN, LOW)
|
||||
byte OregonMessageBuffer[9];
|
||||
unsigned long previousTime = 0;
|
||||
unsigned long currentTime = millis();
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
|
||||
pinMode(TX_PIN, OUTPUT);
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
SEND_LOW();
|
||||
byte ID[] = { 0x1A,0x2D }; //temperature/humidity sensor (THGR2228N)
|
||||
setType(OregonMessageBuffer, ID);
|
||||
setChannel(OregonMessageBuffer, 0x20);
|
||||
|
||||
tempSensors.begin();
|
||||
}
|
||||
|
||||
|
||||
boolean light = false;
|
||||
void loop()
|
||||
{
|
||||
currentTime = millis();
|
||||
if(currentTime - previousTime > 5000) {
|
||||
previousTime = currentTime;
|
||||
|
||||
tempSensors.requestTemperatures();
|
||||
float temp = tempSensors.getTempCByIndex(0);
|
||||
Serial.print("temp = ");
|
||||
Serial.println(temp);
|
||||
|
||||
send433(temp,0,0xBB);
|
||||
delay(500);
|
||||
}
|
||||
}
|
||||
|
||||
void send433(float temperature, byte humidity, byte Identitet)
|
||||
{
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
setId(OregonMessageBuffer, Identitet); //set id of the sensor, BB=187
|
||||
setBatteryLevel(OregonMessageBuffer, 1); // 0 : low, 1 : high
|
||||
setTemperature(OregonMessageBuffer, temperature); //org setTemperature(OregonMessageBuffer, 55.5);
|
||||
setHumidity(OregonMessageBuffer, humidity);
|
||||
calculateAndSetChecksum(OregonMessageBuffer);
|
||||
|
||||
// Show the Oregon Message
|
||||
for (byte i = 0; i < sizeof(OregonMessageBuffer); ++i) {
|
||||
Serial.print(OregonMessageBuffer[i] >> 4, HEX);
|
||||
Serial.print(OregonMessageBuffer[i] & 0x0F, HEX);
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// Send the Message over RF
|
||||
sendOregon(OregonMessageBuffer, sizeof(OregonMessageBuffer));
|
||||
// Send a "pause"
|
||||
SEND_LOW();
|
||||
delayMicroseconds(TWOTIME*8);
|
||||
// Send a copie of the first message. The v2.1 protocol send the message two time
|
||||
sendOregon(OregonMessageBuffer, sizeof(OregonMessageBuffer));
|
||||
SEND_LOW();
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
}
|
||||
|
||||
inline void setId(byte *data, byte ID)
|
||||
{
|
||||
data[3] = ID;
|
||||
}
|
||||
|
||||
void setBatteryLevel(byte *data, byte level)
|
||||
{
|
||||
if(!level) data[4] = 0x0C;
|
||||
else data[4] = 0x00;
|
||||
}
|
||||
|
||||
void setTemperature(byte *data, float temp)
|
||||
{
|
||||
// Set temperature sign
|
||||
if(temp < 0)
|
||||
{
|
||||
data[6] = 0x08;
|
||||
temp *= -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
data[6] = 0x00;
|
||||
}
|
||||
|
||||
// Determine decimal and float part
|
||||
int tempInt = (int)temp;
|
||||
int td = (int)(tempInt / 10);
|
||||
int tf = (int)round((float)((float)tempInt/10 - (float)td) * 10);
|
||||
|
||||
int tempFloat = (int)round((float)(temp - (float)tempInt) * 10);
|
||||
|
||||
// Set temperature decimal part
|
||||
data[5] = (td << 4);
|
||||
data[5] |= tf;
|
||||
|
||||
// Set temperature float part
|
||||
data[4] |= (tempFloat << 4);
|
||||
}
|
||||
|
||||
void setHumidity(byte* data, byte hum)
|
||||
{
|
||||
data[7] = (hum/10);
|
||||
data[6] |= (hum - data[7]*10) << 4;
|
||||
}
|
||||
|
||||
void calculateAndSetChecksum(byte* data)
|
||||
{
|
||||
int sum = 0;
|
||||
for(byte i = 0; i<8;i++)
|
||||
{
|
||||
sum += (data[i]&0xF0) >> 4;
|
||||
sum += (data[i]&0xF);
|
||||
}
|
||||
data[8] = ((sum - 0xa) & 0xFF);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//*********************************************************************************************************
|
||||
|
||||
|
||||
/**
|
||||
* \brief Send logical "0" over RF
|
||||
* \details azero bit be represented by an off-to-on transition
|
||||
* \ of the RF signal at the middle of a clock period.
|
||||
* \ Remenber, the Oregon v2.1 protocol add an inverted bit first
|
||||
*/
|
||||
inline void sendZero(void)
|
||||
{
|
||||
SEND_HIGH();
|
||||
delayMicroseconds(TIME);
|
||||
SEND_LOW();
|
||||
delayMicroseconds(TWOTIME);
|
||||
SEND_HIGH();
|
||||
delayMicroseconds(TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send logical "1" over RF
|
||||
* \details a one bit be represented by an on-to-off transition
|
||||
* \ of the RF signal at the middle of a clock period.
|
||||
* \ Remenber, the Oregon v2.1 protocol add an inverted bit first
|
||||
*/
|
||||
inline void sendOne(void)
|
||||
{
|
||||
SEND_LOW();
|
||||
delayMicroseconds(TIME);
|
||||
SEND_HIGH();
|
||||
delayMicroseconds(TWOTIME);
|
||||
SEND_LOW();
|
||||
delayMicroseconds(TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send a bits quarter (4 bits = MSB from 8 bits value) over RF
|
||||
* \param data Data to send
|
||||
*/
|
||||
inline void sendQuarterMSB(const byte data)
|
||||
{
|
||||
(bitRead(data, 4)) ? sendOne() : sendZero();
|
||||
(bitRead(data, 5)) ? sendOne() : sendZero();
|
||||
(bitRead(data, 6)) ? sendOne() : sendZero();
|
||||
(bitRead(data, 7)) ? sendOne() : sendZero();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send a bits quarter (4 bits = LSB from 8 bits value) over RF
|
||||
* \param data Data to send
|
||||
*/
|
||||
inline void sendQuarterLSB(const byte data)
|
||||
{
|
||||
(bitRead(data, 0)) ? sendOne() : sendZero();
|
||||
(bitRead(data, 1)) ? sendOne() : sendZero();
|
||||
(bitRead(data, 2)) ? sendOne() : sendZero();
|
||||
(bitRead(data, 3)) ? sendOne() : sendZero();
|
||||
}
|
||||
|
||||
/******************************************************************/
|
||||
/******************************************************************/
|
||||
/******************************************************************/
|
||||
|
||||
/**
|
||||
* \brief Send a buffer over RF
|
||||
* \param data Data to send
|
||||
* \param size size of data to send
|
||||
*/
|
||||
void sendData(byte *data, byte size)
|
||||
{
|
||||
for(byte i = 0; i < size; ++i)
|
||||
{
|
||||
sendQuarterLSB(data[i]);
|
||||
sendQuarterMSB(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send an Oregon message
|
||||
* \param data The Oregon message
|
||||
*/
|
||||
void sendOregon(byte *data, byte size)
|
||||
{
|
||||
sendPreamble();
|
||||
//sendSync();
|
||||
sendData(data, size);
|
||||
sendPostamble();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send preamble
|
||||
* \details The preamble consists of 16 "1" bits
|
||||
*/
|
||||
inline void sendPreamble(void)
|
||||
{
|
||||
byte PREAMBLE[]={
|
||||
0xFF,0xFF };
|
||||
sendData(PREAMBLE, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send postamble
|
||||
* \details The postamble consists of 8 "0" bits
|
||||
*/
|
||||
inline void sendPostamble(void)
|
||||
{
|
||||
byte POSTAMBLE[]={
|
||||
0x00 };
|
||||
sendData(POSTAMBLE, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Send sync nibble
|
||||
* \details The sync is 0xA. It is not use in this version since the sync nibble
|
||||
* \ is include in the Oregon message to send.
|
||||
*/
|
||||
inline void sendSync(void)
|
||||
{
|
||||
sendQuarterLSB(0xA);
|
||||
}
|
||||
|
||||
/******************************************************************/
|
||||
/******************************************************************/
|
||||
/******************************************************************/
|
||||
|
||||
/**
|
||||
* \brief Set the sensor type
|
||||
* \param data Oregon message
|
||||
* \param type Sensor type
|
||||
*/
|
||||
inline void setType(byte *data, byte* type)
|
||||
{
|
||||
data[0] = type[0];
|
||||
data[1] = type[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Set the sensor channel
|
||||
* \param data Oregon message
|
||||
* \param channel Sensor channel (0x10, 0x20, 0x30)
|
||||
*/
|
||||
inline void setChannel(byte *data, byte channel)
|
||||
{
|
||||
data[2] = channel;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
80
arduino/lib/BH1750/BH1750FVI.cpp
Executable file
80
arduino/lib/BH1750/BH1750FVI.cpp
Executable file
|
|
@ -0,0 +1,80 @@
|
|||
#include "BH1750FVI.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
BH1750FVI::BH1750FVI(){
|
||||
|
||||
}
|
||||
|
||||
void BH1750FVI::begin(void){
|
||||
Wire.begin();
|
||||
I2CWriteTo(Power_On ); //Turn it On
|
||||
pinMode(AddrPin,OUTPUT);
|
||||
digitalWrite(AddrPin,HIGH);
|
||||
|
||||
}
|
||||
void BH1750FVI::Sleep(void){
|
||||
I2CWriteTo(Power_Down ); //Turn it off , Reset operator won't work in this mode
|
||||
}
|
||||
void BH1750FVI::Reset(void){
|
||||
I2CWriteTo(Power_On ); //Turn it on again
|
||||
I2CWriteTo(reset ); //Reset
|
||||
|
||||
}
|
||||
void BH1750FVI::SetAddress(uint8_t add){
|
||||
switch (add){
|
||||
case Device_Address_L:
|
||||
address_value=Device_Address_L;
|
||||
digitalWrite(AddrPin,LOW);
|
||||
state=false;
|
||||
break;
|
||||
case Device_Address_H:
|
||||
address_value=Device_Address_H;
|
||||
digitalWrite(AddrPin,HIGH);
|
||||
state=true;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
void BH1750FVI::SetMode(uint8_t MODE){
|
||||
switch(MODE){
|
||||
case Continuous_H_resolution_Mode:
|
||||
break;
|
||||
case Continuous_H_resolution_Mode2:
|
||||
break;
|
||||
case Continuous_L_resolution_Mode:
|
||||
break;
|
||||
case OneTime_H_resolution_Mode:
|
||||
break;
|
||||
case OneTime_H_resolution_Mode2:
|
||||
break;
|
||||
case OneTime_L_resolution_Mode:
|
||||
break;
|
||||
}
|
||||
delay(10);
|
||||
I2CWriteTo(MODE);
|
||||
}
|
||||
|
||||
uint16_t BH1750FVI::GetLightIntensity(void){
|
||||
uint16_t Intensity_value;
|
||||
if(state ==true){
|
||||
Wire.beginTransmission(Device_Address_H);
|
||||
Wire.requestFrom(Device_Address_H, 2);
|
||||
}
|
||||
if(state ==false){
|
||||
Wire.beginTransmission(Device_Address_L);
|
||||
Wire.requestFrom(Device_Address_L, 2);
|
||||
}
|
||||
Intensity_value = Wire.read();
|
||||
Intensity_value <<= 8;
|
||||
Intensity_value |= Wire.read();
|
||||
Wire.endTransmission();
|
||||
Intensity_value=Intensity_value/1.2;
|
||||
return Intensity_value;
|
||||
|
||||
}
|
||||
|
||||
void BH1750FVI::I2CWriteTo(uint8_t DataToSend){
|
||||
Wire.beginTransmission(address_value);
|
||||
Wire.write(DataToSend);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
68
arduino/lib/BH1750/BH1750FVI.h
Executable file
68
arduino/lib/BH1750/BH1750FVI.h
Executable file
|
|
@ -0,0 +1,68 @@
|
|||
|
||||
|
||||
/* This library for Digital Light sensor BH1750FVI
|
||||
|
||||
use I2C Communication protocal , SDA,SCL Are required
|
||||
|
||||
to interface with this sensor
|
||||
|
||||
pin configuration :
|
||||
|
||||
VCC >>> 3.3V
|
||||
SDA >>> A4
|
||||
SCL >>> A5
|
||||
ADDR >> A3 "Optional"
|
||||
GND >>> gnd
|
||||
|
||||
written By : Mohannad Rawashdeh
|
||||
www.genotronex.com
|
||||
*/
|
||||
|
||||
#ifndef BH1750FVI_h
|
||||
#define BH1750FVI_h
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#include "Wire.h"
|
||||
|
||||
#define Device_Address_L 0x23 // Device address when address pin LOW
|
||||
#define Device_Address_H 0x5C // Device address when address pin LOW
|
||||
//all command here taken from Data sheet OPECODE Table page 5
|
||||
#define Power_Down 0x00
|
||||
|
||||
#define Power_On 0x01
|
||||
|
||||
#define reset 0x07
|
||||
|
||||
#define Continuous_H_resolution_Mode 0x10
|
||||
|
||||
#define Continuous_H_resolution_Mode2 0x11
|
||||
|
||||
#define Continuous_L_resolution_Mode 0x13
|
||||
|
||||
#define OneTime_H_resolution_Mode 0x20
|
||||
|
||||
#define OneTime_H_resolution_Mode2 0x21
|
||||
|
||||
#define OneTime_L_resolution_Mode 0x23//As well as address value
|
||||
|
||||
#define AddrPin 17 // Address pin enable
|
||||
class BH1750FVI {
|
||||
public:
|
||||
BH1750FVI();
|
||||
void begin(void);
|
||||
void Sleep(void);
|
||||
void SetMode(uint8_t MODE);
|
||||
void Reset(void);
|
||||
void SetAddress(uint8_t add);
|
||||
uint16_t GetLightIntensity(void);
|
||||
|
||||
private:
|
||||
void I2CWriteTo(uint8_t DataToSend);
|
||||
byte address_value;
|
||||
boolean state;
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
60
arduino/lib/BH1750/Examples/BH1750_LCD/BH1750_LCD.ino
Executable file
60
arduino/lib/BH1750/Examples/BH1750_LCD/BH1750_LCD.ino
Executable file
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
This is a simple code to test BH1750FVI Light senosr
|
||||
communicate using I2C Protocol
|
||||
this library enable 2 slave device address
|
||||
Main address 0x23
|
||||
secondary address 0x5C
|
||||
connect this sensor as following :
|
||||
VCC >>> 3.3V
|
||||
SDA >>> A4
|
||||
SCL >>> A5
|
||||
addr >> A3
|
||||
Gnd >>>Gnd
|
||||
|
||||
Written By : Mohannad Rawashdeh
|
||||
|
||||
*/
|
||||
|
||||
// First define the library :
|
||||
#include <BH1750FVI.h> // Sensor Library
|
||||
#include <Wire.h> // I2C Library
|
||||
#include <LiquidCrystal.h>
|
||||
|
||||
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
|
||||
uint16_t Light_Intensity=0;
|
||||
// Call the function
|
||||
|
||||
BH1750FVI LightSensor;
|
||||
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(9600);
|
||||
lcd.begin(16, 2);
|
||||
|
||||
// call begin Function so turn the sensor On .
|
||||
LightSensor.begin();
|
||||
LightSensor.SetAddress(Device_Address_H); //Address 0x5C
|
||||
LightSensor.SetMode(Continuous_H_resolution_Mode);
|
||||
lcd.setCursor(0, 0);
|
||||
lcd.print("BH1750 Sensor");
|
||||
lcd.setCursor(1, 1);
|
||||
lcd.print("Please wait...");
|
||||
delay(3000);
|
||||
lcd.clear();
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
lcd.clear();
|
||||
lcd.setCursor(0, 0);
|
||||
lcd.print(" Intensity = ");
|
||||
lcd.setCursor(5, 1);
|
||||
Light_Intensity = LightSensor.GetLightIntensity();
|
||||
lcd.print(Light_Intensity);
|
||||
lcd.print(" Lux");
|
||||
delay(2000);
|
||||
|
||||
|
||||
}
|
||||
74
arduino/lib/BH1750/Examples/BH1750_Led/BH1750_Led.ino
Executable file
74
arduino/lib/BH1750/Examples/BH1750_Led/BH1750_Led.ino
Executable file
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
This is a simple code to test BH1750FVI Light senosr
|
||||
communicate using I2C Protocol
|
||||
this library enable 2 slave device address
|
||||
Main address 0x23
|
||||
secondary address 0x5C
|
||||
connect this sensor as following :
|
||||
VCC >>> 3.3V
|
||||
SDA >>> A4
|
||||
SCL >>> A5
|
||||
addr >> A3
|
||||
Gnd >>>Gnd
|
||||
|
||||
Written By : Mohannad Rawashdeh
|
||||
|
||||
*/
|
||||
|
||||
// First define the library :
|
||||
#include <BH1750FVI.h> // Sensor Library
|
||||
#include <Wire.h> // I2C Library
|
||||
|
||||
uint16_t Light_Intensity=0;
|
||||
// Call the function
|
||||
#define LedPin 9 // led connecting to pin D9
|
||||
BH1750FVI LightSensor;
|
||||
|
||||
int SensorValue =0;
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(9600);
|
||||
// call begin Function so turn the sensor On .
|
||||
LightSensor.begin();
|
||||
/*
|
||||
Set the address for this sensor
|
||||
you can use 2 different address
|
||||
Device_Address_H "0x5C"
|
||||
Device_Address_L "0x23"
|
||||
you must connect Addr pin to A3 .
|
||||
*/
|
||||
LightSensor.SetAddress(Device_Address_H); //Address 0x5C
|
||||
// To adjust the slave on other address , uncomment this line
|
||||
// lightMeter.SetAddress(Device_Address_L); //Address 0x5C
|
||||
//-----------------------------------------------
|
||||
/*
|
||||
set the Working Mode for this sensor
|
||||
Select the following Mode:
|
||||
Continuous_H_resolution_Mode
|
||||
Continuous_H_resolution_Mode2
|
||||
Continuous_L_resolution_Mode
|
||||
OneTime_H_resolution_Mode
|
||||
OneTime_H_resolution_Mode2
|
||||
OneTime_L_resolution_Mode
|
||||
|
||||
The data sheet recommanded To use Continuous_H_resolution_Mode
|
||||
*/
|
||||
LightSensor.SetMode(Continuous_H_resolution_Mode);
|
||||
pinMode(9,OUTPUT) // Connect LED With 100ohm resistor
|
||||
// to pin D9
|
||||
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
// call GetLightIntensity() Function , so the sensor read
|
||||
//the Intensity Value and send it
|
||||
Light_Intensity=LightSensor.GetLightIntensity();
|
||||
delay(50);
|
||||
|
||||
SensorValue=map(Light_Intensity,0,2000,255,0);
|
||||
SensorValue=constrain(SensorValue,255,0);
|
||||
digitalWrite(LedPin,SensorValue);
|
||||
// ready to another reading .
|
||||
}
|
||||
68
arduino/lib/BH1750/Examples/BH1750_serial/BH1750_serial.ino
Executable file
68
arduino/lib/BH1750/Examples/BH1750_serial/BH1750_serial.ino
Executable file
|
|
@ -0,0 +1,68 @@
|
|||
|
||||
/*
|
||||
This is a simple code to test BH1750FVI Light senosr
|
||||
communicate using I2C Protocol
|
||||
this library enable 2 slave device address
|
||||
Main address 0x23
|
||||
secondary address 0x5C
|
||||
connect this sensor as following :
|
||||
VCC >>> 3.3V
|
||||
SDA >>> A4
|
||||
SCL >>> A5
|
||||
addr >> A3
|
||||
Gnd >>>Gnd
|
||||
|
||||
Written By : Mohannad Rawashdeh
|
||||
|
||||
*/
|
||||
|
||||
// First define the library :
|
||||
|
||||
#include <Wire.h>
|
||||
#include <BH1750FVI.h>
|
||||
|
||||
|
||||
BH1750FVI LightSensor;
|
||||
|
||||
|
||||
void setup() { // put your setup code here, to run once:
|
||||
Serial.begin(9600);
|
||||
LightSensor.begin();
|
||||
/*
|
||||
Set the address for this sensor
|
||||
you can use 2 different address
|
||||
Device_Address_H "0x5C"
|
||||
Device_Address_L "0x23"
|
||||
you must connect Addr pin to A3 .
|
||||
*/
|
||||
LightSensor.SetAddress(Device_Address_H);//Address 0x5C
|
||||
// To adjust the slave on other address , uncomment this line
|
||||
// lightMeter.SetAddress(Device_Address_L); //Address 0x5C
|
||||
//-----------------------------------------------
|
||||
/*
|
||||
set the Working Mode for this sensor
|
||||
Select the following Mode:
|
||||
Continuous_H_resolution_Mode
|
||||
Continuous_H_resolution_Mode2
|
||||
Continuous_L_resolution_Mode
|
||||
OneTime_H_resolution_Mode
|
||||
OneTime_H_resolution_Mode2
|
||||
OneTime_L_resolution_Mode
|
||||
|
||||
The data sheet recommanded To use Continuous_H_resolution_Mode
|
||||
*/
|
||||
|
||||
LightSensor.SetMode(Continuous_H_resolution_Mode);
|
||||
|
||||
Serial.println("Running...");
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
uint16_t lux = LightSensor.GetLightIntensity();// Get Lux value
|
||||
Serial.print("Light: ");
|
||||
Serial.print(lux);
|
||||
Serial.println(" lux");
|
||||
delay(1000);
|
||||
}
|
||||
24
arduino/lib/BH1750/keywords.txt
Executable file
24
arduino/lib/BH1750/keywords.txt
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
#######################################
|
||||
# Syntax Coloring Map For BH1750FVI
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
KEYWORD1
|
||||
BH1750FVI KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
|
||||
KEYWORD2
|
||||
begin KEYWORD2
|
||||
Sleep KEYWORD2SetMode KEYWORD2Reset KEYWORD2GetLightIntensity KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
||||
738
arduino/lib/DallasTemperature/DallasTemperature.cpp
Executable file
738
arduino/lib/DallasTemperature/DallasTemperature.cpp
Executable file
|
|
@ -0,0 +1,738 @@
|
|||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
// Version 3.7.2 modified on Dec 6, 2011 to support Arduino 1.0
|
||||
// See Includes...
|
||||
// Modified by Jordan Hochenbaum
|
||||
|
||||
#include "DallasTemperature.h"
|
||||
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
extern "C" {
|
||||
#include "WConstants.h"
|
||||
}
|
||||
#endif
|
||||
|
||||
DallasTemperature::DallasTemperature(OneWire* _oneWire)
|
||||
#if REQUIRESALARMS
|
||||
: _AlarmHandler(&defaultAlarmHandler)
|
||||
#endif
|
||||
{
|
||||
_wire = _oneWire;
|
||||
devices = 0;
|
||||
parasite = false;
|
||||
bitResolution = 9;
|
||||
waitForConversion = true;
|
||||
checkForConversion = true;
|
||||
}
|
||||
|
||||
// initialise the bus
|
||||
void DallasTemperature::begin(void)
|
||||
{
|
||||
DeviceAddress deviceAddress;
|
||||
|
||||
_wire->reset_search();
|
||||
devices = 0; // Reset the number of devices when we enumerate wire devices
|
||||
|
||||
while (_wire->search(deviceAddress))
|
||||
{
|
||||
if (validAddress(deviceAddress))
|
||||
{
|
||||
if (!parasite && readPowerSupply(deviceAddress)) parasite = true;
|
||||
|
||||
ScratchPad scratchPad;
|
||||
|
||||
readScratchPad(deviceAddress, scratchPad);
|
||||
|
||||
bitResolution = max(bitResolution, getResolution(deviceAddress));
|
||||
|
||||
devices++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// returns the number of devices found on the bus
|
||||
uint8_t DallasTemperature::getDeviceCount(void)
|
||||
{
|
||||
return devices;
|
||||
}
|
||||
|
||||
// returns true if address is valid
|
||||
bool DallasTemperature::validAddress(uint8_t* deviceAddress)
|
||||
{
|
||||
return (_wire->crc8(deviceAddress, 7) == deviceAddress[7]);
|
||||
}
|
||||
|
||||
// finds an address at a given index on the bus
|
||||
// returns true if the device was found
|
||||
bool DallasTemperature::getAddress(uint8_t* deviceAddress, uint8_t index)
|
||||
{
|
||||
uint8_t depth = 0;
|
||||
|
||||
_wire->reset_search();
|
||||
|
||||
while (depth <= index && _wire->search(deviceAddress))
|
||||
{
|
||||
if (depth == index && validAddress(deviceAddress)) return true;
|
||||
depth++;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// attempt to determine if the device at the given address is connected to the bus
|
||||
bool DallasTemperature::isConnected(uint8_t* deviceAddress)
|
||||
{
|
||||
ScratchPad scratchPad;
|
||||
return isConnected(deviceAddress, scratchPad);
|
||||
}
|
||||
|
||||
// attempt to determine if the device at the given address is connected to the bus
|
||||
// also allows for updating the read scratchpad
|
||||
bool DallasTemperature::isConnected(uint8_t* deviceAddress, uint8_t* scratchPad)
|
||||
{
|
||||
readScratchPad(deviceAddress, scratchPad);
|
||||
return (_wire->crc8(scratchPad, 8) == scratchPad[SCRATCHPAD_CRC]);
|
||||
}
|
||||
|
||||
// read device's scratch pad
|
||||
void DallasTemperature::readScratchPad(uint8_t* deviceAddress, uint8_t* scratchPad)
|
||||
{
|
||||
// send the command
|
||||
_wire->reset();
|
||||
_wire->select(deviceAddress);
|
||||
_wire->write(READSCRATCH);
|
||||
|
||||
// TODO => collect all comments & use simple loop
|
||||
// byte 0: temperature LSB
|
||||
// byte 1: temperature MSB
|
||||
// byte 2: high alarm temp
|
||||
// byte 3: low alarm temp
|
||||
// byte 4: DS18S20: store for crc
|
||||
// DS18B20 & DS1822: configuration register
|
||||
// byte 5: internal use & crc
|
||||
// byte 6: DS18S20: COUNT_REMAIN
|
||||
// DS18B20 & DS1822: store for crc
|
||||
// byte 7: DS18S20: COUNT_PER_C
|
||||
// DS18B20 & DS1822: store for crc
|
||||
// byte 8: SCRATCHPAD_CRC
|
||||
//
|
||||
// for(int i=0; i<9; i++)
|
||||
// {
|
||||
// scratchPad[i] = _wire->read();
|
||||
// }
|
||||
|
||||
|
||||
// read the response
|
||||
|
||||
// byte 0: temperature LSB
|
||||
scratchPad[TEMP_LSB] = _wire->read();
|
||||
|
||||
// byte 1: temperature MSB
|
||||
scratchPad[TEMP_MSB] = _wire->read();
|
||||
|
||||
// byte 2: high alarm temp
|
||||
scratchPad[HIGH_ALARM_TEMP] = _wire->read();
|
||||
|
||||
// byte 3: low alarm temp
|
||||
scratchPad[LOW_ALARM_TEMP] = _wire->read();
|
||||
|
||||
// byte 4:
|
||||
// DS18S20: store for crc
|
||||
// DS18B20 & DS1822: configuration register
|
||||
scratchPad[CONFIGURATION] = _wire->read();
|
||||
|
||||
// byte 5:
|
||||
// internal use & crc
|
||||
scratchPad[INTERNAL_BYTE] = _wire->read();
|
||||
|
||||
// byte 6:
|
||||
// DS18S20: COUNT_REMAIN
|
||||
// DS18B20 & DS1822: store for crc
|
||||
scratchPad[COUNT_REMAIN] = _wire->read();
|
||||
|
||||
// byte 7:
|
||||
// DS18S20: COUNT_PER_C
|
||||
// DS18B20 & DS1822: store for crc
|
||||
scratchPad[COUNT_PER_C] = _wire->read();
|
||||
|
||||
// byte 8:
|
||||
// SCTRACHPAD_CRC
|
||||
scratchPad[SCRATCHPAD_CRC] = _wire->read();
|
||||
|
||||
_wire->reset();
|
||||
}
|
||||
|
||||
// writes device's scratch pad
|
||||
void DallasTemperature::writeScratchPad(uint8_t* deviceAddress, const uint8_t* scratchPad)
|
||||
{
|
||||
_wire->reset();
|
||||
_wire->select(deviceAddress);
|
||||
_wire->write(WRITESCRATCH);
|
||||
_wire->write(scratchPad[HIGH_ALARM_TEMP]); // high alarm temp
|
||||
_wire->write(scratchPad[LOW_ALARM_TEMP]); // low alarm temp
|
||||
// DS18S20 does not use the configuration register
|
||||
if (deviceAddress[0] != DS18S20MODEL) _wire->write(scratchPad[CONFIGURATION]); // configuration
|
||||
_wire->reset();
|
||||
// save the newly written values to eeprom
|
||||
_wire->write(COPYSCRATCH, parasite);
|
||||
if (parasite) delay(10); // 10ms delay
|
||||
_wire->reset();
|
||||
}
|
||||
|
||||
// reads the device's power requirements
|
||||
bool DallasTemperature::readPowerSupply(uint8_t* deviceAddress)
|
||||
{
|
||||
bool ret = false;
|
||||
_wire->reset();
|
||||
_wire->select(deviceAddress);
|
||||
_wire->write(READPOWERSUPPLY);
|
||||
if (_wire->read_bit() == 0) ret = true;
|
||||
_wire->reset();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
// set resolution of all devices to 9, 10, 11, or 12 bits
|
||||
// if new resolution is out of range, it is constrained.
|
||||
void DallasTemperature::setResolution(uint8_t newResolution)
|
||||
{
|
||||
bitResolution = constrain(newResolution, 9, 12);
|
||||
DeviceAddress deviceAddress;
|
||||
for (int i=0; i<devices; i++)
|
||||
{
|
||||
getAddress(deviceAddress, i);
|
||||
setResolution(deviceAddress, bitResolution);
|
||||
}
|
||||
}
|
||||
|
||||
// set resolution of a device to 9, 10, 11, or 12 bits
|
||||
// if new resolution is out of range, 9 bits is used.
|
||||
bool DallasTemperature::setResolution(uint8_t* deviceAddress, uint8_t newResolution)
|
||||
{
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad))
|
||||
{
|
||||
// DS18S20 has a fixed 9-bit resolution
|
||||
if (deviceAddress[0] != DS18S20MODEL)
|
||||
{
|
||||
switch (newResolution)
|
||||
{
|
||||
case 12:
|
||||
scratchPad[CONFIGURATION] = TEMP_12_BIT;
|
||||
break;
|
||||
case 11:
|
||||
scratchPad[CONFIGURATION] = TEMP_11_BIT;
|
||||
break;
|
||||
case 10:
|
||||
scratchPad[CONFIGURATION] = TEMP_10_BIT;
|
||||
break;
|
||||
case 9:
|
||||
default:
|
||||
scratchPad[CONFIGURATION] = TEMP_9_BIT;
|
||||
break;
|
||||
}
|
||||
writeScratchPad(deviceAddress, scratchPad);
|
||||
}
|
||||
return true; // new value set
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// returns the global resolution
|
||||
uint8_t DallasTemperature::getResolution()
|
||||
{
|
||||
return bitResolution;
|
||||
}
|
||||
|
||||
// returns the current resolution of the device, 9-12
|
||||
// returns 0 if device not found
|
||||
uint8_t DallasTemperature::getResolution(uint8_t* deviceAddress)
|
||||
{
|
||||
if (deviceAddress[0] == DS18S20MODEL) return 9; // this model has a fixed resolution
|
||||
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad))
|
||||
{
|
||||
switch (scratchPad[CONFIGURATION])
|
||||
{
|
||||
case TEMP_12_BIT:
|
||||
return 12;
|
||||
|
||||
case TEMP_11_BIT:
|
||||
return 11;
|
||||
|
||||
case TEMP_10_BIT:
|
||||
return 10;
|
||||
|
||||
case TEMP_9_BIT:
|
||||
return 9;
|
||||
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// sets the value of the waitForConversion flag
|
||||
// TRUE : function requestTemperature() etc returns when conversion is ready
|
||||
// FALSE: function requestTemperature() etc returns immediately (USE WITH CARE!!)
|
||||
// (1) programmer has to check if the needed delay has passed
|
||||
// (2) but the application can do meaningful things in that time
|
||||
void DallasTemperature::setWaitForConversion(bool flag)
|
||||
{
|
||||
waitForConversion = flag;
|
||||
}
|
||||
|
||||
// gets the value of the waitForConversion flag
|
||||
bool DallasTemperature::getWaitForConversion()
|
||||
{
|
||||
return waitForConversion;
|
||||
}
|
||||
|
||||
|
||||
// sets the value of the checkForConversion flag
|
||||
// TRUE : function requestTemperature() etc will 'listen' to an IC to determine whether a conversion is complete
|
||||
// FALSE: function requestTemperature() etc will wait a set time (worst case scenario) for a conversion to complete
|
||||
void DallasTemperature::setCheckForConversion(bool flag)
|
||||
{
|
||||
checkForConversion = flag;
|
||||
}
|
||||
|
||||
// gets the value of the waitForConversion flag
|
||||
bool DallasTemperature::getCheckForConversion()
|
||||
{
|
||||
return checkForConversion;
|
||||
}
|
||||
|
||||
bool DallasTemperature::isConversionAvailable(uint8_t* deviceAddress)
|
||||
{
|
||||
// Check if the clock has been raised indicating the conversion is complete
|
||||
ScratchPad scratchPad;
|
||||
readScratchPad(deviceAddress, scratchPad);
|
||||
return scratchPad[0];
|
||||
}
|
||||
|
||||
|
||||
// sends command for all devices on the bus to perform a temperature conversion
|
||||
void DallasTemperature::requestTemperatures()
|
||||
{
|
||||
_wire->reset();
|
||||
_wire->skip();
|
||||
_wire->write(STARTCONVO, parasite);
|
||||
|
||||
// ASYNC mode?
|
||||
if (!waitForConversion) return;
|
||||
blockTillConversionComplete(&bitResolution, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// sends command for one device to perform a temperature by address
|
||||
// returns FALSE if device is disconnected
|
||||
// returns TRUE otherwise
|
||||
bool DallasTemperature::requestTemperaturesByAddress(uint8_t* deviceAddress)
|
||||
{
|
||||
|
||||
_wire->reset();
|
||||
_wire->select(deviceAddress);
|
||||
_wire->write(STARTCONVO, parasite);
|
||||
|
||||
// check device
|
||||
ScratchPad scratchPad;
|
||||
if (!isConnected(deviceAddress, scratchPad)) return false;
|
||||
|
||||
|
||||
// ASYNC mode?
|
||||
if (!waitForConversion) return true;
|
||||
uint8_t bitResolution = getResolution(deviceAddress);
|
||||
blockTillConversionComplete(&bitResolution, deviceAddress);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void DallasTemperature::blockTillConversionComplete(uint8_t* bitResolution, uint8_t* deviceAddress)
|
||||
{
|
||||
if(deviceAddress != 0 && checkForConversion && !parasite)
|
||||
{
|
||||
// Continue to check if the IC has responded with a temperature
|
||||
// NB: Could cause issues with multiple devices (one device may respond faster)
|
||||
unsigned long start = millis();
|
||||
while(!isConversionAvailable(0) && ((millis() - start) < 750));
|
||||
}
|
||||
|
||||
// Wait a fix number of cycles till conversion is complete (based on IC datasheet)
|
||||
switch (*bitResolution)
|
||||
{
|
||||
case 9:
|
||||
delay(94);
|
||||
break;
|
||||
case 10:
|
||||
delay(188);
|
||||
break;
|
||||
case 11:
|
||||
delay(375);
|
||||
break;
|
||||
case 12:
|
||||
default:
|
||||
delay(750);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// sends command for one device to perform a temp conversion by index
|
||||
bool DallasTemperature::requestTemperaturesByIndex(uint8_t deviceIndex)
|
||||
{
|
||||
DeviceAddress deviceAddress;
|
||||
getAddress(deviceAddress, deviceIndex);
|
||||
return requestTemperaturesByAddress(deviceAddress);
|
||||
}
|
||||
|
||||
// Fetch temperature for device index
|
||||
float DallasTemperature::getTempCByIndex(uint8_t deviceIndex)
|
||||
{
|
||||
DeviceAddress deviceAddress;
|
||||
getAddress(deviceAddress, deviceIndex);
|
||||
return getTempC((uint8_t*)deviceAddress);
|
||||
}
|
||||
|
||||
// Fetch temperature for device index
|
||||
float DallasTemperature::getTempFByIndex(uint8_t deviceIndex)
|
||||
{
|
||||
return toFahrenheit(getTempCByIndex(deviceIndex));
|
||||
}
|
||||
|
||||
// reads scratchpad and returns the temperature in degrees C
|
||||
float DallasTemperature::calculateTemperature(uint8_t* deviceAddress, uint8_t* scratchPad)
|
||||
{
|
||||
int16_t rawTemperature = (((int16_t)scratchPad[TEMP_MSB]) << 8) | scratchPad[TEMP_LSB];
|
||||
|
||||
switch (deviceAddress[0])
|
||||
{
|
||||
case DS18B20MODEL:
|
||||
case DS1822MODEL:
|
||||
switch (scratchPad[CONFIGURATION])
|
||||
{
|
||||
case TEMP_12_BIT:
|
||||
return (float)rawTemperature * 0.0625;
|
||||
break;
|
||||
case TEMP_11_BIT:
|
||||
return (float)(rawTemperature >> 1) * 0.125;
|
||||
break;
|
||||
case TEMP_10_BIT:
|
||||
return (float)(rawTemperature >> 2) * 0.25;
|
||||
break;
|
||||
case TEMP_9_BIT:
|
||||
return (float)(rawTemperature >> 3) * 0.5;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case DS18S20MODEL:
|
||||
/*
|
||||
|
||||
Resolutions greater than 9 bits can be calculated using the data from
|
||||
the temperature, COUNT REMAIN and COUNT PER <EFBFBD>C registers in the
|
||||
scratchpad. Note that the COUNT PER <EFBFBD>C register is hard-wired to 16
|
||||
(10h). After reading the scratchpad, the TEMP_READ value is obtained
|
||||
by truncating the 0.5<EFBFBD>C bit (bit 0) from the temperature data. The
|
||||
extended resolution temperature can then be calculated using the
|
||||
following equation:
|
||||
|
||||
COUNT_PER_C - COUNT_REMAIN
|
||||
TEMPERATURE = TEMP_READ - 0.25 + --------------------------
|
||||
COUNT_PER_C
|
||||
*/
|
||||
|
||||
// Good spot. Thanks Nic Johns for your contribution
|
||||
return (float)(rawTemperature >> 1) - 0.25 +((float)(scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) / (float)scratchPad[COUNT_PER_C] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// returns temperature in degrees C or DEVICE_DISCONNECTED if the
|
||||
// device's scratch pad cannot be read successfully.
|
||||
// the numeric value of DEVICE_DISCONNECTED is defined in
|
||||
// DallasTemperature.h. It is a large negative number outside the
|
||||
// operating range of the device
|
||||
float DallasTemperature::getTempC(uint8_t* deviceAddress)
|
||||
{
|
||||
// TODO: Multiple devices (up to 64) on the same bus may take
|
||||
// some time to negotiate a response
|
||||
// What happens in case of collision?
|
||||
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad)) return calculateTemperature(deviceAddress, scratchPad);
|
||||
return DEVICE_DISCONNECTED;
|
||||
}
|
||||
|
||||
// returns temperature in degrees F
|
||||
// TODO: - when getTempC returns DEVICE_DISCONNECTED
|
||||
// -127 gets converted to -196.6 F
|
||||
float DallasTemperature::getTempF(uint8_t* deviceAddress)
|
||||
{
|
||||
return toFahrenheit(getTempC(deviceAddress));
|
||||
}
|
||||
|
||||
// returns true if the bus requires parasite power
|
||||
bool DallasTemperature::isParasitePowerMode(void)
|
||||
{
|
||||
return parasite;
|
||||
}
|
||||
|
||||
#if REQUIRESALARMS
|
||||
|
||||
/*
|
||||
|
||||
ALARMS:
|
||||
|
||||
TH and TL Register Format
|
||||
|
||||
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
|
||||
S 2^6 2^5 2^4 2^3 2^2 2^1 2^0
|
||||
|
||||
Only bits 11 through 4 of the temperature register are used
|
||||
in the TH and TL comparison since TH and TL are 8-bit
|
||||
registers. If the measured temperature is lower than or equal
|
||||
to TL or higher than or equal to TH, an alarm condition exists
|
||||
and an alarm flag is set inside the DS18B20. This flag is
|
||||
updated after every temperature measurement; therefore, if the
|
||||
alarm condition goes away, the flag will be turned off after
|
||||
the next temperature conversion.
|
||||
|
||||
*/
|
||||
|
||||
// sets the high alarm temperature for a device in degrees celsius
|
||||
// accepts a float, but the alarm resolution will ignore anything
|
||||
// after a decimal point. valid range is -55C - 125C
|
||||
void DallasTemperature::setHighAlarmTemp(uint8_t* deviceAddress, char celsius)
|
||||
{
|
||||
// make sure the alarm temperature is within the device's range
|
||||
if (celsius > 125) celsius = 125;
|
||||
else if (celsius < -55) celsius = -55;
|
||||
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad))
|
||||
{
|
||||
scratchPad[HIGH_ALARM_TEMP] = (uint8_t)celsius;
|
||||
writeScratchPad(deviceAddress, scratchPad);
|
||||
}
|
||||
}
|
||||
|
||||
// sets the low alarm temperature for a device in degreed celsius
|
||||
// accepts a float, but the alarm resolution will ignore anything
|
||||
// after a decimal point. valid range is -55C - 125C
|
||||
void DallasTemperature::setLowAlarmTemp(uint8_t* deviceAddress, char celsius)
|
||||
{
|
||||
// make sure the alarm temperature is within the device's range
|
||||
if (celsius > 125) celsius = 125;
|
||||
else if (celsius < -55) celsius = -55;
|
||||
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad))
|
||||
{
|
||||
scratchPad[LOW_ALARM_TEMP] = (uint8_t)celsius;
|
||||
writeScratchPad(deviceAddress, scratchPad);
|
||||
}
|
||||
}
|
||||
|
||||
// returns a char with the current high alarm temperature or
|
||||
// DEVICE_DISCONNECTED for an address
|
||||
char DallasTemperature::getHighAlarmTemp(uint8_t* deviceAddress)
|
||||
{
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad)) return (char)scratchPad[HIGH_ALARM_TEMP];
|
||||
return DEVICE_DISCONNECTED;
|
||||
}
|
||||
|
||||
// returns a char with the current low alarm temperature or
|
||||
// DEVICE_DISCONNECTED for an address
|
||||
char DallasTemperature::getLowAlarmTemp(uint8_t* deviceAddress)
|
||||
{
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad)) return (char)scratchPad[LOW_ALARM_TEMP];
|
||||
return DEVICE_DISCONNECTED;
|
||||
}
|
||||
|
||||
// resets internal variables used for the alarm search
|
||||
void DallasTemperature::resetAlarmSearch()
|
||||
{
|
||||
alarmSearchJunction = -1;
|
||||
alarmSearchExhausted = 0;
|
||||
for(uint8_t i = 0; i < 7; i++)
|
||||
alarmSearchAddress[i] = 0;
|
||||
}
|
||||
|
||||
// This is a modified version of the OneWire::search method.
|
||||
//
|
||||
// Also added the OneWire search fix documented here:
|
||||
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295
|
||||
//
|
||||
// Perform an alarm search. If this function returns a '1' then it has
|
||||
// enumerated the next device and you may retrieve the ROM from the
|
||||
// OneWire::address variable. If there are no devices, no further
|
||||
// devices, or something horrible happens in the middle of the
|
||||
// enumeration then a 0 is returned. If a new device is found then
|
||||
// its address is copied to newAddr. Use
|
||||
// DallasTemperature::resetAlarmSearch() to start over.
|
||||
bool DallasTemperature::alarmSearch(uint8_t* newAddr)
|
||||
{
|
||||
uint8_t i;
|
||||
char lastJunction = -1;
|
||||
uint8_t done = 1;
|
||||
|
||||
if (alarmSearchExhausted) return false;
|
||||
if (!_wire->reset()) return false;
|
||||
|
||||
// send the alarm search command
|
||||
_wire->write(0xEC, 0);
|
||||
|
||||
for(i = 0; i < 64; i++)
|
||||
{
|
||||
uint8_t a = _wire->read_bit( );
|
||||
uint8_t nota = _wire->read_bit( );
|
||||
uint8_t ibyte = i / 8;
|
||||
uint8_t ibit = 1 << (i & 7);
|
||||
|
||||
// I don't think this should happen, this means nothing responded, but maybe if
|
||||
// something vanishes during the search it will come up.
|
||||
if (a && nota) return false;
|
||||
|
||||
if (!a && !nota)
|
||||
{
|
||||
if (i == alarmSearchJunction)
|
||||
{
|
||||
// this is our time to decide differently, we went zero last time, go one.
|
||||
a = 1;
|
||||
alarmSearchJunction = lastJunction;
|
||||
}
|
||||
else if (i < alarmSearchJunction)
|
||||
{
|
||||
// take whatever we took last time, look in address
|
||||
if (alarmSearchAddress[ibyte] & ibit) a = 1;
|
||||
else
|
||||
{
|
||||
// Only 0s count as pending junctions, we've already exhasuted the 0 side of 1s
|
||||
a = 0;
|
||||
done = 0;
|
||||
lastJunction = i;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// we are blazing new tree, take the 0
|
||||
a = 0;
|
||||
alarmSearchJunction = i;
|
||||
done = 0;
|
||||
}
|
||||
// OneWire search fix
|
||||
// See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295
|
||||
}
|
||||
|
||||
if (a) alarmSearchAddress[ibyte] |= ibit;
|
||||
else alarmSearchAddress[ibyte] &= ~ibit;
|
||||
|
||||
_wire->write_bit(a);
|
||||
}
|
||||
|
||||
if (done) alarmSearchExhausted = 1;
|
||||
for (i = 0; i < 8; i++) newAddr[i] = alarmSearchAddress[i];
|
||||
return true;
|
||||
}
|
||||
|
||||
// returns true if device address has an alarm condition
|
||||
// TODO: can this be done with only TEMP_MSB REGISTER (faster)
|
||||
// if ((char) scratchPad[TEMP_MSB] <= (char) scratchPad[LOW_ALARM_TEMP]) return true;
|
||||
// if ((char) scratchPad[TEMP_MSB] >= (char) scratchPad[HIGH_ALARM_TEMP]) return true;
|
||||
bool DallasTemperature::hasAlarm(uint8_t* deviceAddress)
|
||||
{
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad))
|
||||
{
|
||||
float temp = calculateTemperature(deviceAddress, scratchPad);
|
||||
|
||||
// check low alarm
|
||||
if ((char)temp <= (char)scratchPad[LOW_ALARM_TEMP]) return true;
|
||||
|
||||
// check high alarm
|
||||
if ((char)temp >= (char)scratchPad[HIGH_ALARM_TEMP]) return true;
|
||||
}
|
||||
|
||||
// no alarm
|
||||
return false;
|
||||
}
|
||||
|
||||
// returns true if any device is reporting an alarm condition on the bus
|
||||
bool DallasTemperature::hasAlarm(void)
|
||||
{
|
||||
DeviceAddress deviceAddress;
|
||||
resetAlarmSearch();
|
||||
return alarmSearch(deviceAddress);
|
||||
}
|
||||
|
||||
// runs the alarm handler for all devices returned by alarmSearch()
|
||||
void DallasTemperature::processAlarms(void)
|
||||
{
|
||||
resetAlarmSearch();
|
||||
DeviceAddress alarmAddr;
|
||||
|
||||
while (alarmSearch(alarmAddr))
|
||||
{
|
||||
if (validAddress(alarmAddr))
|
||||
_AlarmHandler(alarmAddr);
|
||||
}
|
||||
}
|
||||
|
||||
// sets the alarm handler
|
||||
void DallasTemperature::setAlarmHandler(AlarmHandler *handler)
|
||||
{
|
||||
_AlarmHandler = handler;
|
||||
}
|
||||
|
||||
// The default alarm handler
|
||||
void DallasTemperature::defaultAlarmHandler(uint8_t* deviceAddress)
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// Convert float celsius to fahrenheit
|
||||
float DallasTemperature::toFahrenheit(float celsius)
|
||||
{
|
||||
return (celsius * 1.8) + 32;
|
||||
}
|
||||
|
||||
// Convert float fahrenheit to celsius
|
||||
float DallasTemperature::toCelsius(float fahrenheit)
|
||||
{
|
||||
return (fahrenheit - 32) / 1.8;
|
||||
}
|
||||
|
||||
#if REQUIRESNEW
|
||||
|
||||
// MnetCS - Allocates memory for DallasTemperature. Allows us to instance a new object
|
||||
void* DallasTemperature::operator new(unsigned int size) // Implicit NSS obj size
|
||||
{
|
||||
void * p; // void pointer
|
||||
p = malloc(size); // Allocate memory
|
||||
memset((DallasTemperature*)p,0,size); // Initalise memory
|
||||
|
||||
//!!! CANT EXPLICITLY CALL CONSTRUCTOR - workaround by using an init() methodR - workaround by using an init() method
|
||||
return (DallasTemperature*) p; // Cast blank region to NSS pointer
|
||||
}
|
||||
|
||||
// MnetCS 2009 - Unallocates the memory used by this instance
|
||||
void DallasTemperature::operator delete(void* p)
|
||||
{
|
||||
DallasTemperature* pNss = (DallasTemperature*) p; // Cast to NSS pointer
|
||||
pNss->~DallasTemperature(); // Destruct the object
|
||||
|
||||
free(p); // Free the memory
|
||||
}
|
||||
|
||||
#endif
|
||||
242
arduino/lib/DallasTemperature/DallasTemperature.h
Executable file
242
arduino/lib/DallasTemperature/DallasTemperature.h
Executable file
|
|
@ -0,0 +1,242 @@
|
|||
#ifndef DallasTemperature_h
|
||||
#define DallasTemperature_h
|
||||
|
||||
#define DALLASTEMPLIBVERSION "3.7.2"
|
||||
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
// set to true to include code for new and delete operators
|
||||
#ifndef REQUIRESNEW
|
||||
#define REQUIRESNEW false
|
||||
#endif
|
||||
|
||||
// set to true to include code implementing alarm search functions
|
||||
#ifndef REQUIRESALARMS
|
||||
#define REQUIRESALARMS true
|
||||
#endif
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <OneWire.h>
|
||||
|
||||
// Model IDs
|
||||
#define DS18S20MODEL 0x10
|
||||
#define DS18B20MODEL 0x28
|
||||
#define DS1822MODEL 0x22
|
||||
|
||||
// OneWire commands
|
||||
#define STARTCONVO 0x44 // Tells device to take a temperature reading and put it on the scratchpad
|
||||
#define COPYSCRATCH 0x48 // Copy EEPROM
|
||||
#define READSCRATCH 0xBE // Read EEPROM
|
||||
#define WRITESCRATCH 0x4E // Write to EEPROM
|
||||
#define RECALLSCRATCH 0xB8 // Reload from last known
|
||||
#define READPOWERSUPPLY 0xB4 // Determine if device needs parasite power
|
||||
#define ALARMSEARCH 0xEC // Query bus for devices with an alarm condition
|
||||
|
||||
// Scratchpad locations
|
||||
#define TEMP_LSB 0
|
||||
#define TEMP_MSB 1
|
||||
#define HIGH_ALARM_TEMP 2
|
||||
#define LOW_ALARM_TEMP 3
|
||||
#define CONFIGURATION 4
|
||||
#define INTERNAL_BYTE 5
|
||||
#define COUNT_REMAIN 6
|
||||
#define COUNT_PER_C 7
|
||||
#define SCRATCHPAD_CRC 8
|
||||
|
||||
// Device resolution
|
||||
#define TEMP_9_BIT 0x1F // 9 bit
|
||||
#define TEMP_10_BIT 0x3F // 10 bit
|
||||
#define TEMP_11_BIT 0x5F // 11 bit
|
||||
#define TEMP_12_BIT 0x7F // 12 bit
|
||||
|
||||
// Error Codes
|
||||
#define DEVICE_DISCONNECTED -127
|
||||
|
||||
typedef uint8_t DeviceAddress[8];
|
||||
|
||||
class DallasTemperature
|
||||
{
|
||||
public:
|
||||
|
||||
DallasTemperature(OneWire*);
|
||||
|
||||
// initalise bus
|
||||
void begin(void);
|
||||
|
||||
// returns the number of devices found on the bus
|
||||
uint8_t getDeviceCount(void);
|
||||
|
||||
// Is a conversion complete on the wire?
|
||||
bool isConversionComplete(void);
|
||||
|
||||
// returns true if address is valid
|
||||
bool validAddress(uint8_t*);
|
||||
|
||||
// finds an address at a given index on the bus
|
||||
bool getAddress(uint8_t*, const uint8_t);
|
||||
|
||||
// attempt to determine if the device at the given address is connected to the bus
|
||||
bool isConnected(uint8_t*);
|
||||
|
||||
// attempt to determine if the device at the given address is connected to the bus
|
||||
// also allows for updating the read scratchpad
|
||||
bool isConnected(uint8_t*, uint8_t*);
|
||||
|
||||
// read device's scratchpad
|
||||
void readScratchPad(uint8_t*, uint8_t*);
|
||||
|
||||
// write device's scratchpad
|
||||
void writeScratchPad(uint8_t*, const uint8_t*);
|
||||
|
||||
// read device's power requirements
|
||||
bool readPowerSupply(uint8_t*);
|
||||
|
||||
// get global resolution
|
||||
uint8_t getResolution();
|
||||
|
||||
// set global resolution to 9, 10, 11, or 12 bits
|
||||
void setResolution(uint8_t);
|
||||
|
||||
// returns the device resolution, 9-12
|
||||
uint8_t getResolution(uint8_t*);
|
||||
|
||||
// set resolution of a device to 9, 10, 11, or 12 bits
|
||||
bool setResolution(uint8_t*, uint8_t);
|
||||
|
||||
// sets/gets the waitForConversion flag
|
||||
void setWaitForConversion(bool);
|
||||
bool getWaitForConversion(void);
|
||||
|
||||
// sets/gets the checkForConversion flag
|
||||
void setCheckForConversion(bool);
|
||||
bool getCheckForConversion(void);
|
||||
|
||||
// sends command for all devices on the bus to perform a temperature conversion
|
||||
void requestTemperatures(void);
|
||||
|
||||
// sends command for one device to perform a temperature conversion by address
|
||||
bool requestTemperaturesByAddress(uint8_t*);
|
||||
|
||||
// sends command for one device to perform a temperature conversion by index
|
||||
bool requestTemperaturesByIndex(uint8_t);
|
||||
|
||||
// returns temperature in degrees C
|
||||
float getTempC(uint8_t*);
|
||||
|
||||
// returns temperature in degrees F
|
||||
float getTempF(uint8_t*);
|
||||
|
||||
// Get temperature for device index (slow)
|
||||
float getTempCByIndex(uint8_t);
|
||||
|
||||
// Get temperature for device index (slow)
|
||||
float getTempFByIndex(uint8_t);
|
||||
|
||||
// returns true if the bus requires parasite power
|
||||
bool isParasitePowerMode(void);
|
||||
|
||||
bool isConversionAvailable(uint8_t*);
|
||||
|
||||
#if REQUIRESALARMS
|
||||
|
||||
typedef void AlarmHandler(uint8_t*);
|
||||
|
||||
// sets the high alarm temperature for a device
|
||||
// accepts a char. valid range is -55C - 125C
|
||||
void setHighAlarmTemp(uint8_t*, const char);
|
||||
|
||||
// sets the low alarm temperature for a device
|
||||
// accepts a char. valid range is -55C - 125C
|
||||
void setLowAlarmTemp(uint8_t*, const char);
|
||||
|
||||
// returns a signed char with the current high alarm temperature for a device
|
||||
// in the range -55C - 125C
|
||||
char getHighAlarmTemp(uint8_t*);
|
||||
|
||||
// returns a signed char with the current low alarm temperature for a device
|
||||
// in the range -55C - 125C
|
||||
char getLowAlarmTemp(uint8_t*);
|
||||
|
||||
// resets internal variables used for the alarm search
|
||||
void resetAlarmSearch(void);
|
||||
|
||||
// search the wire for devices with active alarms
|
||||
bool alarmSearch(uint8_t*);
|
||||
|
||||
// returns true if ia specific device has an alarm
|
||||
bool hasAlarm(uint8_t*);
|
||||
|
||||
// returns true if any device is reporting an alarm on the bus
|
||||
bool hasAlarm(void);
|
||||
|
||||
// runs the alarm handler for all devices returned by alarmSearch()
|
||||
void processAlarms(void);
|
||||
|
||||
// sets the alarm handler
|
||||
void setAlarmHandler(AlarmHandler *);
|
||||
|
||||
// The default alarm handler
|
||||
static void defaultAlarmHandler(uint8_t*);
|
||||
|
||||
#endif
|
||||
|
||||
// convert from celcius to farenheit
|
||||
static float toFahrenheit(const float);
|
||||
|
||||
// convert from farenheit to celsius
|
||||
static float toCelsius(const float);
|
||||
|
||||
#if REQUIRESNEW
|
||||
|
||||
// initalize memory area
|
||||
void* operator new (unsigned int);
|
||||
|
||||
// delete memory reference
|
||||
void operator delete(void*);
|
||||
|
||||
#endif
|
||||
|
||||
private:
|
||||
typedef uint8_t ScratchPad[9];
|
||||
|
||||
// parasite power on or off
|
||||
bool parasite;
|
||||
|
||||
// used to determine the delay amount needed to allow for the
|
||||
// temperature conversion to take place
|
||||
uint8_t bitResolution;
|
||||
|
||||
// used to requestTemperature with or without delay
|
||||
bool waitForConversion;
|
||||
|
||||
// used to requestTemperature to dynamically check if a conversion is complete
|
||||
bool checkForConversion;
|
||||
|
||||
// count of devices on the bus
|
||||
uint8_t devices;
|
||||
|
||||
// Take a pointer to one wire instance
|
||||
OneWire* _wire;
|
||||
|
||||
// reads scratchpad and returns the temperature in degrees C
|
||||
float calculateTemperature(uint8_t*, uint8_t*);
|
||||
|
||||
void blockTillConversionComplete(uint8_t*,uint8_t*);
|
||||
|
||||
#if REQUIRESALARMS
|
||||
|
||||
// required for alarmSearch
|
||||
uint8_t alarmSearchAddress[8];
|
||||
char alarmSearchJunction;
|
||||
uint8_t alarmSearchExhausted;
|
||||
|
||||
// the alarm handler function pointer
|
||||
AlarmHandler *_AlarmHandler;
|
||||
|
||||
#endif
|
||||
|
||||
};
|
||||
#endif
|
||||
152
arduino/lib/DallasTemperature/Multiple/Multiple.ino
Executable file
152
arduino/lib/DallasTemperature/Multiple/Multiple.ino
Executable file
|
|
@ -0,0 +1,152 @@
|
|||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
|
||||
// Data wire is plugged into port 2 on the Arduino
|
||||
#define ONE_WIRE_BUS 2
|
||||
#define TEMPERATURE_PRECISION 9
|
||||
|
||||
// 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);
|
||||
|
||||
// arrays to hold device addresses
|
||||
DeviceAddress insideThermometer, outsideThermometer, middleThermometer;
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
// start serial port
|
||||
Serial.begin(9600);
|
||||
Serial.println("Dallas Temperature IC Control Library Demo");
|
||||
|
||||
// Start up the library
|
||||
sensors.begin();
|
||||
|
||||
// locate devices on the bus
|
||||
Serial.print("Locating devices...");
|
||||
Serial.print("Found ");
|
||||
Serial.print(sensors.getDeviceCount(), DEC);
|
||||
Serial.println(" devices.");
|
||||
|
||||
// report parasite power requirements
|
||||
Serial.print("Parasite power is: ");
|
||||
if (sensors.isParasitePowerMode()) Serial.println("ON");
|
||||
else Serial.println("OFF");
|
||||
|
||||
// assign address manually. the addresses below will beed to be changed
|
||||
// to valid device addresses on your bus. device address can be retrieved
|
||||
// by using either oneWire.search(deviceAddress) or individually via
|
||||
// sensors.getAddress(deviceAddress, index)
|
||||
//insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
|
||||
//outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
|
||||
|
||||
// search for devices on the bus and assign based on an index. ideally,
|
||||
// you would do this to initially discover addresses on the bus and then
|
||||
// use those addresses and manually assign them (see above) once you know
|
||||
// the devices on your bus (and assuming they don't change).
|
||||
//
|
||||
// method 1: by index
|
||||
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
|
||||
if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
|
||||
if (!sensors.getAddress(middleThermometer, 2)) Serial.println("Unable to find address for Device 2");
|
||||
|
||||
// method 2: search()
|
||||
// search() looks for the next device. Returns 1 if a new address has been
|
||||
// returned. A zero might mean that the bus is shorted, there are no devices,
|
||||
// or you have already retrieved all of them. It might be a good idea to
|
||||
// check the CRC to make sure you didn't get garbage. The order is
|
||||
// deterministic. You will always get the same devices in the same order
|
||||
//
|
||||
// Must be called before search()
|
||||
//oneWire.reset_search();
|
||||
// assigns the first address found to insideThermometer
|
||||
//if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
|
||||
// assigns the seconds address found to outsideThermometer
|
||||
//if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer");
|
||||
|
||||
// show the addresses we found on the bus
|
||||
Serial.print("Device 0 Address: ");
|
||||
printAddress(insideThermometer);
|
||||
Serial.println();
|
||||
|
||||
Serial.print("Device 1 Address: ");
|
||||
printAddress(outsideThermometer);
|
||||
Serial.println();
|
||||
|
||||
Serial.print("Device 2 Address: ");
|
||||
printAddress(middleThermometer);
|
||||
Serial.println();
|
||||
|
||||
// set the resolution to 9 bit
|
||||
sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
|
||||
sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);
|
||||
sensors.setResolution(middleThermometer, TEMPERATURE_PRECISION);
|
||||
|
||||
Serial.print("Device 0 Resolution: ");
|
||||
Serial.print(sensors.getResolution(insideThermometer), DEC);
|
||||
Serial.println();
|
||||
|
||||
Serial.print("Device 1 Resolution: ");
|
||||
Serial.print(sensors.getResolution(outsideThermometer), DEC);
|
||||
Serial.println();
|
||||
|
||||
Serial.print("Device 2 Resolution: ");
|
||||
Serial.print(sensors.getResolution(middleThermometer), DEC);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// function to print a device address
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// function to print the temperature for a device
|
||||
void printTemperature(DeviceAddress deviceAddress)
|
||||
{
|
||||
float tempC = sensors.getTempC(deviceAddress);
|
||||
Serial.print("Temp C: ");
|
||||
Serial.print(tempC);
|
||||
Serial.print(" Temp F: ");
|
||||
Serial.print(DallasTemperature::toFahrenheit(tempC));
|
||||
}
|
||||
|
||||
// function to print a device's resolution
|
||||
void printResolution(DeviceAddress deviceAddress)
|
||||
{
|
||||
Serial.print("Resolution: ");
|
||||
Serial.print(sensors.getResolution(deviceAddress));
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// main function to print information about a device
|
||||
void printData(DeviceAddress deviceAddress)
|
||||
{
|
||||
Serial.print("Device Address: ");
|
||||
printAddress(deviceAddress);
|
||||
Serial.print(" ");
|
||||
printTemperature(deviceAddress);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
// call sensors.requestTemperatures() to issue a global temperature
|
||||
// request to all devices on the bus
|
||||
Serial.print("Requesting temperatures...");
|
||||
sensors.requestTemperatures();
|
||||
Serial.println("DONE");
|
||||
|
||||
// print the device information
|
||||
printData(insideThermometer);
|
||||
printData(outsideThermometer);
|
||||
printData(middleThermometer);
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
53
arduino/lib/DallasTemperature/README.TXT
Executable file
53
arduino/lib/DallasTemperature/README.TXT
Executable file
|
|
@ -0,0 +1,53 @@
|
|||
Arduino Library for Dallas Temperature ICs
|
||||
==========================================
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
This library supports the following devices:
|
||||
DS18B20
|
||||
DS18S20 - Please note there appears to be an issue with this series.
|
||||
DS1822
|
||||
|
||||
You will need a pull-up resistor of about 5 KOhm between the 1-Wire data line
|
||||
and your 5V power. If you are using the DS18B20, ground pins 1 and 3. The
|
||||
centre pin is the data line '1-wire'.
|
||||
|
||||
We have included a "REQUIRESNEW" and "REQUIRESALARMS" definition. If you
|
||||
want to slim down the code feel free to use either of these by including
|
||||
#define REQUIRESNEW or #define REQUIRESALARMS a the top of DallasTemperature.h
|
||||
|
||||
Credits
|
||||
-------
|
||||
|
||||
The OneWire code has been derived from
|
||||
http://www.arduino.cc/playground/Learning/OneWire.
|
||||
Miles Burton <miles@mnetcs.com> originally developed this library.
|
||||
Tim Newsome <nuisance@casualhacker.net> added support for multiple sensors on
|
||||
the same bus.
|
||||
Guil Barros [gfbarros@bappos.com] added getTempByAddress (v3.5)
|
||||
Rob Tillaart [rob.tillaart@gmail.com] added async modus (v3.7.0)
|
||||
|
||||
|
||||
Website
|
||||
-------
|
||||
|
||||
You can find the latest version of the library at
|
||||
http://milesburton.com/index.php?title=Dallas_Temperature_Control_Library
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
85
arduino/lib/DallasTemperature/change.txt
Executable file
85
arduino/lib/DallasTemperature/change.txt
Executable file
|
|
@ -0,0 +1,85 @@
|
|||
|
||||
This file contains the change history of the Dallas Temperature Control Library.
|
||||
|
||||
VERSION 3.7.2 BETA
|
||||
===================
|
||||
DATE: 6 DEC 2011
|
||||
|
||||
- Jordan Hochenbaum [jhochenbaum@gmail.com] updated library for compatibility with Arduino 1.0.
|
||||
|
||||
VERSION 3.7.0 BETA
|
||||
===================
|
||||
DATE: 11 JAN 2011
|
||||
|
||||
- Rob Tillaart [rob.tillaart@gmail.com] added async modus (v3.7.0)
|
||||
The library is backwards compatible with version 3.6.0
|
||||
|
||||
MAJOR: async modus
|
||||
------------------
|
||||
- Added - private bool waitForConversion.
|
||||
This boolean is default set to true in the Constructor to keep the library backwards compatible. If this flag is true calls to requestTemperatures(), requestTemperaturesByAddress() et al, will be blocking with the appropiate time specified (in datasheet) for the resolution used. If the flag is set to false, requestTemperatures() et al, will return immediately after the conversion command is send over the 1-wire interface. The programmer is responsible to wait long enough before reading the temperature values. This enables the application to do other things while waiting for a new reading, like calculations, update LCD, read/write other IO lines etc. See examples.
|
||||
|
||||
- Added - void setWaitForConversion(bool);
|
||||
To set the flag to true or false, depending on the modus needed.
|
||||
|
||||
- Added - bool getWaitForConversion(void);
|
||||
To get the current value of the flag.
|
||||
|
||||
- Changed - void requestTemperatures(void);
|
||||
Added a test (false == waitForConversion) to return immediately after the conversion command instead of waiting until the conversion is ready.
|
||||
|
||||
- Changed - bool requestTemperaturesByAddress(uint8_t*);
|
||||
Added a test (false == waitForConversion) to return immediately after the conversion command instead of waiting until the conversion is ready.
|
||||
|
||||
|
||||
MINOR version number
|
||||
--------------------
|
||||
- Added - #define DALLASTEMPLIBVERSION "3.7.0"
|
||||
To indicate the version number in .h file
|
||||
|
||||
|
||||
MINOR internal var bitResolution
|
||||
----------------------------
|
||||
- Changed - private int conversionDelay - is renamed to - private int bitResolution
|
||||
As this variable holds the resolution. The delay for the conversion is derived from it.
|
||||
|
||||
- Changed - uint8_t getResolution(uint8_t* deviceAddress);
|
||||
If the device is not connected, it returns 0, otherwise it returns the resolution of the device.
|
||||
|
||||
- Changed - bool setResolution(uint8_t* deviceAddress, uint8_t newResolution);
|
||||
If the device is not connected, it returns FALSE (fail), otherwise it returns TRUE (succes).
|
||||
|
||||
- Added - uint8_t getResolution();
|
||||
Returns bitResolution.
|
||||
|
||||
- Added - void setResolution(uint8_t newResolution)
|
||||
Sets the internal variable bitResolution, and all devices to this value
|
||||
|
||||
|
||||
MINOR check connected state
|
||||
----------------------------
|
||||
- Changed - bool requestTemperaturesByIndex(deviceIndex)
|
||||
Changed return type from void to bool. The function returns false if the device identified with [deviceIndex] is not found on the bus and true otherwise.
|
||||
|
||||
- Changed - bool requestTemperaturesByAddress(deviceAddress)
|
||||
Changed return type from void to bool. The function returns false if the device identified with [deviceAddress] is not found on the bus and true otherwise.
|
||||
Added code to handle the DS18S20 which has a 9 bit resolution separately.
|
||||
Changed code so the blocking delay matches the bitResolution set in the device with deviceAddress.
|
||||
|
||||
- Changed - bool requestTemperaturesByIndex(uint8_t deviceIndex)
|
||||
Changed return type from void to bool. The function returns false if the device identified with [deviceIndex] is not found on the bus and true otherwise.
|
||||
|
||||
|
||||
|
||||
VERSION 3.6.0
|
||||
==============
|
||||
DATE: 2010-10-10
|
||||
|
||||
- no detailed change history known except:
|
||||
|
||||
- The OneWire code has been derived from
|
||||
http://www.arduino.cc/playground/Learning/OneWire.
|
||||
- Miles Burton <miles@mnetcs.com> originally developed this library.
|
||||
- Tim Newsome <nuisance@casualhacker.net> added support for multiple sensors on
|
||||
the same bus.
|
||||
- Guil Barros [gfbarros@bappos.com] added getTempByAddress (v3.5)
|
||||
140
arduino/lib/DallasTemperature/examples/Multiple/Multiple.ino
Executable file
140
arduino/lib/DallasTemperature/examples/Multiple/Multiple.ino
Executable file
|
|
@ -0,0 +1,140 @@
|
|||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
|
||||
// Data wire is plugged into port 2 on the Arduino
|
||||
#define ONE_WIRE_BUS 2
|
||||
#define TEMPERATURE_PRECISION 12
|
||||
|
||||
// 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);
|
||||
|
||||
// arrays to hold device addresses
|
||||
DeviceAddress insideThermometer, outsideThermometer;
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
// start serial port
|
||||
Serial.begin(9600);
|
||||
Serial.println("Dallas Temperature IC Control Library Demo");
|
||||
|
||||
// Start up the library
|
||||
sensors.begin();
|
||||
|
||||
// locate devices on the bus
|
||||
Serial.print("Locating devices...");
|
||||
Serial.print("Found ");
|
||||
Serial.print(sensors.getDeviceCount(), DEC);
|
||||
Serial.println(" devices.");
|
||||
|
||||
// report parasite power requirements
|
||||
Serial.print("Parasite power is: ");
|
||||
if (sensors.isParasitePowerMode()) Serial.println("ON");
|
||||
else Serial.println("OFF");
|
||||
|
||||
// assign address manually. the addresses below will beed to be changed
|
||||
// to valid device addresses on your bus. device address can be retrieved
|
||||
// by using either oneWire.search(deviceAddress) or individually via
|
||||
// sensors.getAddress(deviceAddress, index)
|
||||
//insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
|
||||
//outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
|
||||
|
||||
// search for devices on the bus and assign based on an index. ideally,
|
||||
// you would do this to initially discover addresses on the bus and then
|
||||
// use those addresses and manually assign them (see above) once you know
|
||||
// the devices on your bus (and assuming they don't change).
|
||||
//
|
||||
// method 1: by index
|
||||
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
|
||||
if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
|
||||
|
||||
// method 2: search()
|
||||
// search() looks for the next device. Returns 1 if a new address has been
|
||||
// returned. A zero might mean that the bus is shorted, there are no devices,
|
||||
// or you have already retrieved all of them. It might be a good idea to
|
||||
// check the CRC to make sure you didn't get garbage. The order is
|
||||
// deterministic. You will always get the same devices in the same order
|
||||
//
|
||||
// Must be called before search()
|
||||
//oneWire.reset_search();
|
||||
// assigns the first address found to insideThermometer
|
||||
//if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
|
||||
// assigns the seconds address found to outsideThermometer
|
||||
//if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer");
|
||||
|
||||
// show the addresses we found on the bus
|
||||
Serial.print("Device 0 Address: ");
|
||||
printAddress(insideThermometer);
|
||||
Serial.println();
|
||||
|
||||
Serial.print("Device 1 Address: ");
|
||||
printAddress(outsideThermometer);
|
||||
Serial.println();
|
||||
|
||||
// set the resolution
|
||||
sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
|
||||
sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);
|
||||
|
||||
Serial.print("Device 0 Resolution: ");
|
||||
Serial.print(sensors.getResolution(insideThermometer), DEC);
|
||||
Serial.println();
|
||||
|
||||
Serial.print("Device 1 Resolution: ");
|
||||
Serial.print(sensors.getResolution(outsideThermometer), DEC);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// function to print a device address
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// function to print the temperature for a device
|
||||
void printTemperature(DeviceAddress deviceAddress)
|
||||
{
|
||||
float tempC = sensors.getTempC(deviceAddress);
|
||||
Serial.print("Temp C: ");
|
||||
Serial.print(tempC);
|
||||
Serial.print(" Temp F: ");
|
||||
Serial.print(DallasTemperature::toFahrenheit(tempC));
|
||||
}
|
||||
|
||||
// function to print a device's resolution
|
||||
void printResolution(DeviceAddress deviceAddress)
|
||||
{
|
||||
Serial.print("Resolution: ");
|
||||
Serial.print(sensors.getResolution(deviceAddress));
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// main function to print information about a device
|
||||
void printData(DeviceAddress deviceAddress)
|
||||
{
|
||||
Serial.print("Device Address: ");
|
||||
printAddress(deviceAddress);
|
||||
Serial.print(" ");
|
||||
printTemperature(deviceAddress);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
// call sensors.requestTemperatures() to issue a global temperature
|
||||
// request to all devices on the bus
|
||||
Serial.print("Requesting temperatures...");
|
||||
sensors.requestTemperatures();
|
||||
Serial.println("DONE");
|
||||
|
||||
// print the device information
|
||||
printData(insideThermometer);
|
||||
printData(outsideThermometer);
|
||||
}
|
||||
|
||||
33
arduino/lib/DallasTemperature/examples/Simple/Simple.ino
Executable file
33
arduino/lib/DallasTemperature/examples/Simple/Simple.ino
Executable file
|
|
@ -0,0 +1,33 @@
|
|||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
|
||||
// Data wire is plugged into port 2 on the Arduino
|
||||
#define ONE_WIRE_BUS 2
|
||||
|
||||
// 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 setup(void)
|
||||
{
|
||||
// start serial port
|
||||
Serial.begin(9600);
|
||||
Serial.println("Dallas Temperature IC Control Library Demo");
|
||||
|
||||
// Start up the library
|
||||
sensors.begin();
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
// call sensors.requestTemperatures() to issue a global temperature
|
||||
// request to all devices on the bus
|
||||
Serial.print("Requesting temperatures...");
|
||||
sensors.requestTemperatures(); // Send the command to get temperatures
|
||||
Serial.println("DONE");
|
||||
|
||||
Serial.print("Temperature for the device 1 (index 0) is: ");
|
||||
Serial.println(sensors.getTempCByIndex(0));
|
||||
}
|
||||
109
arduino/lib/DallasTemperature/examples/Single/Single.ino
Executable file
109
arduino/lib/DallasTemperature/examples/Single/Single.ino
Executable file
|
|
@ -0,0 +1,109 @@
|
|||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
|
||||
// Data wire is plugged into port 2 on the Arduino
|
||||
#define ONE_WIRE_BUS 2
|
||||
|
||||
// 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);
|
||||
|
||||
// arrays to hold device address
|
||||
DeviceAddress insideThermometer;
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
// start serial port
|
||||
Serial.begin(9600);
|
||||
Serial.println("Dallas Temperature IC Control Library Demo");
|
||||
|
||||
// locate devices on the bus
|
||||
Serial.print("Locating devices...");
|
||||
sensors.begin();
|
||||
Serial.print("Found ");
|
||||
Serial.print(sensors.getDeviceCount(), DEC);
|
||||
Serial.println(" devices.");
|
||||
|
||||
// report parasite power requirements
|
||||
Serial.print("Parasite power is: ");
|
||||
if (sensors.isParasitePowerMode()) Serial.println("ON");
|
||||
else Serial.println("OFF");
|
||||
|
||||
// assign address manually. the addresses below will beed to be changed
|
||||
// to valid device addresses on your bus. device address can be retrieved
|
||||
// by using either oneWire.search(deviceAddress) or individually via
|
||||
// sensors.getAddress(deviceAddress, index)
|
||||
//insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
|
||||
|
||||
// Method 1:
|
||||
// search for devices on the bus and assign based on an index. ideally,
|
||||
// you would do this to initially discover addresses on the bus and then
|
||||
// use those addresses and manually assign them (see above) once you know
|
||||
// the devices on your bus (and assuming they don't change).
|
||||
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
|
||||
|
||||
// method 2: search()
|
||||
// search() looks for the next device. Returns 1 if a new address has been
|
||||
// returned. A zero might mean that the bus is shorted, there are no devices,
|
||||
// or you have already retrieved all of them. It might be a good idea to
|
||||
// check the CRC to make sure you didn't get garbage. The order is
|
||||
// deterministic. You will always get the same devices in the same order
|
||||
//
|
||||
// Must be called before search()
|
||||
//oneWire.reset_search();
|
||||
// assigns the first address found to insideThermometer
|
||||
//if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
|
||||
|
||||
// show the addresses we found on the bus
|
||||
Serial.print("Device 0 Address: ");
|
||||
printAddress(insideThermometer);
|
||||
Serial.println();
|
||||
|
||||
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
|
||||
sensors.setResolution(insideThermometer, 9);
|
||||
|
||||
Serial.print("Device 0 Resolution: ");
|
||||
Serial.print(sensors.getResolution(insideThermometer), DEC);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// function to print the temperature for a device
|
||||
void printTemperature(DeviceAddress deviceAddress)
|
||||
{
|
||||
// method 1 - slower
|
||||
//Serial.print("Temp C: ");
|
||||
//Serial.print(sensors.getTempC(deviceAddress));
|
||||
//Serial.print(" Temp F: ");
|
||||
//Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
|
||||
|
||||
// method 2 - faster
|
||||
float tempC = sensors.getTempC(deviceAddress);
|
||||
Serial.print("Temp C: ");
|
||||
Serial.print(tempC);
|
||||
Serial.print(" Temp F: ");
|
||||
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
// call sensors.requestTemperatures() to issue a global temperature
|
||||
// request to all devices on the bus
|
||||
Serial.print("Requesting temperatures...");
|
||||
sensors.requestTemperatures(); // Send the command to get temperatures
|
||||
Serial.println("DONE");
|
||||
|
||||
// It responds almost immediately. Let's print out the data
|
||||
printTemperature(insideThermometer); // Use a simple function to print out the data
|
||||
}
|
||||
|
||||
// function to print a device address
|
||||
void printAddress(DeviceAddress deviceAddress)
|
||||
{
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
if (deviceAddress[i] < 16) Serial.print("0");
|
||||
Serial.print(deviceAddress[i], HEX);
|
||||
}
|
||||
}
|
||||
124
arduino/lib/DallasTemperature/examples/Tester/Tester.ino
Executable file
124
arduino/lib/DallasTemperature/examples/Tester/Tester.ino
Executable file
|
|
@ -0,0 +1,124 @@
|
|||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
|
||||
// Data wire is plugged into port 2 on the Arduino
|
||||
#define ONE_WIRE_BUS 2
|
||||
#define TEMPERATURE_PRECISION 9
|
||||
|
||||
// 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);
|
||||
|
||||
int numberOfDevices; // Number of temperature devices found
|
||||
|
||||
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
// start serial port
|
||||
Serial.begin(9600);
|
||||
Serial.println("Dallas Temperature IC Control Library Demo");
|
||||
|
||||
// Start up the library
|
||||
sensors.begin();
|
||||
|
||||
// Grab a count of devices on the wire
|
||||
numberOfDevices = sensors.getDeviceCount();
|
||||
|
||||
// locate devices on the bus
|
||||
Serial.print("Locating devices...");
|
||||
|
||||
Serial.print("Found ");
|
||||
Serial.print(numberOfDevices, DEC);
|
||||
Serial.println(" devices.");
|
||||
|
||||
// report parasite power requirements
|
||||
Serial.print("Parasite power is: ");
|
||||
if (sensors.isParasitePowerMode()) Serial.println("ON");
|
||||
else Serial.println("OFF");
|
||||
|
||||
// Loop through each device, print out address
|
||||
for(int i=0;i<numberOfDevices; i++)
|
||||
{
|
||||
// Search the wire for address
|
||||
if(sensors.getAddress(tempDeviceAddress, i))
|
||||
{
|
||||
Serial.print("Found device ");
|
||||
Serial.print(i, DEC);
|
||||
Serial.print(" with address: ");
|
||||
printAddress(tempDeviceAddress);
|
||||
Serial.println();
|
||||
|
||||
Serial.print("Setting resolution to ");
|
||||
Serial.println(TEMPERATURE_PRECISION, DEC);
|
||||
|
||||
// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
|
||||
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
|
||||
|
||||
Serial.print("Resolution actually set to: ");
|
||||
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
|
||||
Serial.println();
|
||||
}else{
|
||||
Serial.print("Found ghost device at ");
|
||||
Serial.print(i, DEC);
|
||||
Serial.print(" but could not detect address. Check power and cabling");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// function to print the temperature for a device
|
||||
void printTemperature(DeviceAddress deviceAddress)
|
||||
{
|
||||
// method 1 - slower
|
||||
//Serial.print("Temp C: ");
|
||||
//Serial.print(sensors.getTempC(deviceAddress));
|
||||
//Serial.print(" Temp F: ");
|
||||
//Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
|
||||
|
||||
// method 2 - faster
|
||||
float tempC = sensors.getTempC(deviceAddress);
|
||||
Serial.print("Temp C: ");
|
||||
Serial.print(tempC);
|
||||
Serial.print(" Temp F: ");
|
||||
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
// call sensors.requestTemperatures() to issue a global temperature
|
||||
// request to all devices on the bus
|
||||
Serial.print("Requesting temperatures...");
|
||||
sensors.requestTemperatures(); // Send the command to get temperatures
|
||||
Serial.println("DONE");
|
||||
|
||||
|
||||
// Loop through each device, print out temperature data
|
||||
for(int i=0;i<numberOfDevices; i++)
|
||||
{
|
||||
// Search the wire for address
|
||||
if(sensors.getAddress(tempDeviceAddress, i))
|
||||
{
|
||||
// Output the device ID
|
||||
Serial.print("Temperature for device: ");
|
||||
Serial.println(i,DEC);
|
||||
|
||||
// It responds almost immediately. Let's print out the data
|
||||
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
|
||||
}
|
||||
//else ghost device! Check your power requirements and cabling
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// function to print a device address
|
||||
void printAddress(DeviceAddress deviceAddress)
|
||||
{
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
if (deviceAddress[i] < 16) Serial.print("0");
|
||||
Serial.print(deviceAddress[i], HEX);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
// This sketch looks for 1-wire devices and
|
||||
// prints their addresses (serial number) to
|
||||
// the UART, in a format that is useful in Arduino sketches
|
||||
// Tutorial:
|
||||
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
|
||||
|
||||
#include <OneWire.h>
|
||||
|
||||
OneWire ds(2); // Connect your 1-wire device to pin 2
|
||||
|
||||
void setup(void) {
|
||||
Serial.begin(9600);
|
||||
discoverOneWireDevices();
|
||||
}
|
||||
|
||||
void discoverOneWireDevices(void) {
|
||||
byte i;
|
||||
byte present = 0;
|
||||
byte data[12];
|
||||
byte addr[8];
|
||||
|
||||
Serial.print("Looking for 1-Wire devices...\n\r");
|
||||
while(ds.search(addr)) {
|
||||
Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");
|
||||
for( i = 0; i < 8; i++) {
|
||||
Serial.print("0x");
|
||||
if (addr[i] < 16) {
|
||||
Serial.print('0');
|
||||
}
|
||||
Serial.print(addr[i], HEX);
|
||||
if (i < 7) {
|
||||
Serial.print(", ");
|
||||
}
|
||||
}
|
||||
if ( OneWire::crc8( addr, 7) != addr[7]) {
|
||||
Serial.print("CRC is not valid!\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
Serial.print("\n\r\n\rThat's it.\r\n");
|
||||
ds.reset_search();
|
||||
return;
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
// nothing to see here
|
||||
}
|
||||
|
||||
|
||||
54
arduino/lib/DallasTemperature/keywords.txt
Executable file
54
arduino/lib/DallasTemperature/keywords.txt
Executable file
|
|
@ -0,0 +1,54 @@
|
|||
#######################################
|
||||
# Syntax Coloring Map For Ultrasound
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
DallasTemperature KEYWORD1
|
||||
OneWire KEYWORD1
|
||||
AlarmHandler KEYWORD1
|
||||
DeviceAddress KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
setResolution KEYWORD2
|
||||
getResolution KEYWORD2
|
||||
getTempC KEYWORD2
|
||||
toFahrenheit KEYWORD2
|
||||
getTempF KEYWORD2
|
||||
getTempCByIndex KEYWORD2
|
||||
getTempFByIndex KEYWORD2
|
||||
setWaitForConversion KEYWORD2
|
||||
getWaitForConversion KEYWORD2
|
||||
requestTemperatures KEYWORD2
|
||||
requestTemperaturesByAddress KEYWORD2
|
||||
requestTemperaturesByIndex KEYWORD2
|
||||
isParasitePowerMode KEYWORD2
|
||||
begin KEYWORD2
|
||||
getDeviceCount KEYWORD2
|
||||
getAddress KEYWORD2
|
||||
validAddress KEYWORD2
|
||||
isConnected KEYWORD2
|
||||
readScratchPad KEYWORD2
|
||||
writeScratchPad KEYWORD2
|
||||
readPowerSupply KEYWORD2
|
||||
setHighAlarmTemp KEYWORD2
|
||||
setLowAlarmTemp KEYWORD2
|
||||
getHighAlarmTemp KEYWORD2
|
||||
getLowAlarmTemp KEYWORD2
|
||||
resetAlarmSearch KEYWORD2
|
||||
alarmSearch KEYWORD2
|
||||
hasAlarm KEYWORD2
|
||||
toCelsius KEYWORD2
|
||||
processAlarmss KEYWORD2
|
||||
setAlarmHandlers KEYWORD2
|
||||
defaultAlarmHandler KEYWORD2
|
||||
calculateTemperature KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue