1 /*
2 * @brief Analog Comparator 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 /* Example uses ACMP #3 */
39 #define CMP_INDEX 3
40
41 /*****************************************************************************
42 * Public types/enumerations/variables
43 ****************************************************************************/
44
45 /*****************************************************************************
46 * Private functions
47 ****************************************************************************/
48
49 /* ACMP Pin mux function - note that SystemInit() may already setup your
50 pin muxing at system startup */
Init_ACMP_PinMux(void)51 static void Init_ACMP_PinMux(void)
52 {
53 #if defined(BOARD_NXP_LPCXPRESSO_1549)
54 /* Disables pullups/pulldowns and disable digital mode */
55 Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 9, (IOCON_MODE_INACT | IOCON_ADMODE_EN));
56
57 /* Assign ADC1_1 to PIO0_9 via SWM (fixed pin) */
58 Chip_SWM_EnableFixedPin(SWM_FIXED_ADC1_1);
59 #else
60 /* Configure your own ACMP pin muxing here if needed */
61 #warning "No ACMP pin muxing defined"
62 #endif
63 }
64
65 /*****************************************************************************
66 * Public functions
67 ****************************************************************************/
68
69 /**
70 * @brief Analog comparator interrupt handler sub-routine
71 * @return Nothing
72 */
ACMP3_IRQHandler(void)73 void ACMP3_IRQHandler(void)
74 {
75 /* Clear the interrupt */
76 Chip_ACMP_ClearIntFlag(LPC_CMP, CMP_INDEX);
77 }
78
79 /**
80 * @brief Main program body
81 * @return Does not return
82 */
main(void)83 int main(void)
84 {
85 /* initialize the board */
86 SystemCoreClockUpdate();
87 Board_Init();
88 Board_LED_Set(0, false);
89
90 /* initialize the ACMP */
91 Chip_ACMP_Init(LPC_CMP);
92
93 /* Setup board specific ACMP pin muxing */
94 Init_ACMP_PinMux();
95
96 /* Positive and negative references, no hysteresis */
97 Chip_ACMP_SetupACMPRefs(LPC_CMP, CMP_INDEX, ACMP_POSIN_ADCIN_1, ACMP_NEGIN_VREF_DIV, ACMP_HYS_NONE);
98 /* Edge triggered interrupt on both edges*/
99 Chip_ACMP_SetupACMPInt(LPC_CMP, CMP_INDEX, false, false, ACMP_EDGESEL_BOTH);
100 Chip_ACMP_EnableCompInt(LPC_CMP, CMP_INDEX);
101 Chip_ACMP_SetupVoltLadder(LPC_CMP, CMP_INDEX, 0x15, false);
102 Chip_ACMP_EnableVoltLadder(LPC_CMP, CMP_INDEX);
103 /* Setup Comparator Filter register to bypass filter and use SysClk without any division */
104 Chip_ACMP_SetCompFiltReg(LPC_CMP, CMP_INDEX, ACMP_SMODE_0, ACMP_CLKDIV_1);
105 /* Enable Comparator */
106 Chip_ACMP_EnableComp(LPC_CMP, CMP_INDEX);
107
108 /* Enable the Interrupt for the compare output */
109 NVIC_EnableIRQ(CMP3_IRQ);
110
111 while (1) {
112 /* Enter low power mode until interrupt */
113 __WFI();
114
115 if (Chip_ACMP_GetCompStatus(LPC_CMP, CMP_INDEX)) {
116 Board_LED_Set(0, true);
117 }
118 else {
119 Board_LED_Set(0, false);
120 }
121 }
122
123 return 0;
124 }
125