PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adc2pots1.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 
30 #include "pic24_all.h"
31 #include "stdio.h"
32 
33 // uncomment the next line to setup this project for a 12-bit ADC
34 #define USE_12BIT_ADC
35 
36 #ifdef USE_12BIT_ADC
37 #define ADC_LEN 12
38 #define ADC_NSTEPS 4096
39 #define ADC_12BIT_FLAG 1
40 #else
41 #define ADC_LEN 10
42 #define ADC_NSTEPS 1024
43 #define ADC_12BIT_FLAG 0
44 #endif
45 
46 /** \file
47  * Performs a basic config of the ADC and samples two channels manually
48  * and sequentially with 12-bit results.
49  * Conversion results are printed to screen as both HEX values and voltages.
50 */
51 
52 int main (void) {
53  uint16_t u16_pot1, u16_pot2;
54  float f_pot1, f_pot2;
55 
56  configBasic(HELLO_MSG);
57 
58  // make RA0/AN0/VREF+ a digital input to kill the pullup and
59  // set the TRISA bit, then make it ANALOG so the ADC will work
60  CONFIG_AN0_AS_ANALOG();
61  CONFIG_AN1_AS_ANALOG();
62 
63  while (1) {
64  configADC1_ManualCH0( ADC_CH0_POS_SAMPLEA_AN0, 31, ADC_12BIT_FLAG );
65  u16_pot1 = convertADC1();
66  configADC1_ManualCH0( ADC_CH0_POS_SAMPLEA_AN1, 31, ADC_12BIT_FLAG );
67  u16_pot2 = convertADC1();
68 
69  f_pot1 = 3.30 / ADC_NSTEPS * u16_pot1;
70  f_pot2 = 3.30 / ADC_NSTEPS * u16_pot2;
71 #ifdef SMALLRAM
72  {
73  //use integers to avoid floating point printf which does not fit in this data space
74  uint16_t ui16_pot1mv,ui16_pot2mv;
75  ui16_pot1mv = f_pot1*1000;
76  ui16_pot2mv = f_pot2*1000;
77  printf("AN0 is 0x%0X or %d mV. | AN1 is 0x%0X or %d mV.\n", \
78  u16_pot1, ui16_pot1mv, u16_pot2, ui16_pot2mv );
79  }
80 
81 #else
82  printf("AN0 is 0x%0X or %1.4fV. | AN1 is 0x%0X or %1.4fV.\n", \
83  u16_pot1, (double) f_pot1, u16_pot2, (double) f_pot2 );
84 #endif
85  DELAY_MS(1500);
86 
87  } //endof while()
88 } // endof main()
89