### Welcome to EDAboard.com

#### jemonlemon

##### Newbie
Hello,
I am an ECE student working with the PIC33F and I am having some difficult getting the ADC to work. I want to read the voltage from the AN1 pin and I made sure that the AN1 pin is getting a selected voltage between 0-3.3V.
I followed the instructions on the Mircochip manual, I feel as I missed some steps as I cannot get the ADC functioning.

Here is what I have for my initialization:
Code:
void ADC_Init()
{
TRISAbits.TRISA1 = 1;

// a) Select port pins as analog inputs

// b) Select voltage reference source

// c) Select analog conversion clock

// d) Determine how many S/H channels is used

// e) Select the appropriate sample/conversion sequence

// f) Select how conversion results are presented in the buffer

// g) Turn on ADC module
}

Here is what is in my main.c:
Code:
int main() {
...
// Retrieve voltage
_delay_us(10);
...
}

While debugging, I find that when the voltage at AN1 is near 0, the ADCValue equals 32768.0. When the voltage is higher than 1, it gives 32848.0. When voltage is around 0.5, it gives 32816.0. If anyone has some insight, I greatly appreciate it.

Thank you in advance.

Last edited:

#### MichalP

##### Junior Member level 1
I think 0 is writing as 32768 , so you need to define this as zero and perform some calc and you will have real values couse 32816 seems to be half

##### Super Moderator
Staff member
According to the datasheet the dsPIC33f has a 10-bit or 12-bit ADC so the value 32768 has bit-15 set, which is outside the maximum value of the ADC output. I suspect you aren't properly reading the value and masking some other bits that are probably status of some sort. Only the lower 12-bit (or 10-bits) should be ADC conversion data.

### thannara123

Points: 2

#### jemonlemon

##### Newbie
I think 0 is writing as 32768 , so you need to define this as zero and perform some calc and you will have real values couse 32816 seems to be half

Ah I see that I may have to add calculations to get the proper value. Although one problem I mention in the next quote.

According to the datasheet the dsPIC33f has a 10-bit or 12-bit ADC so the value 32768 has bit-15 set, which is outside the maximum value of the ADC output. I suspect you aren't properly reading the value and masking some other bits that are probably status of some sort. Only the lower 12-bit (or 10-bits) should be ADC conversion data.

I totally missed that. I ended up masking the 13-15 bits. I do run into a new problem where I am getting is that the values I get from the ADC are inconsistent (when AN1 is getting 2V, the ADC either gives 128.0 or 96.0). Here are some values I get:

 AN1 (Pin 20) input voltage double ADCValue (after masking for only bits 0-11) 0.0232 80.0 0.531 64.0 1.02 64.0 or 96.0 or 16.0 or 48.0 1.968 128.0 or 96.0

Replies
1
Views
1K
Replies
8
Views
8K
Replies
4
Views
2K
Replies
1
Views
1K
Replies
0
Views
2K