hal_adc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**************************************************************************************************
  2. Filename: hal_adc.c
  3. Revised: $Date: 2010-03-12 16:10:36 -0800 (Fri, 12 Mar 2010) $
  4. Revision: $Revision: 21910 $
  5. Description: This file contains the interface to the HAL ADC.
  6. Copyright 2006-2010 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /**************************************************************************************************
  34. * INCLUDES
  35. **************************************************************************************************/
  36. #include "hal_adc.h"
  37. #include "hal_defs.h"
  38. #include "hal_mcu.h"
  39. #include "hal_types.h"
  40. /**************************************************************************************************
  41. * CONSTANTS
  42. **************************************************************************************************/
  43. #define HAL_ADC_EOC 0x80 /* End of Conversion bit */
  44. #define HAL_ADC_START 0x40 /* Starts Conversion */
  45. #define HAL_ADC_STSEL_EXT 0x00 /* External Trigger */
  46. #define HAL_ADC_STSEL_FULL 0x10 /* Full Speed, No Trigger */
  47. #define HAL_ADC_STSEL_T1C0 0x20 /* Timer1, Channel 0 Compare Event Trigger */
  48. #define HAL_ADC_STSEL_ST 0x30 /* ADCCON1.ST =1 Trigger */
  49. #define HAL_ADC_RAND_NORM 0x00 /* Normal Operation */
  50. #define HAL_ADC_RAND_LFSR 0x04 /* Clock LFSR */
  51. #define HAL_ADC_RAND_SEED 0x08 /* Seed Modulator */
  52. #define HAL_ADC_RAND_STOP 0x0c /* Stop Random Generator */
  53. #define HAL_ADC_RAND_BITS 0x0c /* Bits [3:2] */
  54. #define HAL_ADC_DEC_064 0x00 /* Decimate by 64 : 8-bit resolution */
  55. #define HAL_ADC_DEC_128 0x10 /* Decimate by 128 : 10-bit resolution */
  56. #define HAL_ADC_DEC_256 0x20 /* Decimate by 256 : 12-bit resolution */
  57. #define HAL_ADC_DEC_512 0x30 /* Decimate by 512 : 14-bit resolution */
  58. #define HAL_ADC_DEC_BITS 0x30 /* Bits [5:4] */
  59. #define HAL_ADC_STSEL HAL_ADC_STSEL_ST
  60. #define HAL_ADC_RAND_GEN HAL_ADC_RAND_STOP
  61. #define HAL_ADC_REF_VOLT HAL_ADC_REF_AVDD
  62. #define HAL_ADC_DEC_RATE HAL_ADC_DEC_064
  63. #define HAL_ADC_SCHN HAL_ADC_CHN_VDD3
  64. #define HAL_ADC_ECHN HAL_ADC_CHN_GND
  65. /* ------------------------------------------------------------------------------------------------
  66. * Local Variables
  67. * ------------------------------------------------------------------------------------------------
  68. */
  69. #if (HAL_ADC == TRUE)
  70. static uint8 adcRef;
  71. #endif
  72. /**************************************************************************************************
  73. * @fn HalAdcInit
  74. *
  75. * @brief Initialize ADC Service
  76. *
  77. * @param None
  78. *
  79. * @return None
  80. **************************************************************************************************/
  81. void HalAdcInit (void)
  82. {
  83. #if (HAL_ADC == TRUE)
  84. adcRef = HAL_ADC_REF_VOLT;
  85. #endif
  86. }
  87. /**************************************************************************************************
  88. * @fn HalAdcRead
  89. *
  90. * @brief Read the ADC based on given channel and resolution
  91. *
  92. * @param channel - channel where ADC will be read
  93. * @param resolution - the resolution of the value
  94. *
  95. * @return 16 bit value of the ADC in offset binary format.
  96. *
  97. * Note that the ADC is "bipolar", which means the GND (0V) level is mid-scale.
  98. * Note2: This function assumes that ADCCON3 contains the voltage reference.
  99. **************************************************************************************************/
  100. uint16 HalAdcRead (uint8 channel, uint8 resolution)
  101. {
  102. int16 reading = 0;
  103. #if (HAL_ADC == TRUE)
  104. uint8 i, resbits;
  105. uint8 adcChannel = 1;
  106. /*
  107. * If Analog input channel is AIN0..AIN7, make sure corresponing P0 I/O pin is enabled. The code
  108. * does NOT disable the pin at the end of this function. I think it is better to leave the pin
  109. * enabled because the results will be more accurate. Because of the inherent capacitance on the
  110. * pin, it takes time for the voltage on the pin to charge up to its steady-state level. If
  111. * HalAdcRead() has to turn on the pin for every conversion, the results may show a lower voltage
  112. * than actuality because the pin did not have time to fully charge.
  113. */
  114. if (channel < 8)
  115. {
  116. for (i=0; i < channel; i++)
  117. {
  118. adcChannel <<= 1;
  119. }
  120. }
  121. /* Enable channel */
  122. ADCCFG |= adcChannel;
  123. /* Convert resolution to decimation rate */
  124. switch (resolution)
  125. {
  126. case HAL_ADC_RESOLUTION_8:
  127. resbits = HAL_ADC_DEC_064;
  128. break;
  129. case HAL_ADC_RESOLUTION_10:
  130. resbits = HAL_ADC_DEC_128;
  131. break;
  132. case HAL_ADC_RESOLUTION_12:
  133. resbits = HAL_ADC_DEC_256;
  134. break;
  135. case HAL_ADC_RESOLUTION_14:
  136. default:
  137. resbits = HAL_ADC_DEC_512;
  138. break;
  139. }
  140. /* writing to this register starts the extra conversion */
  141. ADCCON3 = channel | resbits | adcRef;
  142. /* Wait for the conversion to be done */
  143. while (!(ADCCON1 & HAL_ADC_EOC));
  144. /* Disable channel after done conversion */
  145. ADCCFG &= (adcChannel ^ 0xFF);
  146. /* Read the result */
  147. reading = (int16) (ADCL);
  148. reading |= (int16) (ADCH << 8);
  149. /* Treat small negative as 0 */
  150. if (reading < 0)
  151. reading = 0;
  152. switch (resolution)
  153. {
  154. case HAL_ADC_RESOLUTION_8:
  155. reading >>= 8;
  156. break;
  157. case HAL_ADC_RESOLUTION_10:
  158. reading >>= 6;
  159. break;
  160. case HAL_ADC_RESOLUTION_12:
  161. reading >>= 4;
  162. break;
  163. case HAL_ADC_RESOLUTION_14:
  164. default:
  165. reading >>= 2;
  166. break;
  167. }
  168. #else
  169. // unused arguments
  170. (void) channel;
  171. (void) resolution;
  172. #endif
  173. return ((uint16)reading);
  174. }
  175. /**************************************************************************************************
  176. * @fn HalAdcSetReference
  177. *
  178. * @brief Sets the reference voltage for the ADC and initializes the service
  179. *
  180. * @param reference - the reference voltage to be used by the ADC
  181. *
  182. * @return none
  183. *
  184. **************************************************************************************************/
  185. void HalAdcSetReference ( uint8 reference )
  186. {
  187. #if (HAL_ADC == TRUE)
  188. adcRef = reference;
  189. #endif
  190. }
  191. /*********************************************************************
  192. * @fn HalAdcCheckVdd
  193. *
  194. * @brief Check for minimum Vdd specified.
  195. *
  196. * @param vdd - The board-specific Vdd reading to check for.
  197. *
  198. * @return TRUE if the Vdd measured is greater than the 'vdd' minimum parameter;
  199. * FALSE if not.
  200. *
  201. *********************************************************************/
  202. bool HalAdcCheckVdd(uint8 vdd)
  203. {
  204. ADCCON3 = 0x0F;
  205. while (!(ADCCON1 & 0x80));
  206. return (ADCH > vdd);
  207. }
  208. /**************************************************************************************************
  209. **************************************************************************************************/