Analog to Digital Converter Library
ADC: (Page # 13)
The ADC library consists of several functions which can be used to configure the AD converter in the PIC18. Before checking out the code, please read through this document which describes how the ADC module in the PIC18 functions:
http://ww1.microchip.com/downloads/en/devicedoc/adc.pdf
OpenADC(arguments)
For initializing/configuring the AD Module such, the OPEN ADC function is used. It also configures pins as analog or digital. It has different arguments for different processors.
SetChanADC(arguments)
It is used to select the Analog channel with one wants to work with.
ConvertADC()
This function is used to start the conversion process.
BusyADC()
This function is used check whether the ADC is busy with an ongoing conversion.
ReadADC()
This function is used to read the result of an Analog to digital conversion.
CloseADC()
This is used to turn off the ADC module.
Here is the code:
Code
//This is a program that shows the use of ADC with
//Microchips Libraries.It simply converts the potentiometer
//reading and shows it on the LEDs connected to PORTB.It uses
//interrupts on channel AD0
//The ADC Section is documented on P#13 of C18 Libraries
//Use 181320Basic.DSN
//For acquisition time and other calculations i have refered to page 162 of the datasheet
#include<p18f1320.h>// Include files and definitions for the Processor
#include<adc.h>// Include function definitions for the ADC library
#pragma config LVP=OFF,OSC=INTIO2,WDT=OFF// Configuration settings for the 18f1320
#pragma code hi_prioriint=0x008// Set address to ADC interrupt vector high priority
void ADCData(void);// Function definition for ADC read routine
void chk_isr(void);// ISR Routine
void hi_prioriint(void)//ISR Handler
{
_asm
GOTO chk_isr
_endasm
}
#pragma interrupt chk_isr
void chk_isr(void)
{
if(PIR1bits.ADIF)// If conversion complete
ADCData();// Call read routine
}
void main( void )
{
TRISB=0x00;// Make port B and output
TRISA=0b11111100;// Make last 2 pins of PORTA outputs
OSCCON=0X60;// Internal 4Mhz oscillator
OpenADC(ADC_FOSC_8 &// ADC clock frequency AD converter time
ADC_RIGHT_JUST &// Make the reading Right Justified
ADC_6_TAD ,// Select an Acquisition time of 6 TAD
ADC_CH3 &// Enable Channel 3 of the AD Module
ADC_INT_ON &// Turn on AD module interrupts
ADC_VREFPLUS_VDD// Make Vref the power supply
, 0x72 //Value of ADCON1
);
INTCONbits.PEIE=1;//Enable peripheral interrupts
INTCONbits.GIEH=1;// Enable all Interrupts
while(1)// wait here
{
SetChanADC(ADC_CH3);// select Channel 3 of the ADC
ConvertADC();// Convert the Analog Reading
while(BusyADC());// Wait here while ADC is performing a conversion
}
}
void ADCData(void)// Reads converted value and outputs it
{
unsigned int adcdata;
adcdata=ReadADC();// Read ADC value
LATB=adcdata;// output Lower byte to PORT B
LATA=(adcdata>>8);// Output Higher 2 bits to PORTAs last 2 btis
PIR1bits.ADIF=0;// Enable AD conversion interrupt
}
The code and the accompanying simulation can be downloaded from: