| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adc.c
Go to the documentation of this file.
1 /*
2 * @brief ADC example
3 * This example show how to the ADC in 3 mode : Polling, Interrupt and DMA
4 *
5 * @note
6 * Copyright(C) NXP Semiconductors, 2012
7 * All rights reserved.
8 *
9 * @par
10 * Software that is described herein is for illustrative purposes only
11 * which provides customers with programming information regarding the
12 * LPC products. This software is supplied "AS IS" without any warranties of
13 * any kind, and NXP Semiconductors and its licensor disclaim any and
14 * all warranties, express or implied, including all implied warranties of
15 * merchantability, fitness for a particular purpose and non-infringement of
16 * intellectual property rights. NXP Semiconductors assumes no responsibility
17 * or liability for the use of the software, conveys no license or rights under any
18 * patent, copyright, mask work right, or any other intellectual property rights in
19 * or to any products. NXP Semiconductors reserves the right to make changes
20 * in the software without notification. NXP Semiconductors also makes no
21 * representation or warranty that such application will be suitable for the
22 * specified use without further testing or modification.
23 *
24 * @par
25 * Permission to use, copy, modify, and distribute this software and its
26 * documentation is hereby granted, under NXP Semiconductors' and its
27 * licensor's relevant copyrights in the software, without fee, provided that it
28 * is used in conjunction with NXP Semiconductors microcontrollers. This
29 * copyright, permission, and disclaimer notice must appear in all copies of
30 * this code.
31 */
32
33 #include "board.h"
34
74 /*****************************************************************************
75 * Private types/enumerations/variables
76 ****************************************************************************/
77
78 #if defined(BOARD_KEIL_MCB_18574357)
79 #define _ADC_CHANNLE ADC_CH1
80 #define _LPC_ADC_ID LPC_ADC0
81 #define _LPC_ADC_IRQ ADC0_IRQn
82 #define _GPDMA_CONN_ADC GPDMA_CONN_ADC_0
83 #elif defined(BOARD_HITEX_EVA_18504350)
84 #define _ADC_CHANNLE ADC_CH2
85 #define _LPC_ADC_ID LPC_ADC1
86 #define _LPC_ADC_IRQ ADC1_IRQn
87 #define _GPDMA_CONN_ADC GPDMA_CONN_ADC_1
88 #endif
89
90 static char WelcomeMenu[] = "\r\nHello NXP Semiconductors \r\n"
91 "ADC DEMO \r\n"
92 "Sample rate : 400kHz \r\n"
93 "Bit accuracy : 10 bits \r\n"
94 "Press \'c\' to continue or \'x\' to quit\r\n"
95 "Press \'o\' or \'p\' to set Sample rate\r\n"
96 "Press \'k\' or \'l\' to set Bit accuracy "
97 "(valid only when Burst mode is enabled)\r\n"
98 "Press \'b\' to ENABLE or DISABLE Burst Mode\r\n";
99 static char SelectMenu[] = "\r\nPress number 1-3 to choose ADC running mode:\r\n"
100 "\t1: Polling Mode \r\n"
101 "\t2: Interrupt Mode \r\n"
102 "\t3: DMA Mode \r\n";
103
104 static ADC_Clock_Setup_T ADCSetup;
105 static volatile uint8_t Burst_Mode_Flag = 0, Interrupt_Continue_Flag;
106 static volatile uint8_t ADC_Interrupt_Done_Flag, channelTC, dmaChannelNum;
107 uint32_t DMAbuffer;
108 /*****************************************************************************
109 * Public types/enumerations/variables
110 ****************************************************************************/
111
112 /*****************************************************************************
113 * Private functions
114 ****************************************************************************/
115
116 /* Print ADC value and delay */
117 static void App_print_ADC_value(uint16_t data)
118 {
119 volatile uint32_t j;
120 j = 5000000;
121 DEBUGOUT("ADC value is : 0x%04x\r\n", data);
122 /* Delay */
123 while (j--) {}
124 }
125
126 /* DMA routine for ADC example */
127 static void App_DMA_Test(void)
128 {
129 uint16_t dataADC;
130
131 /* Initialize GPDMA controller */
132 Chip_GPDMA_Init(LPC_GPDMA);
133 /* Setting GPDMA interrupt */
134 NVIC_DisableIRQ(DMA_IRQn);
135 NVIC_SetPriority(DMA_IRQn, ((0x01 << 3) | 0x01));
136 NVIC_EnableIRQ(DMA_IRQn);
137
138 Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
139 /* Get the free channel for DMA transfer */
140 dmaChannelNum = Chip_DMA_GetFreeChannel(LPC_GPDMA, _GPDMA_CONN_ADC);
141 /* Enable burst mode if any, the AD converter does repeated conversions
142 at the rate selected by the CLKS field in burst mode automatically */
143 if (Burst_Mode_Flag) {
144 Chip_ADC_Burst_Cmd(_LPC_ADC_ID, ENABLE);
145 }
146 /* Get adc value until get 'x' character */
147 while (DEBUGIN() != 'x') {
148 /* Start A/D conversion if not using burst mode */
149 if (!Burst_Mode_Flag) {
150 Chip_ADC_Set_StartMode(_LPC_ADC_ID, ADC_START_NOW, ADC_TRIGGERMODE_RISING);
151 }
152 channelTC = 0;
153 Chip_DMA_Transfer(LPC_GPDMA, dmaChannelNum,
154 _GPDMA_CONN_ADC,
155 (uint32_t) &DMAbuffer,
156 GPDMA_TRANSFERTYPE_P2M_CONTROLLER_DMA,
157 1);
158
159 /* Waiting for reading ADC value completed */
160 while (channelTC == 0) {}
161
162 /* Get the ADC value fron Data register*/
163 dataADC = ADC_DR_RESULT(DMAbuffer);
164 App_print_ADC_value(dataADC);
165 }
166 /* Disable interrupts, release DMA channel */
167 Chip_DMA_Stop(LPC_GPDMA, dmaChannelNum);
168 NVIC_DisableIRQ(DMA_IRQn);
169 /* Disable burst mode if any */
170 if (Burst_Mode_Flag) {
171 Chip_ADC_Burst_Cmd(_LPC_ADC_ID, DISABLE);
172 }
173 }
174
175 /* Interrupt routine for ADC example */
176 static void App_Interrupt_Test(void)
177 {
178 /* Enable ADC Interrupt */
179 NVIC_EnableIRQ(_LPC_ADC_IRQ);
180 Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
181 /* Enable burst mode if any, the AD converter does repeated conversions
182 at the rate selected by the CLKS field in burst mode automatically */
183 if (Burst_Mode_Flag) {
184 Chip_ADC_Burst_Cmd(_LPC_ADC_ID, ENABLE);
185 }
186 Interrupt_Continue_Flag = 1;
187 ADC_Interrupt_Done_Flag = 1;
188 while (Interrupt_Continue_Flag) {
189 if (!Burst_Mode_Flag && ADC_Interrupt_Done_Flag) {
190 ADC_Interrupt_Done_Flag = 0;
191 Chip_ADC_Set_StartMode(_LPC_ADC_ID, ADC_START_NOW, ADC_TRIGGERMODE_RISING);
192 }
193 }
194 /* Disable burst mode if any */
195 if (Burst_Mode_Flag) {
196 Chip_ADC_Burst_Cmd(_LPC_ADC_ID, DISABLE);
197 }
198 /* Disable ADC interrupt */
199 NVIC_DisableIRQ(_LPC_ADC_IRQ);
200 }
201
202 /* Polling routine for ADC example */
203 static void App_Polling_Test(void)
204 {
205 uint16_t dataADC;
206
207 /* Select using burst mode or not */
208 if (Burst_Mode_Flag) {
209 Chip_ADC_Burst_Cmd(_LPC_ADC_ID, ENABLE);
210 }
211 else {
212 Chip_ADC_Burst_Cmd(_LPC_ADC_ID, DISABLE);
213 }
214
215 /* Get adc value until get 'x' character */
216 while (DEBUGIN() != 'x') {
217 /* Start A/D conversion if not using burst mode */
218 if (!Burst_Mode_Flag) {
219 Chip_ADC_Set_StartMode(_LPC_ADC_ID, ADC_START_NOW, ADC_TRIGGERMODE_RISING);
220 }
221 /* Waiting for A/D conversion complete */
222 while (Chip_ADC_Read_Status(_LPC_ADC_ID, _ADC_CHANNLE, ADC_DR_DONE_STAT) != SET) {}
223 /* Read ADC value */
224 Chip_ADC_Read_Value(_LPC_ADC_ID, _ADC_CHANNLE, &dataADC);
225 /* Print ADC value */
226 App_print_ADC_value(dataADC);
227 }
228
229 /* Disable burst mode, if any */
230 if (Burst_Mode_Flag) {
231 Chip_ADC_Burst_Cmd(_LPC_ADC_ID, DISABLE);
232 }
233 }
234
235 /*****************************************************************************
236 * Public functions
237 ****************************************************************************/
238
243 void ADC0_IRQHandler(void)
244 {
245 uint16_t dataADC;
246 /* Interrupt mode: Call the stream interrupt handler */
247 NVIC_DisableIRQ(_LPC_ADC_IRQ);
248 Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, DISABLE);
249 Chip_ADC_Read_Value(_LPC_ADC_ID, _ADC_CHANNLE, &dataADC);
250 ADC_Interrupt_Done_Flag = 1;
251 App_print_ADC_value(dataADC);
252 if (DEBUGIN() != 'x') {
253 NVIC_EnableIRQ(_LPC_ADC_IRQ);
254 Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
255 }
256 else {Interrupt_Continue_Flag = 0; }
257 }
258
263 void ADC1_IRQHandler(void)
264 {
265 uint16_t dataADC;
266 /* Interrupt mode: Call the stream interrupt handler */
267 NVIC_DisableIRQ(_LPC_ADC_IRQ);
268 Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, DISABLE);
269 Chip_ADC_Read_Value(_LPC_ADC_ID, _ADC_CHANNLE, &dataADC);
270 ADC_Interrupt_Done_Flag = 1;
271 App_print_ADC_value(dataADC);
272 if (DEBUGIN() != 'x') {
273 NVIC_EnableIRQ(_LPC_ADC_IRQ);
274 Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
275 }
276 else {Interrupt_Continue_Flag = 0; }
277 }
278
283 void DMA_IRQHandler(void)
284 {
285 if (Chip_DMA_Interrupt(LPC_GPDMA, dmaChannelNum) == SUCCESS) {
286 channelTC++;
287 }
288 else {
289 /* Process error here */
290 }
291 }
292
297 int main(void)
298 {
299 bool end_Flag = false;
300 uint32_t _bitRate = 400000;
301 CHIP_ADC_RESOLUTION_T _bitAccuracy = ADC_10BITS;
302 uint8_t bufferUART;
303 Board_Init();
304 Board_ADC_Init();
305 /*ADC Init */
306 Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup);
307 Chip_ADC_Channel_Enable_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
308
309 while (!end_Flag) {
310 DEBUGOUT(WelcomeMenu);
311 while (!end_Flag) {
312 bufferUART = 0xFF;
313 bufferUART = DEBUGIN();
314 if (bufferUART == 'c') {
315 DEBUGOUT(SelectMenu);
316 bufferUART = 0xFF;
317 while (bufferUART == 0xFF) {
318 bufferUART = DEBUGIN();
319 if ((bufferUART != '1') && (bufferUART != '2') && (bufferUART != '3')) {
320 bufferUART = 0xFF;
321 }
322 }
323 switch (bufferUART) {
324 case '1': /* Polling Mode */
325 App_Polling_Test();
326 break;
327
328 case '2': /* Interrupt Mode */
329 App_Interrupt_Test();
330 break;
331
332 case '3': /* DMA mode */
333 App_DMA_Test();
334 break;
335 }
336 break;
337 }
338 else if (bufferUART == 'x') {
339 end_Flag = true;
340 DEBUGOUT("\r\nADC demo terminated!");
341 }
342 else if (bufferUART == 'o') {
343 _bitRate -= _bitRate > 0 ? 1000 : 0;
344 Chip_ADC_Set_SampleRate(_LPC_ADC_ID, &ADCSetup, _bitRate);
345 DEBUGOUT("Rate : %d Sample/s - Accuracy : %d bit \r\n", _bitRate, 10 - _bitAccuracy);
346 }
347 else if (bufferUART == 'p') {
348 _bitRate += _bitRate < 400000 ? 1000 : 0;
349 Chip_ADC_Set_SampleRate(_LPC_ADC_ID, &ADCSetup, _bitRate);
350 DEBUGOUT("Rate : %d Sample/s - Accuracy : %d bit \r\n", _bitRate, 10 - _bitAccuracy);
351 }
352 else if (bufferUART == 'k') {
353 _bitAccuracy += _bitAccuracy < ADC_3BITS ? 1 : 0;
354 Chip_ADC_Set_Resolution(_LPC_ADC_ID, &ADCSetup, _bitAccuracy);
355 DEBUGOUT("Rate : %d Sample/s - Accuracy : %d bit \r\n", _bitRate, 10 - _bitAccuracy);
356 }
357 else if (bufferUART == 'l') {
358 _bitAccuracy -= _bitAccuracy > 0 ? 1 : 0;
359 Chip_ADC_Set_Resolution(_LPC_ADC_ID, &ADCSetup, _bitAccuracy);
360 DEBUGOUT("Rate : %d Sample/s - Accuracy : %d bit \r\n", _bitRate, 10 - _bitAccuracy);
361 }
362 else if (bufferUART == 'b') {
363 Burst_Mode_Flag = !Burst_Mode_Flag;
364 ADCSetup.burstMode = Burst_Mode_Flag;
365 Chip_ADC_Set_SampleRate(_LPC_ADC_ID, &ADCSetup, _bitRate);
366 if (Burst_Mode_Flag) {
367 DEBUGOUT("Burst Mode ENABLED\r\n");
368 }
369 else {
370 DEBUGOUT("Burst Mode DISABLED\r\n");
371 }
372 }
373 }
374 }
375 return 0;
376 }
377