ADCON Registers

Submitted by rehpej on Wed, 09/26/2018 - 21:14

Good day,

I'm new to PIC programming, and I'm lost on how to set the ADCON registers for my PIC. I am using PIC18F4685 and it have ADCON0, 1 and 2. I have set my ADCON base on the PIC datasheet.

ADCON0 = 0b00000001; // Set RA0 as analog input
ADCON1 = 0b00000000; // Bit5 as AVss Bit4 as AVdd
ADCON2 = 0b10101101; /* Bit7 as Right Justified
                            Bits5-3 I don't know how to select the TAD
                            Bits2-0 Fosc/8, I am using __XTAL_FREQ 8000000 */

At ADCON0, I have turned on the ADC and select channel0 (RA0). But what if I want to select other channels since my PIC have 8 analog channels.

At ADCON1, reference voltage is the supplied voltage (bits 5 and 4). And the port conf bits are all analog input (bits 3 to 0). Bits 7 and 6 are unimplemented.

At ADCON2, this part is where I am confused. Bit 7 is right justified and Bit 6 is unimplemented.

Bits 5 to 3 is set to 101 or 12TAD, I have search to google and the minimum TAD for a 10bit ADC is 12TAD. How to know what TAD to choose? At the datasheet, A/D clock period minimum is at 0.7us and maximum is at 25us. 

Bits 2 to 0 is set to 101 or FOSC/16. Can I choose anything? I am using a crystal oscillator at 8MHz.

Please do correct me if I'm wrong. I am still new to this and need some guidance.

Thank you.

Hi rehpej, welcome to Circuit Digest Forms.

Being a beginner I wonder why you have selected the PIC18F4685 MCU, it is a powerful PIC and can be a bit tricky to get started since it has no good support community.

Anyway to begin with the PIC18F4685 has 11 analog channels and not 8 all 11 channels are 10-bit ADC. Refer page 247 on datasheet. There are totally 5 registers associated with ADC module they are listed below with their usage 

• A/D Result High Register (ADRESH) -> Gives the result of ADC 

• A/D Result Low Register (ADRESL) -> Gives the result of ADC

• A/D Control Register 0 (ADCON0)  ->Controls the operation of ADC module

• A/D Control Register 1 (ADCON1) ->Configures the function of port pins

• A/D Control Register 2 (ADCON2) -> Configures the clock source.

So to read an ADc value from a pin into your MCU you have to follow the below steps

  1. Initialize the ADC Module
  2. Select the analog channel
  3. Start ADC by making Go/Done bit high
  4. Wait for the Go/DONE bit to get low
  5. Get the ADC result from ADRESH and ADRESL register

You can read this article https://circuitdigest.com/microcontroller-projects/pic-microcontroller-… understand how to use ADC with PIC.

 

At ADCON0, I have turned on the ADC and select channel0 (RA0). But what if I want to select other channels since my PIC have 8 analog channels.

ADCON0 = 0b00000001; Setting the ADCON0 this way will select the (AN0). you can use the bit 5-2 CHS3:CHS0 (refer page 247 on datasheet) for selecting other channels. There are totally 11.

At ADCON1, reference voltage is the supplied voltage (bits 5 and 4). And the port conf bits are all analog input (bits 3 to 0). Bits 7 and 6 are unimplemented.

Yes you are correct here 

At ADCON2, this part is where I am confused. Bit 7 is right justified and Bit 6 is unimplemented.

Bits 5 to 3 is set to 101 or 12TAD, I have search to google and the minimum TAD for a 10bit ADC is 12TAD. How to know what TAD to choose? 

No, you have been misguided 12TAD is not the minimum value of 10-bit ADC. I am pasting the register bits form datasheet below

Capture_1.PNG

Your version of PIC uses SAR type ADC. which stands for successive approximation type ADC. I do not want to get into much details here, but cuddly put you can assume that the value of TAD decides the accuracy of the ADC result and is also directly related to the clock frequency. I am not sure if there is way to calculate the TAD based on clock frequency but what I can say is that your ADC will work fine with all values of TAD it is just your accuracy that will get varied. You can experiment with different values to know how it works.

Bits 2 to 0 is set to 101 or FOSC/16. Can I choose anything? I am using a crystal oscillator at 8MHz.

Yes you can choose any frequency you want, it will only affect the conversion speed. 

  Joined August 16, 2016      998
Tuesday at 12:29 AM

Thank you for the response.

I am using PIC18F4685 because it was just given to me. :D

  Joined September 24, 2018      3
Monday at 03:19 PM