Not using AtoD
//----- SETUP THE A TO D PINS -----
ADCON0 = 0b00000000; //AtoD disabled
//ADCON1 = 0b00000000;
//ADCON2 = 0b10111110; //Right justified
ANSELA = 0b00000000; //0 = digital
ANSELB = 0b00000000;
ANSELC = 0b00000000;
Using AtoD
Initialise
//----- SETUP THE A TO D PINS -----
VREFCON0 = 0b10110000; //FVR enabled, set to 4.096V
ADCON0 = 0b00000001; //AtoD enabled
ADCON1 = 0b00001000; //Vref connected to FVR BUF2
ADCON2 = 0b10111010; //Right justified, ADCS=010 (Needs setting for osc)
ANSELA = 0b00000011; //0 = digital
ANSELB = 0b00000000;
ANSELC = 0b00000000;
ADCON0bits.GO = 1; //Start a conversion
Read Values
WORD atod_result_0;
WORD atod_result_1;
//************************************
//************************************
//********** PROCESS A TO D **********
//************************************
//************************************
void process_atod (void)
{
WORD atod_result;
BYTE b_temp;
static BYTE result_count = 0;
//Exit if last conversion is not complete
if (BusyADC())
return;
//Get result of last conversion
atod_result = ReadADC();
b_temp = (ADCON0 >> 2) & 0x0f;
switch (b_temp)
{
case 0:
//--------------------
//----- AN0 DONE -----
//--------------------
if (result_count == 0)
{
atod_result_0 = 0;
atod_result_1 = 0;
}
atod_result_0 += atod_result;
ADCON0 = 0x01 | (1 << 2); //Set AN1 input
break;
case 1:
//--------------------
//----- AN1 DONE -----
//--------------------
atod_result_1 += atod_result;
result_count++; //<<<<DO ON LAST ONE ONLY
ADCON0 = 0x01 | (0 << 2); //Set AN0 input
break;
default:
ADCON0 = 0x01 | (0 << 2); //Set AN0 input
break;
} //switch (b_temp)
//----- START NEXT CONVERSION -----
ADCON0bits.GO = 1; //Start a conversion
if (result_count >= 16) //<<<Set number of readings to average (adjust the >>= below to match)
{
//------------------------------------
//------------------------------------
//----- DONE NEW SET OF READINGS -----
//------------------------------------
//------------------------------------
result_count = 0;
atod_result_0 >>= 4;
atod_result_1 >>= 4;
comms_tx_current_sense = atod_result_0;
comms_tx_voltage_sense = atod_result_1;
}
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.