# CTMU Time Measurement

#### Techbee

Joined Sep 8, 2015
14
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:
Code:
while (1) {

//start sampling

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

// stop sampling

//Wait for A/D conversion to complete

time_delay = CapVoltage * (CTMUCap / CTMUISrc);
printf("time_delay=%f\n\r",time_delay);
}
}

setup
Code:
void ADC_ctmu_Init() {
ANSBbits.ANSB10 = 1;
TRISEbits.TRISE4=1;
TRISEbits.TRISE3=1;
ANSEbits.ANSE4=0;
//    ANSEbits.ANSE3=0;
AD1CHS0 = 10; //Select the analog channel(10)
AD1CON2 = 0x0000; //VR+ = AVDD, V- = AVSS, Don't scan,
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
}

Thankyou,
Techbee

#### nsaspook

Joined Aug 27, 2009
7,356
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:

#### Techbee

Joined Sep 8, 2015
14
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

Thanks,
Techbee.

#### nsaspook

Joined Aug 27, 2009
7,356
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.

#### Techbee

Joined Sep 8, 2015
14
So EDGE1 pin - Low to High
EDGE2 pin -low to high

Analog pin-Capacitor
Am i right???

#### nsaspook

Joined Aug 27, 2009
7,356
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...
Loop:

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).
C:
// 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.

#### Techbee

Joined Sep 8, 2015
14
Hi,

I did not understand the trimming process.
How to trim the current???

#### nsaspook

Joined Aug 27, 2009
7,356