CTMU Time Delay Measurement - PIC24F16KA302

Discussion in 'Embedded Systems and Microcontrollers' started by Odyss3us, Feb 13, 2014.

  1. Odyss3us

    Thread Starter New Member

    Feb 12, 2014
    9
    0
    Hi,

    I'm trying to use the CTMU unit in a PIC24F16KA302 in an impedance measuring system. I'm applying a sinusoidal signal to a component and creating a voltage and current signal which I can then use to find the magnitude and phase differences of, giving me the impedance of that component. I'm using comparators to turn the sinusoidal voltage and current signals into square waves and I'm trying to measure the phase differences between these square waves with the CTMU but I'm having problems getting it to work.

    I'm applying a square wave representing the voltage signal to CTED1 and the current signal to CTED2.

    By following the App Notes for the CTMU, I'm able to set up the CTMU and calibrate the current and capacitance, and I'm getting reasonable values (58 uA on the 55 uA setting, 8 pF for the A/D hold capacitor).

    I can't seem to use the CTMU to measure the time difference between CTED1 and CTED2 however. Also when I turn on the CTMU the voltage signal (CTED1) which should have a peak voltage of 3.3V gets dragged down to a peak of around 1.5V, which I assume means that the CTMU is drawing large amounts of current since the voltage signal comes from a low impedance output compartor. I have no idea idea why this is happening.

    Any help would be greatly appreciated.

    Here is my code for trying to get a reading from the CTMU:

    float Vread = 0, time_delay = 0;
    CTMUCON1 = 0x0500; //trigger is enabled and edge 1 must come before edge 2
    CTMUCON2 = 0xCCC8; // Edge polarites are positive, CTED 1 and 2 are selected, EDG1and EDG2 set to 0
    CTMUICON = 0x8700; // 55uA, current adjusted to low setting

    AD1CHS = 0x1111; // Disconnect the Analog channels from ADC to minimize stray capacitance
    AD1CSSL = 0x0000;
    AD1CON1 = 0x8040; // Enable ADC - Sample finishing should be controlled by CTMU (but isn't because sampling doesn't finished)
    AD1CON2 = 0x0000;
    AD1CON3 = 0x0000;
    AD1CTMUENLbits.CTMEN0 = 1; // Not sure what this does / if I need it

    AD1CON1bits.SAMP = 1; // start sampling

    CTMUCON1bits.EDGEN = 1; // enable edges
    CTMUCON1bits.CTMUEN = 1; // enable CTMU


    while(!IFS0bits.AD1IF); //Wait for A/D conversion to complete


    Vread = ADC1BUF0;
    time_delay = Vread*(CTMUCap/CTMUISrc) * (ADREF /ADSCALE); // Get time difference


    Thank you
     
  2. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,912
    2,180
  3. Odyss3us

    Thread Starter New Member

    Feb 12, 2014
    9
    0
    Hi,

    Thanks for replying. I actually already found that code and looked at it. I was hoping I could do this without using interrupts. I'll try setting the cted pins as in puts, I don't think I did that, should they be set as analog inputs? Or just set the tris registers?

    Would you mind looking at my a/d code, I'm not really sure that it's right. My understanding is that I should start the sampling, enable edges and the ctmu, and after the ctmu has both edges rise it will send a trigger to the a/d to stop sampling. But I'm not sure my code will do that. Any suggestions?

    Thanks again
     
  4. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,912
    2,180
    The inputs should be digital so just set the TRIS registers. You don't have to use interrupts, polling the flags also will work. There are several ways to trigger the ADC conversion but my method does not auto-trigger. Time measurement is based on the amount of voltage on the sample and hold capacitor. First discharge the sample cap to zero volts (the internal and/or external A/D hold capacitance), arm the CTMU so that the trigger1 edge starts the current source charging the cap, when the trigger2 edge happens the current source stops charging. The voltage on the sample cap is now measured with the ADC. The ADC register value represents a linear slope of time from zero to max voltage of the current source/ADC measurement range.

    http://www.flickr.com/photos/nsaspook/6535324205/in/set-72157628477730187
    In this demo I'm varying (increasing) the CTMU sample/hold capacitance by placing my finger near a charge plate wired to the ADC input with a fixed time sample period. You can see the value of voltage changing on the scope. If you fix the capacitance value and change the sample period the effect is the same.
     
    Last edited: Feb 13, 2014
  5. Odyss3us

    Thread Starter New Member

    Feb 12, 2014
    9
    0
    Sorry for not being more clear, I have two square waves with a fairly small phase shift between them. I want to measure this phase shift. The problem is the frequency of the square waves is fairly high. I'm looking for a way to setup the ctmu so that it triggers an a/d conversion immediately after both cted pins have a rising edge. Or to alternatively turn off the ctmu after one such event. My concern is that if the a/d is not triggered to convert automatically then the ctmu will charge the a/d sample capacitor over multiple rising edges. If this happens then the voltage on the sample cap will represent some integer multiple of the time difference between the rising edges of the square waves, where I only want one. Hopefully that makes sense. Any suggestions?
     
  6. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,912
    2,180
    The edge trigger is a one-shot process that won't re-enable the current source until both edge status bits are reset so you have plenty time (in respect to the rate of your two square wave edge changes) to run the A/D conversion without worrying about the voltage changing until you clear both bits again.

    Edge sequencing with proper edge and polarity bits are enabled.

    Both edge status bits are cleared
    wave #1 -> External input CTED1 sets EDG1STAT -> current source starts charging
    wave #2 -> External input CTED2 sets EDG2STAT -> current source stops charging, voltage level relates to time between edges
    edge flags remain set and no additional charging until edge flags are cleared by software.
    CTMU interrupt flag is set -> use an ISR or poll to start A/D conversion
    A/D conversion complete, read value, etc...
    Loop:
     
    Last edited: Feb 13, 2014
  7. Odyss3us

    Thread Starter New Member

    Feb 12, 2014
    9
    0
    Ya I just re-read the datahsheet and found that, thanks for the help
     
Loading...