CTMU Time Measurement

Discussion in 'Embedded Systems and Microcontrollers' started by Techbee, Sep 11, 2015.

  1. Techbee

    Thread Starter New Member

    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:
    Code (Text):
    2. while (1) {
    4.         //start sampling
    5.         AD1CON1bits.SAMP = 1;
    7.         //enable ctmu
    8.         CTMUCON1bits.EDGEN = 1;
    9.         CTMUCON1bits.CTMUEN = 1;
    10.         printf("TIME\t");
    11.         // wait for the second edge to happen
    12.         while (!CTMUCON2bits.EDG2STAT);
    14.         // stop sampling
    15.         AD1CON1bits.SAMP = 0;
    17.         //Wait for A/D conversion to complete
    18.         while (!IFS0bits.AD1IF);
    20.         Vread = ADC1BUF0;
    21.         printf("Vread=%f\t",Vread);
    22.         CapVoltage = Vread * ADREF / ADSCALE;
    23.         time_delay = CapVoltage * (CTMUCap / CTMUISrc);
    24.         printf("time_delay=%f\n\r",time_delay);
    25.     }
    26. }

    Code (Text):
    2. void ADC_ctmu_Init() {
    3.     ANSBbits.ANSB10 = 1;
    4.     TRISEbits.TRISE4=1;
    5.     TRISEbits.TRISE3=1;
    6.     ANSEbits.ANSE4=0;
    7. //    ANSEbits.ANSE3=0;
    8.     AD1CHS0 = 10; //Select the analog channel(10)
    9.     AD1CSSL = 0x0000; //
    10.     AD1CON1 = 0x8000; //Turn On ADC, continue in Idle mode,
    11.     AD1CON2 = 0x0000; //VR+ = AVDD, V- = AVSS, Don't scan,
    12.     AD1CON3 = 0x0000; //ADC uses system clock,
    13.     CTMUCON1bits.TGEN = 0;
    14.     CTMUCON1 = 0x0500; //make sure CTMU is disabled
    15.     CTMUCON2 = 0xCCC8;
    16.     CTMUCON2bits.EDG1SEL=9;//CTED 8
    17.     CTMUCON2bits.EDG2SEL=4;//CTED 9
    18.     CTMUICON = 0x8700; // 55uA, low adjustment
    19. }
    Please give me suggetions.

  2. nsaspook

    AAC Fanatic!

    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: Sep 11, 2015
  3. Techbee

    Thread Starter New Member

    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

  4. nsaspook

    AAC Fanatic!

    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.
  5. Techbee

    Thread Starter New Member

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

    Analog pin-Capacitor
    Am i right???
  6. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    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).
    Code (C):
    2. // for the PIC18
    3. #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.
  7. Techbee

    Thread Starter New Member

    Sep 8, 2015

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

    AAC Fanatic!

    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.