Understanding the A/D Converter of your PIC
A built in analog to digital converter (A/D Converter or ADC) makes life easy when you’re using your PIC. As you know, an ADC is a powerful and even necessary tool in many projects. There are a lot of options, registers, and bits that can get confusing so this post will be dedicated to outlining and describing them. In the next post, we’ll briefly go over how to put them together to get everything functioning.
It should be noted that PICs use a sample-and-hold method for getting readings. In other words, the pin where you put the analogue signal is filling up a very small capacitor, which, when the conversion takes place, is disconnected from the pin. The voltage on this capacitor is then measured (I don’t know which method they use with the PICs) and then it shoots the value to you. It is just important to note that you can’t connect high-impedance output devices to the input of the ADC or it won’t fill the capacitor fast enough. The maximum output impedance is 10k ohms for the 16F690 and 2.5K for the 16F877A, so if you are above this (such as with some pH sensors), you’ll probably want a voltage follower circuit to drop your output impedance.
As is the norm, the names and values shown below are based on the PIC16F690 but a great deal will be in common with ADC modules in other PICs. And once you understand how one works, it isn’t such a big deal to switch to another.
ADFM – A/D conversion result ForMat select bit: Since the output is 10-bits and is placed into two 8-bit registers, this allows you to decide whether the LSB is all the way to the right or the MSB is all the way to the left. Set it for right justified, clear it for left justified.
VCFG – Voltage reference pin: Some PICs have a pin that you can place a voltage on to act as a reference for your reading. Say you have 2^10 resolution but want finer resolution and won’t be wandering from 0 to 5 volts but only from 0 – 2.5 volts. Then you use the reference voltage. PICs typically allow the upper limit to only drop to around 2-3 volts and some PICs allow a lower limit and others don’t. The reference limit can be found in the electrical characteristics of your data sheet under the ADC Characteristics. Set the bit if you want to use the reference pin, clear it if you don’t.
CHS(3:0) – adc CHannelS: Typically, if your PIC has a built-in ADC, it will only have one ADC, but several different channels. This basically means that if you have multiple sources that you’d like to convert, you can attach them to different pins, and you can switch between channels to first read from one pin then another. On your pin diagram, those labeled AN0-AN7 (or whatever yours goes to), are the different channels of the ADC.
ADON – ADc ON: This controls whether or not the ADC is on and consuming power. Set the bit to turn on the ADC, clear it to turn off the ADC.
ADCS2(2:0) – ADC clock Source: Depending on the values you put in these three slots, your PIC clock is divided to be used as the ADC clock. Optimum time for an A/D conversion is between two and six microseconds. Refer to your data sheet to figure out which values best fit your situation.
ADIE – ADc Interrupt Enable: If this is enabled, then you’ll get an interrupt when the ADC interrupt flag goes high, assuming that the PEIE and GIE bits are set. Set to enable, clear to disable.
ADIF – ADc Interrupt Flag: This interrupt flag is set when an A/D conversion is done. It needs to be cleared in software.
ADRESL – ADC RESults Low: This is an 8-bit register that the results of the ADC conversion go into. If you have a greater than 8-bit resolution ADC, this holds only the lower eight bits. How the information is placed into these registers is based on your setting of the ADFM.
ADRESH – ADC RESults High: This the other 8-bit register for the upper bits of your results.
GODONE – GO/DONE: This bit marks the start of the actual conversion process. When you set the bit, the conversion starts. It is cleared in hardware when the conversion is done.
And that’s it! Don’t be surprised if your particular PIC has more or less registers and bits but this covers the basics. If something is unclear or you have any questions, let me know.