Rev 155 | Blame | Last modification | View Log | Download | RSS feed
#include <xc.h>#include "defines.h"#include "base_ADC.h"static ADC_DATA *adc_data_p;void ADC_Init(ADC_DATA *data, char TAD, char FOSC) {adc_data_p = data;ADC_AN0_TRIS = 1;ADC_AN1_TRIS = 1;ADC_AN2_TRIS = 1;adc_data_p->last_channel = 0;adc_data_p->result = 0;ADCON0bits.VCFG1 = 0; // VRef- = AVssADCON0bits.VCFG0 = 1; // VRef+ != AVddADCON1bits.ADFM = 1; // Right justified resultADCON1bits.ADCAL = 1; // Calibrate A/DADCON1bits.ACQT = TAD;ADCON1bits.ADCS = FOSC;ADCON0bits.ADON = 1; // Enable A/D moduleADCON0bits.GO_DONE = 1; // Start calibrationwhile (ADCON0bits.GO_DONE); // Wait for calibration to finishPIR1bits.ADIF = 0; // Clear the IF flagADCON1bits.ADCAL = 0; // Normal A/D operationPIE1bits.ADIE = 1; // Enable A/D interrupt}void ADC_Start(char channel) {adc_data_p->last_channel = channel;ADCON0bits.CHS = channel; // Set A/D channelADCON0bits.GO_DONE = 1; // Start A/D conversion}void ADC_Stop() {ADCON0bits.ADON = 0; // Disable A/D module}void ADC_Interrupt_Handler() {adc_data_p->result = ADRES;}char ADC_Get_Result(unsigned int* ret) {if (ADCON0bits.GO_DONE) {return 0;} else {*ret = adc_data_p->result;return 1;}}