1 /**********************************************************************
\r
2 * $Id$ lpc18xx_adc.h 2011-06-02
\r
4 * @file lpc18xx_adc.h
\r
5 * @brief Contains all macro definitions and function prototypes
\r
6 * support for ADC firmware library on LPC18xx
\r
8 * @date 02. June. 2011
\r
9 * @author NXP MCU SW Application Team
\r
11 * Copyright(C) 2011, NXP Semiconductor
\r
12 * All rights reserved.
\r
14 ***********************************************************************
\r
15 * Software that is described herein is for illustrative purposes only
\r
16 * which provides customers with programming information regarding the
\r
17 * products. This software is supplied "AS IS" without any warranties.
\r
18 * NXP Semiconductors assumes no responsibility or liability for the
\r
19 * use of the software, conveys no license or title under any patent,
\r
20 * copyright, or mask work right to the product. NXP Semiconductors
\r
21 * reserves the right to make changes in the software without
\r
22 * notification. NXP Semiconductors also make no representation or
\r
23 * warranty that such application will be suitable for the specified
\r
24 * use without further testing or modification.
\r
25 **********************************************************************/
\r
27 /* Peripheral group ----------------------------------------------------------- */
\r
28 /** @defgroup ADC ADC (Analog to Digital Converter)
\r
29 * @ingroup LPC1800CMSIS_FwLib_Drivers
\r
33 #ifndef LPC18XX_ADC_H_
\r
34 #define LPC18XX_ADC_H_
\r
36 /* Includes ------------------------------------------------------------------- */
\r
37 #include "LPC18xx.h"
\r
38 #include "lpc_types.h"
\r
46 /* Private macros ------------------------------------------------------------- */
\r
47 /** @defgroup ADC_Private_Macros ADC Private Macros
\r
51 /* -------------------------- BIT DEFINITIONS ----------------------------------- */
\r
52 /*********************************************************************//**
\r
53 * Macro defines for ADC control register
\r
54 **********************************************************************/
\r
55 /** Selects which of the AD0.0:7 pins is (are) to be sampled and converted */
\r
56 #define ADC_CR_CH_SEL(n) ((1UL << n))
\r
57 /** The APB clock (PCLK) is divided by (this value plus one)
\r
58 * to produce the clock for the A/D */
\r
59 #define ADC_CR_CLKDIV(n) ((n<<8))
\r
60 /** Repeated conversions A/D enable bit */
\r
61 #define ADC_CR_BURST ((1UL<<16))
\r
62 /** number of accuracy bits */
\r
63 #define ADC_CR_BITACC(n) (((n)<<17))
\r
64 /** ADC convert in power down mode */
\r
65 #define ADC_CR_PDN ((1UL<<21))
\r
66 /** Start mask bits */
\r
67 #define ADC_CR_START_MASK ((7UL<<24))
\r
68 /** Select Start Mode */
\r
69 #define ADC_CR_START_MODE_SEL(SEL) ((SEL<<24))
\r
70 /** Start conversion now */
\r
71 #define ADC_CR_START_NOW ((1UL<<24))
\r
72 /** Start conversion when the edge selected by bit 27 occurs on CTOUT_15 */
\r
73 #define ADC_CR_START_CTOUT15 ((2UL<<24))
\r
74 /** Start conversion when the edge selected by bit 27 occurs on CTOUT_8 */
\r
75 #define ADC_CR_START_CTOUT8 ((3UL<<24))
\r
76 /** Start conversion when the edge selected by bit 27 occurs on ADCTRIG0 */
\r
77 #define ADC_CR_START_ADCTRIG0 ((4UL<<24))
\r
78 /** Start conversion when the edge selected by bit 27 occurs on ADCTRIG1 */
\r
79 #define ADC_CR_START_ADCTRIG1 ((5UL<<24))
\r
80 /** Start conversion when the edge selected by bit 27 occurs on Motocon PWM output MCOA2 */
\r
81 #define ADC_CR_START_MCOA2 ((6UL<<24))
\r
82 /** Start conversion on a falling edge on the selected CAP/MAT signal */
\r
83 #define ADC_CR_EDGE ((1UL<<27))
\r
85 /*********************************************************************//**
\r
86 * Macro defines for ADC Global Data register
\r
87 **********************************************************************/
\r
88 /** When DONE is 1, this field contains result value of ADC conversion */
\r
89 #define ADC_GDR_RESULT(n) (((n>>4)&0xFFF))
\r
90 /** These bits contain the channel from which the LS bits were converted */
\r
91 #define ADC_GDR_CH(n) (((n>>24)&0x7))
\r
92 /** This bit is 1 in burst mode if the results of one or
\r
93 * more conversions was (were) lost */
\r
94 #define ADC_GDR_OVERRUN_FLAG ((1UL<<30))
\r
95 /** This bit is set to 1 when an A/D conversion completes */
\r
96 #define ADC_GDR_DONE_FLAG ((1UL<<31))
\r
98 /** This bits is used to mask for Channel */
\r
99 #define ADC_GDR_CH_MASK ((7UL<<24))
\r
100 /*********************************************************************//**
\r
101 * Macro defines for ADC Interrupt register
\r
102 **********************************************************************/
\r
103 /** These bits allow control over which A/D channels generate
\r
104 * interrupts for conversion completion */
\r
105 #define ADC_INTEN_CH(n) ((1UL<<n))
\r
106 /** When 1, enables the global DONE flag in ADDR to generate an interrupt */
\r
107 #define ADC_INTEN_GLOBAL ((1UL<<8))
\r
109 /*********************************************************************//**
\r
110 * Macro defines for ADC Data register
\r
111 **********************************************************************/
\r
112 /** When DONE is 1, this field contains result value of ADC conversion */
\r
113 #define ADC_DR_RESULT(n) (((n>>6)&0x3FF))
\r
114 /** These bits mirror the OVERRRUN status flags that appear in the
\r
115 * result register for each A/D channel */
\r
116 #define ADC_DR_OVERRUN_FLAG ((1UL<<30))
\r
117 /** This bit is set to 1 when an A/D conversion completes. It is cleared
\r
118 * when this register is read */
\r
119 #define ADC_DR_DONE_FLAG ((1UL<<31))
\r
121 /*********************************************************************//**
\r
122 * Macro defines for ADC Status register
\r
123 **********************************************************************/
\r
124 /** These bits mirror the DONE status flags that appear in the result
\r
125 * register for each A/D channel */
\r
126 #define ADC_STAT_CH_DONE_FLAG(n) ((n&0xFF))
\r
127 /** These bits mirror the OVERRRUN status flags that appear in the
\r
128 * result register for each A/D channel */
\r
129 #define ADC_STAT_CH_OVERRUN_FLAG(n) (((n>>8)&0xFF))
\r
130 /** This bit is the A/D interrupt flag */
\r
131 #define ADC_STAT_INT_FLAG ((1UL<<16))
\r
133 /*********************************************************************//**
\r
134 * Macro defines for ADC Trim register
\r
135 **********************************************************************/
\r
136 /** Offset trim bits for ADC operation */
\r
137 #define ADC_ADCOFFS(n) (((n&0xF)<<4))
\r
138 /** Written to boot code*/
\r
139 #define ADC_TRIM(n) (((n&0xF)<<8))
\r
141 /* ------------------- CHECK PARAM DEFINITIONS ------------------------- */
\r
142 /** Check ADC parameter */
\r
143 #define PARAM_ADCx(n) (((uint32_t *)n)==((uint32_t *)LPC_ADC0) || ((uint32_t *)n)==((uint32_t *)LPC_ADC1))
\r
145 /** Check ADC state parameter */
\r
146 #define PARAM_ADC_START_ON_EDGE_OPT(OPT) ((OPT == ADC_START_ON_RISING)||(OPT == ADC_START_ON_FALLING))
\r
148 /** Check ADC state parameter */
\r
149 #define PARAM_ADC_DATA_STATUS(OPT) ((OPT== ADC_DATA_BURST)||(OPT== ADC_DATA_DONE))
\r
151 /** Check ADC rate parameter */
\r
152 #define PARAM_ADC_RATE(rate) ((rate>0)&&(rate<=200000))
\r
154 /** Check ADC bits accuracy parameter */
\r
155 #define PARAM_ADC_BITSACC(x) ((x>=3)&&(x<=10))
\r
157 /** Check ADC channel selection parameter */
\r
158 #define PARAM_ADC_CHANNEL_SELECTION(SEL) ((SEL == ADC_CHANNEL_0)||(ADC_CHANNEL_1)\
\r
159 ||(SEL == ADC_CHANNEL_2)|(ADC_CHANNEL_3)\
\r
160 ||(SEL == ADC_CHANNEL_4)||(ADC_CHANNEL_5)\
\r
161 ||(SEL == ADC_CHANNEL_6)||(ADC_CHANNEL_7))
\r
163 /** Check ADC start option parameter */
\r
164 #define PARAM_ADC_START_OPT(OPT) ((OPT == ADC_START_CONTINUOUS)||(OPT == ADC_START_NOW)\
\r
165 ||(OPT == ADC_START_ON_CTOUT15)||(OPT == ADC_START_ON_CTOUT8)\
\r
166 ||(OPT == ADC_START_ON_ADCTRIG0)||(OPT == ADC_START_ON_ADCTRIG1)\
\r
167 ||(OPT == ADC_START_ON_MCOA2))
\r
169 /** Check ADC interrupt type parameter */
\r
170 #define PARAM_ADC_TYPE_INT_OPT(OPT) ((OPT == ADC_ADINTEN0)||(OPT == ADC_ADINTEN1)\
\r
171 ||(OPT == ADC_ADINTEN2)||(OPT == ADC_ADINTEN3)\
\r
172 ||(OPT == ADC_ADINTEN4)||(OPT == ADC_ADINTEN5)\
\r
173 ||(OPT == ADC_ADINTEN6)||(OPT == ADC_ADINTEN7)\
\r
174 ||(OPT == ADC_ADGINTEN))
\r
181 /* Public Types --------------------------------------------------------------- */
\r
182 /** @defgroup ADC_Public_Types ADC Public Types
\r
186 /*********************************************************************//**
\r
187 * @brief ADC enumeration
\r
188 **********************************************************************/
\r
189 /** @brief Channel Selection */
\r
192 ADC_CHANNEL_0 = 0, /*!< Channel 0 */
\r
193 ADC_CHANNEL_1, /*!< Channel 1 */
\r
194 ADC_CHANNEL_2, /*!< Channel 2 */
\r
195 ADC_CHANNEL_3, /*!< Channel 3 */
\r
196 ADC_CHANNEL_4, /*!< Channel 4 */
\r
197 ADC_CHANNEL_5, /*!< Channel 5 */
\r
198 ADC_CHANNEL_6, /*!< Channel 6 */
\r
199 ADC_CHANNEL_7 /*!< Channel 7 */
\r
200 }ADC_CHANNEL_SELECTION;
\r
202 /** @brief Type of start option */
\r
205 ADC_START_CONTINUOUS =0, /*!< Continuous mode */
\r
206 ADC_START_NOW, /*!< Start conversion now */
\r
207 ADC_START_ON_CTOUT15, /*!< Start conversion when the edge selected
\r
208 * by bit 27 occurs on CTOUT_15 */
\r
209 ADC_START_ON_CTOUT8, /*!< Start conversion when the edge selected
\r
210 * by bit 27 occurs on CTOUT_8 */
\r
211 ADC_START_ON_ADCTRIG0, /*!< Start conversion when the edge selected
\r
212 * by bit 27 occurs on ADCTRIG0 */
\r
213 ADC_START_ON_ADCTRIG1, /*!< Start conversion when the edge selected
\r
214 * by bit 27 occurs on ADCTRIG1 */
\r
215 ADC_START_ON_MCOA2 /*!< Start conversion when the edge selected
\r
216 * by bit 27 occurs on Motocon PWM output MCOA2 */
\r
220 /** @brief Type of edge when start conversion on the selected CAP/MAT signal */
\r
223 ADC_START_ON_RISING = 0, /*!< Start conversion on a rising edge
\r
224 *on the selected CAP/MAT signal */
\r
225 ADC_START_ON_FALLING /*!< Start conversion on a falling edge
\r
226 *on the selected CAP/MAT signal */
\r
227 } ADC_START_ON_EDGE_OPT;
\r
229 /** @brief* ADC type interrupt enum */
\r
232 ADC_ADINTEN0 = 0, /*!< Interrupt channel 0 */
\r
233 ADC_ADINTEN1, /*!< Interrupt channel 1 */
\r
234 ADC_ADINTEN2, /*!< Interrupt channel 2 */
\r
235 ADC_ADINTEN3, /*!< Interrupt channel 3 */
\r
236 ADC_ADINTEN4, /*!< Interrupt channel 4 */
\r
237 ADC_ADINTEN5, /*!< Interrupt channel 5 */
\r
238 ADC_ADINTEN6, /*!< Interrupt channel 6 */
\r
239 ADC_ADINTEN7, /*!< Interrupt channel 7 */
\r
240 ADC_ADGINTEN /*!< Individual channel/global flag done generate an interrupt */
\r
243 /** @brief ADC Data status */
\r
246 ADC_DATA_BURST = 0, /*Burst bit*/
\r
247 ADC_DATA_DONE /*Done bit*/
\r
255 /* Public Functions ----------------------------------------------------------- */
\r
256 /** @defgroup ADC_Public_Functions ADC Public Functions
\r
259 /* Init/DeInit ADC peripheral ----------------*/
\r
260 void ADC_Init(LPC_ADCn_Type *ADCx, uint32_t rate, uint8_t bits_accuracy);
\r
261 void ADC_DeInit(LPC_ADCn_Type *ADCx);
\r
263 /* Enable/Disable ADC functions --------------*/
\r
264 void ADC_BurstCmd(LPC_ADCn_Type *ADCx, FunctionalState NewState);
\r
265 void ADC_PowerdownCmd(LPC_ADCn_Type *ADCx, FunctionalState NewState);
\r
266 void ADC_StartCmd(LPC_ADCn_Type *ADCx, uint8_t start_mode);
\r
267 void ADC_ChannelCmd (LPC_ADCn_Type *ADCx, uint8_t Channel, FunctionalState NewState);
\r
269 /* Configure ADC functions -------------------*/
\r
270 void ADC_EdgeStartConfig(LPC_ADCn_Type *ADCx, uint8_t EdgeOption);
\r
271 void ADC_IntConfig (LPC_ADCn_Type *ADCx, ADC_TYPE_INT_OPT IntType, FunctionalState NewState);
\r
273 /* Get ADC information functions -------------------*/
\r
274 uint16_t ADC_ChannelGetData(LPC_ADCn_Type *ADCx, uint8_t channel);
\r
275 FlagStatus ADC_ChannelGetStatus(LPC_ADCn_Type *ADCx, uint8_t channel, uint32_t StatusType);
\r
276 uint32_t ADC_GlobalGetData(LPC_ADCn_Type *ADCx);
\r
277 FlagStatus ADC_GlobalGetStatus(LPC_ADCn_Type *ADCx, uint32_t StatusType);
\r
289 #endif /* LPC18XX_ADC_H_ */
\r
295 /* --------------------------------- End Of File ------------------------------ */
\r