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 <stdlib.h>
00035 
00036 #include <io.h>
00037 
00038 #include "contiki.h"
00039 #include "dev/light.h"
00040 
00041 /*
00042  * Initialize periodic readings from the 2 photo diodes. The most
00043  * recent readings will be stored in ADC internal registers/memory.
00044  */
00045 void
00046 sensors_light_init(void)
00047 {
00048   P6SEL |= 0x30;
00049   P6DIR = 0xff;
00050   P6OUT = 0x00;
00051 
00052   /* Set up the ADC. */
00053   ADC12CTL0 = REF2_5V + SHT0_6 + SHT1_6 + MSC; // Setup ADC12, ref., sampling time
00054   ADC12CTL1 = SHP + CONSEQ_3 + CSTARTADD_0;     // Use sampling timer, repeat-sequenc-of-channels
00055 
00056   ADC12MCTL0 = (INCH_4 + SREF_0); // photodiode 1 (P64)
00057   ADC12MCTL1 = (INCH_5 + SREF_0); // photodiode 2 (P65)
00058 
00059   ADC12CTL0 |= ADC12ON + REFON;
00060 
00061   ADC12CTL0 |= ENC;             // enable conversion
00062   ADC12CTL0 |= ADC12SC;         // sample & convert
00063 }
00064 
00065 /* Photosynthetically Active Radiation. */
00066 unsigned
00067 sensors_light1(void)
00068 {
00069   return ADC12MEM0;
00070 }
00071 
00072 /* Total Solar Radiation. */
00073 unsigned
00074 sensors_light2(void)
00075 {
00076   return ADC12MEM1;
00077 }
00078 
00079 /*
00080  * Most of this information taken from
00081  * http://www.moteiv.com/community/Getting_Data_from_Tmote_Sky%27s_Sensors
00082  *
00083  * The Photosynthetically Active Radiation (PAR) sensor as well as the
00084  * Total Solar Radiation (TSR) sensor uses the 2.5V reference voltage
00085  * to produce the raw ADC value.
00086 
00087  * The voltage across each sensor is:
00088  *
00089  * VsensorPAR = ADCValuePAR/4096 * Vref       (1a)
00090  * VsensorTSR = ADCValueTSR/4096 * Vref       (1b)
00091  * where Vref = 2.5V
00092  *
00093  * This voltage creates a current through a resistor R=100KOhm and this
00094  * current has a linear relationship with the light intensity in Lux.
00095  * IPAR = VsensorPAR / 100,000             (2a) 
00096  * ITSR = VsensorTSR / 100,000             (2b) 
00097  *
00098  * S1087    (PAR) lx = 1e6 * IPAR * 1000   (3a)  
00099  * S1087-01 (TSR) lx = 1e5 * ITSR * 1000   (3b)
00100  *
00101  * lxPAR = 10e9 * ADCValuePAR *(1/4096)* Vref * 10e-5  or
00102  * lxPAR = 3125* ADCvaluePAR / 512  
00103  * and 
00104  * lxTSR = 10e8 * ADCValueTSR *(1/4096)* Vref * 10e-5  or
00105  * lxTSR = 625* ADCvalueTSR / 1024  
00106 */
00107 
00108 #if 0
00109 /* Photosynthetically Active Radiation in Lux units. */
00110 unsigned
00111 sensors_light1_lux(void)
00112 {
00113   unsigned temp;
00114   temp = (uint32_t)ADC12MEM0;
00115 
00116   temp = (temp*3125)>> 9; 
00117   return (uint16_t)(temp & 0xFFFF); 
00118 }
00119 
00120 /* Total Solar Radiation in Lux units. */
00121 unsigned
00122 sensors_light2_lux(void)
00123 {
00124   unsigned temp;
00125   temp = (uint32_t)ADC12MEM1;
00126 
00127   temp = (temp*625)>> 10; 
00128   return (uint16_t)(temp & 0xFFFF); 
00129 }
00130 #endif

Generated on Mon Apr 11 14:23:45 2011 for Contiki 2.5 by  doxygen 1.6.1