LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 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 
62 /*****************************************************************************
63  * Private types/enumerations/variables
64  ****************************************************************************/
65 
66 #if defined(BOARD_KEIL_MCB_18574357)
67 #define _ADC_CHANNLE ADC_CH1
68 #define _LPC_ADC_ID LPC_ADC0
69 #define _LPC_ADC_IRQ ADC0_IRQn
70 #define _GPDMA_CONN_ADC GPDMA_CONN_ADC_0
71 #elif defined(BOARD_HITEX_EVA_18504350)
72 #define _ADC_CHANNLE ADC_CH2
73 #define _LPC_ADC_ID LPC_ADC1
74 #define _LPC_ADC_IRQ ADC1_IRQn
75 #define _GPDMA_CONN_ADC GPDMA_CONN_ADC_1
76 #endif
77 
78 static char WelcomeMenu[] = "\r\nHello NXP Semiconductors \r\n"
79  "ADC DEMO \r\n"
80  "Sample rate : 400kHz \r\n"
81  "Bit accuracy : 10 bits \r\n"
82  "Press \'c\' to continue or \'x\' to quit\r\n"
83  "Press \'o\' or \'p\' to set Sample rate "
84  "(valid only when Burst mode is enabled)\r\n"
85  "Press \'k\' or \'l\' to set Bit accuracy\r\n"
86  "Press \'b\' to ENABLE or DISABLE Burst Mode\r\n";
87 static char SelectMenu[] = "\r\nPress number 1-3 to choose ADC running mode:\r\n"
88  "\t1: Polling Mode \r\n"
89  "\t2: Interrupt Mode \r\n"
90  "\t3: DMA Mode \r\n";
91 static uint8_t mode;
93 static volatile uint8_t Burst_Mode_Flag = 0, Interrupt_Continue_Flag;
96 /*****************************************************************************
97  * Public types/enumerations/variables
98  ****************************************************************************/
99 
100 /*****************************************************************************
101  * Private functions
102  ****************************************************************************/
103 
104 /* Print ADC value and delay */
105 static void App_print_ADC_value(uint16_t data)
106 {
107  volatile uint32_t j;
108  j = 5000000;
109  DEBUGOUT("ADC value is : 0x%04x\r\n", data);
110  /* Delay */
111  while (j--) {}
112 }
113 
114 /* DMA routine for ADC example */
115 static void App_DMA_Test(void)
116 {
117  uint16_t dataADC;
118 
119  /* Initialize GPDMA controller */
120  Chip_GPDMA_Init();
121  /* Setting GPDMA interrupt */
122  NVIC_DisableIRQ(DMA_IRQn);
123  NVIC_SetPriority(DMA_IRQn, ((0x01 << 3) | 0x01));
124  NVIC_EnableIRQ(DMA_IRQn);
125  /* Setting ADC interrupt, ADC Interrupt must be enable in DMA mode */
126  NVIC_EnableIRQ(_LPC_ADC_IRQ);
127  Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
128  /* Get the free channel for DMA transfer */
129  dmaChannelNum = Chip_DMA_GetFreeChannel(_GPDMA_CONN_ADC);
130  /* Enable burst mode if any, the AD converter does repeated conversions
131  at the rate selected by the CLKS field in burst mode automatically */
132  if (Burst_Mode_Flag) {
133  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, ENABLE);
134  }
135  /* Get adc value until get 'x' character */
136  while (DEBUGIN() != 'x') {
137  /* Start A/D conversion if not using burst mode */
138  if (!Burst_Mode_Flag) {
140  }
141  channelTC = 0;
143  _GPDMA_CONN_ADC,
144  (uint32_t) &DMAbuffer,
146  1);
147 
148  /* Waiting for reading ADC value completed */
149  while (channelTC == 0) {}
150 
151  /* Get the ADC value fron Data register*/
152  dataADC = ADC_DR_RESULT(DMAbuffer);
153  App_print_ADC_value(dataADC);
154  }
155  /* Disable interrupts, release DMA channel */
157  NVIC_DisableIRQ(DMA_IRQn);
158  NVIC_DisableIRQ(_LPC_ADC_IRQ);
159  /* Disable burst mode if any */
160  if (Burst_Mode_Flag) {
161  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, DISABLE);
162  }
163 }
164 
165 /* Interrupt routine for ADC example */
166 static void App_Interrupt_Test(void)
167 {
168  /* Enable ADC Interrupt */
169  NVIC_EnableIRQ(_LPC_ADC_IRQ);
170  Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
171  /* Enable burst mode if any, the AD converter does repeated conversions
172  at the rate selected by the CLKS field in burst mode automatically */
173  if (Burst_Mode_Flag) {
174  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, ENABLE);
175  }
178  while (Interrupt_Continue_Flag) {
182  }
183  }
184  /* Disable burst mode if any */
185  if (Burst_Mode_Flag) {
186  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, DISABLE);
187  }
188  /* Disable ADC interrupt */
189  NVIC_DisableIRQ(_LPC_ADC_IRQ);
190 }
191 
192 /* Polling routine for ADC example */
193 static void App_Polling_Test(void)
194 {
195  uint16_t dataADC;
196 
197  /* Select using burst mode or not */
198  if (Burst_Mode_Flag) {
199  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, ENABLE);
200  }
201  else {
202  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, DISABLE);
203  }
204 
205  /* Get adc value until get 'x' character */
206  while (DEBUGIN() != 'x') {
207  /* Start A/D conversion if not using burst mode */
208  if (!Burst_Mode_Flag) {
210  }
211  /* Waiting for A/D conversion complete */
212  while (Chip_ADC_Read_Status(_LPC_ADC_ID, _ADC_CHANNLE, ADC_DR_DONE_STAT) != SET) {}
213  /* Read ADC value */
214  Chip_ADC_Read_Value(_LPC_ADC_ID, _ADC_CHANNLE, &dataADC);
215  /* Print ADC value */
216  App_print_ADC_value(dataADC);
217  }
218 
219  /* Disable burst mode, if any */
220  if (Burst_Mode_Flag) {
221  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, DISABLE);
222  }
223 }
224 
225 /*****************************************************************************
226  * Public functions
227  ****************************************************************************/
228 
233 void ADC0_IRQHandler(void)
234 {
235  uint16_t dataADC;
236 
237  if (mode == 2) {
238  /* DMA mode: The ADC interrupt must be enable in DMA mode.
239  Read the value to clear the interrupt flag */
240  Chip_ADC_Read_Value(_LPC_ADC_ID, _ADC_CHANNLE, &dataADC);
241  }
242  else {
243  /* Interrupt mode: Call the stream interrupt handler */
244  NVIC_DisableIRQ(_LPC_ADC_IRQ);
245  Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, DISABLE);
246  Chip_ADC_Read_Value(_LPC_ADC_ID, _ADC_CHANNLE, &dataADC);
248  App_print_ADC_value(dataADC);
249  if (DEBUGIN() != 'x') {
250  NVIC_EnableIRQ(_LPC_ADC_IRQ);
251  Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
252  }
253  else {Interrupt_Continue_Flag = 0; }
254  }
255 }
256 
261 void ADC1_IRQHandler(void)
262 {
263  uint16_t dataADC;
264 
265  if (mode == 2) {
266  /* DMA mode: The ADC interrupt must be enable in DMA mode.
267  Read the value to clear the interrupt flag */
268  Chip_ADC_Read_Value(_LPC_ADC_ID, _ADC_CHANNLE, &dataADC);
269  }
270  else {
271  /* Interrupt mode: Call the stream interrupt handler */
272  NVIC_DisableIRQ(_LPC_ADC_IRQ);
273  Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, DISABLE);
274  Chip_ADC_Read_Value(_LPC_ADC_ID, _ADC_CHANNLE, &dataADC);
276  App_print_ADC_value(dataADC);
277  if (DEBUGIN() != 'x') {
278  NVIC_EnableIRQ(_LPC_ADC_IRQ);
279  Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
280  }
281  else {Interrupt_Continue_Flag = 0; }
282  }
283 }
284 
289 void DMA_IRQHandler(void)
290 {
292  channelTC++;
293  }
294  else {
295  /* Process error here */
296  }
297 }
298 
303 int main(void)
304 {
305  bool end_Flag = false;
306  uint32_t _bitRate = 400000;
307  ADC_Resolution _bitAccuracy = ADC_10BITS;
308  uint8_t bufferUART;
309  Board_Init();
310  Board_ADC_Init();
311  /*ADC Init */
312  Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup);
313  Chip_ADC_Channel_Enable_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
314 
315  while (!end_Flag) {
317  while (!end_Flag) {
318  bufferUART = 0xFF;
319  bufferUART = DEBUGIN();
320  if (bufferUART == 'c') {
322  bufferUART = 0xFF;
323  while (bufferUART == 0xFF) {
324  bufferUART = DEBUGIN();
325  if ((bufferUART != '1') && (bufferUART != '2') && (bufferUART != '3')) {
326  bufferUART = 0xFF;
327  }
328  }
329  switch (bufferUART) {
330  case '1': /* Polling Mode */
331  mode = 0;
333  break;
334 
335  case '2': /* Interrupt Mode */
336  mode = 1;
338  break;
339 
340  case '3': /* DMA mode */
341  mode = 2;
342  App_DMA_Test();
343  break;
344  }
345  break;
346  }
347  else if (bufferUART == 'x') {
348  end_Flag = true;
349  DEBUGOUT("\r\nADC demo terminated!");
350  }
351  else if (bufferUART == 'o') {
352  _bitRate -= _bitRate > 0 ? 1000 : 0;
353  Chip_ADC_Set_SampleRate(_LPC_ADC_ID, &ADCSetup, _bitRate);
354  DEBUGOUT("Rate : %d Sample/s - Accuracy : %d bit \r\n", _bitRate, 10 - _bitAccuracy);
355  }
356  else if (bufferUART == 'p') {
357  _bitRate += _bitRate < 400000 ? 1000 : 0;
358  Chip_ADC_Set_SampleRate(_LPC_ADC_ID, &ADCSetup, _bitRate);
359  DEBUGOUT("Rate : %d Sample/s - Accuracy : %d bit \r\n", _bitRate, 10 - _bitAccuracy);
360  }
361  else if (bufferUART == 'k') {
362  _bitAccuracy += _bitAccuracy < ADC_3BITS ? 1 : 0;
363  Chip_ADC_Set_Resolution(_LPC_ADC_ID, &ADCSetup, _bitAccuracy);
364  DEBUGOUT("Rate : %d Sample/s - Accuracy : %d bit \r\n", _bitRate, 10 - _bitAccuracy);
365  }
366  else if (bufferUART == 'l') {
367  _bitAccuracy -= _bitAccuracy > 0 ? 1 : 0;
368  Chip_ADC_Set_Resolution(_LPC_ADC_ID, &ADCSetup, _bitAccuracy);
369  DEBUGOUT("Rate : %d Sample/s - Accuracy : %d bit \r\n", _bitRate, 10 - _bitAccuracy);
370  }
371  else if (bufferUART == 'b') {
373  if (Burst_Mode_Flag) {
374  DEBUGOUT("Burst Mode ENABLED\r\n");
375  }
376  else {
377  DEBUGOUT("Burst Mode DISABLED\r\n");
378  }
379  }
380  }
381  }
382  return 0;
383 }
384