My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

lpc17xx_adc.c 11KB

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