Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Setting up ADC in PIC16F684

Status
Not open for further replies.

Wingmax

New Member
This is a discussion for setting up the ADC module in PIC16F684. For detail operation of ADC please refer to Microchip data.

There are five registers involved in ADC operation: ANSEL, ADCON0, ADCON1, ADRESH, ADRESL. We only need to set up ANSEL, ADCON0 and ADCON1. The conversion result will be stored in ADSEH and ADSEL.

Please refer to block diagram (from Microchip data):

ADC_Block.JPG

We will set up the registers in the order of the red numbers in the diagram.

HTML:
(1)  Select analogue input. In our example, we chosed pin 11 of the chip,    
      which is AN2.
        
                               ANSEL=  0b00000100;

(2) Set CHS bits in ADCON0 to select the channel to connect to sample and  
     hold circuit. 
                  
                               ADCON0 <4:2>  =    _ _ _  0 1 0 _ _

(3) Set voltage reference source in ADCON0<6>.  0= VDD, 1= Vref pin. We 
     choose VDD as reference. So,

                                ADCON0 <6> =    _ 0 _ _ _ _ _ _ 

(4) Initiate ADC module by setting "1" in  ADCON0 <0>  ( doesn't mean A/D 
     conversion starts).
     Set DO/DONE bit in ADCON0<1> to off, not starting conversion yet. 
     ADCON0<1>= 0.   So,

                                ADCON0<1:0> =  _ _ _ _ _ _ 0 1

(5) Set A/D conversion format, left or right justified, in ADCON0<7>. The 
     result of the conversion is in 10 bits and will be stored in  ADRESH and 
    ADRESL register.  Left justified is :  xxxxxxxx   xx000000
                                          ADRESH      ADRESL
                                  
                            Right justified is: 000000xx   xxxxxxxx
                                                 ADRESH      ADRESL
    We choose left justified, so
      
                                ADCON0 <7> =  0 _ _ _ _ _ _ _

    To sum up for ADCON0:

                                ADCON0= 0b00001001; 

  
The setting with reference to the block diagram is complete.
When we want to start the conversion, we simply set GO/DONE bit to 1, ie 

(6)                             GODONE= 1;

 Next we have to set the conversion clock:

(7) Set the A/D conversion clock  (TAD)  in ADCON1 <6:4>.
     We have to ensure a mimimum of 1.6 uS TAD for proper A/D  operation.  
     For 4 MHz device frequency, we should select 8 Tosc (2.0 uS, which is >  
     minimum  requirement).  ADCON1 <6:4> =  001.   

                                 ADCON1= 00010000;  
      
     If we don't want to calulate this value, we can just set ADCON1 <6:4> to 
     x11. The TAD is 4 uS with this setting. 


To sum it all up, we only need three C statments to set up the ADC. 
Analogue input= AN2
Voltage reference source= VDD
Device OSC= 4MHz

C statments:
                 
                 
                     ANSEL=  0b00000100;
                     ADCON0= 0b00001001; 
                     ADCON1= 00010000;

Note, the ADC setting for this chip is different to 16F87x.

Next we will use this setting and connect a POT to AN2 to control 8 LEDs......

PS.Can someone please tell me how to attach a bigger picture on the same page.
 
Last edited:
Nice Post!!! :)

Finally, a post that is thorough, clear, and not full of useless garbage! Thank you for posting this set up!

...Now I assume all that's left is to set the ADCON0 bit 1 to start a conversion and then to read the result from ADRESH and ADRESL. The PIC16F684 will automatically clear the ADCON0 bit 1 upon completion of the conversion, as I understand it.

Can it really be this easy? I guess there's only one way to find out!

Thank you for the post!

Samuel Aaron Ward
Hurricane, West Virginia, USA
 
You can get a bigger version by typing [img]https://www.electro-tech-online.com/attachments/adc_block-jpg.17423/[/img].

Like so,
adc_block-jpg.17423


You might also want to add a mention of the acquisition time.

BTW, Nice post.

Mike.
 
Last edited by a moderator:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top