Contiki 2.6
|
00001 /* 00002 * Copyright (c) 2008 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 are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in 00012 * the documentation and/or other materials provided with the 00013 * distribution. 00014 * * Neither the name of the copyright holders nor the names of 00015 * contributors may be used to endorse or promote products derived 00016 * from this software without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00022 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00028 * POSSIBILITY OF SUCH DAMAGE. 00029 */ 00030 /** 00031 * \file 00032 * 00033 * \brief 00034 * Functions to control the ADC of the MCU. This is used to read the 00035 * joystick. 00036 * 00037 * \author 00038 * Mike Vidales mavida404@gmail.com 00039 * 00040 */ 00041 00042 #ifndef __ADC_H__ 00043 #define __ADC_H__ 00044 00045 #include <avr/io.h> 00046 #include <stdio.h> 00047 #include <stdint.h> 00048 #include <stdbool.h> 00049 00050 #define adc_conversion_ongoing (ADCSRA |= (1<<ADSC)) 00051 #define adc_conversion_done (!(ADCSRA |= (1<<ADSC))) 00052 00053 /** \brief Lists the different ways in which the ADC can be triggered. */ 00054 typedef enum { 00055 ADC_TRIG_FREE_RUN = ((0<<ADTS2)|(0<<ADTS1)|(0<<ADTS0)), 00056 ADC_TRIG_ANACOMP = ((0<<ADTS2)|(0<<ADTS1)|(1<<ADTS0)), 00057 ADC_TRIG_EXTINT0 = ((0<<ADTS2)|(1<<ADTS1)|(0<<ADTS0)), 00058 ADC_TRIG_TIM0_COMPA = ((0<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)), 00059 ADC_TRIG_TIM0_OVF = ((1<<ADTS2)|(0<<ADTS1)|(0<<ADTS0)), 00060 ADC_TRIG_TIM1_COMPB = ((1<<ADTS2)|(0<<ADTS1)|(1<<ADTS0)), 00061 ADC_TRIG_TIM1_OVF = ((1<<ADTS2)|(1<<ADTS1)|(0<<ADTS0)), 00062 ADC_TRIG_TIM1_CAPT = ((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)) 00063 } adc_trig_t; 00064 00065 /** \brief Lists a variety of prescalers used with the ADC. */ 00066 typedef enum { 00067 ADC_PS_2 = ((0<<ADPS2)|(0<<ADPS1)|(1<<ADPS0)), 00068 ADC_PS_4 = ((0<<ADPS2)|(1<<ADPS1)|(0<<ADPS0)), 00069 ADC_PS_8 = ((0<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)), 00070 ADC_PS_16 = ((1<<ADPS2)|(0<<ADPS1)|(0<<ADPS0)), 00071 ADC_PS_32 = ((1<<ADPS2)|(0<<ADPS1)|(1<<ADPS0)), 00072 ADC_PS_64 = ((1<<ADPS2)|(1<<ADPS1)|(0<<ADPS0)), 00073 ADC_PS_128 = ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)) 00074 } adc_ps_t; 00075 00076 /** 00077 * \brief Lists the ways in which the voltage reference can be configured 00078 * for use with the ADC. 00079 */ 00080 typedef enum { 00081 ADC_REF_AREF = ((0<<REFS1)|(0<<REFS0)), 00082 ADC_REF_AVCC = ((0<<REFS1)|(1<<REFS0)), 00083 ADC_REF_INT = ((1<<REFS1)|(1<<REFS0)) 00084 } adc_ref_t; 00085 00086 /** \brief Lists each channel's mask value for the ADC MUX. */ 00087 typedef enum { 00088 ADC_CHAN_ADC0 = ((0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(0<<MUX0)), 00089 ADC_CHAN_ADC1 = ((0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(1<<MUX0)), 00090 ADC_CHAN_ADC2 = ((0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(1<<MUX1)|(0<<MUX0)), 00091 ADC_CHAN_ADC3 = ((0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(1<<MUX1)|(1<<MUX0)), 00092 ADC_CHAN_ADC4 = ((0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(0<<MUX1)|(0<<MUX0)), 00093 ADC_CHAN_ADC5 = ((0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(0<<MUX1)|(1<<MUX0)), 00094 ADC_CHAN_ADC6 = ((0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(1<<MUX1)|(0<<MUX0)), 00095 ADC_CHAN_ADC7 = ((0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(1<<MUX1)|(1<<MUX0)) 00096 } adc_chan_t; 00097 00098 /** \brief Lists the two ADC adjustment values. */ 00099 typedef enum { 00100 ADC_ADJ_RIGHT = 0, 00101 ADC_ADJ_LEFT = 1 00102 } adc_adj_t; 00103 00104 int adc_init(adc_chan_t chan, adc_trig_t trig, adc_ref_t ref, adc_ps_t prescale); 00105 void adc_deinit(void); 00106 int adc_conversion_start(void); 00107 int16_t adc_result_get(adc_adj_t adjust); 00108 00109 #endif /* __ADC_H__ */ 00110