Thursday, February 21, 2008

PIC Analog to digital conversion registers

<<<Previous tutorial
Next Tutorial >>>
There are totally four registers for A2d conversion in PIC16f877 and PIC16f877A .They are the following .
(NOTE : Almost all the PIC series of IC's have this same register configuration .So knowing this is enough to program all the PIC series IC's )
















































registers for analogue inputs.
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
ADRESHA2D Result Register - High Byte
ADRESLA2D Result Register - Low Byte
ADCON0ADCS1ADCS0CHS2CHS1CHS0GO/DONE-ADON
ADCON1ADFM---PCFG3PCFG2PCFG1PCFG0




ADCON0 Configuration
Since PIC has 10 bit A2D and its a 8 bit microcontroller it needs an extra register to store the extra 2 bits resulting in the A2D conversion .Thats the job the first two registers ADRESH and ADRESL name itself is self explanatory .
ADRESH:Stores the higher 8 bits .
ADRESL:Stores the lower 8 bits .
So now we are done with the first two registers .

The next register that we are gonna see in PIC16F877A is ADCONO .
The first two bits ADCS1 and ADCS0 is required to select the A2D clock .The table for clock selection is given below .If I am using 5 MHZ I will select 01 thats fosc/8 .



































ADCS1ADCS0A/D Conversion Clock
Select bits.
Max. Clock Freq.
00Fosc/21.25MHz
01Fosc/85MHz
10FOsc/3220MHz
11Frc (Internal A2D RC Osc.)Typ. 4uS




The next set of bits are channel select .YOu can select the appropriate channels by selecting the appropriate bits .So now I am using 5 MHZ and selecting RA2 for example , now bit selection will be 01010xxx .The bits that are named as X are still to be selected .

The next bit is GO/DONE .Setting this bit high starts A2D conversion and it is automatically cleared when it is complete .So monitoring the status of this bit can tell us about the status of Analog to digital conversion .So we have to set it high to start conversion .So now our bit configuration will be 010101xx .

If you look at the first table you will see bit1 is useless .So put a zero there .So now our bit configuration becomes 0101010x .
The last bit is ADON .When the bit is set it turns on the A2D when cleared it turns off .So we are setting 1 there .Now it becomes
01010101 .This is the bit configuration to select 5 MHZ clock channel AN2 .The next three bits are useless .




































































CHS2CHS1CHS0ChannelPin
000Channel0RA0/AN0
001Channel1RA1/AN1
010Channel2RA2/AN2
011Channel3RA3/AN3
100Channel4RA5/AN4
101Channel5RE0/AN5
110Channel6RE1/AN6
111Channel7RE2/AN7




ADCON1 Configuration
The next channel is ADCON1 . The bit7 ADFM(Analog to digital conversion format ) is used to select the format .Setting this bit high makes it 10 bit converter .And clearing it makes it 8 bit ie the result only appears in ADRESH and ADRESL is ignored .
Now we are entering thre most complicated part of the register selection .Please look at the following table to select the configuration you need .

Visit WINPIC PROG to learn more in detail about the register selection .


































































































































































































































PCFG3:

PCFG0
AN7

RE2
AN6

RE1
AN5

RE0
AN4

RA5
AN3

RA3
AN2

RA2
AN1

RA1
AN0

RA0
Vref+Vref-
0000AAAAAAAAVddVss
0001AAAAVref+AAARA3Vss
0010DDDAAAAAVddVss
0011DDDAVref+AAARA3Vss
0100DDDDADAAVddVss
0101DDDDVref+DAARA3Vss
0110DDDDDDDDVddVss
0111DDDDDDDDVddVss
1000AAAAVref+Vref-AARA3RA2
1001DDAAAAAAVddVss
1010DDAAVref+AAARA3Vss
1011DDAAVref+Vref-AARA3RA2
1100DDDAVref+Vref-AARA3RA2
1101DDDDVref+Vref-AARA3RA2
1110DDDDDDDAVddVss
1111DDDDVref+Vref-DARA3RA2




in the next tutorial we are going to kick start with PIC16F877A programming in C .I am gonna use mikroc compiler .

Previous

7 comments:

Aman Vashisht said...

a good resource for robotics. And the tutorial of PIC is also good to see and learn for us

Arun Vijay said...

Thank you Aman. I will try to come up with more good ones in the future .

Anonymous said...

thank you for the information. i need help in programming PIC 16F877A can i talk to you using mail? my email is sirc4526@yahoo.com thankz in advance

axy87 said...

huh...very good tutorial...
it helps a lot in my final year project....=)

Anonymous said...

hy arun.. nice tutorial.. can u give tutorial on 10 bit adc for pic18f4550.. i'm stuck.. plss help

komal gupta said...

can anyone help me out to solve a analog to digital converter program coding

Krish said...

Hi actually i have an doubt it might be stupid but im confused please help me

0 0 Fosc/2 1.25MHz
0 1 Fosc/8 5MHz
1 0 FOsc/32 20MHz

what if im using a 12MHz crystal in my board..which one should i use.