Contiki 2.5
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  * @(#)$Id: light.c,v 1.1 2006/08/02 14:44:46 bg- Exp $
32  */
33 
34 #include <stdlib.h>
35 #include "contiki.h"
36 #include "dev/light.h"
37 
38 /*
39  * Initialize periodic readings from the 2 photo diodes. The most
40  * recent readings will be stored in ADC internal registers/memory.
41  */
42 void
43 sensors_light_init(void)
44 {
45  P6SEL |= 0x30;
46  P6DIR = 0xff;
47  P6OUT = 0x00;
48 
49  /* Set up the ADC. */
50  ADC12CTL0 = REF2_5V + SHT0_6 + SHT1_6 + MSC; // Setup ADC12, ref., sampling time
51  ADC12CTL1 = SHP + CONSEQ_3 + CSTARTADD_0; // Use sampling timer, repeat-sequenc-of-channels
52 
53  ADC12MCTL0 = (INCH_4 + SREF_0); // photodiode 1 (P64)
54  ADC12MCTL1 = (INCH_5 + SREF_0); // photodiode 2 (P65)
55 
56  ADC12CTL0 |= ADC12ON + REFON;
57 
58  ADC12CTL0 |= ENC; // enable conversion
59  ADC12CTL0 |= ADC12SC; // sample & convert
60 }
61 
62 /* Photosynthetically Active Radiation. */
63 unsigned
64 sensors_light1(void)
65 {
66  return ADC12MEM0;
67 }
68 
69 /* Total Solar Radiation. */
70 unsigned
71 sensors_light2(void)
72 {
73  return ADC12MEM1;
74 }
75 
76 /*
77  * Most of this information taken from
78  * http://www.moteiv.com/community/Getting_Data_from_Tmote_Sky%27s_Sensors
79  *
80  * The Photosynthetically Active Radiation (PAR) sensor as well as the
81  * Total Solar Radiation (TSR) sensor uses the 2.5V reference voltage
82  * to produce the raw ADC value.
83 
84  * The voltage across each sensor is:
85  *
86  * VsensorPAR = ADCValuePAR/4096 * Vref (1a)
87  * VsensorTSR = ADCValueTSR/4096 * Vref (1b)
88  * where Vref = 2.5V
89  *
90  * This voltage creates a current through a resistor R=100KOhm and this
91  * current has a linear relationship with the light intensity in Lux.
92  * IPAR = VsensorPAR / 100,000 (2a)
93  * ITSR = VsensorTSR / 100,000 (2b)
94  *
95  * S1087 (PAR) lx = 1e6 * IPAR * 1000 (3a)
96  * S1087-01 (TSR) lx = 1e5 * ITSR * 1000 (3b)
97  *
98  * lxPAR = 10e9 * ADCValuePAR *(1/4096)* Vref * 10e-5 or
99  * lxPAR = 3125* ADCvaluePAR / 512
100  * and
101  * lxTSR = 10e8 * ADCValueTSR *(1/4096)* Vref * 10e-5 or
102  * lxTSR = 625* ADCvalueTSR / 1024
103 */
104 
105 #if 0
106 /* Photosynthetically Active Radiation in Lux units. */
107 unsigned
108 sensors_light1_lux(void)
109 {
110  unsigned temp;
111  temp = (uint32_t)ADC12MEM0;
112 
113  temp = (temp*3125)>> 9;
114  return (uint16_t)(temp & 0xFFFF);
115 }
116 
117 /* Total Solar Radiation in Lux units. */
118 unsigned
119 sensors_light2_lux(void)
120 {
121  unsigned temp;
122  temp = (uint32_t)ADC12MEM1;
123 
124  temp = (temp*625)>> 10;
125  return (uint16_t)(temp & 0xFFFF);
126 }
127 #endif