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):

We will set up the registers in the order of the red numbers in the diagram.
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.
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):

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: