LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adc_11xx.c
Go to the documentation of this file.
1 /*
2  * @brief LPC11xx 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 */
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 (burstMode) {
61  fullAdcRate = adcRate * clks;
62  }
63  else {
64  fullAdcRate = adcRate * 11;
65  }
66 
67  /* Get the round value by fomular: (2*A + B)/(2*B) */
68  div = ((adcBlockFreq * 2 + fullAdcRate) / (fullAdcRate * 2)) - 1;
69  return div;
70 }
71 
72 /*****************************************************************************
73  * Public functions
74  ****************************************************************************/
75 
76 /* Shutdown ADC */
78 {
79  IP_ADC_DeInit(pADC);
80 
81  /* Enable ADC clocking and IO config pins are enabled in Channel enable cmd */
83 
84  /* Disable power to the ADC block. */
86 }
87 
88 /* Initialize the ADC peripheral and the ADC setup structure to default value */
90 {
91  uint8_t div;
92  /* Enable power to the ADC block. */
94 
95  /* Enable ADC clocking and IO config pins are enabled in Channel enable cmd */
97 
98  ADCSetup->adcRate = 400000;
99  ADCSetup->bitsAccuracy = ADC_10BITS;
100  ADCSetup->burstMode = false;
101  div = getAdcClkDiv(false, 400000, 11);
102  IP_ADC_Init(pADC, div, ADC_10BITS, ADC_CR_PDN);
103 }
104 
105 /* Select the mode starting the AD conversion */
107 {
108  if ((mode != ADC_START_NOW) && (mode != ADC_NO_START)) {
109  IP_ADC_EdgeStartConfig(pADC, (uint8_t) EdgeOption);
110  }
111  IP_ADC_SetStartMode(pADC, (uint8_t) mode);
112 }
113 
114 /* Set the ADC Sample rate */
116 {
117  uint8_t div;
118 
119  ADCSetup->adcRate = rate;
120  div = getAdcClkDiv(ADCSetup->burstMode, rate, (11 - ADCSetup->bitsAccuracy));
121  IP_ADC_Init(pADC, div, ADCSetup->bitsAccuracy, ADC_CR_PDN);
122 }
123 
124 /* Set the ADC accuracy bits */
126 {
127  ADCSetup->bitsAccuracy = resolution;
128  Chip_ADC_Set_SampleRate(pADC, ADCSetup, ADCSetup->adcRate);
129 }
130 
131 /* Enable or disable the ADC channel on ADC peripheral */
133 {
134  IP_ADC_SetChannelNumber(pADC, channel, NewState);
135 }
136 
137 /* Enable burst mode */
139 {
141  IP_ADC_SetBurstMode(pADC, NewState);
142 }
143 
144 /* Read the ADC value and convert it to 8bits value */
146 {
147  uint16_t temp;
148  Status rt;
149 
150  rt = IP_ADC_Get_Val(pADC, channel, &temp);
151  *data = (uint8_t) temp;
152 
153  return rt;
154 }