LPCOpen Platform  v1.03
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 
78 /*****************************************************************************
79  * Private types/enumerations/variables
80  ****************************************************************************/
81 #ifdef BOARD_NXP_XPRESSO_1769
82 #define _ADC_CHANNLE ADC_CH0
83 #else
84 #define _ADC_CHANNLE ADC_CH2
85 #endif
86 #define _LPC_ADC_ID LPC_ADC
87 #define _LPC_ADC_IRQ ADC_IRQn
88 #define _GPDMA_CONN_ADC GPDMA_CONN_ADC
89 static char WelcomeMenu[] = "\r\nHello NXP Semiconductors \r\n"
90  "ADC DEMO \r\n"
91 #if defined(CHIP_LPC175X_6X)
92  "Sample rate : 200kHz \r\n"
93 #else
94  "Sample rate : 400kHz \r\n"
95 #endif
96  "Press \'c\' to continue or \'x\' to quit\r\n"
97  "Press \'o\' or \'p\' to set Sample rate\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 
105 static volatile uint8_t Burst_Mode_Flag = 0, Interrupt_Continue_Flag;
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 */
133  /* Setting GPDMA interrupt */
134  NVIC_DisableIRQ(DMA_IRQn);
135  NVIC_SetPriority(DMA_IRQn, ((0x01 << 3) | 0x01));
136  NVIC_EnableIRQ(DMA_IRQn);
137  /* Setting ADC interrupt, ADC Interrupt must be disable in DMA mode */
138  NVIC_DisableIRQ(_LPC_ADC_IRQ);
139  Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
140  /* Get the free channel for DMA transfer */
141  dmaChannelNum = Chip_DMA_GetFreeChannel(LPC_GPDMA, _GPDMA_CONN_ADC);
142  /* Enable burst mode if any, the AD converter does repeated conversions
143  at the rate selected by the CLKS field in burst mode automatically */
144  if (Burst_Mode_Flag) {
145  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, ENABLE);
146  }
147  /* Get adc value until get 'x' character */
148  while (DEBUGIN() != 'x') {
149  /* Start A/D conversion if not using burst mode */
150  if (!Burst_Mode_Flag) {
152  }
153  channelTC = 0;
155  _GPDMA_CONN_ADC,
156  (uint32_t) &DMAbuffer,
158  1);
159 
160  /* Waiting for reading ADC value completed */
161  while (channelTC == 0) {}
162 
163  /* Get the ADC value fron Data register*/
164  dataADC = ADC_DR_RESULT(DMAbuffer);
165  App_print_ADC_value(dataADC);
166  }
167  /* Disable interrupts, release DMA channel */
169  NVIC_DisableIRQ(DMA_IRQn);
170  /* Disable burst mode if any */
171  if (Burst_Mode_Flag) {
172  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, DISABLE);
173  }
174 }
175 
176 /* Interrupt routine for ADC example */
177 static void App_Interrupt_Test(void)
178 {
179  /* Enable ADC Interrupt */
180  NVIC_EnableIRQ(_LPC_ADC_IRQ);
181  Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
182  /* Enable burst mode if any, the AD converter does repeated conversions
183  at the rate selected by the CLKS field in burst mode automatically */
184  if (Burst_Mode_Flag) {
185  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, ENABLE);
186  }
189  while (Interrupt_Continue_Flag) {
193  }
194  }
195  /* Disable burst mode if any */
196  if (Burst_Mode_Flag) {
197  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, DISABLE);
198  }
199  /* Disable ADC interrupt */
200  NVIC_DisableIRQ(_LPC_ADC_IRQ);
201 }
202 
203 /* Polling routine for ADC example */
204 static void App_Polling_Test(void)
205 {
206  uint16_t dataADC;
207 
208  /* Select using burst mode or not */
209  if (Burst_Mode_Flag) {
210  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, ENABLE);
211  }
212  else {
213  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, DISABLE);
214  }
215 
216  /* Get adc value until get 'x' character */
217  while (DEBUGIN() != 'x') {
218  /* Start A/D conversion if not using burst mode */
219  if (!Burst_Mode_Flag) {
221  }
222  /* Waiting for A/D conversion complete */
223  while (Chip_ADC_Read_Status(_LPC_ADC_ID, _ADC_CHANNLE, ADC_DR_DONE_STAT) != SET) {}
224  /* Read ADC value */
225  Chip_ADC_Read_Value(_LPC_ADC_ID, _ADC_CHANNLE, &dataADC);
226  /* Print ADC value */
227  App_print_ADC_value(dataADC);
228  }
229 
230  /* Disable burst mode, if any */
231  if (Burst_Mode_Flag) {
232  Chip_ADC_Burst_Cmd(_LPC_ADC_ID, DISABLE);
233  }
234 }
235 
236 /*****************************************************************************
237  * Public functions
238  ****************************************************************************/
239 
244 void ADC_IRQHandler(void)
245 {
246  uint16_t dataADC;
247  /* Interrupt mode: Call the stream interrupt handler */
248  NVIC_DisableIRQ(_LPC_ADC_IRQ);
249  Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, DISABLE);
250  Chip_ADC_Read_Value(_LPC_ADC_ID, _ADC_CHANNLE, &dataADC);
252  App_print_ADC_value(dataADC);
253  if (DEBUGIN() != 'x') {
254  NVIC_EnableIRQ(_LPC_ADC_IRQ);
255  Chip_ADC_Channel_Int_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
256  }
257  else {Interrupt_Continue_Flag = 0; }
258 }
259 
264 void DMA_IRQHandler(void)
265 {
267  channelTC++;
268  }
269  else {
270  /* Process error here */
271  }
272 }
273 
278 int main(void)
279 {
280  bool end_Flag = false;
281  uint32_t _bitRate = ADC_MAX_SAMPLE_RATE;
282  uint8_t bufferUART;
283 
284  Board_Init();
285 
286  /* Chip_IOCON_PinMux(0, 25, IOCON_ADMODE_EN, IOCON_FUNC1); */
287  /*ADC Init */
288  Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup);
289  Chip_ADC_Channel_Enable_Cmd(_LPC_ADC_ID, _ADC_CHANNLE, ENABLE);
290 
291  while (!end_Flag) {
293  while (!end_Flag) {
294  bufferUART = 0xFF;
295  bufferUART = DEBUGIN();
296  if (bufferUART == 'c') {
298  bufferUART = 0xFF;
299  while (bufferUART == 0xFF) {
300  bufferUART = DEBUGIN();
301  if ((bufferUART != '1') && (bufferUART != '2') && (bufferUART != '3')) {
302  bufferUART = 0xFF;
303  }
304  }
305  switch (bufferUART) {
306  case '1': /* Polling Mode */
308  break;
309 
310  case '2': /* Interrupt Mode */
312  break;
313 
314  case '3': /* DMA mode */
315  App_DMA_Test();
316  break;
317  }
318  break;
319  }
320  else if (bufferUART == 'x') {
321  end_Flag = true;
322  DEBUGOUT("\r\nADC demo terminated!");
323  }
324  else if (bufferUART == 'o') {
325  _bitRate -= _bitRate > 0 ? 1000 : 0;
326  Chip_ADC_Set_SampleRate(_LPC_ADC_ID, &ADCSetup, _bitRate);
327  DEBUGOUT("Rate : %d Sample/s\r\n", _bitRate);
328  }
329  else if (bufferUART == 'p') {
330  _bitRate += _bitRate < 400000 ? 1000 : 0;
331  Chip_ADC_Set_SampleRate(_LPC_ADC_ID, &ADCSetup, _bitRate);
332  DEBUGOUT("Rate : %d Sample/s\r\n", _bitRate);
333  }
334  else if (bufferUART == 'b') {
336  ADCSetup.burstMode = Burst_Mode_Flag;
337  Chip_ADC_Set_SampleRate(_LPC_ADC_ID, &ADCSetup, _bitRate);
338  if (Burst_Mode_Flag) {
339  DEBUGOUT("Burst Mode ENABLED\r\n");
340  }
341  else {
342  DEBUGOUT("Burst Mode DISABLED\r\n");
343  }
344  }
345  }
346  }
347  return 0;
348 }
349