adc.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2004 by Ole Reinhardt <ole.reinhardt@kernelconcepts.de>,
  3. * Kernelconcepts http://www.kernelconcepts.de
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. 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 EGNITE SOFTWARE GMBH AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
  22. * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  26. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * For additional information see http://www.ethernut.de/
  32. *
  33. */
  34. /*!
  35. * \file include/dev/adc.h
  36. * \brief Header for AVR Adc driver
  37. */
  38. /*!
  39. * \addtogroup xgAvrAdc
  40. */
  41. /*@{*/
  42. #ifndef _ADC_H_
  43. #define _ADC_H_
  44. // ENUM declaring possible ADC reference voltages
  45. // - AVCC = 5V
  46. // - AREF = External reference
  47. // - INTERNAL_256 = 2.56V
  48. enum adc_ref_type
  49. {
  50. AVCC = 0,
  51. AREF,
  52. INTERNAL_256
  53. };
  54. typedef enum adc_ref_type adc_ref_t;
  55. // ENUM declaring possible ADC modes
  56. // FREE_RUNNING:
  57. // Free-running mode. Samples continuously taken
  58. // every 13 cycles of ADC clock after
  59. // ADC_start_conversion() is called.
  60. // SINGLE_CONVERSION:
  61. // Single-conversion mode. One sample taken every time
  62. // ADC_start_conversion() is called.
  63. enum adc_mode_type
  64. {
  65. ADC_OFF,
  66. FREE_RUNNING,
  67. SINGLE_CONVERSION
  68. };
  69. typedef enum adc_mode_type adc_mode_t;
  70. // ENUM declaring possible ADC channels
  71. enum adc_channel_type
  72. {
  73. ADC0=0,
  74. ADC1=1,
  75. ADC2=2,
  76. ADC3=3,
  77. ADC4=4,
  78. ADC5=5,
  79. ADC6=6,
  80. ADC7=7
  81. };
  82. typedef enum adc_channel_type adc_channel_t;
  83. #define ADC_PRESCALE_DIV2 0x00 ///< 0x01,0x00 -> CPU clk/2
  84. #define ADC_PRESCALE_DIV4 0x02 ///< 0x02 -> CPU clk/4
  85. #define ADC_PRESCALE_DIV8 0x03 ///< 0x03 -> CPU clk/8
  86. #define ADC_PRESCALE_DIV16 0x04 ///< 0x04 -> CPU clk/16
  87. #define ADC_PRESCALE_DIV32 0x05 ///< 0x05 -> CPU clk/32
  88. #define ADC_PRESCALE_DIV64 0x06 ///< 0x06 -> CPU clk/64
  89. #define ADC_PRESCALE_DIV128 0x07 ///< 0x07 -> CPU clk/128
  90. // ADC_Init
  91. //
  92. // This function initializes the ADC based on the
  93. // #defines in config.h
  94. //
  95. // post: ADC initialized and primed for call to
  96. // start_conversion
  97. void ADCInit(void);
  98. // ADC_SetRef
  99. //
  100. // Allows setting of reference voltage for ADC.
  101. //
  102. // NOTE: This function stops ADC conversion. One must
  103. // call ADC_start_conversion to restart the ADC.
  104. //
  105. // pre: "reference" is a valid ADC reference from the
  106. // choices given above
  107. // post: ADC conversion stopped and reference voltage
  108. // set to desired choice
  109. void ADCSetRef(adc_ref_t reference);
  110. // ADC_SetMode
  111. //
  112. // Allows setting of ADC conversion mode: either
  113. // single-conversion or free-running.
  114. //
  115. // NOTE: This function stops ADC conversion. One must
  116. // call ADC_start_conversion to restart the ADC.
  117. //
  118. // pre: "mode" is a valid ADC reference from the
  119. // choices given above
  120. // post: ADC conversion stopped and the ADC mode is
  121. // set to desired choice
  122. void ADCSetMode(adc_mode_t mode);
  123. // ADC_SetPrescale
  124. //
  125. // Allows setting of ADC clock prescalar (ADC rate).
  126. // The ADC rate is given by the system clock rate
  127. // divided by the prescalar value. Possible prescalar
  128. // values range from 2-128 in powers of 2 (2,4,8,etc.)
  129. //
  130. // NOTE: This function stops ADC conversion. One must
  131. // call ADC_start_conversion to restart the ADC.
  132. //
  133. // pre: "prescalar" is a valid ADC reference from the
  134. // choices given above
  135. // post: ADC conversion stopped and ADC prescalar
  136. // set to desired choice
  137. u_char ADCSetPrescale(u_char prescalar);
  138. // ADC_SetChannel
  139. //
  140. // Sets the channel that the ADC reads. The ADC
  141. // may only read from one channel at a time.
  142. //
  143. // pre: "adc_channel" is a valid ADC reference from the
  144. // choices given above
  145. // post: ADC conversion stopped and ADC channel
  146. // set to desired choice
  147. void ADCSetChannel(adc_channel_t adc_channel);
  148. // ADC_BufferFlush
  149. //
  150. // Flushes the local buffer used to store ADC values
  151. // between conversion and the user's call to ADC_read
  152. //
  153. // NOTE: It is recommended that one calls buffer flush
  154. // if any changes are made to the ADC's state.
  155. //
  156. // pre: none
  157. // post: Local ADC buffer has been flushed
  158. void ADCBufferFlush(void);
  159. // ADC_StartConversion
  160. //
  161. // Begins ADC conversion. If in single-conversion mode,
  162. // this function will only convert one value. If in
  163. // free-running mode, this function will begin
  164. // continuous conversion at the rate set by the
  165. // prescalar (see ADC_set_prescalar).
  166. //
  167. // NOTE: Converted values from the ADC are stored
  168. // in a local buffer. The user must call
  169. // ADC_read to obtain these values.
  170. //
  171. // pre: none
  172. // post: The ADC has started conversion. Completion of
  173. // any conversions is not guaranteed.
  174. void ADCStartConversion(void);
  175. // ADC_StartLowNoiseConversion
  176. //
  177. // Set Conversion Mode to SINGLE_CONVERSION, Enters
  178. // adc sleep mode and wait until conversion interrupt occurs.
  179. // CPU will go to sleep mode!!!
  180. // BE AWARE OF WHAT IS WRITTEN IN THE AVR DATASHEET
  181. //
  182. // NOTE: Converted values from the ADC are stored
  183. // in a local buffer. The user must call
  184. // ADC_read to obtain these values.
  185. //
  186. // Only implemented for avr_gcc. Any other architecture
  187. // and compiler will use normal conversion
  188. // pre: none
  189. // post: The ADC has started conversion. Completion of
  190. // any conversions is not guaranteed.
  191. void ADCStartLowNoiseConversion(void);
  192. // ADC_StopConversion
  193. //
  194. // Stops ADC conversion if ADC is in free-running
  195. // mode. Has no effect if ADC is in single-conversion
  196. // mode.
  197. //
  198. // pre: none
  199. // post: ADC conversion has been stopped.
  200. void ADCStopConversion(void);
  201. // ADC_read
  202. //
  203. // Reads ADC values from local buffer. Reads one ADC
  204. // conversion value at a time.
  205. //
  206. // return: 0 = OK
  207. // 1 = No ADC value to read. "value" is invalid
  208. //
  209. // pre: "value" points to a valid variable.
  210. // post: If no errors, one ADC conversion value has
  211. // been read and placed in the variable pointed
  212. // to by "value"
  213. u_char ADCRead(u_short *value);
  214. // ADC_GetMode
  215. // returns current conversion mode
  216. inline adc_mode_t ADCGetMode(void);
  217. #endif
  218. /*@}*/