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
00039 #define CONFIG_LED2() CONFIG_RB5_AS_DIG_OUTPUT()
00040 #define LED2 _LATB5
00041
00042 #define ADC_LEN 10
00043 #define ADC_NSTEPS 1024
00044 #define ADC_12BIT_FLAG 0
00045
00046 uint16 au16_buffer[8];
00047 volatile uint16 au16_sum[8];
00048 volatile uint8 u8_gotData;
00049
00050
00051
00052
00053
00054 void _ISR _ADC1Interrupt (void) {
00055 static uint8 u8_adcCount=64;
00056 uint8 u8_i;
00057 uint16* au16_adcHWBuff = (uint16*) &ADC1BUF0;
00058
00059
00060
00061
00062
00063 if (AD1CON2 & ADC_ALT_BUF_STATUS_2) {
00064 for ( u8_i=0; u8_i<8; u8_i++) {
00065 au16_buffer[u8_i] += au16_adcHWBuff[u8_i];
00066 }
00067 } else {
00068 for ( u8_i=8; u8_i<16; u8_i++) {
00069 au16_buffer[u8_i-8] += au16_adcHWBuff[u8_i];
00070 }
00071 }
00072
00073 _AD1IF = 0;
00074
00075 SET_SAMP_BIT_ADC1();
00076
00077 u8_adcCount--;
00078 if (u8_adcCount==0) {
00079 u8_adcCount = 64;
00080 u8_gotData = 1;
00081 for ( u8_i=0; u8_i<8; u8_i++) {
00082 au16_sum[u8_i] = au16_buffer[u8_i];
00083 au16_buffer[u8_i] = 0;
00084 }
00085 };
00086
00087
00088 LED2 = !LED2;
00089 }
00090
00103 int main (void) {
00104 uint8 u8_i;
00105 uint16 u16_pot;
00106 uint32 u32_ticks;
00107 float f_pot;
00108
00109 configBasic(HELLO_MSG);
00110
00111
00112
00113 CONFIG_AN0_AS_ANALOG();
00114 CONFIG_AN1_AS_ANALOG();
00115 CONFIG_AN2_AS_ANALOG();
00116 CONFIG_AN12_AS_ANALOG();
00117
00118 CONFIG_LED2();
00119
00120 u8_gotData = 0;
00121
00122
00123 T3CONbits.TON = 0;
00124 T2CONbits.TON = 0;
00125 T2CON = T2_32BIT_MODE_ON | T2_PS_1_1 | T2_SOURCE_INT;
00126 TMR3 = 0;
00127 TMR2 = 0;
00128 u32_ticks = usToU32Ticks(15625, getTimerPrescale(T2CONbits)) - 1;
00129 PR3 = u32_ticks>>16;
00130 PR2 = u32_ticks & 0xFFFF;
00131 T2CONbits.TON = 1;
00132
00133 configADC1_Simul4ChanIrq(2, ADC_CH123_POS_SAMPLEA_AN0AN1AN2, ADC_CONV_CLK_10Tcy );
00134 SET_SAMP_BIT_ADC1();
00135
00136 while (1) {
00137 while (!u8_gotData) {
00138 doHeartbeat();
00139 }
00140 u8_gotData = 0;
00141 for ( u8_i=0; u8_i<4; u8_i++) {
00142 u16_pot = au16_sum[u8_i];
00143 f_pot = (3.3 / 1023 / 64 ) * u16_pot;
00144 printf("r");
00145 outChar( '0'+u8_i );
00146 printf(":0x%04X=%1.3fV ", u16_pot, (double) f_pot );
00147 }
00148 printf("\n");
00149 }
00150 }