uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lpc17xx_adc.c
Go to the documentation of this file.
1 /***********************************************************************/
20 /* Peripheral group ----------------------------------------------------------- */
25 /* Includes ------------------------------------------------------------------- */
26 #include "lpc17xx_adc.h"
27 #include "lpc17xx_clkpwr.h"
28 
29 /* If this source file built with example, the LPC17xx FW library configuration
30  * file in each example directory ("lpc17xx_libcfg.h") must be included,
31  * otherwise the default FW library configuration file must be included instead
32  */
33 #ifdef __BUILD_WITH_EXAMPLE__
34 #include "lpc17xx_libcfg.h"
35 #else
36 #include "lpc17xx_libcfg_default.h"
37 #endif /* __BUILD_WITH_EXAMPLE__ */
38 
39 
40 #ifdef _ADC
41 
42 /* Public Functions ----------------------------------------------------------- */
47 /*********************************************************************/
56 void ADC_Init(LPC_ADC_TypeDef *ADCx, uint32_t rate)
57 {
58  uint32_t temp, tmp;
59 
60  CHECK_PARAM(PARAM_ADCx(ADCx));
62 
63  // Turn on power and clock
65 
66  ADCx->ADCR = 0;
67 
68  //Enable PDN bit
69  tmp = ADC_CR_PDN;
70  // Set clock frequency
72  /* The APB clock (PCLK_ADC0) is divided by (CLKDIV+1) to produce the clock for
73  * A/D converter, which should be less than or equal to 13MHz.
74  * A fully conversion requires 65 of these clocks.
75  * ADC clock = PCLK_ADC0 / (CLKDIV + 1);
76  * ADC rate = ADC clock / 65;
77  */
78  temp = (temp /(rate * 65)) - 1;
79  tmp |= ADC_CR_CLKDIV(temp);
80 
81  ADCx->ADCR = tmp;
82 }
83 
84 
85 /*********************************************************************/
90 void ADC_DeInit(LPC_ADC_TypeDef *ADCx)
91 {
92  CHECK_PARAM(PARAM_ADCx(ADCx));
93 
94  // Clear PDN bit
95  ADCx->ADCR &= ~ADC_CR_PDN;
96  // Turn on power and clock
98 }
99 
100 
101 /*********************************************************************/
106 uint32_t ADC_GetData(uint32_t channel)
107 {
108  uint32_t adc_value;
109 
111 
112  adc_value = *(uint32_t *)((&LPC_ADC->ADDR0) + channel);
113  return ADC_GDR_RESULT(adc_value);
114 }
115 
116 /*********************************************************************/
131 void ADC_StartCmd(LPC_ADC_TypeDef *ADCx, uint8_t start_mode)
132 {
133  CHECK_PARAM(PARAM_ADCx(ADCx));
134  CHECK_PARAM(PARAM_ADC_START_OPT(start_mode));
135 
136  ADCx->ADCR &= ~ADC_CR_START_MASK;
137  ADCx->ADCR |=ADC_CR_START_MODE_SEL((uint32_t)start_mode);
138 }
139 
140 
141 /*********************************************************************/
149 void ADC_BurstCmd(LPC_ADC_TypeDef *ADCx, FunctionalState NewState)
150 {
151  CHECK_PARAM(PARAM_ADCx(ADCx));
152 
153  ADCx->ADCR &= ~ADC_CR_BURST;
154  if (NewState){
155  ADCx->ADCR |= ADC_CR_BURST;
156  }
157 }
158 
159 /*********************************************************************/
168 {
169  CHECK_PARAM(PARAM_ADCx(ADCx));
170 
171  ADCx->ADCR &= ~ADC_CR_PDN;
172  if (NewState){
173  ADCx->ADCR |= ADC_CR_PDN;
174  }
175 }
176 
177 /*********************************************************************/
185 void ADC_EdgeStartConfig(LPC_ADC_TypeDef *ADCx, uint8_t EdgeOption)
186 {
187  CHECK_PARAM(PARAM_ADCx(ADCx));
189 
190  ADCx->ADCR &= ~ADC_CR_EDGE;
191  if (EdgeOption){
192  ADCx->ADCR |= ADC_CR_EDGE;
193  }
194 }
195 
196 /*********************************************************************/
210 void ADC_IntConfig (LPC_ADC_TypeDef *ADCx, ADC_TYPE_INT_OPT IntType, FunctionalState NewState)
211 {
212  CHECK_PARAM(PARAM_ADCx(ADCx));
214 
215  ADCx->ADINTEN &= ~ADC_INTEN_CH(IntType);
216  if (NewState){
217  ADCx->ADINTEN |= ADC_INTEN_CH(IntType);
218  }
219 }
220 
221 /*********************************************************************/
229 void ADC_ChannelCmd (LPC_ADC_TypeDef *ADCx, uint8_t Channel, FunctionalState NewState)
230 {
231  CHECK_PARAM(PARAM_ADCx(ADCx));
233 
234  if (NewState == ENABLE) {
235  ADCx->ADCR |= ADC_CR_CH_SEL(Channel);
236  } else {
237  ADCx->ADCR &= ~ADC_CR_CH_SEL(Channel);
238  }
239 }
240 
241 /*********************************************************************/
247 uint16_t ADC_ChannelGetData(LPC_ADC_TypeDef *ADCx, uint8_t channel)
248 {
249  uint32_t adc_value;
250 
251  CHECK_PARAM(PARAM_ADCx(ADCx));
253 
254  adc_value = *(uint32_t *) ((&ADCx->ADDR0) + channel);
255  return ADC_DR_RESULT(adc_value);
256 }
257 
258 /*********************************************************************/
267 FlagStatus ADC_ChannelGetStatus(LPC_ADC_TypeDef *ADCx, uint8_t channel, uint32_t StatusType)
268 {
269  uint32_t temp;
270 
271  CHECK_PARAM(PARAM_ADCx(ADCx));
273  CHECK_PARAM(PARAM_ADC_DATA_STATUS(StatusType));
274 
275  temp = *(uint32_t *) ((&ADCx->ADDR0) + channel);
276  if (StatusType) {
277  temp &= ADC_DR_DONE_FLAG;
278  }else{
279  temp &= ADC_DR_OVERRUN_FLAG;
280  }
281  if (temp) {
282  return SET;
283  } else {
284  return RESET;
285  }
286 
287 }
288 
289 /*********************************************************************/
294 uint32_t ADC_GlobalGetData(LPC_ADC_TypeDef *ADCx)
295 {
296  CHECK_PARAM(PARAM_ADCx(ADCx));
297 
298  return ((uint32_t)(ADCx->ADGDR));
299 }
300 
301 /*********************************************************************/
309 FlagStatus ADC_GlobalGetStatus(LPC_ADC_TypeDef *ADCx, uint32_t StatusType)
310 {
311  uint32_t temp;
312 
313  CHECK_PARAM(PARAM_ADCx(ADCx));
314  CHECK_PARAM(PARAM_ADC_DATA_STATUS(StatusType));
315 
316  temp = ADCx->ADGDR;
317  if (StatusType){
318  temp &= ADC_DR_DONE_FLAG;
319  }else{
320  temp &= ADC_DR_OVERRUN_FLAG;
321  }
322  if (temp){
323  return SET;
324  }else{
325  return RESET;
326  }
327 }
328 
333 #endif /* _ADC */
334 
339 /* --------------------------------- End Of File ------------------------------ */
340