• AVR Freaks

Hot!PIC18F25K42: Help with CVD and ADCC for capacitive measurements

New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2020/02/12 04:14:17
  • Location: 0
  • Status: offline
2020/02/14 03:30:41 (permalink)

PIC18F25K42: Help with CVD and ADCC for capacitive measurements

I am a beginner with PIC and have very little experience with coding in general. I am currently using PIC18F25K42 with MPLAB X IDE, XC8 Compiler and ICD3 Programmer. I want to use the CVD to first measure capacitance values of regular through-hole capacitors first, then use the CVD to detect capacitive touch. I developed a code for the former (capacitance meter) using the help of the Code Configurator, however it is not measuring the capacitance correctly/getting correct ADC result when using the debugger and setting watches and I cannot find the reasons why. I am using the following datasheets for help:
  • Capacitive Touch Using Only an ADC (“CVD”) [AN1298]
  • Capacitive Voltage Divider (CVD) Operation on 8-Bit PIC® Microcontrollers [TB3198]
  • Datasheet for PIC
Here are a few things which I think could be wrong:
I believe that Chold (internal capacitor) being a low value of 28pF means that I cannot use a large range of capacitances to measure, as the CVD final voltage and therefore ADC result would be quite low (Currently using a capacitor of 100nF). Should I use a smaller capacitor to measure?
Is my way of calling the ADC and getting conversion correct? I am also unsure if I connected and configured the CVD correctly according to the 7 steps in AN1298, and may have possibly called functions already generated by the MCC.
Finally, I believe maybe the fact that ADCC result is in uint16_t format while I require a double/float value in calculations could be causing an issue. (I have attached an image of which calculation I am using)
Any help would be appreciated, it has been stated that CVD makes capacitive touch sensing easier, but my lack of experience is getting the better of me. Thank you in advance. I will attach images to help. I do not require a large range, as long as I can measure any capacitance value, be it small or large. Thank you in advance!!
Here is my code:
void main(void)
// Initialise the device
// If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
// If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
// Use the following macros to:
// Enable the Global Interrupts
// Disable the Global Interrupts

while (1)
double Capacitance;
double Chold = 0.000000000028 ; //Chold value
double voltage;
double result;
uint16_t prechargetime = 4000;
uint16_t acquisitionValue = 4000;
ADCON1bits.PPOL = 1;

TRISAbits.TRISA3 = 0; //set to digital output
PORTAbits.RA3 = 1;
ADPCH = 0x3; //connect chold circuit to Vref+ in A3 (set to Vdd)


CVDin_TRIS = 0;
CVDin_ANS = 0;
CVDin_PORT = 0; // ground the sensor



CVDin_TRIS = 1; //set to analog input
CVDin_ANS = 1;
ADPCH = 0x14; //connect chold to capacitor in C4 (external capacitor to be measured)


ADCC_GetSingleConversion(CVDin); //start conversion
result =(ADRESH<<8)+ADRESL;


voltage = result * 5 / 4095;  //convert final voltage reading from digital to analog

Capacitance = ((Chold * 5) / voltage) - Chold;  //calculate the capacitance

Attached Image(s)


1 Reply Related Threads

    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2020/02/12 04:14:17
    • Location: 0
    • Status: offline
    Re: PIC18F25K42: Help with CVD and ADCC for capacitive measurements 2020/02/19 05:54:07 (permalink)
    Any help would be appreciated as I am lost, even after reading data sheets and other forum posts. Thank you
    Jump to:
    © 2020 APG vNext Commercial Version 4.5