| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

EVAL-ADT7420.ino
Go to the documentation of this file.
1 /***************************************************************************//**
2 * @file EVAL-ADT7420.ino
3 * @brief Exerciser program for adt7420 no-OS driver
4 * @author Gbrisebois (gregory.brisebois@analog.com)
5 ********************************************************************************
6 * Copyright 2017(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *******************************************************************************/
39
40 #include <Arduino.h>
41 #include <stdint.h>
42 #include <Linduino.h>
43 #include <UserInterface.h>
44 #include <EEPROM.h>
45 #include <platform_drivers.h>
46 extern "C" {
47 #include "adt7420.h"
48 };
49
50 i2c_init_param i2c_params =
51 {
52 GENERIC_I2C, // i2c type
53 0, // i2c id
54 100000, // i2c max speed (hz)
55 0x48, // i2c slave address
56 };
57
58 adt7420_init_param init_params =
59 {
60 i2c_params, // i2c parameters
61 0, // resolution setting
62 };
63
64 adt7420_dev *device;
65
66 int32_t connected = SUCCESS;
67
68 void setup()
69 {
70 Serial.begin(115200);
71
72 // Give serial port a chance to initialize
73 delay(100);
74
75 // Initialize
76 connected = adt7420_init(&device, init_params);
77
78 if (connected != SUCCESS)
79 {
80 Serial.println("Connection to device failed :(");
81 }
82 else
83 {
84 Serial.println("Connection to device succeeded!");
85 print_title();
86 }
87 }
88
89 void loop()
90 {
91 // If there is no device, don't continue
92 if (connected != SUCCESS)
93 {
94 return;
95 }
96
97 print_prompt();
98
99 uint8_t user_command = read_int();
100 Serial.println(user_command);
101 Serial.flush();
102
103 switch (user_command)
104 {
105 case 1:
106 menu_1_read_temperature();
107 break;
108
109 case 2:
110 menu_2_set_resolution();
111 break;
112
113 case 3:
114 menu_3_set_op_mode();
115 break;
116
117 case 4:
118 menu_4_bunchoftemps();
119 break;
120
121 case 5:
122 //menu_5_critical();
123 break;
124
125 case 9:
126 //EEPROM_WRITE_TEST();
127 break;
128
129 default:
130 Serial.println(F("Invalid option"));
131 break;
132 }
133 }
134
135 void print_title()
136 {
137 Serial.println(F("*****************************************************************"));
138 Serial.println(F("* EVAL-7420SDZ Demonstration Program *"));
139 Serial.println(F("* *"));
140 Serial.println(F("* This program demonstrates communication with the ADT7420 *"));
141 Serial.println(F("* high accuracy digital temperature sensor *"));
142 Serial.println(F("* *"));
143 Serial.println(F("* Set the baud rate to 115200 select the newline terminator. *"));
144 Serial.println(F("*****************************************************************"));
145 }
146
147 void print_prompt()
148 {
149 Serial.println(F("\nCommand Summary:"));
150
151 Serial.println(F(" 1- Read temperature"));
152 Serial.println(F(" 2- Set resolution"));
153 Serial.println(F(" 3- Set operation mode"));
154 Serial.println(F(" 4- Poll for a bunch of temperatures"));
155 Serial.println(F(" X- Get critical temperature setting"));
156 Serial.println(F(" X- Test & clear Linduino EEPROM"));
157 Serial.println();
158
159 Serial.print(F("Enter a command: "));
160 }
161
162 uint8_t menu_1_read_temperature()
163 {
164 float temp = adt7420_get_temperature(device);
165
166 Serial.print("Current temperature: ");
167 Serial.print(temp);
168 Serial.println(F(" C"));
169 }
170
171 uint8_t menu_2_set_resolution()
172 {
173 Serial.println(F(" Available resolutions:"));
174 Serial.println(F(" 1- 13-bit"));
175 Serial.println(F(" 2- 16-bit"));
176 Serial.print(F(" Select an option: "));
177
178 uint8_t new_res = read_int();
179 Serial.println(new_res);
180
181 new_res = (new_res == 1) ? 0 : 1;
182
183 adt7420_set_resolution(device, new_res);
184
185 Serial.print(F("Set resolution to "));
186 Serial.print((13 + 3 * new_res));
187 Serial.println(F("-bit"));
188
189 //EEPROM.write(64, 1);
190 //EEPROM.write(65, new_res);
191
192 return 0;
193 }
194
195 uint8_t menu_3_set_op_mode()
196 {
197 Serial.println(F(" Available operation modes:"));
198 Serial.println(F(" 1- Continuous conversion mode (default)"));
199 Serial.println(F(" 2- One-shot mode"));
200 Serial.println(F(" 3- 1 SPS mode"));
201 Serial.println(F(" 4- Shutdown"));
202 Serial.print(F(" Select a mode: "));
203
204 uint8_t new_mode = read_int();
205 Serial.println(new_mode);
206
207 switch (new_mode)
208 {
209 case 1:
210 adt7420_set_operation_mode(device, ADT7420_OP_MODE_CONT_CONV);
211 break;
212
213 case 2:
214 adt7420_set_operation_mode(device, ADT7420_OP_MODE_ONE_SHOT);
215 break;
216
217 case 3:
218 adt7420_set_operation_mode(device, ADT7420_OP_MODE_1_SPS);
219 break;
220
221 case 4:
222 adt7420_set_operation_mode(device, ADT7420_OP_MODE_SHUTDOWN);
223 break;
224
225 default:
226 Serial.println(F("Invalid option"));
227 break;
228 }
229
230 return 0;
231 }
232
233 uint8_t menu_4_bunchoftemps()
234 {
235 Serial.print(F(" Enter number of desired samples: "));
236 uint16_t num_samples = read_int();
237 Serial.println(num_samples);
238
239 Serial.print(F(" Enter a desired frequency in samples/sec (max 10): "));
240 uint16_t sample_freq = read_int();
241 sample_freq = constrain(sample_freq, 1, 10);
242 Serial.println(sample_freq);
243
244 uint16_t delay_sec = 1000 / sample_freq;
245
246 Serial.print(F(" Gathering "));
247 Serial.print(num_samples / sample_freq);
248 Serial.println(F(" seconds of samples, press enter to continue"));
249
250 uint8_t temp = read_int();
251
252 for (int i = 0; i < num_samples; i++)
253 {
254 Serial.print(F(" #"));
255 Serial.print(i + 1);
256 Serial.print(F(":\t"));
257
258 float temp = adt7420_get_temperature(device);
259 Serial.println(temp);
260
261 delay(delay_sec);
262 }
263
264 return 0;
265 }
266
267 /** Convert a temperature to the code the sensor understands */
268 void temp_to_code(float temp, uint8_t *msb, uint8_t *lsb, uint8_t resolution) // res of 0 = 13 bit, 1 = 16bit
269 {
270 uint16_t code = 0;
271
272 if (resolution)
273 {
274 if (temp < 0)
275 {
276 code = (uint16_t)((temp * 128) + 65536);
277 }
278 else
279 {
280 code = (uint16_t)(temp * 128);
281 }
282 }
283 else
284 {
285 if (temp < 0)
286 {
287 code = (uint16_t) (temp * 16) + 8192;
288 }
289 else
290 {
291 code = (uint16_t) (temp * 16) + 8192;
292 }
293
294 code <<= 3;
295 }
296
297 *msb = (uint8_t)(code >> 8);
298 *lsb = (uint8_t)(code & 255);
299 }
connected
static int32_t connected
Definition: EVAL-ADT7420.ino:66
user_command
unsigned char user_command
Definition: Serial_EEPROM_Test.ino:75
device
adt7420_dev * device
Definition: EVAL-ADT7420.ino:64
i2c_init_param
Definition: platform_drivers.h:77
Linduino.h
Header File for Linduino Libraries and Demo Code.
i2c_params
i2c_init_param i2c_params
Definition: EVAL-ADT7420.ino:50
print_prompt
static void print_prompt()
Definition: EVAL-ADT7420.ino:147
num_samples
volatile int num_samples
Total number of samples that the user desires.
Definition: DC2839A.ino:97
temp_to_code
static void temp_to_code(float temp, uint8_t *msb, uint8_t *lsb, uint8_t resolution)
Convert a temperature to the code the sensor understands.
Definition: EVAL-ADT7420.ino:268
platform_drivers.h
Header file of Generic Platform Drivers.
menu_1_read_temperature
static uint8_t menu_1_read_temperature()
Definition: EVAL-ADT7420.ino:162
menu_3_set_op_mode
static uint8_t menu_3_set_op_mode()
Definition: EVAL-ADT7420.ino:195
loop
static void loop()
Definition: EVAL-ADT7420.ino:89
setup
static void setup()
Definition: EVAL-ADT7420.ino:68
UserInterface.h
read_int
int32_t read_int()
Definition: UserInterface.cpp:93
print_title
static void print_title()
Definition: EVAL-ADT7420.ino:135
init_params
adt7420_init_param init_params
Definition: EVAL-ADT7420.ino:58
menu_4_bunchoftemps
static uint8_t menu_4_bunchoftemps()
Definition: EVAL-ADT7420.ino:233
SUCCESS
#define SUCCESS
Definition: LT_PMBus.h:62
GENERIC_I2C
Definition: platform_drivers.h:74
i
static int i
Definition: DC2430A.ino:184
menu_2_set_resolution
static uint8_t menu_2_set_resolution()
Definition: EVAL-ADT7420.ino:171