• AVR Freaks

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

Author
DanTS
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2020/02/12 04:14:17
  • Location: 0
  • Status: offline
2020/02/19 09:54:14 (permalink)
0

PIC18F25K42: Help with CVD and ADCC for capacitive measurements

Hello,
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
SYSTEM_Initialize();
// 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
//INTERRUPT_GlobalInterruptEnable();
// Disable the Global Interrupts

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

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

__delay_ms(100);

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

__delay_ms(100);

//ADCC_LoadAcquisitionRegister(acquisitionValue);

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

__delay_ms(100);

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

__delay_ms(100);

voltage = result * 5 / 4095;  //convert final voltage reading from digital to analog
__delay_ms(500);

Capacitance = ((Chold * 5) / voltage) - Chold;  //calculate the capacitance
__delay_ms(500);
ADCC_StopConversion();}
}
post edited by DanTS - 2020/02/19 10:21:45

Attached Image(s)

#1

8 Replies Related Threads

    Kiwi Mike AZ
    Super Member
    • Total Posts : 2056
    • Reward points : 0
    • Status: offline
    Re: PIC18F25K42: Help with CVD and ADCC for capacitive measurements 2020/02/19 10:11:28 (permalink)
    0
     
    There is a good demo on how to use CVD for cap touch here:
    https://mplabxpress.microchip.com/mplabcloud/example/details/531
    The ADC is basically the same as in the PIC18FxxK42 family.
     
     
    Your use case is different since you are actually wanting to measure the real value of a capacitor.
     
    To do that you may need to use a Reference Capacitance, or do some form of calibration as the internal capacitance is not a precise value and will vary between parts.
     
    The CVD is designed for Cap Touch, not for measuring actual capacitance.
    Cap touch is all about relative values, averages and amount of change so does not need accurate reading of a capacitor value.
     
     
     
     
     
     
     
     
     
    post edited by Kiwi Mike AZ - 2020/02/19 10:16:09
    #2
    DanTS
    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 10:26:01 (permalink)
    0
    Hello,
    Thank you for your reply. I checked that demo, and they seem to be using MCC as well. However, their code is much smaller. Does that mean the MCC generated code complete all the steps in CVD, without me needing to write code for it (e.g. ground the sensor, charge Chold, then connect Chold to the sensor, etc)? So by just doing ADCC_startCOnversion the CVD will start automatically?
    For me, I want to measure the capacitance of thru-hole capacitors using the final voltage value. I am using the debugger in MPLAB X IDE to get a value and see if the code is working. However, it is returning 0 for both adc result and for the capacitance variables. Is the way im initialising and setting up the cvd wrong? Or is the way i convert the uint16_t ADC result into a float for capacitance wrong?
    Thank you in advance.
     
    Edit: should i use timer and tau = RC instead? That could possibly help however I am not good in coding, i tried that before but i didnt know how properly set the timer up and account for overflow, etc.
     
    I read in this forum that CVD method can be used for capacitance measurements. I am using 10nF at the moment to be as close as possible to the 28pF Chold value. How would i implement a reference capacitor?
    post edited by DanTS - 2020/02/19 10:32:55
    #3
    Kiwi Mike AZ
    Super Member
    • Total Posts : 2056
    • Reward points : 0
    • Status: offline
    Re: PIC18F25K42: Help with CVD and ADCC for capacitive measurements 2020/02/19 12:24:33 (permalink)
    0
     
    The ADC with Computation, when configured correctly, does the entire Capacitive sampling cycle for you and gives you a result. The sample code shows how to configure the ADC to do this.
    You do not need to step through the separate steps like you did.
    You can even set it up to take multiple samples and average them in one pass.
    This is a more complex peripheral than it used to be and with the correct configuration it is quite powerful in what it can do.
    I recommend reading the data sheet chapter (maybe a few times) and appropriate tech briefs and application notes to get a better understanding of how it works and how it can be configured to do your specific task.
     
    A reference capacitor is just a capacitor of known value that on your board that you run a CVD on, take the result and create the appropriate scaling/correction math that you can use to measure the unknown capacitor with. This is basically a built in calibration.
     
     
     
     
     
     
     
     
     
     
    #4
    DanTS
    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 18:14:07 (permalink)
    0
    Thank you very much Kiwi Mike AZ, your response is much appreciated. If using the MCC is enough then that would save a lot of hassle. The only things to configure would be to set the precharge and acquisition time correct? I was confused about what value to set these, I just chose 4000 out of 8191 (as shown in the attached image in the first post) to make sure that firstly the Chold would fully get charged and that the capacitive voltage divider would have enough time to stabilise into the new voltage. Would you say that this configuration is alright, or will there be any negative consequences in choosing these values (or any negative consequences in choosing specific values in general)? Is there anything else to configure in the MCC for the CVD to work automatically?
     
    By the multiple readings, I hope you mean using burst average mode, and take for example 32 reads and shift by 5 bits in the Computation window? I believe that was implemented on the touch piano demo correct?
     
    I read the AN1298 as well as TB3198 a few times. TB3198 was missing 2 crucial examples which would have helped a lot and I have submitted a call on the support website to get those. Since I am new in coding, possibly I got confused and tried to code for more things than necessary in this case.
     
    Can you give advice in setting and coding for a reference capacitor? I tried searching about calibration but did not find something that would help me.
     
    My main aim is to get a capacitive touch in the end, possibly of an array of sensors (2x2, possibly scale up to 8x8 array of capacitive sensors). It would be helpful in my project to get the change in capacitance value as well before and after touch is detected. Is it possible if you can clarify what threshold i could use for a touch to be detected? the CVD piano demo used a result of less than 1000 of the ADC value. Would that work in this case too? How can I get the change in capacitance value or set a reference value to judge the change in capacitance?
     
    Thank you very much for being this patient with me through all these questions, it is really appreciated. I am simply stuck and felt discouraged in my ability so came to the forums as a last resort effort to get help. I want to get better with using microcontrollers and coding but need guidancegrin
    post edited by DanTS - 2020/02/19 18:15:35

    Attached Image(s)

    #5
    futz
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2008/03/08 05:31:00
    • Location: 0
    • Status: offline
    Re: PIC18F25K42: Help with CVD and ADCC for capacitive measurements 2020/04/15 16:07:55 (permalink)
    0
    Hi DanTS. Did you ever get an answer on the two missing examples in TB3198? I'm trying to get CVD working and without some direction, which presumably I would find in those examples, I can't get anywhere with it. The PIC datasheet (18F27K42) just doesn't seem to have enough info to get it going.
    #6
    RISC
    Super Member
    • Total Posts : 5776
    • Reward points : 0
    • Status: offline
    Re: PIC18F25K42: Help with CVD and ADCC for capacitive measurements 2020/04/16 07:36:41 (permalink)
    0
    Hi,
    If you are trying to use CVD for a keyboard, I strongly suggest that you use MCC plugin as it has a touch library for many 8 bits PICs including the PIC18F27K42. MCC has built-in help near most libraries.
    You can get started here for MCC and many other subjects : http://www.microchipdeveloper.com
    https://microchipdeveloper.com/mcc:start
    https://microchipdevelope...touch:mcc-installation
    Regards
    #7
    futz
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2008/03/08 05:31:00
    • Location: 0
    • Status: offline
    Re: PIC18F25K42: Help with CVD and ADCC for capacitive measurements 2020/04/16 10:26:27 (permalink)
    0
    Hi RISC. Thanks for the reply. I'm not interested in using libraries. I want to learn how to do it. If I just wanted to get the job done and learn nothing then I'd use libs and get on with it. Makes perfect sense. But that doesn't interest me at all, at least for what I'm doing right now. I'm not in a hurry. I don't mind the CVD module doing most of the work for me, but I at least want to learn how to configure and call it properly.
     
    I have some ultra simple code written, but when I set the GO bit and then poll GO, it never goes low to indicate a completed conversion. And I'm not sure why. I'm probably doing something stupid - I don't use ADCs very often - but the datasheet is pretty terse on some of their info. I'm using the TB3198 configuration example, with a couple minor changes and additions, and am following the 18F27K42 datasheet example. One weird thing is that the datasheet example claims that the ADERR register contains the CVD result? Really? Seems like ADRES makes more sense. Whatever. I have code to display the results of both on an LCD whenever I figure out why my conversion isn't happening.
     
    Ah well. I'll go through all those config bits once again and keep on trying different things. Eventually I'll figure it out.
     
    post edited by futz - 2020/04/16 11:12:37
    #8
    ThomasF
    Starting Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2010/10/01 01:29:17
    • Location: 0
    • Status: offline
    Re: PIC18F25K42: Help with CVD and ADCC for capacitive measurements 2020/05/26 23:36:41 (permalink)
    0
    futz
    I have some ultra simple code written, but when I set the GO bit and then poll GO, it never goes low to indicate a completed conversion.


    Do you use double sampling? Then it might be a hardware bug. See Errata "3.2 Burst Average Mode Double Sampling".
    #9
    Jump to:
    © 2020 APG vNext Commercial Version 4.5