PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adc_test.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 #include "pic24_all.h"
30 #include <stdio.h>
31 
32 /** \file
33  * Demonstrates reading the internal ADC in 10-bit mode that is
34  * connected to an external potentiometer, and printing out the input voltage.
35  *
36  */
37 
38 #define VREF 3.3 //assume Vref = 3.3 volts
39 
40 int main (void) {
41  uint16_t u16_adcVal;
42  float f_adcVal;
43 
44  configBasic(HELLO_MSG);
45  CONFIG_AN5_AS_ANALOG();
46 
47  // Configure A/D to sample AN5 for 31 Tad periods in 10-bit mode
48  // then perform a single conversion.
49  configADC1_ManualCH0(ADC_CH0_POS_SAMPLEA_AN5, 31, 0);
50  while (1) {
51  u16_adcVal = convertADC1(); //get ADC value
52  f_adcVal = u16_adcVal;
53  f_adcVal = f_adcVal/1024.0 * VREF; //convert to float 0.0 to VREF
54  printf("ADC input: %4.2f V (0x%04x)\n", (double) f_adcVal, u16_adcVal);
55  DELAY_MS(300); //delay so that we do not flood the UART.
56  doHeartbeat();
57  } //end while(1)
58 
59 }