LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adc_001.c
Go to the documentation of this file.
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 
32 #include "adc_001.h"
33 
34 /*****************************************************************************
35  * Private types/enumerations/variables
36  ****************************************************************************/
37 
38 /*****************************************************************************
39  * Public types/enumerations/variables
40  ****************************************************************************/
41 
42 /*****************************************************************************
43  * Private functions
44  ****************************************************************************/
45 
46 /* Configure clock for ADC */
47 static void SetClock(IP_ADC_001_Type *pADC, uint32_t adcRate, uint32_t adcPerClock, uint8_t bitsAccuracy)
48 {
49  uint32_t temp, adcBitRate;
50 
51  /* The APB clock (PCLK_ADC0) is divided by (CLKDIV+1) to produce the clock for
52  A/D converter, which should be less than or equal to 4.5MHz.
53  A fully conversion requires (bits_accuracy+1) of these clocks.
54  ADC clock = PCLK_ADC0 / (CLKDIV + 1);
55  ADC rate = ADC clock / (bits_accuracy+1);
56  */
57  adcBitRate = (adcRate * (11 - bitsAccuracy));
58 
59  /* Get the round value by fomular: (2*A + B)/(2*B) */
60  temp = ((adcPerClock * 2 + adcBitRate) / (adcBitRate * 2)) - 1;
61 
62  /* Enable PDN bit and clock bits */
63  pADC->CR &= ~(ADC_CR_CLKDIV(0xFF) | ADC_CR_BITACC(0x07));
64  pADC->CR |= ADC_CR_CLKDIV(temp) | ADC_CR_BITACC(bitsAccuracy);
65 }
66 
67 /*****************************************************************************
68  * Public functions
69  ****************************************************************************/
70 
71 /* Initialize the ADC */
72 void IP_ADC_Init(IP_ADC_001_Type *pADC, uint32_t adcRate, uint32_t adcPerClock, uint8_t bitsAccuracy)
73 {
74  pADC->INTEN = 0; /* Disable all interrupts */
75  pADC->CR |= ADC_CR_PDN; /* Set PDN bit for ADC*/
76  SetClock(pADC, adcRate, adcPerClock, bitsAccuracy);
77 }
78 
79 /* Shutdown ADC */
81 {
82  pADC->INTEN = 0x00000100;
83  pADC->CR = 0;
84 }
85 
86 /* Set burst mode for ADC */
88 {
89  if (NewState == DISABLE) {
90  pADC->CR &= ~ADC_CR_BURST;
91  }
92  else {
93  pADC->CR |= ADC_CR_BURST;
94  }
95 }
96 
97 /* Get the ADC value */
98 Status IP_ADC_Get_Val(IP_ADC_001_Type *pADC, uint8_t channel, uint16_t *data)
99 {
100  uint32_t temp;
101  temp = pADC->DR[channel];
102  if (!ADC_DR_DONE(temp)) {
103  return ERROR;
104  }
105  // if(ADC_DR_OVERRUN(temp) && (pADC->CR & ADC_CR_BURST))
106  // return ERROR;
107  *data = (uint16_t) ADC_DR_RESULT(temp);
108  return SUCCESS;
109 }
110 
111 /* Get ADC Channel status from ADC data register */
112 FlagStatus IP_ADC_GetStatus(IP_ADC_001_Type *pADC, uint8_t channel, uint32_t StatusType)
113 {
114  switch (StatusType) {
115  case ADC_DR_DONE_STAT:
116  return (pADC->STAT & (1UL << channel)) ? SET : RESET;
117 
118  case ADC_DR_OVERRUN_STAT:
119  channel += 8;
120  return (pADC->STAT & (1UL << channel)) ? SET : RESET;
121 
122  case ADC_DR_ADINT_STAT:
123  return pADC->STAT >> 16 ? SET : RESET;
124 
125  default:
126  break;
127  }
128  return RESET;
129 }
130 
131 /* Set the edge start condition */
132 void IP_ADC_EdgeStartConfig(IP_ADC_001_Type *pADC, uint8_t edge_mode)
133 {
134  if (edge_mode) {
135  pADC->CR |= ADC_CR_EDGE;
136  }
137  else {
138  pADC->CR &= ~ADC_CR_EDGE;
139  }
140 }
141 
142 /* Enable/Disable ADC channel number */
143 void IP_ADC_SetChannelNumber(IP_ADC_001_Type *pADC, uint8_t channel, FunctionalState NewState)
144 {
145  if (NewState == ENABLE) {
146  pADC->CR |= ADC_CR_CH_SEL(channel);
147  }
148  else {
149  pADC->CR &= ~ADC_CR_START_MASK;
150  pADC->CR &= ~ADC_CR_CH_SEL(channel);
151  }
152 }
153 
154 /* Set start mode for ADC */
155 void IP_ADC_SetStartMode(IP_ADC_001_Type *pADC, uint8_t start_mode)
156 {
157  pADC->CR &= ~ADC_CR_START_MASK;
158  pADC->CR |= ADC_CR_START_MODE_SEL((uint32_t) start_mode);
159 }
160 
161 /* Enable/Disable interrupt for ADC channel */
162 void IP_ADC_Int_Enable(IP_ADC_001_Type *pADC, uint8_t channel, FunctionalState NewState)
163 {
164  if (NewState == ENABLE) {
165  pADC->INTEN |= (1UL << channel);
166  }
167  else {
168  pADC->INTEN &= (~(1UL << channel));
169  }
170 }