µracoli Manual Version foo

- µracoli
- User Guide
- Reference Guide

mcu_temp.h
1 /* Copyright (c) 2014 Daniel Thiele, Axel Wachtler
2 All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of the authors nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE. */
28
29 #ifndef MCU_TEMP_H
30 #define MCU_TEMP_H
31
40 /* === includes ============================================================ */
41 #include "sensor_defs.h"
42
43 /* === macros ============================================================== */
44
45 /* === types =============================================================== */
46 typedef struct {
47 sensor_driver_t g;
48 int16_t adc_offset;
49 } mcu_temp_ctx_t;
50
51 /* === prototypes ========================================================== */
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 /* === high level sensor functions ========================================= */
56
57 static inline void mcu_temp_trigger(void *pctx, bool one_shot)
58 {
59 ADCSRC = 10<<ADSUT0; // set start-up time
60 ADCSRB = 1<<MUX5; // set MUX5 first
61 ADMUX = (3<<REFS0) + (9<<MUX0); // store new ADMUX, 1.6V AREF
62 // switch ADC on, set prescaler, start conversion
63 ADCSRA = (1<<ADEN) + (1<<ADSC) + (4<<ADPS0);
64 }
65
69 static inline uint8_t mcu_temp_get_val(void *pctx, uint8_t *pdata)
70 {
71 uint8_t rv, i;
72 int16_t t_accu = 0;
73 #define NB_AVG (5)
74
75 rv = sizeof(sensor_temperature_t);
76
77 if (pdata != NULL)
78 {
79 for (i = 0; i < NB_AVG; i++)
80 {
81 while( (ADCSRA & (1<<ADSC))); // wait for conversion end
82 t_accu += (ADC - ((mcu_temp_ctx_t*)pctx)->adc_offset);
83 }
84 t_accu -= 1;
85 ADCSRA = 0; // disable the ADC
86 ((sensor_temperature_t*)pdata)->type = SENSOR_DATA_TEMPERATURE;
87 ((sensor_temperature_t*)pdata)->sensor = SENSOR_MCU_T;
88 ((sensor_temperature_t*)pdata)->temp = (1.13 * (t_accu/(float)(NB_AVG)) - 272.8);
89 }
90 else
91 {
92 rv = 0;
93 }
94 return rv;
95 }
96
98 static inline uint8_t mcu_temp_get_raw(void *pctx, uint8_t *pdata)
99 {
100 uint8_t rv = 0;
101 return rv;
102 }
103
124 static inline uint8_t sensor_create_mcu_temp(void *pdata, bool raw)
125 {
126 uint8_t rv = sizeof(mcu_temp_ctx_t);
127 mcu_temp_ctx_t *pcfg;
128
129 if (pdata != NULL)
130 {
131 /* init generic sensor data */
132 pcfg = (mcu_temp_ctx_t *)pdata;
133 pcfg->g.id = SENSOR_MCU_T;
134 pcfg->g.f_trigger = mcu_temp_trigger;
135 pcfg->g.f_get_val = raw ? mcu_temp_get_raw : mcu_temp_get_val;
136 pcfg->g.f_sleep = NULL;
137
138 /* initialize sensor */
139
140 /* get ADC offset */
141 ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1); /* PS 64 */
142 ADCSRB = 0;
143 ADMUX = (1 << REFS1) | (1 << REFS0) | (1 << MUX3); /* reference: 1.6V, differential ADC0-ADC0 10x */
144
145 _delay_us(200); /* some time to settle */
146 while( (ADCSRA & (1<<ADSC))); // wait for conversion end
147 _delay_us(200);
148 while( (ADCSRA & (1<<ADSC)));
149
150 ADCSRA = 0; // disable the ADC
151 ((mcu_temp_ctx_t*) pcfg)->adc_offset = ADC;
152 }
153 return rv;
154 }
155
156 #ifdef __cplusplus
157 } /* extern "C" */
158 #endif
159
160 #endif /* #ifndef MCU_TEMP_H */
sensor_create_mcu_temp
static uint8_t sensor_create_mcu_temp(void *pdata, bool raw)
Definition: mcu_temp.h:124
sensor_driver_t
Definition: sensor_defs.h:59
sensor_temperature_t
Definition: sensor_defs.h:69
mcu_temp_get_val
static uint8_t mcu_temp_get_val(void *pctx, uint8_t *pdata)
Definition: mcu_temp.h:69
mcu_temp_get_raw
static uint8_t mcu_temp_get_raw(void *pctx, uint8_t *pdata)
Definition: mcu_temp.h:98