avr/cpp/ADC.h

Go to the documentation of this file.
00001 /**********************************************************************************************************************\
00002 
00003         C++ library for Atmel AVR microcontrollers
00004         Copyright (C) 2007 Lauri Kirikal, Mikk Leini, MT� TT� Robotiklubi
00005 
00006         This program is free software; you can redistribute it and/or
00007         modify it under the terms of the GNU General Public License
00008         as published by the Free Software Foundation; either version 2
00009         of the License, or (at your option) any later version.
00010 
00011         This program is distributed in the hope that it will be useful,
00012         but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License
00017         along with this program; if not, write to the Free Software
00018         Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 
00020         See http://creativecommons.org/licenses/GPL/2.0/
00021 
00022         MT� TT� Robotiklubi  http://www.robotiklubi.ee robotiklubi@gmail.com
00023         Lauri Kirikal        laurikirikal@gmail.com
00024         Mikk Leini           mikk.leini@gmail.com
00025 
00026 \**********************************************************************************************************************/
00027 
00028 #ifndef __AVR_CPP_ADC_H__
00029 #define __AVR_CPP_ADC_H__
00030 
00031 #define __ADC_SIMPLE__                  0x00
00032 #define __ADC_DIFFERENTIAL__    0x01
00033 #define __ADC_DIGITAL_INPUT__   0x02
00034 #define __ADC_AUTO_TRIGGER__    0x04
00035 #define __ADC_INTERNAL_REF1__   0x08
00036 #define __ADC_HIGH_SPEED__              0x10
00037 
00038 
00039 #if defined(__AVR_ATmega8__)
00040 
00041 #define __ADC_PROPERTIES__              (__ADC_SIMPLE__)
00042 
00043 #elif defined(__AVR_ATmega128__)
00044 
00045 #define __ADC_PROPERTIES__              (__ADC_SIMPLE__ | __ADC_DIFFERENTIAL__)
00046 
00047 #elif defined(__AVR_ATmega64__)
00048 
00049 #define __ADC_PROPERTIES__              (__ADC_SIMPLE__ | __ADC_DIFFERENTIAL__ | __ADC_AUTO_TRIGGER__)
00050 
00051 #elif defined(__AVR_AT90USB1287__)
00052 
00053 #define __ADC_PROPERTIES__              (__ADC_SIMPLE__ | __ADC_DIFFERENTIAL__ | __ADC_AUTO_TRIGGER__ | __ADC_DIGITAL_INPUT__ | __ADC_HIGH_SPEED__)
00054 
00055 #elif defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
00056 
00057 #define __ADC_PROPERTIES__              (__ADC_SIMPLE__ | __ADC_DIFFERENTIAL__ | __ADC_AUTO_TRIGGER__ | __ADC_DIGITAL_INPUT__ | __ADC_INTERNAL_REF1__)
00058 
00059 #elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
00060 
00061 #define __ADC_PROPERTIES__              (__ADC_SIMPLE__ | __ADC_AUTO_TRIGGER__ | __ADC_DIGITAL_INPUT__)
00062 
00063 #else
00064 
00065 #error "Device is not selected or selected device is not supported."
00066 
00067 #endif
00068 
00069 #ifdef __ADC_PROPERTIES__
00070 
00071 #include "IO.h"
00072 #include "Assembler.h"
00073 
00074 #define ADC_ns          AnalogToDigital
00075 #define ADC_struct      AnalogToDigital::ConversionCompleteInterrupt
00076 
00077 
00078 /**********************************************************************************************************************\
00079 
00080         Analog to digital converter
00081 
00082 \**********************************************************************************************************************/
00083 
00084 namespace AVRCpp
00085 {
00086         namespace AnalogToDigital
00087         {
00088                 typedef _ADC Result;
00089                 
00090 #if __ADC_PROPERTIES__ & __ADC_INTERNAL_REF1__
00091 
00092                 enum Reference
00093                 {
00094                         AREF            = 0x00,
00095                         AVCC            = _REFS0,
00096                         Internal1V      = _REFS1,
00097                         InternalRef     = _REFS1 | _REFS0
00098                         
00099                 }; // enum Reference
00100 
00101 #else // if __ADC_PROPERTIES__ & __ADC_INTERNAL_REF1__
00102 
00103                 enum Reference
00104                 {
00105                         AREF            = 0x00,
00106                         AVCC            = _REFS0,
00107                         InternalRef     = _REFS1 | _REFS0
00108                         
00109                 }; // enum Reference
00110 
00111 #endif // if __ADC_PROPERTIES__ & __ADC_INTERNAL_REF1__
00112 
00113                 
00114                 enum Adjustment
00115                 {
00116                         AlignRight      = 0x00,
00117                         AlignLeft       = _ADLAR
00118                         
00119                 }; // enum Adjustment
00120                 
00121                 enum InterruptEnableFlag
00122                 {
00123                         InterruptEnable = _ADIE,
00124                         InterruptDisable = 0x00
00125                         
00126                 }; // enum InterruptEnableFlag
00127                 
00128                 enum EnableFlag
00129                 {
00130                         ADCEnable       = _ADEN,
00131                         ADCDisable      = 0x00
00132                         
00133                 }; // enum EnableFlag
00134                 
00135                 enum ConversionStart
00136                 {
00137                         StartNow        = _ADSC,
00138                         StartLater      = 0x00
00139                         
00140                 }; // enum ConversionStart
00141                 
00142                 enum FreeRunning
00143                 {
00144                         FreeRunMode             = 0x20,
00145                         FreeRunStopped  = 0x00
00146                         
00147                 }; // enum FreeRunning
00148                 
00149                 enum Prescaler
00150                 {
00151                         Div2    = 0x01,
00152                         Div4    = 0x02,
00153                         Div8    = 0x03,
00154                         Div16   = 0x04,
00155                         Div32   = 0x05,
00156                         Div64   = 0x06,
00157                         Div128  = 0x07
00158                         
00159                 }; // enum Prescaler
00160                 
00161 
00162 #if __ADC_PROPERTIES__ & __ADC_DIFFERENTIAL__
00163                 
00164                 enum AnalogChannel
00165                 {
00166                         ADC0            = 0x00,
00167                         ADC1            = 0x01,
00168                         ADC2            = 0x02,
00169                         ADC3            = 0x03,
00170                         ADC4            = 0x04,
00171                         ADC5            = 0x05,
00172                         ADC6            = 0x06,
00173                         ADC7            = 0x07,
00174                         Vbandgap        = 0x1E,
00175                         GND                     = 0x1F
00176                         
00177                 }; // enum AnalogChannel
00178 
00179                 enum Gain
00180                 {
00181                         Gain1x          = 0x10,
00182                         Gain10x         = 0x08,
00183                         Gain200x        = 0x0a
00184 
00185                 }; // enum Gain
00186                 
00187 #else // if __ADC_PROPERTIES__ & __ADC_DIFFERENTIAL__
00188                 
00189                 enum AnalogChannel
00190                 {
00191                         ADC0            = 0x00,
00192                         ADC1            = 0x01,
00193                         ADC2            = 0x02,
00194                         ADC3            = 0x03,
00195                         ADC4            = 0x04,
00196                         ADC5            = 0x05,
00197                         ADC6            = 0x06,
00198                         ADC7            = 0x07,
00199                         Vbandgap        = 0x0E,
00200                         GND                     = 0x0F
00201 
00202                 }; // enum AnalogChannel
00203                 
00204 #endif // if __ADC_PROPERTIES__ & __ADC_DIFFERENTIAL__
00205                 
00206                 
00207 #if __ADC_PROPERTIES__ & __ADC_AUTO_TRIGGER__
00208                 
00209                 enum AutoTriggerSource
00210                 {
00211                         FreeRunningMode                         = 0x00,
00212                         AnalogComparator                        = 0x01,
00213                         ExternalInterruptRequest0       = 0x02,
00214                         Counter0CompareMatch            = 0x03,
00215                         Counter0Overflow                        = 0x04,
00216                         Counter1CompareMatchB           = 0x05,
00217                         Counter1Overflow                        = 0x06,
00218                         Counter1CaptureEvent            = 0x07
00219                         
00220                 }; // enum AutoTriggerSource
00221 
00222 #endif
00223                 
00224                 struct ConversionCompleteInterrupt : Interrupt<Bits<_ADCSRA, _ADIE>, Bits<_ADCSRA, _ADIF> > { __INTERRUPT_HANDLER_SUPPORT__ };
00225                 
00226                 inline bool IsConverting() { return IsBitsSet<_ADCSRA>(_ADSC); }
00227                 inline void WaitWhileConverting() { while (IsConverting() ); }
00228                 
00229                 inline void EnsureChangeSafety()
00230                 {
00231                         if (IsConverting() )
00232                                 Assembler::NOP();
00233                         
00234                 } // EnsureChangeSafety
00235                 
00236                 
00237                 inline void SelectionSetUp (
00238                                 Reference reference,
00239                                 Adjustment adjustment,
00240                                 AnalogChannel channel )
00241                 {
00242                         EnsureChangeSafety();
00243                         ADMUX = reference | adjustment | channel;
00244                         
00245                 } // SelectionSetUp
00246                 
00247                 
00248                 inline void ControlSetUp (
00249                                 EnableFlag enableFlag,
00250                                 ConversionStart conversionStart,
00251                                 FreeRunning freeRunning,
00252                                 InterruptEnableFlag interruptEnableFlag,
00253                                 Prescaler prescaler )
00254                 {
00255                         ADCSRA = enableFlag
00256                                         | conversionStart
00257                                         | freeRunning
00258                                         | interruptEnableFlag
00259                                         | prescaler;
00260                         
00261                 } // ControlSetUp
00262                 
00263                 
00264                 inline void SetChannel(AnalogChannel channel)
00265                 {
00266                         EnsureChangeSafety();
00267                         ChangeBits<_ADMUX>(_MUX0 | _MUX1 | _MUX2 | _MUX3, channel);
00268                         
00269                 } // SetChannel
00270                 
00271                 
00272                 inline void SelectPrescaler(Prescaler prescaler)
00273                 {
00274                         ChangeBits<_ADCSRA>(_ADPS0 | _ADPS1 | _ADPS2, prescaler);
00275                         
00276                 } // SelectPrescaler
00277 
00278                 
00279                 inline void Enable() { SetBits<_ADCSRA>(_ADEN); }
00280                 inline void Disable() { ClearBits<_ADCSRA>(_ADEN); }
00281                 inline bool IsEnabled() { return IsBitsSet<_ADCSRA>(_ADEN); }
00282                 
00283                 inline void StartConversion() { SetBits<_ADCSRA>(_ADSC); }              
00284                 
00285 #if __ADC_PROPERTIES__ & __ADC_HIGH_SPEED__
00286                 
00287                 inline void EnableHighSpeedMode() { SetBits<_ADCSRB>(_ADHSM); }
00288                 inline void DisableHighSpeedMode() { ClearBits<_ADCSRB>(_ADHSM); }
00289                 inline bool IsHighSpeedModeEnabled() { return IsBitsSet<_ADCSRB>(_ADHSM); }
00290                 
00291 #endif // if __ADC_PROPERTIES__ & __ADC_HIGH_SPEED__
00292                 
00293                 
00294 #if __ADC_PROPERTIES__ & __ADC_DIFFERENTIAL__
00295                 
00296                 inline void SetDifferentialChannels(AnalogChannel positive, AnalogChannel negative, Gain gain)
00297                 {
00298                         if(gain == Gain1x)
00299                         {
00300                                 ChangeBits<_ADMUX>(_MUX0 | _MUX1 | _MUX2 | _MUX3 | _MUX4, gain | positive | ((negative << 2) & 0x08));
00301                         }
00302                         else
00303                         {
00304                                 ChangeBits<_ADMUX>(_MUX0 | _MUX1 | _MUX2 | _MUX3, gain | (negative << 1) | (positive & 0x01));
00305                         }
00306                         
00307                 } // SetDifferentialChannels
00308                 
00309 #endif // if __ADC_PROPERTIES__ & __ADC_DIFFERENTIAL__
00310                 
00311                 
00312 #if __ADC_PROPERTIES__ & __ADC_AUTO_TRIGGER__
00313                 
00314                 inline void EnableAutoTrigger() { SetBits<_ADCSRA>(_ADATE); }
00315                 inline void DisableAutoTrigger() { ClearBits<_ADCSRA>(_ADATE); }
00316                 inline bool IsAutoTriggerEnabled() { return IsBitsSet<_ADCSRA>(_ADATE); }
00317                 
00318                 inline void SetAutoTriggerSource(AutoTriggerSource source) { ChangeBits<_ADCSRB>(_ADTS2 | _ADTS1 | _ADTS0, source); }
00319                 
00320                 inline void EnableFreeRun() { EnableAutoTrigger(); SetAutoTriggerSource(FreeRunningMode); }
00321                 inline void DisableFreeRun() { DisableAutoTrigger(); }
00322                 inline bool IsFreeRunEnabled() { return !IsBitsSet<_ADCSRB>(_ADTS2 | _ADTS1 | _ADTS0); }
00323                 
00324                 inline void StartFreeRun() { EnableFreeRun(); EnableAutoTrigger(); }
00325                 inline void StopFreeRun() { DisableFreeRun(); }
00326                 
00327 #else // if __ADC_PROPERTIES__ & __ADC_AUTO_TRIGGER__
00328                 
00329                 inline void EnableFreeRun() { SetBits<_ADCSRA>(_ADFR); }
00330                 inline void DisableFreeRun() { ClearBits<_ADCSRA>(_ADFR); }
00331                 inline bool IsFreeRunEnabled() { return IsBitsSet<_ADCSRA>(_ADFR); }
00332                 
00333                 inline void StartFreeRun() { SetBits<_ADCSRA>(_ADSC | _ADFR); }
00334                 inline void StopFreeRun() { DisableFreeRun(); }
00335                 
00336 #endif // if __ADC_PROPERTIES__ & __ADC_AUTO_TRIGGER__
00337                 
00338                 
00339 #if __ADC_PROPERTIES__ & __ADC_DIGITAL_INPUT__
00340                 
00347                 inline void DigitalInputDisable(uint8_t pins) { SetBits<_DIDR0>(pins); }
00349                 inline void DigitalInputEnable(uint8_t pins) { ClearBits<_DIDR0>(pins); }
00351                 inline void SetDigitalInput(uint8_t pins) { DIDR0 = ~pins; }
00352                 
00353 #endif // if __ADC_PROPERTIES__ & __ADC_DIGITAL_INPUT__
00354                 
00355         } // namespace AnalogToDigital
00356         
00357 } // namespace AVRCpp
00358 
00359 /**********************************************************************************************************************/
00360 
00361 #endif // ifdef __ADC_PROPERTIES__
00362 
00363 #endif // ifndef __AVR_CPP_ADC_H__

Generated on Sat Sep 15 23:40:05 2007 for AVR C++ Lib for AT90USB1287 by  doxygen 1.5.2
SourceForge.net Logo MTÜ TTÜ Robotiklubi