A/D Converter Code

This is a short summary explaining basic configuration needed for using PIC A/D Module. Simple  assembly source code is available at the bottom of the page. Much more detailed explanation can be found in the datasheet here. As you probably know PIC16F87X family  has an Analog to Digital Converter (ADC) as part of its peripheral devices. PIC16F877A has 8 analog inputs/channels.5 are PORTA inputs and remaining 3 are PORTE. Converted 10 bit result is stored in 2 register ADRESH,ADRESL. Here is ADC block diagram.
ADC Block Diagram

3 bits CHS2-CHS0 in ADCON0 register control the analog channel selected for conversion.2 higher bits ADCS1,ADSC0 are responsible for ADC converter clock settings and for 20MHz oscillator  should be set to “10”. ADON bit switches A/D unit ON/OFF.
ADCON0 Register
Setting bit GO/DONE will start a conversion. By polling this bit we can find out if conversion has ended and result is valid (If interrupts are not used). Another important register is ADCON1. 4 lower bits PCFG3-PCFG0 are A/D Port Configuration control bits. These bits control if a PORT input will be analog or digital. For example initializing ADCON1 with 0x02 will make all PORTA analog and PORTE digital.
ADCON1 Register
Bit 7 ADFM determines result alignment. ADFM = “1” Right justified, ADFM = “0” Left justified.
AD Result Justification
If you don’t need full 10 bit result use ADFM = “0” and take 8 Upper bits available in ADRESH post conversion. This way all of your functions will be much simpler and easier to implement. Simple Digital Voltmeter A/D function uses these settings.A/D module is much slower than PIC and needs delays subroutines to acquire the signal and convert it. For exact timing specification see microcontroller datasheet. The provided code assumes that 8 LED’s are connected to PORTD and input signal is AN1 (Pin 3). It will simply convert input analog signal and show it on PORTD.Assembly source code available here.

Posted in Assembly Code Examples
 

Leave a Reply

Your email address will not be published. Required fields are marked *

*