00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "pic24_all.h"
00031 #include "stdio.h"
00032
00033 #ifdef _DMA0IF
00034 #error "This processor selection has the DMA module; this code example is incompatible with a PIC24 CPU that has DMA."
00035
00036 #endif
00037
00038 #define CONFIG_LED2() CONFIG_RB0_AS_DIG_OUTPUT()
00039 #define LED2 _LATB0
00040
00041
00042
00043 #define USE_12BIT_ADC
00044
00045 #ifdef USE_12BIT_ADC
00046 #define ADC_LEN 12
00047 #define ADC_NSTEPS 4096
00048 #define ADC_12BIT_FLAG 1
00049 #else
00050 #define ADC_LEN 10
00051 #define ADC_NSTEPS 1024
00052 #define ADC_12BIT_FLAG 0
00053 #endif
00054
00055
00056 volatile uint16 au16_buffer[16];
00057 volatile uint8 u8_waiting;
00058
00059
00060
00061
00062
00063
00064 void _ISR _ADC1Interrupt (void) {
00065 uint8 u8_i;
00066 uint16* au16_adcHWBuff = (uint16*) &ADC1BUF0;
00067
00068 for ( u8_i=0; u8_i<16; u8_i++) {
00069 au16_buffer[u8_i] = au16_adcHWBuff[u8_i];
00070 }
00071 u8_waiting = 0;
00072 _AD1IF = 0;
00073
00074
00075 LED2 = !LED2;
00076 }
00077
00090 int main (void) {
00091 uint8 u8_i;
00092 uint16 u16_pot;
00093 float f_pot;
00094
00095 configBasic(HELLO_MSG);
00096
00097
00098
00099 CONFIG_AN0_AS_ANALOG();
00100 CONFIG_AN1_AS_ANALOG();
00101 CONFIG_AN4_AS_ANALOG();
00102 CONFIG_AN5_AS_ANALOG();
00103 CONFIG_AN10_AS_ANALOG();
00104 CONFIG_AN11_AS_ANALOG();
00105 CONFIG_AN12_AS_ANALOG();
00106
00107 CONFIG_LED2();
00108
00109 configADC1_AutoScanIrqCH0( ADC_SCAN_AN0 | ADC_SCAN_AN1 | ADC_SCAN_AN4 | \
00110 ADC_SCAN_AN5 | ADC_SCAN_AN10 | ADC_SCAN_AN11 | ADC_SCAN_AN12,
00111 31, ADC_12BIT_FLAG);
00112
00113
00114 while ( !AD1CON1bits.DONE) {};
00115 while (1) {
00116 while (u8_waiting){};
00117 u8_waiting = 0;
00118 for ( u8_i=0; u8_i<16; u8_i++) {
00119 u16_pot = au16_buffer[u8_i];
00120 f_pot = 3.3 / ADC_NSTEPS * u16_pot;
00121 printf("r");
00122 if (u8_i < 10) outChar( '0'+u8_i );
00123 else outChar( 'A'-10+u8_i );
00124 printf(":0x%04X=%1.3fV ", u16_pot, (double) f_pot );
00125 if ((u8_i % 4) == 3) printf("\n");
00126 }
00127 printf("\n");
00128
00129 doHeartbeat();
00130 DELAY_MS(1500);
00131 }
00132 }
00133