adc_001.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * @brief ADC Registers and control functions
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2012
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights 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. #include "adc_001.h"
  32. /*****************************************************************************
  33. * Private types/enumerations/variables
  34. ****************************************************************************/
  35. /*****************************************************************************
  36. * Public types/enumerations/variables
  37. ****************************************************************************/
  38. /*****************************************************************************
  39. * Private functions
  40. ****************************************************************************/
  41. /* Configure clock for ADC */
  42. static void SetClock(IP_ADC_001_Type *pADC, uint32_t adcRate, uint32_t adcPerClock, uint8_t bitsAccuracy)
  43. {
  44. uint32_t temp, adcBitRate;
  45. /* The APB clock (PCLK_ADC0) is divided by (CLKDIV+1) to produce the clock for
  46. A/D converter, which should be less than or equal to 4.5MHz.
  47. A fully conversion requires (bits_accuracy+1) of these clocks.
  48. ADC clock = PCLK_ADC0 / (CLKDIV + 1);
  49. ADC rate = ADC clock / (bits_accuracy+1);
  50. */
  51. adcBitRate = (adcRate * (11 - bitsAccuracy));
  52. /* Get the round value by fomular: (2*A + B)/(2*B) */
  53. temp = ((adcPerClock * 2 + adcBitRate) / (adcBitRate * 2)) - 1;
  54. /* Enable PDN bit and clock bits */
  55. pADC->CR &= ~(ADC_CR_CLKDIV(0xFF) | ADC_CR_BITACC(0x07));
  56. pADC->CR |= ADC_CR_CLKDIV(temp) | ADC_CR_BITACC(bitsAccuracy);
  57. }
  58. /*****************************************************************************
  59. * Public functions
  60. ****************************************************************************/
  61. /* Initialize the ADC */
  62. void IP_ADC_Init(IP_ADC_001_Type *pADC, uint32_t adcRate, uint32_t adcPerClock, uint8_t bitsAccuracy)
  63. {
  64. pADC->INTEN = 0; /* Disable all interrupts */
  65. pADC->CR |= ADC_CR_PDN; /* Set PDN bit for ADC*/
  66. SetClock(pADC, adcRate, adcPerClock, bitsAccuracy);
  67. }
  68. /* Shutdown ADC */
  69. void IP_ADC_DeInit(IP_ADC_001_Type *pADC)
  70. {
  71. pADC->INTEN = 0x00000100;
  72. pADC->CR = 0;
  73. }
  74. /* Set burst mode for ADC */
  75. void IP_ADC_SetBurstMode(IP_ADC_001_Type *pADC, FunctionalState NewState)
  76. {
  77. if (NewState == DISABLE) {
  78. pADC->CR &= ~ADC_CR_BURST;
  79. }
  80. else {
  81. pADC->CR |= ADC_CR_BURST;
  82. }
  83. }
  84. /* Get the ADC value */
  85. Status IP_ADC_Get_Val(IP_ADC_001_Type *pADC, uint8_t channel, uint16_t *data)
  86. {
  87. uint32_t temp;
  88. temp = pADC->DR[channel];
  89. if (!ADC_DR_DONE(temp)) {
  90. return ERROR;
  91. }
  92. // if(ADC_DR_OVERRUN(temp) && (pADC->CR & ADC_CR_BURST))
  93. // return ERROR;
  94. *data = (uint16_t) ADC_DR_RESULT(temp);
  95. return SUCCESS;
  96. }
  97. /* Get ADC Channel status from ADC data register */
  98. FlagStatus IP_ADC_GetStatus(IP_ADC_001_Type *pADC, uint8_t channel, uint32_t StatusType)
  99. {
  100. switch (StatusType) {
  101. case ADC_DR_DONE_STAT:
  102. return (pADC->STAT & (1UL << channel)) ? SET : RESET;
  103. case ADC_DR_OVERRUN_STAT:
  104. channel += 8;
  105. return (pADC->STAT & (1UL << channel)) ? SET : RESET;
  106. case ADC_DR_ADINT_STAT:
  107. return pADC->STAT >> 16 ? SET : RESET;
  108. default:
  109. break;
  110. }
  111. return RESET;
  112. }
  113. /* Set the edge start condition */
  114. void IP_ADC_EdgeStartConfig(IP_ADC_001_Type *pADC, uint8_t edge_mode)
  115. {
  116. if (edge_mode) {
  117. pADC->CR |= ADC_CR_EDGE;
  118. }
  119. else {
  120. pADC->CR &= ~ADC_CR_EDGE;
  121. }
  122. }
  123. /* Enable/Disable ADC channel number */
  124. void IP_ADC_SetChannelNumber(IP_ADC_001_Type *pADC, uint8_t channel, FunctionalState NewState)
  125. {
  126. if (NewState == ENABLE) {
  127. pADC->CR |= ADC_CR_CH_SEL(channel);
  128. }
  129. else {
  130. pADC->CR &= ~ADC_CR_START_MASK;
  131. pADC->CR &= ~ADC_CR_CH_SEL(channel);
  132. }
  133. }
  134. /* Set start mode for ADC */
  135. void IP_ADC_SetStartMode(IP_ADC_001_Type *pADC, uint8_t start_mode)
  136. {
  137. pADC->CR &= ~ADC_CR_START_MASK;
  138. pADC->CR |= ADC_CR_START_MODE_SEL((uint32_t) start_mode);
  139. }
  140. /* Enable/Disable interrupt for ADC channel */
  141. void IP_ADC_Int_Enable(IP_ADC_001_Type *pADC, uint8_t channel, FunctionalState NewState)
  142. {
  143. if (NewState == ENABLE) {
  144. pADC->INTEN |= (1UL << channel);
  145. }
  146. else {
  147. pADC->INTEN &= (~(1UL << channel));
  148. }
  149. }