Hi guys,
I'm new to this whole microcontroller thing and might therefore easilly
lack of some knowledge. After going through many forums I still couldn't
figure out, how to get my 12-bit ADC conversion working.
In my card I have a PIC24EP512GU810 processor connected to a thermometer
sensor MCP9700 on pin RB2. As I said, I tried many examples and read the
datasheet over and over. Maybe I'm just too stupid. But perhaps you
could help me.
Here is the relevant code:
1 | void initadcExample(void){
|
2 | /* Set port configuration */
|
3 | ANSELA = ANSELB = ANSELC = ANSELD = ANSELE = ANSELG = 0x0000;
|
4 | ANSELBbits.ANSB0 = 0; // Ensure AN0/RB0 is analog
|
5 | ANSELBbits.ANSB1 = 0; // Ensure AN1/RB1 is analog
|
6 | ANSELBbits.ANSB2 = 1; // Ensure AN2/RB2 is analog
|
7 | ANSELBbits.ANSB5 = 0; // Ensure AN5/RB5 is analog
|
8 | /* Initialize and enable ADC module */
|
9 | AD1CON1 = 0x000C; // Enable simultaneous sampling and auto-sample
|
10 | AD1CON2 = 0; // 0x0300; // Sample 4 channels
|
11 | AD1CON3 = 0; // 0x000F;
|
12 | AD1CON1bits.AD12B = 1; // 12-bit ADC operation => CHPS, CH123NB, CH123SB, CH123NA, CH123SA unimplemented
|
13 | AD1CON4 = 0x0000;
|
14 | AD1CSSH = 0x0000;
|
15 | AD1CSSL = 0x0000;
|
16 | AD1CHS0bits.CH0SA = 2; // Select AN2 for CH0 +ve input
|
17 | AD1CHS0bits.CH0NA = 0; // Select Vref- for CH0 -ve input
|
18 | AD1CHS123bits.CH123SA = 0; // Select AN0 for CH1 +ve input
|
19 | // Select AN1 for CH2 +ve input
|
20 | // Select AN2 for CH3 +ve input
|
21 | AD1CHS123bits.CH123NA = 0; // Select Vref- for CH1/CH2/CH3 -ve inputs
|
22 | AD1CON1bits.ADON = 1;
|
23 | }
|
24 |
|
25 | int readadc(){
|
26 | AD1CON1bits.SAMP = 0; // Start the conversions
|
27 | while(!_AD1IF); // wait for all 4 conversions to be done
|
28 | _AD1IF = 0; // Clear conversion done status bit
|
29 | return ADC1BUF0; // result stored in ADC1BUF3
|
30 | }
|