Contiki 2.5
adc.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 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 are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in
12  * the documentation and/or other materials provided with the
13  * distribution.
14  * * Neither the name of the copyright holders nor the names of
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 /**
31  * \file
32  *
33  * \brief
34  * Functions to control the ADC of the MCU. This is used to read the
35  * joystick.
36  *
37  * \author
38  * Mike Vidales mavida404@gmail.com
39  *
40  */
41 
42 #ifndef __ADC_H__
43 #define __ADC_H__
44 
45 #include <avr/io.h>
46 #include <stdio.h>
47 #include <stdint.h>
48 #include <stdbool.h>
49 
50 #define adc_conversion_ongoing (ADCSRA |= (1<<ADSC))
51 #define adc_conversion_done (!(ADCSRA |= (1<<ADSC)))
52 
53 /** \brief Lists the different ways in which the ADC can be triggered. */
54 typedef enum {
55  ADC_TRIG_FREE_RUN = ((0<<ADTS2)|(0<<ADTS1)|(0<<ADTS0)),
56  ADC_TRIG_ANACOMP = ((0<<ADTS2)|(0<<ADTS1)|(1<<ADTS0)),
57  ADC_TRIG_EXTINT0 = ((0<<ADTS2)|(1<<ADTS1)|(0<<ADTS0)),
58  ADC_TRIG_TIM0_COMPA = ((0<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)),
59  ADC_TRIG_TIM0_OVF = ((1<<ADTS2)|(0<<ADTS1)|(0<<ADTS0)),
60  ADC_TRIG_TIM1_COMPB = ((1<<ADTS2)|(0<<ADTS1)|(1<<ADTS0)),
61  ADC_TRIG_TIM1_OVF = ((1<<ADTS2)|(1<<ADTS1)|(0<<ADTS0)),
62  ADC_TRIG_TIM1_CAPT = ((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0))
63 } adc_trig_t;
64 
65 /** \brief Lists a variety of prescalers used with the ADC. */
66 typedef enum {
67  ADC_PS_2 = ((0<<ADPS2)|(0<<ADPS1)|(1<<ADPS0)),
68  ADC_PS_4 = ((0<<ADPS2)|(1<<ADPS1)|(0<<ADPS0)),
69  ADC_PS_8 = ((0<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)),
70  ADC_PS_16 = ((1<<ADPS2)|(0<<ADPS1)|(0<<ADPS0)),
71  ADC_PS_32 = ((1<<ADPS2)|(0<<ADPS1)|(1<<ADPS0)),
72  ADC_PS_64 = ((1<<ADPS2)|(1<<ADPS1)|(0<<ADPS0)),
73  ADC_PS_128 = ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0))
74 } adc_ps_t;
75 
76 /**
77  * \brief Lists the ways in which the voltage reference can be configured
78  * for use with the ADC.
79 */
80 typedef enum {
81  ADC_REF_AREF = ((0<<REFS1)|(0<<REFS0)),
82  ADC_REF_AVCC = ((0<<REFS1)|(1<<REFS0)),
83  ADC_REF_INT = ((1<<REFS1)|(1<<REFS0))
84 } adc_ref_t;
85 
86 /** \brief Lists each channel's mask value for the ADC MUX. */
87 typedef enum {
88  ADC_CHAN_ADC0 = ((0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(0<<MUX0)),
89  ADC_CHAN_ADC1 = ((0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(1<<MUX0)),
90  ADC_CHAN_ADC2 = ((0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(1<<MUX1)|(0<<MUX0)),
91  ADC_CHAN_ADC3 = ((0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(1<<MUX1)|(1<<MUX0)),
92  ADC_CHAN_ADC4 = ((0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(0<<MUX1)|(0<<MUX0)),
93  ADC_CHAN_ADC5 = ((0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(0<<MUX1)|(1<<MUX0)),
94  ADC_CHAN_ADC6 = ((0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(1<<MUX1)|(0<<MUX0)),
95  ADC_CHAN_ADC7 = ((0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(1<<MUX1)|(1<<MUX0))
96 } adc_chan_t;
97 
98 /** \brief Lists the two ADC adjustment values. */
99 typedef enum {
100  ADC_ADJ_RIGHT = 0,
101  ADC_ADJ_LEFT = 1
102 } adc_adj_t;
103 
104 int adc_init(adc_chan_t chan, adc_trig_t trig, adc_ref_t ref, adc_ps_t prescale);
105 void adc_deinit(void);
106 int adc_conversion_start(void);
107 int16_t adc_result_get(adc_adj_t adjust);
108 
109 #endif /* __ADC_H__ */
110