LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adc_17xx_40xx.c
Go to the documentation of this file.
1 /*
2  * @brief LPC17xx/40xx ADC 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 */
47 STATIC uint8_t getAdcClkDiv(uint32_t adcRate)
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 12.4MHz.
55  * A fully conversion requires 31 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 #if defined(CHIP_LPC175X_6X)
60  adcBlockFreq = Chip_Clock_GetPeripheralClockRate(SYSCTL_PCLK_ADC);
61  fullAdcRate = adcRate * 65;
62 #else
63  adcBlockFreq = Chip_Clock_GetPeripheralClockRate();
64  fullAdcRate = adcRate * 31;
65 #endif
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);
81 }
82 
83 /* Initialize the ADC peripheral and the ADC setup structure to default value */
85 {
86  uint8_t div;
87  /* Enable ADC clocking and get ADC clock rate */
89 
90  ADCSetup->adcRate = ADC_MAX_SAMPLE_RATE;
91  ADCSetup->burstMode = false;
93  IP_ADC_Init(pADC, div, 0, ADC_CR_PDN);
94 }
95 
96 /* Select the mode starting the AD conversion */
98 {
99  if ((mode != ADC_START_NOW) && (mode != ADC_NO_START)) {
100  IP_ADC_EdgeStartConfig(pADC, (uint8_t) EdgeOption);
101  }
102  IP_ADC_SetStartMode(pADC, (uint8_t) mode);
103 }
104 
105 /* Set the ADC Sample rate */
107 {
108  uint8_t div;
109  ADCSetup->adcRate = rate;
110  div = getAdcClkDiv(rate);
111  IP_ADC_Init(pADC, div, 0, ADC_CR_PDN);
112 
113 }
114 
115 /* Enable or disable the ADC channel on ADC peripheral */
117 {
118  IP_ADC_SetChannelNumber(pADC, channel, NewState);
119 }
120 
121 /* Enable burst mode */
123 {
125  IP_ADC_SetBurstMode(pADC, NewState);
126 }
127 
128 /* Read the ADC value and convert it to 8bits value */
130 {
131  uint16_t temp;
132  Status rt;
133 
134  rt = IP_ADC_Get_Val(pADC, channel, &temp);
135  *data = (uint8_t) temp;
136 
137  return rt;
138 }