CTMU Time Measurement

Thread Starter


Joined Sep 8, 2015
Hi all,
I am working on CTMU of PIC24FJ64GA306. I am stucked in time measurement of CTMU.
Here i am trying to find the time delay between two pulses.
What i am doing is giving pulses to CTED9 AND CTED8,
A resistor of 10K is connected across the analog pin 10.

Here is the step am following
1. Initialize the ADC and the CTMU.
2. Set EDG1STAT.
3. Set EDG2STAT.
4. Perform an analog-to-digital conversion.
5. Calculate the time between edges as T = (C/I) * V

CTMU Time measurement:
while (1) {

        //start sampling
        AD1CON1bits.SAMP = 1;

        //enable ctmu
        CTMUCON1bits.EDGEN = 1;
        CTMUCON1bits.CTMUEN = 1;
        // wait for the second edge to happen
        while (!CTMUCON2bits.EDG2STAT);

        // stop sampling
        AD1CON1bits.SAMP = 0;

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

        Vread = ADC1BUF0;
        CapVoltage = Vread * ADREF / ADSCALE;
        time_delay = CapVoltage * (CTMUCap / CTMUISrc);

void ADC_ctmu_Init() {
    ANSBbits.ANSB10 = 1;
//    ANSEbits.ANSE3=0;
    AD1CHS0 = 10; //Select the analog channel(10)
    AD1CSSL = 0x0000; //
    AD1CON1 = 0x8000; //Turn On ADC, continue in Idle mode,
    AD1CON2 = 0x0000; //VR+ = AVDD, V- = AVSS, Don't scan,
    AD1CON3 = 0x0000; //ADC uses system clock,
    CTMUCON1bits.TGEN = 0;
    CTMUCON1 = 0x0500; //make sure CTMU is disabled
    CTMUCON2 = 0xCCC8;
    CTMUCON2bits.EDG1SEL=9;//CTED 8
    CTMUCON2bits.EDG2SEL=4;//CTED 9
    CTMUICON = 0x8700; // 55uA, low adjustment
Please give me suggetions.



Joined Aug 27, 2009
You are using the same constant current source with capacitor charge ramp measurement as before with the actual capacitance value measurement but this time the capacitance remains the same but the charge time varies as the time between the selected CTMU edge input pins. You need a capacitor on the analog pin for the time measure code. You can use another ADC channel/pin for the current calibration.


I have an ongoing CTMU (PIC18) project with some code here. It's maybe too advanced for you now but eventually you should be able to get a handle on what's being done.

Last edited:

Thread Starter


Joined Sep 8, 2015
Hi Nsaspook,
Thanks for the reply, Let me clear the hardware connection first.
As for time measurement there is three pins we can access, CTEDx(edge1),CTEDx(edge2),analog pin.
In the datasheet, they have given three pins.

For measuring the time how we need to configure these pins.
I have attached the image along with this reply




Joined Aug 27, 2009
Configure these pins how?

Both edge pins need to be setup as inputs (by using PPS or other config options) and the analog pin set to a unused channel for the smallest time measurement between edge 1 low to high (start charge) and edge 2 low to high (stop charge) by using the ADC to measure the amount of charge voltage on the small (a few pf) capacitance of just the pin and circuit inside the chip to ground.


Joined Aug 27, 2009
So EDGE1 pin - Low to High
EDGE2 pin -low to high

Analog pin-Capacitor
Am i right???
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...

You can add a capacitor if you need to extend the range of time measurements for a set source current calibration. For my project using light links the 'flight' time of travel from light transmitter to light receiver time for 50 meters on the cable only uses the parasitic capacitance of the internal chip die as I can select an ADC channel number that disconnects the S/H input from the pin mux on the 18F45K80 PIC (there should be a channel for this on the PIC24).
// for the PIC18
#define CTMU_CHAN        28    // mux disconnected for lowest possible measurement capacitor
This allows a length resolution of about 0.1 meters using the 45K80 and the 12bit ADC setting with the lowest current source setting.


Joined Aug 27, 2009
Use your calibration resistor connected to the current source output to generate a calibration voltage at the needed current range then adjust these bits until the measured voltage matches the needed calibration voltage with that resistor in the circuit.