PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adc7scan1.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 #ifdef _DMA0IF
34 #error "This processor selection has the DMA module; this code example is incompatible with a PIC24 CPU that has DMA."
35 
36 #endif
37 
38 #define CONFIG_LED2() CONFIG_RB0_AS_DIG_OUTPUT()
39 #define LED2 _LATB0
40 
41 
42 // uncomment the next line to setup this project for a 12-bit ADC
43 #define USE_12BIT_ADC
44 
45 #ifdef USE_12BIT_ADC
46 #define ADC_LEN 12
47 #define ADC_NSTEPS 4096
48 #define ADC_12BIT_FLAG 1
49 #else
50 #define ADC_LEN 10
51 #define ADC_NSTEPS 1024
52 #define ADC_12BIT_FLAG 0
53 #endif
54 
55 
56 volatile uint16_t au16_buffer[16];
57 volatile uint8_t u8_waiting;
58 
59 /***
60  *** HERE IS THE CODE!
61  ***
62  ***/
63 
64 void _ISR _ADC1Interrupt (void) {
65  uint8_t u8_i;
66  uint16_t* au16_adcHWBuff = (uint16_t*) &ADC1BUF0;
67 
68  for ( u8_i=0; u8_i<16; u8_i++) {
69  au16_buffer[u8_i] = au16_adcHWBuff[u8_i];
70  } //end for()
71  u8_waiting = 0; // signal main() that data is ready
72  _AD1IF = 0; //clear the interrupt flag
73 
74  // toggle an LED so we can measure how often ADC IRQs are coming in
75  LED2 = !LED2;
76 }
77 
78 /** \file
79  * Performs a basic config of the ADC and samples seven channels sequentially
80  * with automatic channel scanning. ADC values are 12-bit results.
81  * Samples are obtained continuously. Uses ADC completion interrupts to get
82  * values from ADCxBUFn registers. Main routine fetches the "latest" values
83  * from memory.
84  *
85  * Conversion results are printed to screen to match adc2pots1.c project
86  * (HEX values and voltages are printed.)
87  * This is only for PIC24 CPUs without DMA.
88 */
89 
90 int main (void) {
91  uint8_t u8_i;
92  uint16_t u16_pot;
93  float f_pot;
94 
95  configBasic(HELLO_MSG);
96 
97  // make RA0/AN0/VREF+ a digital input to kill the pullup and
98  // set the TRISA bit, then make it ANALOG so the ADC will work
99  CONFIG_AN0_AS_ANALOG();
100  CONFIG_AN1_AS_ANALOG();
101  CONFIG_AN4_AS_ANALOG();
102  CONFIG_AN5_AS_ANALOG();
103  CONFIG_AN10_AS_ANALOG();
104  CONFIG_AN11_AS_ANALOG();
105  CONFIG_AN12_AS_ANALOG();
106 
107  CONFIG_LED2();
108 
109  configADC1_AutoScanIrqCH0( ADC_SCAN_AN0 | ADC_SCAN_AN1 | ADC_SCAN_AN4 | \
110  ADC_SCAN_AN5 | ADC_SCAN_AN10 | ADC_SCAN_AN11 | ADC_SCAN_AN12,
111  31, ADC_12BIT_FLAG);
112 
113  // wait for first conversion to finish before proceeding
114  while ( !AD1CON1bits.DONE) {};
115  while (1) {
116  while (u8_waiting) {}; // wait for valid data in ISR
117  u8_waiting = 0;
118  for ( u8_i=0; u8_i<16; u8_i++) {
119  u16_pot = au16_buffer[u8_i];
120  f_pot = 3.3 / ADC_NSTEPS * u16_pot;
121  printf("r");
122  if (u8_i < 10) outChar( '0'+u8_i );
123  else outChar( 'A'-10+u8_i );
124 #ifdef SMALLRAM
125  {
126  uint16_t ui16_potmv;
127  ui16_potmv = f_pot*1000;
128  printf(":0x%04X=%d mV ", u16_pot, ui16_potmv );
129  }
130 #else
131  printf(":0x%04X=%1.3fV ", u16_pot, (double) f_pot );
132 #endif
133  if ((u8_i % 4) == 3) printf("\n");
134  } //end for()
135  printf("\n");
136 
137  doHeartbeat();
138  DELAY_MS(1500);
139  } //endof while()
140 } // endof main()
141