lpc_adc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**********************************************************************
  2. * $Id$ lpc_adc.c 2011-06-02
  3. *//**
  4. * @file lpc_adc.c
  5. * @brief Contains all functions support for ADC firmware library on
  6. * LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @addtogroup ADC
  34. * @{
  35. */
  36. #ifdef __BUILD_WITH_EXAMPLE__
  37. #include "lpc_libcfg.h"
  38. #else
  39. #include "lpc_libcfg_default.h"
  40. #endif /* __BUILD_WITH_EXAMPLE__ */
  41. #ifdef _ADC
  42. /* Includes ------------------------------------------------------------------- */
  43. #include "lpc_types.h"
  44. #include "lpc_adc.h"
  45. #include "lpc_clkpwr.h"
  46. /* Public Functions ----------------------------------------------------------- */
  47. /** @addtogroup ADC_Public_Functions
  48. * @{
  49. */
  50. /*********************************************************************//**
  51. * @brief Initial for ADC
  52. * + Set bit PCADC
  53. * + Set clock for ADC
  54. * + Set Clock Frequency
  55. * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
  56. * @param[in] rate ADC conversion rate, should be <=200KHz
  57. * @return None
  58. **********************************************************************/
  59. void ADC_Init(LPC_ADC_TypeDef *ADCx, uint32_t rate)
  60. {
  61. uint32_t ADCPClk, temp, tmp;
  62. // Turn on power and clock
  63. CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCADC, ENABLE);
  64. ADCx->CR = 0;
  65. //Enable PDN bit
  66. tmp = ADC_CR_PDN;
  67. // Set clock frequency
  68. ADCPClk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
  69. /* The APB clock (PCLK_ADC0) is divided by (CLKDIV+1) to produce the clock for
  70. * A/D converter, which should be less than or equal to 12.4MHz.
  71. * A fully conversion requires 31 of these clocks.
  72. * ADC clock = PCLK_ADC0 / (CLKDIV + 1);
  73. * ADC rate = ADC clock / 31;
  74. */
  75. temp = rate * 31;
  76. temp = (ADCPClk * 2 + temp)/(2 * temp) - 1; //get the round value by fomular: (2*A + B)/(2*A)
  77. tmp |= ADC_CR_CLKDIV(temp);
  78. ADCx->CR = tmp;
  79. }
  80. /*********************************************************************//**
  81. * @brief Close ADC
  82. * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
  83. * @return None
  84. **********************************************************************/
  85. void ADC_DeInit(LPC_ADC_TypeDef *ADCx)
  86. {
  87. if (ADCx->CR & ADC_CR_START_MASK) //need to stop START bits before DeInit
  88. ADCx->CR &= ~ADC_CR_START_MASK;
  89. // Clear SEL bits
  90. ADCx->CR &= ~0xFF;
  91. // Clear PDN bit
  92. ADCx->CR &= ~ADC_CR_PDN;
  93. // Turn on power and clock
  94. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCADC, DISABLE);
  95. }
  96. /*********************************************************************//**
  97. * @brief Get Result conversion from A/D data register
  98. * @param[in] channel number which want to read back the result
  99. * @return Result of conversion
  100. *********************************************************************/
  101. uint32_t ADC_GetData(uint32_t channel)
  102. {
  103. uint32_t adc_value;
  104. adc_value = *(uint32_t *)((&LPC_ADC->DR[0]) + channel);
  105. return ADC_GDR_RESULT(adc_value);
  106. }
  107. /*********************************************************************//**
  108. * @brief Set start mode for ADC
  109. * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
  110. * @param[in] start_mode Start mode choose one of modes in
  111. * 'ADC_START_OPT' enumeration type definition, should be:
  112. * - ADC_START_CONTINUOUS
  113. * - ADC_START_NOW
  114. * - ADC_START_ON_EINT0
  115. * - ADC_START_ON_CAP01
  116. * - ADC_START_ON_MAT01
  117. * - ADC_START_ON_MAT03
  118. * - ADC_START_ON_MAT10
  119. * - ADC_START_ON_MAT11
  120. * @return None
  121. *********************************************************************/
  122. void ADC_StartCmd(LPC_ADC_TypeDef *ADCx, uint8_t start_mode)
  123. {
  124. ADCx->CR &= ~ADC_CR_START_MASK;
  125. ADCx->CR |=ADC_CR_START_MODE_SEL((uint32_t)start_mode);
  126. }
  127. /*********************************************************************//**
  128. * @brief ADC Burst mode setting
  129. * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
  130. * @param[in] NewState
  131. * - 1: Set Burst mode
  132. * - 0: reset Burst mode
  133. * @return None
  134. **********************************************************************/
  135. void ADC_BurstCmd(LPC_ADC_TypeDef *ADCx, FunctionalState NewState)
  136. {
  137. ADCx->CR &= ~ADC_CR_BURST;
  138. if (NewState){
  139. ADCx->CR |= ADC_CR_BURST;
  140. }
  141. }
  142. /*********************************************************************//**
  143. * @brief Set AD conversion in power mode
  144. * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
  145. * @param[in] NewState
  146. * - 1: AD converter is optional
  147. * - 0: AD Converter is in power down mode
  148. * @return None
  149. **********************************************************************/
  150. void ADC_PowerdownCmd(LPC_ADC_TypeDef *ADCx, FunctionalState NewState)
  151. {
  152. ADCx->CR &= ~ADC_CR_PDN;
  153. if (NewState){
  154. ADCx->CR |= ADC_CR_PDN;
  155. }
  156. }
  157. /*********************************************************************//**
  158. * @brief Set Edge start configuration
  159. * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
  160. * @param[in] EdgeOption is ADC_START_ON_RISING and ADC_START_ON_FALLING
  161. * 0:ADC_START_ON_RISING
  162. * 1:ADC_START_ON_FALLING
  163. * @return None
  164. **********************************************************************/
  165. void ADC_EdgeStartConfig(LPC_ADC_TypeDef *ADCx, uint8_t EdgeOption)
  166. {
  167. ADCx->CR &= ~ADC_CR_EDGE;
  168. if (EdgeOption){
  169. ADCx->CR |= ADC_CR_EDGE;
  170. }
  171. }
  172. /*********************************************************************//**
  173. * @brief ADC interrupt configuration
  174. * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
  175. * @param[in] IntType: type of interrupt, should be:
  176. * - ADC_ADINTEN0: Interrupt channel 0
  177. * - ADC_ADINTEN1: Interrupt channel 1
  178. * ...
  179. * - ADC_ADINTEN7: Interrupt channel 7
  180. * - ADC_ADGINTEN: Individual channel/global flag done generate an interrupt
  181. * @param[in] NewState:
  182. * - SET : enable ADC interrupt
  183. * - RESET: disable ADC interrupt
  184. * @return None
  185. **********************************************************************/
  186. void ADC_IntConfig (LPC_ADC_TypeDef *ADCx, ADC_TYPE_INT_OPT IntType, FunctionalState NewState)
  187. {
  188. ADCx->INTEN &= ~ADC_INTEN_CH(IntType);
  189. if (NewState){
  190. ADCx->INTEN |= ADC_INTEN_CH(IntType);
  191. }
  192. }
  193. /*********************************************************************//**
  194. * @brief Enable/Disable ADC channel number
  195. * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
  196. * @param[in] Channel channel number
  197. * @param[in] NewState Enable or Disable
  198. *
  199. * @return None
  200. **********************************************************************/
  201. void ADC_ChannelCmd (LPC_ADC_TypeDef *ADCx, uint8_t Channel, FunctionalState NewState)
  202. {
  203. if (NewState == ENABLE) {
  204. ADCx->CR |= ADC_CR_CH_SEL(Channel);
  205. } else {
  206. if (ADCx->CR & ADC_CR_START_MASK) //need to stop START bits before disable channel
  207. ADCx->CR &= ~ADC_CR_START_MASK;
  208. ADCx->CR &= ~ADC_CR_CH_SEL(Channel);
  209. }
  210. }
  211. /*********************************************************************//**
  212. * @brief Get ADC result
  213. * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
  214. * @param[in] channel: channel number, should be 0...7
  215. * @return Data conversion
  216. **********************************************************************/
  217. uint16_t ADC_ChannelGetData(LPC_ADC_TypeDef *ADCx, uint8_t channel)
  218. {
  219. uint32_t adc_value;
  220. adc_value = *(uint32_t *) ((&ADCx->DR[0]) + channel);
  221. return ADC_DR_RESULT(adc_value);
  222. }
  223. /*********************************************************************//**
  224. * @brief Get ADC Chanel status from ADC data register
  225. * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
  226. * @param[in] channel: channel number, should be 0..7
  227. * @param[in] StatusType
  228. * 0:Burst status
  229. * 1:Done status
  230. * @return SET / RESET
  231. **********************************************************************/
  232. FlagStatus ADC_ChannelGetStatus(LPC_ADC_TypeDef *ADCx, uint8_t channel, uint32_t StatusType)
  233. {
  234. uint32_t temp;
  235. temp = *(uint32_t *) ((&ADCx->DR[0]) + channel);
  236. if (StatusType)
  237. {
  238. temp &= ADC_DR_DONE_FLAG;
  239. }
  240. else
  241. {
  242. temp &= ADC_DR_OVERRUN_FLAG;
  243. }
  244. if (temp)
  245. {
  246. return SET;
  247. }
  248. else
  249. {
  250. return RESET;
  251. }
  252. }
  253. /*********************************************************************//**
  254. * @brief Get ADC Data from AD Global register
  255. * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
  256. * @return Result of conversion
  257. **********************************************************************/
  258. uint32_t ADC_GlobalGetData(LPC_ADC_TypeDef *ADCx)
  259. {
  260. return ((uint32_t)(ADCx->GDR));
  261. }
  262. /*********************************************************************//**
  263. * @brief Get ADC Chanel status from AD global data register
  264. * @param[in] ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
  265. * @param[in] StatusType
  266. * 0:Burst status
  267. * 1:Done status
  268. * @return SET / RESET
  269. **********************************************************************/
  270. FlagStatus ADC_GlobalGetStatus(LPC_ADC_TypeDef *ADCx, uint32_t StatusType)
  271. {
  272. uint32_t temp;
  273. temp = ADCx->GDR;
  274. if (StatusType){
  275. temp &= ADC_DR_DONE_FLAG;
  276. }else{
  277. temp &= ADC_DR_OVERRUN_FLAG;
  278. }
  279. if (temp){
  280. return SET;
  281. }else{
  282. return RESET;
  283. }
  284. }
  285. /**
  286. * @}
  287. */
  288. #endif /*_ADC*/
  289. /**
  290. * @}
  291. */
  292. /* --------------------------------- End Of File ------------------------------ */