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 #include "adc.h" 00043 00044 /** 00045 * \addtogroup lcd 00046 * \{ 00047 */ 00048 00049 static bool adc_initialized; 00050 static bool adc_conversion_started; 00051 00052 /*---------------------------------------------------------------------------*/ 00053 00054 /** 00055 * \brief This function will init the ADC with the following parameters. 00056 * 00057 * \param chan Determines the ADC channel to open. 00058 * \param trig Sets what type of trigger is needed. 00059 * \param ref Sets the proper reference voltage. 00060 * \param prescale Sets the prescale to be used against the XTAL choice. 00061 * 00062 * \return 0 00063 */ 00064 int 00065 adc_init(adc_chan_t chan, adc_trig_t trig, adc_ref_t ref, adc_ps_t prescale) 00066 { 00067 /* Enable ADC module */ 00068 PRR &= ~(1 << PRADC); 00069 00070 /* Configure */ 00071 ADCSRA = (1<<ADEN)|prescale; 00072 ADMUX = (uint8_t)ref|(uint8_t)chan; 00073 ADCSRB = trig; 00074 00075 adc_initialized = true; 00076 adc_conversion_started = false; 00077 00078 return 0; 00079 } 00080 00081 /*---------------------------------------------------------------------------*/ 00082 00083 /** 00084 * \brief This will disable the adc. 00085 */ 00086 void 00087 adc_deinit(void) 00088 { 00089 /* Disable ADC */ 00090 ADCSRA &= ~(1<<ADEN); 00091 PRR |= (1 << PRADC); 00092 00093 adc_initialized = false; 00094 adc_conversion_started = false; 00095 } 00096 00097 /*---------------------------------------------------------------------------*/ 00098 00099 /** 00100 * \brief This will start an ADC conversion 00101 * 00102 * \return 0 00103 */ 00104 int 00105 adc_conversion_start(void) 00106 { 00107 if (adc_initialized == false){ 00108 return EOF; 00109 } 00110 adc_conversion_started = true; 00111 ADCSRA |= (1<<ADSC); 00112 return 0; 00113 } 00114 00115 /*---------------------------------------------------------------------------*/ 00116 00117 /** 00118 * \brief This will read the ADC result during the ADC conversion and return 00119 * the raw ADC conversion result. 00120 * 00121 * \param adjust This will Left or Right Adjust the ADC conversion result. 00122 * 00123 * \return ADC raw 16-byte ADC conversion result. 00124 */ 00125 int16_t 00126 adc_result_get(adc_adj_t adjust) 00127 { 00128 if (adc_conversion_started == false){ 00129 return EOF; 00130 } 00131 if (ADCSRA & (1<<ADSC)){ 00132 return EOF; 00133 } 00134 adc_conversion_started = false; 00135 ADMUX |= (adjust<<ADLAR); 00136 return (int16_t)ADC; 00137 } 00138 00139 /** \} */