SZS ez430-RF2480 1.0

ZigBee Sensor Network with synchronized time and time-stamped measurements.
adc.c
Go to the documentation of this file.
1 /*
2  * adc.c
3  *
4  * Date: 19/06/2014
5  * Author: Fernando Biazi Nascimento
6  * Copyright © 2014 Fernando Biazi Nascimento. All rights reserved.
7  *
8  * License of use and copy on file license.txt
9  *
10  */
11 
23 /*
24  * ======== Standard MSP430 includes ========
25  */
26 #include <msp430.h>
27 
28 /*
29  * ======== Includes ========
30  */
31 
32 #include "adc.h"
33 
34 /*
35  * ======== Constants ========
36  */
37 
38 /*
39  * ======== Macros ========
40  */
41 
54 #define ADC_FLAGTEMPDONE BIT0
55 
58 #define ADC_FLAGVOLTDONE BIT1
59 
62 #define ADC_FLAGLUXDONE BIT2
63 
70 #define ADC_LUXSENSFEEDBIT BIT1
71 
72 /*
73  * ======== Types ========
74  */
75 
79 typedef enum adc_state {
87 } adc_state;
88 
89 /*
90  * ======== Global Variables ========
91  */
92 
93 /*
94  * ======== Local Variables ========
95  */
96 
128 UInt16 adc_temp;
136 UInt16 adc_voltage;
144 UInt16 adc_lux;
152 UInt8 adc_flags;
153 
154 /*
155  * ======== Local Functions ========
156  */
157 
158 /*
159  * ================
160  */
161 
171 void adc_init() {
173 
174  adc_flags = 0;
175 
176  // turn off lux sensor feed.
177  P2DIR &= ~ADC_LUXSENSFEEDBIT;
178  // set output value of lux sensor feed to 1.
179  P2OUT |= ADC_LUXSENSFEEDBIT;
180 
181  adc_temp = 0;
182  adc_tempTime.seconds.secondsLow = 0;
183  adc_tempTime.seconds.secondsHigh = 0;
184  adc_tempTime.nanoSeconds = 0;
185  adc_voltage = 0;
186  adc_voltageTime.seconds.secondsLow = 0;
187  adc_voltageTime.seconds.secondsHigh = 0;
188  adc_voltageTime.nanoSeconds = 0;
189  adc_lux = 0;
190  adc_luxTime.seconds.secondsLow = 0;
191  adc_luxTime.seconds.secondsHigh = 0;
192  adc_luxTime.nanoSeconds = 0;
193 }
194 
218 UInt16 adc_proccess() {
219  switch (adc_currentState) {
220  case ADC_S_IDLE:
221  // clear temperature done
223  // prepare to read temperature
224  ADC10CTL1 = INCH_10 + ADC10DIV_4; // CH10 - Temp Sensor; ADC10CLK/5
225  // VR+ = VREF+ and VR- = AVSS; 64 x ADC10CLKs; ADC10 Reference on; ADC10 On/Enable;
226  // ADC10 Interrupt Enable; ADC10 Sampling Rate 0=200ksps; 1=50ksps:
227  ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE + ADC10SR;
228  adc_currentState = ADC_S_CHARGING_T; // next state
229  return 10; // schedule to run again in 10ms
230  case ADC_S_CHARGING_T:
232  ADC10CTL0 |= ENC | ADC10SC; // ADC10 Enable Conversion; ADC10 Start Conversion
233  return 5; // schedule to run again in 5ms
234  case ADC_S_READING_T:
235  if (!(adc_flags & ADC_FLAGTEMPDONE)) {
236  return 5; // temperature not ready, new check in 5ms
237  }
238  // clear voltage done
240  ;
241  // prepare to read voltage
242  ADC10CTL1 = INCH_11 + ADC10DIV_3; // CH11 - Voltage Sensor; ADC10CLK/4
243  // VR+ = VREF+ and VR- = AVSS; 16 x ADC10CLKs; ADC10 Reference on; ADC10 On/Enable;
244  // ADC10 Interrupt Enable:
245  ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE;
246  adc_currentState = ADC_S_CHARGING_V; // next state
247  return 10; // schedule to run again in 10ms
248  case ADC_S_CHARGING_V:
250  ADC10CTL0 |= ENC | ADC10SC; // ADC10 Enable Conversion; ADC10 Start Conversion
251  return 5;
252  case ADC_S_READING_V:
253  if (!(adc_flags & ADC_FLAGVOLTDONE)) {
254  return 5; // voltage not ready, new check in 5ms
255  }
256  // clear lux done
258  // feed lux sensor by activating P2.1 direction to out, its value is configured to be 1 on initialization.
259  P2DIR |= ADC_LUXSENSFEEDBIT;
260  // prepare to read luminescence
261  ADC10CTL1 = INCH_0 + ADC10DIV_4; // CH00 - Lux Sensor; ADC10CLK/5
262  // VR+ = VREF+ and VR- = AVSS; 64 x ADC10CLKs; ADC10 Reference on; Reference is 2.5V; ADC10 On/Enable;
263  // ADC10 Interrupt Enable; ADC10 Sampling Rate 0=200ksps; 1=50ksps:
264  ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + REF2_5V + ADC10ON + ADC10IE
265  + ADC10SR;
266  adc_currentState = ADC_S_CHARGING_L; // next state
267  return 10; // schedule to run again in 10ms
268  case ADC_S_CHARGING_L:
270  ADC10CTL0 |= ENC | ADC10SC; // ADC10 Enable Conversion; ADC10 Start Conversion
271  return 5;
272  case ADC_S_READING_L:
273  if (!(adc_flags & ADC_FLAGLUXDONE)) {
274  return 5; // illuminance not ready, new check in 5ms
275  }
276  // turn off lux sensor feed.
277  P2DIR &= ~ADC_LUXSENSFEEDBIT;
279  return 955;
280  default:
282  return 1;
283  }
284 }
285 
294 void adc_getTemp(UInt16 *temp, clock_timeStamp *tempTime) {
295  *temp = adc_temp;
296  tempTime->seconds.secondsLow = adc_tempTime.seconds.secondsLow;
297  tempTime->seconds.secondsHigh = adc_tempTime.seconds.secondsHigh;
298  tempTime->nanoSeconds = adc_tempTime.nanoSeconds;
299 }
300 
309 void adc_getVolt(UInt16 *volt, clock_timeStamp *voltTime) {
310  *volt = adc_voltage;
311  voltTime->seconds.secondsLow = adc_voltageTime.seconds.secondsLow;
312  voltTime->seconds.secondsHigh = adc_voltageTime.seconds.secondsHigh;
313  voltTime->nanoSeconds = adc_voltageTime.nanoSeconds;
314 }
315 
324 void adc_getLux(UInt16 *lux, clock_timeStamp *luxTime) {
325  *lux = adc_lux;
326  luxTime->seconds.secondsLow = adc_luxTime.seconds.secondsLow;
327  luxTime->seconds.secondsHigh = adc_luxTime.seconds.secondsHigh;
328  luxTime->nanoSeconds = adc_luxTime.nanoSeconds;
329 }
330 
340 void ADC10_ISR() {
341  switch (adc_currentState) {
342  case ADC_S_READING_T:
343  // °C = ((A10/1024)*1500mV)-986mV)*1/3.55mV = A10*423/1024 - 278
344  //adc_temp = ((ADC10MEM - 673) * 423) >> 10;
345  // Make calculations on the receiving device
346  adc_temp = ADC10MEM;
347  ADC10CTL0 &= ~(ADC10IE & ENC); // Disable conversion
348  ADC10CTL0 = 0; // Disable ADC, some values must be cleared after ENC
349  clock_getTime(&adc_tempTime);
351  break;
352  case ADC_S_READING_V:
353  // Measurement of AVcc/2 is in range 0 to 1023 indicating voltage from 0 to 1.5V.
354  //adc_voltage = (ADC10MEM * 30 + 512) >> 10; // val * 30 / 1024 + .5
355  // Make calculations on the receiving device
356  adc_voltage = ADC10MEM;
357  ADC10CTL0 &= ~(ADC10IE & ENC); // Disable conversion
358  ADC10CTL0 = 0; // Disable ADC, some values must be cleared after ENC
359  clock_getTime(&adc_voltageTime);
361  break;
362  case ADC_S_READING_L:
363  // Measurement range 0 to 1023: 0 to 3V on sensor; series R 470 Kohm up to Vcc
364  // light sensor datasheet gives a nonlinear/large variation scale.
365  // Dark: 20Mohm; 10lux: 20Kohm to 100 Kohm; 100lux: 5kohm
366  // results(V) would be @100lux: 0,0315789474V; @10lux: 0,1224489796 to 0,5263157895V; @dark: 2,9311187103V.
367  // ADC10MEM @100lux: 10.8; @10lux: 41.8 to 179.6; @dark: 1000.5
368  // Using:
369  // IF (ADC10MEM > 105) adc_lux = 0
370  // Else adc_lux = -ADC10MEM + 111
371  //if (ADC10MEM > 105) {
372  // adc_lux = 0;
373  //} else {
374  // adc_lux = -ADC10MEM + 111; // -ADC10MEM + 111
375  //}
376  // Make calculations on the receiving device
377  adc_lux = ADC10MEM;
378  ADC10CTL0 &= ~(ADC10IE & ENC); // Disable conversion
379  ADC10CTL0 = 0; // Disable ADC, some values must be cleared after ENC
380  clock_getTime(&adc_luxTime);
382  break;
383  default:
384  break;
385  }
386 }
#define ADC_FLAGTEMPDONE
Flag to indicate temperature reading done.
Definition: adc.c:54
#define ADC_FLAGVOLTDONE
Flag to indicate voltage reading done.
Definition: adc.c:58
UInt16 adc_voltage
Value of last voltage reading.
Definition: adc.c:136
#define ADC_LUXSENSFEEDBIT
Feed bit of the luminance sensor, on port 2 (P2.1).
Definition: adc.c:70
UInt16 adc_temp
Value of last temperature reading.
Definition: adc.c:128
void adc_getLux(UInt16 *lux, clock_timeStamp *luxTime)
This function get current stored luminescence.
Definition: adc.c:324
void adc_init()
This function initializes the ADC variables.
Definition: adc.c:171
Reading voltage value.
Definition: adc.c:84
adc_state
States of the ADC10.
Definition: adc.c:79
clock_timeStamp adc_voltageTime
Time of last voltage reading.
Definition: adc.c:140
Charging ADC for luminance reading.
Definition: adc.c:85
Time stamp.
Definition: clock.h:75
ADC10 connected sensors reading interface.
UInt8 adc_flags
Internal flags for controlling ADC10.
Definition: adc.c:152
#define ADC_FLAGLUXDONE
Flag to indicate luminance reading done.
Definition: adc.c:62
clock_timeStamp adc_tempTime
Time of last temperature reading.
Definition: adc.c:132
Charging ADC for voltage reading.
Definition: adc.c:83
Waiting for next time of measurements.
Definition: adc.c:80
adc_state adc_currentState
Current state of the ADC10.
Definition: adc.c:124
void adc_getVolt(UInt16 *volt, clock_timeStamp *voltTime)
This function get current stored voltage.
Definition: adc.c:309
UInt16 adc_lux
Value of last luminance reading.
Definition: adc.c:144
void ADC10_ISR()
ADC10 Interrupt Service Request.
Definition: adc.c:340
void adc_getTemp(UInt16 *temp, clock_timeStamp *tempTime)
This function get current stored temperature.
Definition: adc.c:294
Charging ADC for temperature reading.
Definition: adc.c:81
clock_timeStamp adc_luxTime
Time of last luminance reading.
Definition: adc.c:148
Reading temperature value.
Definition: adc.c:82
UInt16 adc_proccess()
This function processes the tasks of the ADC.
Definition: adc.c:218
Reading luminance value.
Definition: adc.c:86
void clock_getTime(clock_timeStamp *destTime)
Get time.
Definition: clock.c:218