ADC_AVR8.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2012.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaim all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. * \brief ADC Peripheral Driver (AVR8)
  28. *
  29. * On-chip Analogue-to-Digital converter (ADC) driver for supported U4, U6 and U7 model AVRs that contain an ADC
  30. * peripheral internally.
  31. *
  32. * \note This file should not be included directly. It is automatically included as needed by the ADC driver
  33. * dispatch header located in LUFA/Drivers/Peripheral/ADC.h.
  34. */
  35. /** \ingroup Group_ADC
  36. * \defgroup Group_ADC_AVR8 ADC Peripheral Driver (AVR8)
  37. *
  38. * \section Sec_ModDescription Module Description
  39. * On-chip Analogue-to-Digital converter (ADC) driver for supported U4, U6 and U7 model AVRs that contain an ADC
  40. * peripheral internally.
  41. *
  42. * \note This file should not be included directly. It is automatically included as needed by the ADC driver
  43. * dispatch header located in LUFA/Drivers/Peripheral/ADC.h.
  44. *
  45. * \section Sec_ExampleUsage Example Usage
  46. * The following snippet is an example of how this module may be used within a typical
  47. * application.
  48. *
  49. * \code
  50. * // Initialize the ADC driver before first use
  51. * ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32);
  52. *
  53. * // Must setup the ADC channel to read beforehand
  54. * ADC_SetupChannel(1);
  55. *
  56. * // Perform a single conversion of the ADC channel 1
  57. * ADC_GetChannelReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ADC_CHANNEL1);
  58. * printf("Conversion Result: %d\r\n", ADC_GetResult());
  59. *
  60. * // Start reading ADC channel 1 in free running (continuous conversion) mode
  61. * ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ADC_CHANNEL1);
  62. * for (;;)
  63. * {
  64. * while (!(ADC_IsReadingComplete())) {};
  65. * printf("Conversion Result: %d\r\n", ADC_GetResult());
  66. * }
  67. * \endcode
  68. *
  69. * @{
  70. */
  71. #ifndef __ADC_AVR8_H__
  72. #define __ADC_AVR8_H__
  73. /* Includes: */
  74. #include "../../../Common/Common.h"
  75. /* Enable C linkage for C++ Compilers: */
  76. #if defined(__cplusplus)
  77. extern "C" {
  78. #endif
  79. /* Preprocessor Checks: */
  80. #if !defined(__INCLUDE_FROM_ADC_H)
  81. #error Do not include this file directly. Include LUFA/Drivers/Peripheral/ADC.h instead.
  82. #endif
  83. #if !(defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || \
  84. defined(__AVR_AT90USB1287__) || defined(__AVR_AT90USB647__) || \
  85. defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || \
  86. defined(__AVR_ATmega32U6__))
  87. #error The ADC peripheral driver is not currently available for your selected microcontroller model.
  88. #endif
  89. /* Private Interface - For use in library only: */
  90. #if !defined(__DOXYGEN__)
  91. /* Macros: */
  92. #define _ADC_GET_MUX_MASK2(y) ADC_CHANNEL ## y
  93. #define _ADC_GET_MUX_MASK(y) _ADC_GET_MUX_MASK2(y)
  94. #endif
  95. /* Public Interface - May be used in end-application: */
  96. /* Macros: */
  97. /** \name ADC Reference Configuration Masks */
  98. //@{
  99. /** Reference mask, for using the voltage present at the AVR's AREF pin for the ADC reference. */
  100. #define ADC_REFERENCE_AREF 0
  101. /** Reference mask, for using the voltage present at the AVR's AVCC pin for the ADC reference. */
  102. #define ADC_REFERENCE_AVCC (1 << REFS0)
  103. /** Reference mask, for using the internally generated 2.56V reference voltage as the ADC reference. */
  104. #define ADC_REFERENCE_INT2560MV ((1 << REFS1) | (1 << REFS0))
  105. //@}
  106. /** \name ADC Result Adjustment Configuration Masks */
  107. //@{
  108. /** Left-adjusts the 10-bit ADC result, so that the upper 8 bits of the value returned by the
  109. * \ref ADC_GetResult() macro contain the 8 most significant bits of the result.
  110. */
  111. #define ADC_LEFT_ADJUSTED (1 << ADLAR)
  112. /** Right-adjusts the 10-bit ADC result, so that the lower 8 bits of the value returned by the
  113. * \ref ADC_GetResult() macro contain the 8 least significant bits of the result.
  114. */
  115. #define ADC_RIGHT_ADJUSTED (0 << ADLAR)
  116. //@}
  117. /** \name ADC Mode Configuration Masks */
  118. //@{
  119. /** Sets the ADC mode to free running, so that conversions take place continuously as fast as the ADC
  120. * is capable of at the given input clock speed.
  121. */
  122. #define ADC_FREE_RUNNING (1 << ADATE)
  123. /** Sets the ADC mode to single conversion, so that only a single conversion will take place before
  124. * the ADC returns to idle.
  125. */
  126. #define ADC_SINGLE_CONVERSION (0 << ADATE)
  127. //@}
  128. /** \name ADC Prescaler Configuration Masks */
  129. //@{
  130. /** Sets the ADC input clock to prescale by a factor of 2 the AVR's system clock. */
  131. #define ADC_PRESCALE_2 (1 << ADPS0)
  132. /** Sets the ADC input clock to prescale by a factor of 4 the AVR's system clock. */
  133. #define ADC_PRESCALE_4 (1 << ADPS1)
  134. /** Sets the ADC input clock to prescale by a factor of 8 the AVR's system clock. */
  135. #define ADC_PRESCALE_8 ((1 << ADPS0) | (1 << ADPS1))
  136. /** Sets the ADC input clock to prescale by a factor of 16 the AVR's system clock. */
  137. #define ADC_PRESCALE_16 (1 << ADPS2)
  138. /** Sets the ADC input clock to prescale by a factor of 32 the AVR's system clock. */
  139. #define ADC_PRESCALE_32 ((1 << ADPS2) | (1 << ADPS0))
  140. /** Sets the ADC input clock to prescale by a factor of 64 the AVR's system clock. */
  141. #define ADC_PRESCALE_64 ((1 << ADPS2) | (1 << ADPS1))
  142. /** Sets the ADC input clock to prescale by a factor of 128 the AVR's system clock. */
  143. #define ADC_PRESCALE_128 ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0))
  144. //@}
  145. /** \name ADC MUX Masks */
  146. //@{
  147. /** MUX mask define for the ADC0 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading(). */
  148. #define ADC_CHANNEL0 (0x00 << MUX0)
  149. /** MUX mask define for the ADC1 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading(). */
  150. #define ADC_CHANNEL1 (0x01 << MUX0)
  151. #if (!(defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) || defined(__DOXYGEN__))
  152. /** MUX mask define for the ADC2 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading().
  153. *
  154. * \note Not available on all AVR models.
  155. */
  156. #define ADC_CHANNEL2 (0x02 << MUX0)
  157. /** MUX mask define for the ADC3 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading().
  158. *
  159. * \note Not available on all AVR models.
  160. */
  161. #define ADC_CHANNEL3 (0x03 << MUX0)
  162. #endif
  163. /** MUX mask define for the ADC4 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading(). */
  164. #define ADC_CHANNEL4 (0x04 << MUX0)
  165. /** MUX mask define for the ADC5 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading(). */
  166. #define ADC_CHANNEL5 (0x05 << MUX0)
  167. /** MUX mask define for the ADC6 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading(). */
  168. #define ADC_CHANNEL6 (0x06 << MUX0)
  169. /** MUX mask define for the ADC7 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */
  170. #define ADC_CHANNEL7 (0x07 << MUX0)
  171. #if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__DOXYGEN__))
  172. /** MUX mask define for the ADC8 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading().
  173. *
  174. * \note Not available on all AVR models.
  175. */
  176. #define ADC_CHANNEL8 ((1 << 8) | (0x00 << MUX0))
  177. /** MUX mask define for the ADC9 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading().
  178. *
  179. * \note Not available on all AVR models.
  180. */
  181. #define ADC_CHANNEL9 ((1 << 8) | (0x01 << MUX0))
  182. /** MUX mask define for the ADC10 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading().
  183. *
  184. * \note Not available on all AVR models.
  185. */
  186. #define ADC_CHANNEL10 ((1 << 8) | (0x02 << MUX0))
  187. /** MUX mask define for the ADC11 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading().
  188. *
  189. * \note Not available on all AVR models.
  190. */
  191. #define ADC_CHANNEL11 ((1 << 8) | (0x03 << MUX0))
  192. /** MUX mask define for the ADC12 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading().
  193. *
  194. * \note Not available on all AVR models.
  195. */
  196. #define ADC_CHANNEL12 ((1 << 8) | (0x04 << MUX0))
  197. /** MUX mask define for the ADC13 channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading().
  198. *
  199. * \note Not available on all AVR models.
  200. */
  201. #define ADC_CHANNEL13 ((1 << 8) | (0x05 << MUX0))
  202. /** MUX mask define for the internal temperature sensor channel of the ADC. See \ref ADC_StartReading() and
  203. * \ref ADC_GetChannelReading().
  204. *
  205. * \note Not available on all AVR models.
  206. */
  207. #define ADC_INT_TEMP_SENS ((1 << 8) | (0x07 << MUX0))
  208. #endif
  209. /** MUX mask define for the internal 1.1V band-gap channel of the ADC. See \ref ADC_StartReading() and \ref ADC_GetChannelReading(). */
  210. #define ADC_1100MV_BANDGAP (0x1E << MUX0)
  211. /** Retrieves the ADC MUX mask for the given ADC channel number.
  212. *
  213. * \attention This macro will only work correctly on channel numbers that are compile-time
  214. * constants defined by the preprocessor.
  215. *
  216. * \param[in] Channel Index of the ADC channel whose MUX mask is to be retrieved.
  217. */
  218. #define ADC_GET_CHANNEL_MASK(Channel) _ADC_GET_MUX_MASK(Channel)
  219. //@}
  220. /* Inline Functions: */
  221. /** Configures the given ADC channel, ready for ADC conversions. This function sets the
  222. * associated port pin as an input and disables the digital portion of the I/O to reduce
  223. * power consumption.
  224. *
  225. * \note This must only be called for ADC channels with are connected to a physical port
  226. * pin of the AVR, denoted by its special alternative function ADCx.
  227. *
  228. * \warning The channel number must be specified as an integer, and <b>not</b> a \c ADC_CHANNEL* mask.
  229. *
  230. * \param[in] ChannelIndex ADC channel number to set up for conversions.
  231. */
  232. static inline void ADC_SetupChannel(const uint8_t ChannelIndex)
  233. {
  234. #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || \
  235. defined(__AVR_AT90USB1287__) || defined(__AVR_AT90USB647__) || \
  236. defined(__AVR_ATmega32U6__))
  237. DDRF &= ~(1 << ChannelIndex);
  238. DIDR0 |= (1 << ChannelIndex);
  239. #elif (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
  240. if (ChannelIndex < 8)
  241. {
  242. DDRF &= ~(1 << ChannelIndex);
  243. DIDR0 |= (1 << ChannelIndex);
  244. }
  245. else if (ChannelIndex == 8)
  246. {
  247. DDRD &= ~(1 << 4);
  248. DIDR2 |= (1 << 0);
  249. }
  250. else if (ChannelIndex < 11)
  251. {
  252. DDRD &= ~(1 << (ChannelIndex - 3));
  253. DIDR2 |= (1 << (ChannelIndex - 8));
  254. }
  255. else
  256. {
  257. DDRB &= ~(1 << (ChannelIndex - 7));
  258. DIDR2 |= (1 << (ChannelIndex - 8));
  259. }
  260. #endif
  261. }
  262. /** De-configures the given ADC channel, re-enabling digital I/O mode instead of analog. This
  263. * function sets the associated port pin as an input and re-enabled the digital portion of
  264. * the I/O.
  265. *
  266. * \note This must only be called for ADC channels with are connected to a physical port
  267. * pin of the AVR, denoted by its special alternative function ADCx.
  268. *
  269. * \warning The channel number must be specified as an integer, and <b>not</b> a \c ADC_CHANNEL* mask.
  270. *
  271. * \param[in] ChannelIndex ADC channel number to set up for conversions.
  272. */
  273. static inline void ADC_DisableChannel(const uint8_t ChannelIndex)
  274. {
  275. #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || \
  276. defined(__AVR_AT90USB1287__) || defined(__AVR_AT90USB647__) || \
  277. defined(__AVR_ATmega32U6__))
  278. DDRF &= ~(1 << ChannelIndex);
  279. DIDR0 &= ~(1 << ChannelIndex);
  280. #elif (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
  281. if (ChannelIndex < 8)
  282. {
  283. DDRF &= ~(1 << ChannelIndex);
  284. DIDR0 &= ~(1 << ChannelIndex);
  285. }
  286. else if (ChannelIndex == 8)
  287. {
  288. DDRD &= ~(1 << 4);
  289. DIDR2 &= ~(1 << 0);
  290. }
  291. else if (ChannelIndex < 11)
  292. {
  293. DDRD &= ~(1 << (ChannelIndex - 3));
  294. DIDR2 &= ~(1 << (ChannelIndex - 8));
  295. }
  296. else
  297. {
  298. DDRB &= ~(1 << (ChannelIndex - 7));
  299. DIDR2 &= ~(1 << (ChannelIndex - 8));
  300. }
  301. #endif
  302. }
  303. /** Starts the reading of the given channel, but does not wait until the conversion has completed.
  304. * Once executed, the conversion status can be determined via the \ref ADC_IsReadingComplete() macro and
  305. * the result read via the \ref ADC_GetResult() macro.
  306. *
  307. * If the ADC has been initialized in free running mode, calling this function once will begin the repeated
  308. * conversions. If the ADC is in single conversion mode (or the channel to convert from is to be changed),
  309. * this function must be called each time a conversion is to take place.
  310. *
  311. * \param[in] MUXMask ADC channel mask, reference mask and adjustment mask.
  312. */
  313. static inline void ADC_StartReading(const uint16_t MUXMask)
  314. {
  315. ADMUX = MUXMask;
  316. #if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__DOXYGEN__))
  317. if (MUXMask & (1 << 8))
  318. ADCSRB |= (1 << MUX5);
  319. else
  320. ADCSRB &= ~(1 << MUX5);
  321. #endif
  322. ADCSRA |= (1 << ADSC);
  323. }
  324. /** Indicates if the current ADC conversion is completed, or still in progress.
  325. *
  326. * \return Boolean false if the reading is still taking place, or true if the conversion is
  327. * complete and ready to be read out with \ref ADC_GetResult().
  328. */
  329. static inline bool ADC_IsReadingComplete(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  330. static inline bool ADC_IsReadingComplete(void)
  331. {
  332. return ((ADCSRA & (1 << ADIF)) ? true : false);
  333. }
  334. /** Retrieves the conversion value of the last completed ADC conversion and clears the reading
  335. * completion flag.
  336. *
  337. * \return The result of the last ADC conversion as an unsigned value.
  338. */
  339. static inline uint16_t ADC_GetResult(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  340. static inline uint16_t ADC_GetResult(void)
  341. {
  342. ADCSRA |= (1 << ADIF);
  343. return ADC;
  344. }
  345. /** Performs a complete single reading from channel, including a polling spin-loop to wait for the
  346. * conversion to complete, and the returning of the converted value.
  347. *
  348. * \note For free running mode, the automated conversions should be initialized with a single call
  349. * to \ref ADC_StartReading() to select the channel and begin the automated conversions, and
  350. * the results read directly from the \ref ADC_GetResult() instead to reduce overhead.
  351. *
  352. * \param[in] MUXMask Mask comprising of an ADC channel mask, reference mask and adjustment mask.
  353. *
  354. * \return Converted ADC result for the given ADC channel.
  355. */
  356. static inline uint16_t ADC_GetChannelReading(const uint16_t MUXMask) ATTR_WARN_UNUSED_RESULT;
  357. static inline uint16_t ADC_GetChannelReading(const uint16_t MUXMask)
  358. {
  359. ADC_StartReading(MUXMask);
  360. while (!(ADC_IsReadingComplete()));
  361. return ADC_GetResult();
  362. }
  363. /** Initializes the ADC, ready for conversions. This must be called before any other ADC operations.
  364. * The "mode" parameter should be a mask comprised of a conversion mode (free running or single) and
  365. * prescaler masks.
  366. *
  367. * \param[in] Mode Mask of ADC prescale and mode settings.
  368. */
  369. static inline void ADC_Init(const uint8_t Mode) ATTR_ALWAYS_INLINE;
  370. static inline void ADC_Init(const uint8_t Mode)
  371. {
  372. ADCSRA = ((1 << ADEN) | Mode);
  373. }
  374. /** Turns off the ADC. If this is called, any further ADC operations will require a call to
  375. * \ref ADC_Init() before the ADC can be used again.
  376. */
  377. static inline void ADC_Disable(void) ATTR_ALWAYS_INLINE;
  378. static inline void ADC_Disable(void)
  379. {
  380. ADCSRA = 0;
  381. }
  382. /** Indicates if the ADC is currently enabled.
  383. *
  384. * \return Boolean \c true if the ADC subsystem is currently enabled, \c false otherwise.
  385. */
  386. static inline bool ADC_GetStatus(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  387. static inline bool ADC_GetStatus(void)
  388. {
  389. return ((ADCSRA & (1 << ADEN)) ? true : false);
  390. }
  391. /* Disable C linkage for C++ Compilers: */
  392. #if defined(__cplusplus)
  393. }
  394. #endif
  395. #endif
  396. /** @} */