Contiki 3.x
light.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. 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  * 3. Neither the name of the Institute 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 INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * 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 INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 #include <stdlib.h>
34 #include "contiki.h"
35 #include "dev/light.h"
36 
37 /*
38  * Initialize periodic readings from the 2 photo diodes. The most
39  * recent readings will be stored in ADC internal registers/memory.
40  */
41 void
42 sensors_light_init(void)
43 {
44  P6SEL |= 0x30;
45  P6DIR = 0xff;
46  P6OUT = 0x00;
47 
48  /* Set up the ADC. */
49  ADC12CTL0 = REF2_5V + SHT0_6 + SHT1_6 + MSC; // Setup ADC12, ref., sampling time
50  ADC12CTL1 = SHP + CONSEQ_3 + CSTARTADD_0; // Use sampling timer, repeat-sequenc-of-channels
51 
52  ADC12MCTL0 = (INCH_4 + SREF_0); // photodiode 1 (P64)
53  ADC12MCTL1 = (INCH_5 + SREF_0); // photodiode 2 (P65)
54 
55  ADC12CTL0 |= ADC12ON + REFON;
56 
57  ADC12CTL0 |= ENC; // enable conversion
58  ADC12CTL0 |= ADC12SC; // sample & convert
59 }
60 
61 /* Photosynthetically Active Radiation. */
62 unsigned
63 sensors_light1(void)
64 {
65  return ADC12MEM0;
66 }
67 
68 /* Total Solar Radiation. */
69 unsigned
70 sensors_light2(void)
71 {
72  return ADC12MEM1;
73 }
74 
75 /*
76  * Most of this information taken from
77  * http://www.moteiv.com/community/Getting_Data_from_Tmote_Sky%27s_Sensors
78  *
79  * The Photosynthetically Active Radiation (PAR) sensor as well as the
80  * Total Solar Radiation (TSR) sensor uses the 2.5V reference voltage
81  * to produce the raw ADC value.
82 
83  * The voltage across each sensor is:
84  *
85  * VsensorPAR = ADCValuePAR/4096 * Vref (1a)
86  * VsensorTSR = ADCValueTSR/4096 * Vref (1b)
87  * where Vref = 2.5V
88  *
89  * This voltage creates a current through a resistor R=100KOhm and this
90  * current has a linear relationship with the light intensity in Lux.
91  * IPAR = VsensorPAR / 100,000 (2a)
92  * ITSR = VsensorTSR / 100,000 (2b)
93  *
94  * S1087 (PAR) lx = 1e6 * IPAR * 1000 (3a)
95  * S1087-01 (TSR) lx = 1e5 * ITSR * 1000 (3b)
96  *
97  * lxPAR = 10e9 * ADCValuePAR *(1/4096)* Vref * 10e-5 or
98  * lxPAR = 3125* ADCvaluePAR / 512
99  * and
100  * lxTSR = 10e8 * ADCValueTSR *(1/4096)* Vref * 10e-5 or
101  * lxTSR = 625* ADCvalueTSR / 1024
102 */
103 
104 #if 0
105 /* Photosynthetically Active Radiation in Lux units. */
106 unsigned
107 sensors_light1_lux(void)
108 {
109  unsigned temp;
110  temp = (uint32_t)ADC12MEM0;
111 
112  temp = (temp*3125)>> 9;
113  return (uint16_t)(temp & 0xFFFF);
114 }
115 
116 /* Total Solar Radiation in Lux units. */
117 unsigned
118 sensors_light2_lux(void)
119 {
120  unsigned temp;
121  temp = (uint32_t)ADC12MEM1;
122 
123  temp = (temp*625)>> 10;
124  return (uint16_t)(temp & 0xFFFF);
125 }
126 #endif