35 #error "This processor selection does not have a DMA module; this code example is incompatible with a PIC24 CPU that does not have DMA."
40 #define CONFIG_LED2() CONFIG_RB5_AS_DIG_OUTPUT()
44 #define ADC_NSTEPS 1024
45 #define ADC_12BIT_FLAG 0
49 #define CONVERSIONS_PER_INPUT 1 //for this example, assumed always to be '1'
50 #define MAX_CHANNELS 16
52 #define MAX_TRANSFER (CONVERSIONS_PER_INPUT*MAX_CHANNELS) //make power of two for alignment to work
57 uint16_t au16_bufferA[MAX_TRANSFER] __attribute__((space(dma),aligned(MAX_TRANSFER*2)));
58 uint16_t au16_bufferB[MAX_TRANSFER] __attribute__((space(dma),aligned(MAX_TRANSFER*2)));
71 AD1CON1 = ADC_CLK_TMR | ADC_SAMPLE_SIMULTANEOUS | ADC_ADDMABM_ORDER;
72 AD1CON3 = (u16_numTcyMask & 0x00FF);
73 AD1CON2 = ADC_VREF_AVDD_AVSS | ADC_CONVERT_CH0123;
74 #if (defined(__PIC24H__)|| defined(__dsPIC33F__))
75 AD1CHS0 = ADC_CH0_NEG_SAMPLEA_VREFN | (u8_ch0Select & 0x1F);
76 AD1CHS123 = u16_ch123SelectMask;
78 AD1CHS = ADC_CH0_NEG_SAMPLEA_VREFN | (u8_ch0Select & 0x1F);
80 AD1CON4 = ADC_1_WORD_PER_INPUT;
84 DMA0PAD = (
unsigned int) &ADC1BUF0;
85 DMA0REQ = DMA_IRQ_ADC1;
86 DMA0STA = __builtin_dmaoffset(au16_bufferA);
87 DMA0STB = __builtin_dmaoffset(au16_bufferB);
92 DMA_DIR_READ_PERIPHERAL |
95 DMA_AMODE_REGISTER_POSTINC |
96 DMA_MODE_CONTINUOUS_PING_PONG);
103 AD1CON1bits.ADON = 1;
107 volatile uint16_t au16_sum[MAX_TRANSFER];
109 volatile uint8_t u8_activeBuffer;
111 void _ISRFAST _DMA0Interrupt(
void) {
117 if (u8_activeBuffer) {
118 au16_adcHWBuff = (
uint16_t*) &au16_bufferB;
121 au16_adcHWBuff = (
uint16_t*) &au16_bufferA;
126 for ( u8_i=0; u8_i<MAX_TRANSFER; u8_i++) {
127 au16_buffer[u8_i] += au16_adcHWBuff[u8_i];
133 if (u8_adcCount==0) {
136 for ( u8_i=0; u8_i<MAX_TRANSFER; u8_i++) {
137 au16_sum[u8_i] = au16_buffer[u8_i];
138 au16_buffer[u8_i] = 0;
166 CONFIG_AN0_AS_ANALOG();
167 CONFIG_AN1_AS_ANALOG();
168 CONFIG_AN2_AS_ANALOG();
169 CONFIG_AN12_AS_ANALOG();
178 T2CON = T2_32BIT_MODE_ON | T2_PS_1_1 | T2_SOURCE_INT;
183 PR2 = u32_ticks & 0xFFFF;
186 configDMA_ADC(12, ADC_CH123_POS_SAMPLEA_AN0AN1AN2, ADC_CONV_CLK_10Tcy );
190 while (!u8_gotData) {
194 for ( u8_i=0; u8_i<4; u8_i++) {
195 u16_pot = au16_sum[u8_i];
196 f_pot = (3.3 / 1023 / 64 ) * u16_pot;
199 printf(
":0x%04X=%1.3fV ", u16_pot, (
double) f_pot );