-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensors_avr.cpp
154 lines (110 loc) · 4.17 KB
/
sensors_avr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* trackuino copyright (C) 2010 EA5HAV Javi
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* Credit to:
*
* cathedrow for this idea on using the ADC as a volt meter:
* http://code.google.com/p/tinkerit/wiki/SecretVoltmeter
*/
#ifdef AVR
#include "config.h"
#include "pin.h"
#include "sensors_avr.h"
#if (ARDUINO + 1) >= 100
# include <Arduino.h>
#else
# include <WProgram.h>
#endif
#include <SoftwareSerial.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#ifdef SOFTSERIALDEBUG
extern SoftwareSerial softdebug;
#endif
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire temp_sensor_wire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature temp_sensor(&temp_sensor_wire);
// arrays to hold device address
DeviceAddress insideThermometer = { 0x28, 0x36, 0x91, 0x8E, 0x05, 0x00, 0x00, 0xC2 };
int sensors_vin()
{
analogReference(DEFAULT); // Ref=3.3V
analogRead(VMETER_PIN); // Disregard the 1st conversion after changing ref (p.256)
delay(10); // This is needed when switching references
uint16_t adc = analogRead(VMETER_PIN);
uint16_t mV = 3300L * adc / 1024;
// Vin = mV * R2 / (R1 + R2)
int vin = (uint32_t)mV * (VMETER_R1 + VMETER_R2) / VMETER_R2;
return vin;
}
void getdata(byte *a, byte *b)
{
Wire.requestFrom(sensor,2);//Sends content of first two registers
*a = Wire.read(); //first byte recieved stored here
*b = Wire.read(); //second byte recieved stored here
}
long sensors_pressure(){
byte aa,bb;
float pressure =0;
getdata(&aa,&bb);
float c = aa*256+bb;//combines byte 1 and byte 2
pressure= ((c-1638)*1.0342)/(14745-1638)*100000;// Conversion found from technical notes/ pressure is in bar
const float p0 = 101325; // Pressure at sea level (Pa)
long press_alt = (float)44330.0 * (1.0 - pow(((float) pressure/p0), 0.190295)); //convert pressure value in pressure altitude (m)
#ifdef DEBUG_SENS
softdebug.println();
softdebug.print("Pressure:");
softdebug.print(pressure);
softdebug.println(" Pa");
softdebug.print("Pressure Altitude");
softdebug.print(press_alt);
softdebug.println("m");
#endif
return press_alt;
}
int sensors_temperature(){
// call temp_sensor.requestTemperatures() to issue a global temperature request to all devices on the bus
temp_sensor.requestTemperatures(); // Send the command to get temperatures
// call temp_sensor.getTempC to read temperature in degrees Celsius from the device
int temperature =(int) temp_sensor.getTempC(insideThermometer);
#ifdef DEBUG_SENS
softdebug.println();
softdebug.print("Temp C: ");
softdebug.println(temperature);
#endif
return temperature;
}
void sensor_setup(){
temp_sensor.begin();//initialize one wire sensor
temp_sensor.setResolution(insideThermometer, 9); //configure sensor parameters
#ifdef DEBUG_SENS
// report on finding the devices on the bus or not
if (!temp_sensor.getAddress(insideThermometer, 0)) softdebug.println("Unable to find address for Device 0");
else{
// report parasite power requirements
softdebug.print("Parasite power is: ");
if (temp_sensor.isParasitePowerMode()) softdebug.println("ON");
else softdebug.println("OFF");
sensors_temperature(); //print temperature for debugging
}
//print voltage sensor value for debugging
softdebug.print("Vin=");
softdebug.println(sensors_vin());
#endif //DEBUG_SENS
}
#endif // ifdef AVR