LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dac.c
Go to the documentation of this file.
1 /*
2  * @brief DAC example
3  * This example show how to use the D/A Conversion in 3 modes: 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 
68 /*****************************************************************************
69  * Private types/enumerations/variables
70  ****************************************************************************/
71 #define DATA_SIZE 0x400
72 static char WelcomeMenu[] = "\r\nHello NXP Semiconductors \r\n"
73  "DAC DEMO \r\n"
74  "Press \'c\' to continue or \'x\' to quit\r\n";
75 static char SelectMenu[] = "\r\nPress number 1-3 to choose DAC running mode:\r\n"
76  "\t1: Polling Mode \r\n"
77  "\t2: Interrupt Mode \r\n"
78  "\t3: DMA Mode \r\n";
79 static volatile uint8_t channelTC, dmaChannelNum;
82 /*****************************************************************************
83  * Public types/enumerations/variables
84  ****************************************************************************/
85 
86 /*****************************************************************************
87  * Private functions
88  ****************************************************************************/
89 
90 /* DMA routine for DAC example */
91 static void App_DMA_Test(void)
92 {
93  uint32_t tmp = 0;
94  volatile uint32_t i = 0;
95 
96  /* Initialize GPDMA controller */
98  /* Setting GPDMA interrupt */
99  NVIC_DisableIRQ(DMA_IRQn);
100  NVIC_SetPriority(DMA_IRQn, ((0x01 << 3) | 0x01));
101  NVIC_EnableIRQ(DMA_IRQn);
102  /* Get the free channel for DMA transfer */
104 
105  /* Output DAC value until get 'x' character */
106  while (DEBUGIN() != 'x') {
107  /* Start D/A conversion */
108  tmp++;
109  if (tmp == (DATA_SIZE - 1)) {
110  tmp = 0;
111  }
112  /* pre-format the data to DACR register */
114  for (i = 0; i < 0x10000; i++) ;
115 
116  channelTC = 0;
118  (uint32_t) &DMAbuffer,
121  1);
122 
123  /* Waiting for writing DAC value completed */
124  while (channelTC == 0) {}
125  }
126  /* Disable interrupts, release DMA channel */
128  NVIC_DisableIRQ(DMA_IRQn);
129 }
130 
131 /* Interrupt routine for DAC example */
132 static void App_Interrupt_Test(void)
133 {
134  uint32_t tmp = 0;
135  volatile uint32_t i = 0;
136 
137  NVIC_DisableIRQ(DAC_IRQn);
138  NVIC_SetPriority(DAC_IRQn, ((0x01 << 2) | 0x01));
139 
142  while (Interrupt_Continue_Flag) {
143  tmp++;
144  if (tmp == (DATA_SIZE - 1)) {
145  tmp = 0;
146  }
150  NVIC_EnableIRQ(DAC_IRQn);
151  }
152  for (i = 0; i < 0x10000; i++) ;
153  }
154 
155  /* Disable DAC interrupt */
156  NVIC_DisableIRQ(DAC_IRQn);
157 }
158 
159 /* Polling routine for DAC example */
160 static void App_Polling_Test(void)
161 {
162  uint32_t tmp = 0;
163  volatile uint32_t i = 0;
164 
165  while (DEBUGIN() != 'x') {
166  tmp++;
167  if (tmp == (DATA_SIZE - 1)) {
168  tmp = 0;
169  }
171 
172  while (!(Chip_DAC_GetIntStatus(LPC_DAC))) {}
173 
174  for (i = 0; i < 0x10000; i++) ;
175  }
176 }
177 
178 /*****************************************************************************
179  * Public functions
180  ****************************************************************************/
181 
186 void DAC_IRQHandler(void)
187 {
188  NVIC_DisableIRQ(DAC_IRQn);
190  if (DEBUGIN() == 'x') {
192  }
193 }
194 
199 void DMA_IRQHandler(void)
200 {
202  channelTC++;
203  }
204  else {
205  /* Process error here */
206  }
207 }
208 
213 int main(void)
214 {
215  bool end_Flag = false;
216  uint8_t bufferUART;
217 
218  Board_Init();
220  /* DAC Init */
222  /* set time out for DAC*/
225 
226  while (!end_Flag) {
228  while (!end_Flag) {
229  bufferUART = 0xFF;
230  bufferUART = DEBUGIN();
231  if (bufferUART == 'c') {
233  bufferUART = 0xFF;
234  while (bufferUART == 0xFF) {
235  bufferUART = DEBUGIN();
236  if ((bufferUART != '1') && (bufferUART != '2') && (bufferUART != '3')) {
237  bufferUART = 0xFF;
238  }
239  }
240  switch (bufferUART) {
241  case '1': /* Polling Mode */
243  break;
244 
245  case '2': /* Interrupt Mode */
247  break;
248 
249  case '3': /* DMA mode */
250  App_DMA_Test();
251  break;
252  }
253  break;
254  }
255  else if (bufferUART == 'x') {
256  end_Flag = true;
257  DEBUGOUT("\r\nDAC demo terminated!");
258  }
259  }
260  }
261  return 0;
262 }
263