1 /*
2  * @brief Digital to Analog converter example.
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2014
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products.  This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights.  NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers.  This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #include "board.h"
33 
34 /*****************************************************************************
35  * Private types/enumerations/variables
36  ****************************************************************************/
37 
38 /* DAC internal timer reload value to generate interrupts at 2KHz or 0.5ms*/
39 #define DAC_TIM_RELOAD ((uint32_t) 0x8C9F)
40 /* Increments for DAC input at every interrupt to generate a saw tooth wave of 100Hz*/
41 #define DAC_IN_UPDATE  ((uint32_t) 215)
42 
43 static volatile uint32_t dac_input;
44 
45 /*****************************************************************************
46  * Public types/enumerations/variables
47  ****************************************************************************/
48 
49 /*****************************************************************************
50  * Private functions
51  ****************************************************************************/
52 
53 /* DAC OUT Pin mux function - note that SystemInit() may already setup your
54    pin muxing at system startup */
Init_DAC_PinMux(void)55 static void Init_DAC_PinMux(void)
56 {
57 #if defined(BOARD_NXP_LPCXPRESSO_1549)
58 	/* Disables pullups/pulldowns and disable digital mode */
59 	Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 12, (IOCON_MODE_INACT | IOCON_ADMODE_EN));
60 
61 	/* Assign DAC_OUT to P0.12 via SWM (fixed pin) */
62 	Chip_SWM_EnableFixedPin(SWM_FIXED_DAC_OUT);
63 #else
64 	/* Configure your own ACMP pin muxing here if needed */
65 #warning "No DAC Out pin muxing defined"
66 #endif
67 }
68 
69 /*****************************************************************************
70  * Public functions
71  ****************************************************************************/
72 
73 /**
74  * @brief	DAC interrupt handler sub-routine
75  * @return	Nothing
76  */
DAC_IRQHandler(void)77 void DAC_IRQHandler(void)
78 {
79 	if (Chip_DAC_GetIntStatus(LPC_DAC)) {
80 		/* Update DAC Input value*/
81 		dac_input += DAC_IN_UPDATE;
82 		/* Boundary check for max DAC input*/
83 		if (dac_input > 4095) {
84 			/* Cycle DAC values */
85 			dac_input = 0;
86 		}
87 		/* Updating DAC Value register will clear interrupt */
88 		Chip_DAC_UpdateValue(LPC_DAC, dac_input);
89 	}
90 }
91 
92 /**
93  * @brief	Main program body
94  * @return	Does not return
95  */
main(void)96 int main(void)
97 {
98 	/* initialize the board */
99 	SystemCoreClockUpdate();
100 	Board_Init();
101 
102 	/* initialize the DAC */
103 	Chip_DAC_Init(LPC_DAC);
104 	/* Setup board specific DAC pin muxing */
105 	Init_DAC_PinMux();
106 	/* Initialize DAC input to 0 */
107 	dac_input = 0;
108 	/* Set up DAC internal timer to trigger interrupts every 0.5ms/2KHz */
109 	Chip_DAC_SetReqInterval(LPC_DAC, 2000);
110 	Chip_DAC_EnableIntTimer(LPC_DAC);
111 	/* Disable double buffering */
112 	Chip_DAC_EnableDoubleBuffer(LPC_DAC);
113 	/* Set trigger source as Internal Timer */
114 	Chip_DAC_SetTrgSrcInternal(LPC_DAC);
115 	/* Start DAC with zero voltage */
116 	Chip_DAC_UpdateValue(LPC_DAC, dac_input);
117 	/* Enable the Interrupt for DAC */
118 	NVIC_EnableIRQ(DAC_IRQ);
119 
120 	while (1) {
121 		/* Enter low power mode until DAC interrupt */
122 		__WFI();
123 	}
124 
125 	return 0;
126 }
127