/
adc_polling_test.c
189 lines (171 loc) · 5.81 KB
/
adc_polling_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**********************************************************************
* $Id$ adc_polling_test.c 2010-05-21
*//**
* @file adc_polling_test.c
* @brief This example describes how to use ADC conversion in
* polling mode
* @version 2.0
* @date 21. May. 2010
* @author NXP MCU SW Application Team
*
* Copyright(C) 2010, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
**********************************************************************/
#include "lpc17xx_adc.h"
#include "lpc17xx_libcfg.h"
#include "lpc17xx_pinsel.h"
#include "debug_frmwrk.h"
/* Example group ----------------------------------------------------------- */
/** @defgroup ADC_Polling Polling
* @ingroup ADC_Examples
* @{
*/
/************************** PRIVATE DEFINITIONS ***********************/
#define MCB_LPC_1768
//#define IAR_LPC_1768
#ifdef MCB_LPC_1768
#define _ADC_INT ADC_ADINTEN2
#define _ADC_CHANNEL ADC_CHANNEL_2
#elif defined (IAR_LPC_1768)
#define _ADC_INT ADC_ADINTEN5
#define _ADC_CHANNEL ADC_CHANNEL_5
#endif
/************************** PRIVATE VARIABLES *************************/
uint8_t menu1[] =
"********************************************************************************\n\r"
"Hello NXP Semiconductors \n\r"
" ADC demo \n\r"
"\t - MCU: LPC1768 \n\r"
"\t - Core: ARM CORTEX-M3 \n\r"
"\t - Communicate via: UART0 - 115200 bps \n\r"
" Use ADC with 12-bit resolution rate of 200KHz, read in polling mode\n\r"
" To get ADC value and display via UART0\n\r"
" Turn the potentiometer to see how ADC value changes\n\r"
"********************************************************************************\n\r";
/************************** PRIVATE FUNCTION *************************/
void print_menu(void);
/*-------------------------PRIVATE FUNCTIONS------------------------------*/
/*********************************************************************//**
* @brief Print menu
* @param[in] None
* @return None
**********************************************************************/
void print_menu(void)
{
_DBG(menu1);
}
/*-------------------------MAIN FUNCTION------------------------------*/
/*********************************************************************//**
* @brief c_entry: Main ADC program body
* @param[in] None
* @return int
**********************************************************************/
int c_entry(void)
{
PINSEL_CFG_Type PinCfg;
uint32_t adc_value, tmp;
/* Initialize debug via UART0
* – 115200bps
* – 8 data bit
* – No parity
* – 1 stop bit
* – No flow control
*/
debug_frmwrk_init();
// print welcome screen
print_menu();
/* Initialize ADC ----------------------------------------------------*/
/* Because the potentiometer on different boards (MCB & IAR) connect
* with different ADC channel, so we have to configure correct ADC channel
* on each board respectively.
* If you want to check other ADC channels, you have to wire this ADC pin directly
* to potentiometer pin (please see schematic doc for more reference)
*/
#ifdef MCB_LPC_1768
/*
* Init ADC pin connect
* AD0.2 on P0.25
*/
PinCfg.Funcnum = 1;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Pinnum = 25;
PinCfg.Portnum = 0;
PINSEL_ConfigPin(&PinCfg);
#elif defined (IAR_LPC_1768)
/*
* Init ADC pin connect
* AD0.5 on P1.31
*/
PinCfg.Funcnum = 3;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Pinnum = 31;
PinCfg.Portnum = 1;
PINSEL_ConfigPin(&PinCfg);
#endif
/* Configuration for ADC :
* Select: ADC channel 2 (if using MCB1700 board)
* ADC channel 5 (if using IAR-LPC1768 board)
* ADC conversion rate = 200Khz
*/
ADC_Init(LPC_ADC, 200000);
ADC_IntConfig(LPC_ADC,_ADC_INT,DISABLE);
ADC_ChannelCmd(LPC_ADC,_ADC_CHANNEL,ENABLE);
while(1)
{
// Start conversion
ADC_StartCmd(LPC_ADC,ADC_START_NOW);
//Wait conversion complete
while (!(ADC_ChannelGetStatus(LPC_ADC,_ADC_CHANNEL,ADC_DATA_DONE)));
adc_value = ADC_ChannelGetData(LPC_ADC,_ADC_CHANNEL);
//Display the result of conversion on the UART0
#ifdef MCB_LPC_1768
_DBG("ADC value on channel 2: ");
#elif defined (IAR_LPC_1768)
_DBG("ADC value on channel 5: ");
#endif
_DBD32(adc_value);
_DBG_("");
//delay
for(tmp = 0; tmp < 1000000; tmp++);
}
ADC_DeInit(LPC_ADC);
return 1;
}
/* Support required entry point for other toolchain */
int main (void)
{
return c_entry();
}
#ifdef DEBUG
/*******************************************************************************
* @brief Reports the name of the source file and the source line number
* where the CHECK_PARAM error has occurred.
* @param[in] file Pointer to the source file name
* @param[in] line assert_param error line source number
* @return None
*******************************************************************************/
void check_failed(uint8_t *file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while(1);
}
#endif
/*
* @}
*/