LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adc_18xx_43xx.c
Go to the documentation of this file.
1 /*
2  * @brief LPC18xx/43xx A/D conversion driver
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 "chip.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 /* Returns the clock for the selected ADC */
48 {
49  CHIP_CCU_CLK_T adcclk;
50 
51  if (pADC == LPC_ADC0) {
52  adcclk = CLK_APB3_ADC0;
53  }
54  else {
55  adcclk = CLK_APB3_ADC1;
56  }
57 
58  return adcclk;
59 }
60 
61 /* Get divider value */
62 STATIC uint8_t getAdcClkDiv(LPC_ADC_T *pADC, bool burstMode, uint32_t adcRate,uint8_t clks)
63 {
64  uint32_t adcBlockFreq;
65  uint32_t fullAdcRate;
66  uint8_t div;
67 
68  /* The APB clock (PCLK_ADC0) is divided by (CLKDIV+1) to produce the clock for
69  A/D converter, which should be less than or equal to 4.5MHz.
70  A fully conversion requires (bits_accuracy+1) of these clocks.
71  ADC Clock = PCLK_ADC0 / (CLKDIV + 1);
72  ADC rate = ADC clock / (the number of clocks required for each conversion);
73  */
74  adcBlockFreq = Chip_Clock_GetRate(Chip_ADC_GetClk(pADC));
75  if(burstMode)
76  fullAdcRate = adcRate * clks;
77  else
78  fullAdcRate = adcRate * 11;
79 
80  /* Get the round value by fomular: (2*A + B)/(2*B) */
81  div = ((adcBlockFreq * 2 + fullAdcRate) / (fullAdcRate * 2)) - 1;
82  return div;
83 }
84 
85 
86 /*****************************************************************************
87  * Public functions
88  ****************************************************************************/
89 
90 /* Shutdown ADC */
92 {
93  IP_ADC_DeInit(pADC);
95 }
96 
97 /* Initialize the ADC peripheral and the ADC setup structure to default value */
99 {
100  uint8_t div;
101  CHIP_CCU_CLK_T adcclk = Chip_ADC_GetClk(pADC);
102 
103  /* Enable ADC clocking */
104  Chip_Clock_EnableOpts(adcclk, true, true, 1);
105 
106  ADCSetup->adcRate = 400000;
107  ADCSetup->bitsAccuracy = ADC_10BITS;
108  ADCSetup->burstMode = false;
109  div = getAdcClkDiv(pADC, false, 400000, 11);
110  IP_ADC_Init(pADC, div, ADC_10BITS, ADC_CR_PDN);
111 }
112 
113 /* Select the mode starting the AD conversion */
115 {
116  if ((mode != ADC_START_NOW) && (mode != ADC_NO_START)) {
117  IP_ADC_EdgeStartConfig(pADC, (uint8_t) EdgeOption);
118  }
119  IP_ADC_SetStartMode(pADC, (uint8_t) mode);
120 }
121 
122 /* Set the ADC Sample rate */
124 {
125  uint8_t div;
126  ADCSetup->adcRate = rate;
127  div = getAdcClkDiv(pADC, ADCSetup->burstMode, rate, (11- ADCSetup->bitsAccuracy));
128  IP_ADC_Init(pADC, div, ADCSetup->bitsAccuracy, ADC_CR_PDN);
129 }
130 
131 /* Set the ADC accuracy bits */
133 {
134  ADCSetup->bitsAccuracy = resolution;
135  Chip_ADC_Set_SampleRate(pADC, ADCSetup,ADCSetup->adcRate);
136 }
137 
138 /* Enable or disable the ADC channel on ADC peripheral */
140 {
141  IP_ADC_SetChannelNumber(pADC, channel, NewState);
142 }
143 
144 /* Enable burst mode */
146 {
148  IP_ADC_SetBurstMode(pADC, NewState);
149 }
150 
151 /* Read the ADC value and convert it to 8bits value */
153 {
154  uint16_t temp;
155  Status rt;
156 
157  rt = IP_ADC_Get_Val(pADC, channel, &temp);
158  *data = (uint8_t) temp;
159 
160  return rt;
161 }