Contiki-Inga 3.x
adc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, TU Braunschweig.
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 
30 /**
31  * \file
32  * ADC driver implementation
33  * \author
34  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
35  */
36 
37 /**
38  * \addtogroup inga_sensors_driver
39  * @{
40  */
41 
42 /**
43  * \addtogroup adc_driver
44  * @{
45  */
46 #include "adc.h"
47 #include <stdio.h>
48 #include <avr/pgmspace.h>
49 
50 #define DEBUG 0
51 #if DEBUG
52 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
53 #else
54 #define PRINTF(...)
55 #endif
56 
57 static uint8_t aref_connected;
58 
59 void
60 adc_init(uint8_t mode, uint8_t ref)
61 {
62  ADCSRA = ((ADC_ENABLE) | (ADC_PRESCALE_64));
63  ADCSRB = 0x00;
64 
65  /* Safety check:
66  * Test AREF pin by starting a single conversion with AREF as reference
67  * and 0V channel input.
68  * If this results in a value of 1023, AREF pin seems to be unconnected.*/
69  ADMUX = 0x1F; /* AREF input, 0V reference */
70  ADCSRA |= (1 << ADSC); /* Start conversion */
71  while (ADCSRA & (1 << ADSC)); /* Wait till done */
72 
73  if (ADC == 1023) {
74  PRINTF("adc: AREF unconnected\n");
75  aref_connected = 0;
76  } else {
77  PRINTF("adc: AREF connected\n");
78  aref_connected = 1;
79  }
80 
81  /* Do not allow to set internal reference with AREF connected */
82  if ((aref_connected) && (ref != ADC_REF_AREF)) {
83  PRINTF("adc: Internal reference voltage not allowed!\n");
84  return 1;
85  }
86 
87  ADMUX = ref;
88 
89  if (mode != ADC_SINGLE_CONVERSION) {
90  ADCSRB |= (0x07 & mode);
91  ADCSRA |= ((ADC_TRIGGER_ENABLE) | (ADC_INTERRUPT_ENABLE));
92  }
93 }
94 /*----------------------------------------------------------------------------*/
95 void
96 adc_set_mux(uint8_t mux)
97 {
98  static uint8_t used_adcs = 0;
99  /*save energy by disabling the i/o input buffer*/
100  if (mux < 8) {
101  used_adcs |= (1 << mux);
102  DIDR0 |= used_adcs;
103  }
104  ADMUX &= (0xE0);
105  ADMUX |= mux;
106  ADCSRA |= ADC_START;
107 }
108 /*----------------------------------------------------------------------------*/
109 uint16_t
111 {
112  if (ADCSRA & ADC_TRIGGER_ENABLE) {
113  /*just read the ADC data register*/
114  return ADCW;
115  } else {
116  /*start single conversion*/
117  while (ADCSRA & (1 << ADSC)) {
118  ;
119  }
120  return ADCW;
121  }
122 }
123 /*----------------------------------------------------------------------------*/
124 uint16_t
125 adc_get_value_from(uint8_t chn)
126 {
127  adc_set_mux(chn);
128  return adc_get_value();
129 }
130 /*----------------------------------------------------------------------------*/
131 void
133 {
134  ADCSRA = ADC_STOP;
135  ADCSRB = ADC_STOP;
136  ADMUX = ADC_STOP;
137 }
138 /*----------------------------------------------------------------------------*/