Contiki 2.6

light.c

00001 /*
00002  * Copyright (c) 2005, Swedish Institute of Computer Science
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * @(#)$Id: light.c,v 1.1 2006/08/02 14:44:46 bg- Exp $
00032  */
00033 
00034 #include "contiki.h"
00035 #include "dev/light.h"
00036 #include <stdlib.h>
00037 
00038 /*
00039  * Initialize periodic readings from the 2 photo diodes. The most
00040  * recent readings will be stored in ADC internal registers/memory.
00041  */
00042 void
00043 sensors_light_init(void)
00044 {
00045   P6SEL |= 0x30;
00046   P6DIR = 0xff;
00047   P6OUT = 0x00;
00048 
00049   /* Set up the ADC. */
00050   ADC12CTL0 = REF2_5V + SHT0_6 + SHT1_6 + MSC; // Setup ADC12, ref., sampling time
00051   ADC12CTL1 = SHP + CONSEQ_3 + CSTARTADD_0;     // Use sampling timer, repeat-sequenc-of-channels
00052 
00053   ADC12MCTL0 = (INCH_4 + SREF_0); // photodiode 1 (P64)
00054   ADC12MCTL1 = (INCH_5 + SREF_0); // photodiode 2 (P65)
00055 
00056   ADC12CTL0 |= ADC12ON + REFON;
00057 
00058   ADC12CTL0 |= ENC;             // enable conversion
00059   ADC12CTL0 |= ADC12SC;         // sample & convert
00060 }
00061 
00062 /* Photosynthetically Active Radiation. */
00063 unsigned
00064 sensors_light1(void)
00065 {
00066   return ADC12MEM0;
00067 }
00068 
00069 /* Total Solar Radiation. */
00070 unsigned
00071 sensors_light2(void)
00072 {
00073   return ADC12MEM1;
00074 }
00075 
00076 /*
00077  * Most of this information taken from
00078  * http://www.moteiv.com/community/Getting_Data_from_Tmote_Sky%27s_Sensors
00079  *
00080  * The Photosynthetically Active Radiation (PAR) sensor as well as the
00081  * Total Solar Radiation (TSR) sensor uses the 2.5V reference voltage
00082  * to produce the raw ADC value.
00083 
00084  * The voltage across each sensor is:
00085  *
00086  * VsensorPAR = ADCValuePAR/4096 * Vref       (1a)
00087  * VsensorTSR = ADCValueTSR/4096 * Vref       (1b)
00088  * where Vref = 2.5V
00089  *
00090  * This voltage creates a current through a resistor R=100KOhm and this
00091  * current has a linear relationship with the light intensity in Lux.
00092  * IPAR = VsensorPAR / 100,000             (2a)
00093  * ITSR = VsensorTSR / 100,000             (2b)
00094  *
00095  * S1087    (PAR) lx = 1e6 * IPAR * 1000   (3a)
00096  * S1087-01 (TSR) lx = 1e5 * ITSR * 1000   (3b)
00097  *
00098  * lxPAR = 10e9 * ADCValuePAR *(1/4096)* Vref * 10e-5  or
00099  * lxPAR = 3125* ADCvaluePAR / 512
00100  * and
00101  * lxTSR = 10e8 * ADCValueTSR *(1/4096)* Vref * 10e-5  or
00102  * lxTSR = 625* ADCvalueTSR / 1024
00103 */
00104 
00105 #if 0
00106 /* Photosynthetically Active Radiation in Lux units. */
00107 unsigned
00108 sensors_light1_lux(void)
00109 {
00110   unsigned temp;
00111   temp = (uint32_t)ADC12MEM0;
00112 
00113   temp = (temp*3125)>> 9;
00114   return (uint16_t)(temp & 0xFFFF);
00115 }
00116 
00117 /* Total Solar Radiation in Lux units. */
00118 unsigned
00119 sensors_light2_lux(void)
00120 {
00121   unsigned temp;
00122   temp = (uint32_t)ADC12MEM1;
00123 
00124   temp = (temp*625)>> 10;
00125   return (uint16_t)(temp & 0xFFFF);
00126 }
00127 #endif