This is something that would be typically used to measure Battery voltage over time.
Here’s a complete stand-alone module that can be used to investigate this feature.
// ===========================================
// o Measure VDD using BAND GAP Voltage, 1.2V
// on PIC24FJ256GB004 PIM w/ Explorer 16 Board
// o ICD3, MPLAB v8.85, C30 v3.31
// o June 2012
// o Note: Please see Microchip Application Note AN1072
// for an more in-depth example of this using a PIC16F690
// ===========================================
#include <p24fxxxx.h>
#ifdef __PIC24FJ64GB004__ //Defined by MPLAB when using 24FJ64GB004 device
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & FWDTEN_OFF & ICS_PGx1)
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI & I2C1SEL_SEC & IOL1WAY_ON)
_CONFIG3(SOSCSEL_SOSC & WUTSEL_LEG & WPDIS_WPDIS & WPCFG_WPCFGDIS)
_CONFIG4(DSWDTEN_OFF & RTCOSC_SOSC & DSWDTPS_DSWDTPS6 & DSWDTOSC_SOSC & DSBOREN_OFF)
#else
#error ERROR: INCORRECT PIC selected for this project: Use PIC24FJ64GB004.
#endif
unsigned long VIN_Result,VDD_Result;
unsigned long Count;
int main(void)
{
AD1CON2 = 0; // AVdd, AVss, int every conversion, MUXA only
AD1CON3 = 0x1F05; // 31 Tad auto-sample, Tad = 5*Tcy
AD1CON1 = 0x80E4; // Turn on, auto sample start, auto-convert
AD1CHS = 15; // VBG 1.2V
AD1PCFGbits.PCFG15 = 0; // Disable digital input on VBG Channel
AD1CHS = 15; // CHAN for VBG 1.2V
AD1CSSL = 0; // No scanned inputs
while (1)
{
VIN_Result = VDD_Result = 0;
// ———————————————————————
// Wait for conversion to complete
// ———————————————————————
while(!AD1CON1bits.DONE)
;
// ———————————————————————
// Read A2D result
// ———————————————————————
Count = (unsigned long) ADC1BUF0;
// ———————————————————————
// FYI Only – Let’s see what VIN would be, assuming VDD is 3.3V
// ———————————————————————
VIN_Result = (Count * 3300)/1024;
// ———————————————————————
// Scale 1.2V to 1200 for calculation.
// ———————————————————————
VDD_Result = (unsigned long)((1200 * 1023.0))/(Count);
// ———————————————————————
// Set Breakpoint here…
// ———————————————————————
Nop();
Nop();
Nop();
Nop();
} // End of while(1)…
} // End of main()…


