LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adc_13xx.c
Go to the documentation of this file.
1 /*
2  * @brief LPC13xx 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 /* Get divider value for LPC1343 */
47 STATIC uint8_t getAdcClkDiv(bool burstMode, uint32_t adcRate, uint8_t clks)
48 {
49  uint32_t adcBlockFreq;
50  uint32_t fullAdcRate;
51  uint8_t div;
52 
53  /* The APB clock (PCLK_ADC0) is divided by (CLKDIV+1) to produce the clock for
54  A/D converter, which should be less than or equal to 4.5MHz.
55  A fully conversion requires (bits_accuracy+1) of these clocks.
56  ADC Clock = PCLK_ADC0 / (CLKDIV + 1);
57  ADC rate = ADC clock / (the number of clocks required for each conversion);
58  */
59  adcBlockFreq = Chip_Clock_GetSystemClockRate();
60 #if defined(CHIP_LPC1347)
61  fullAdcRate = adcRate * 31;
62  if (clks == ADC_10BITS) {
63  fullAdcRate /= 2;
64  }
65 
66 #else
67  if (burstMode) {
68  fullAdcRate = adcRate * clks;
69  }
70  else {
71  fullAdcRate = adcRate * 11;
72  }
73 #endif
74 
75  /* Get the round value by fomular: (2*A + B)/(2*B) */
76  div = ((adcBlockFreq * 2 + fullAdcRate) / (fullAdcRate * 2)) - 1;
77  return div;
78 }
79 
80 /*****************************************************************************
81  * Public functions
82  ****************************************************************************/
83 
84 /* Shutdown ADC */
86 {
87  IP_ADC_DeInit(pADC);
88 
89  /* Enable ADC clocking and IO config pins are enabled in Channel enable cmd */
91 
92  /* Disable power to the ADC block. */
94 }
95 
96 /* Initialize the ADC peripheral and the ADC setup structure to default value */
98 {
99  uint8_t div;
100  /* Enable power to the ADC block. */
102 
103  /* Enable ADC clocking and IO config pins are enabled in Channel enable cmd */
105 
106 #if defined(CHIP_LPC1347)
107  ADCSetup->adcRate = 500000;
108  ADCSetup->bitsAccuracy = ADC_12BITS;
109  ADCSetup->burstMode = false;
110  div = getAdcClkDiv(false, 500000, ADC_12BITS);
111  IP_ADC_Init(pADC, div, 0, ADC_CR_LPWRMODE);
112 
113 #else
114  ADCSetup->adcRate = 400000;
115  ADCSetup->bitsAccuracy = ADC_10BITS;
116  ADCSetup->burstMode = false;
117  div = getAdcClkDiv(false, 400000, 11);
118  IP_ADC_Init(pADC, div, ADC_10BITS, 0);
119 #endif
120 }
121 
122 /* Select the mode starting the AD conversion */
124 {
125  if ((mode != ADC_START_NOW) && (mode != ADC_NO_START)) {
126  IP_ADC_EdgeStartConfig(pADC, (uint8_t) EdgeOption);
127  }
128  IP_ADC_SetStartMode(pADC, (uint8_t) mode);
129 }
130 
131 /* Set the ADC Sample rate */
133 {
134  uint8_t div;
135 
136  ADCSetup->adcRate = rate;
137 #if defined(CHIP_LPC1347)
138  div = getAdcClkDiv(ADCSetup->burstMode, rate, ADCSetup->bitsAccuracy);
139  if (ADCSetup->bitsAccuracy == ADC_10BITS) {
140  IP_ADC_Init(pADC, div, ADCSetup->bitsAccuracy, ADC_CR_LPWRMODE | ADC_CR_MODE10BIT);
141  }
142  else {
143  IP_ADC_Init(pADC, div, ADCSetup->bitsAccuracy, ADC_CR_LPWRMODE);
144  }
145 
146 #else
147  div = getAdcClkDiv(ADCSetup->burstMode, rate, (11 - ADCSetup->bitsAccuracy));
148  IP_ADC_Init(pADC, div, ADCSetup->bitsAccuracy, 0);
149 #endif
150 
151 }
152 
153 /* Set the ADC accuracy bits */
155 {
156  ADCSetup->bitsAccuracy = resolution;
157  Chip_ADC_Set_SampleRate(pADC, ADCSetup, ADCSetup->adcRate);
158 }
159 
160 /* Enable or disable the ADC channel on ADC peripheral */
162 {
163  IP_ADC_SetChannelNumber(pADC, channel, NewState);
164 }
165 
166 /* Enable burst mode */
168 {
170  IP_ADC_SetBurstMode(pADC, NewState);
171 }
172 
173 /* Read the ADC value and convert it to 8bits value */
175 {
176  uint16_t temp;
177  Status rt;
178 
179  rt = IP_ADC_Get_Val(pADC, channel, &temp);
180  *data = (uint8_t) temp;
181 
182  return rt;
183 }