PIC24F CTMU Problems

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

  1. Odyss3us

    Thread Starter New Member

    Feb 12, 2014

    I'm trying to use the CTMU of a PIC24F16KA302 to measure the time difference between two pulses on CTED1 and CTED2.I've set up the CTMU to calibrate its capacitance and current and that seems to work properly.

    However when I try to measure the voltage on the ADC's sampling capacitor the values that the ADC returns are garbage.

    Sometimes the ADC is maxed out to 1023 (I'm using the 10 bit ADC mode) and I'll turn up the time delay between pulses and the voltage value will go down instead of up. A lot of the time the ADC return the value 127 or 512 no matter what the time delay between the pulses is. I have no idea whats going on.

    Any suggestions would be greatly appreciated.

    Here is my code for measuring the time difference:

    float Vread = 0, time_delay = 0, CapVoltage = 0;


    CTMUCON1 = 0x0500;
    CTMUCON2 = 0xCCC8; // sets CTED 1 and 2 as inputs
    CTMUICON = 0x8700; // 55 uA range, lowest setting
    TRISAbits.TRISA6 = 1;
    TRISBbits.TRISB12 = 1;
    ANSA = 0x0000;
    ANSB = 0x0000;
    AD1CHS = 0x1111; //setting up A/D
    AD1CSSL = 0x0000;
    AD1CON1 = 0x8000;
    AD1CON2 = 0x0000;
    AD1CON3 = 0x0000;

    AD1CON1bits.SAMP = 1; // starts sampling

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

    while(!CTMUCON2bits.EDG2); // waits for second event

    AD1CON1bits.SAMP = 0; //stops sampling and starts converting
    Vread = ADC1BUF0;
    CapVoltage = Vread*ADREF/ADSCALE;
    time_delay = CapVoltage*(CTMUCap/CTMUISrc) * 1000;

    return time_delay;

  2. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    After you enable sampling but before you enable timing edges be sure to drain the CTMU charge channel to be sure it's zero volts like in both of the calibration routines.

    If you change the AD1CHS positive bits to the internal upper or lower guard channels do you get the correct result register readings for those fixed voltages?
  3. Odyss3us

    Thread Starter New Member

    Feb 12, 2014

    I first tried setting the ADC channel to the upper guardband rail and the program refused to run, like the value was a restricted value or something which was weird.

    So then I tried setting it to the lower guardband rail which worked properly.
    It returned the correct value.

    I then tried adding in the code to drain the current source channel after sampling and before enabling edges. It didn't work, I'm still seeing the same problems.

    For example under my current setting the max time difference I should be able to measure before the ADC cap saturates is 500ish nanoseconds. If I set the actual time difference much greater to something like 5 microseconds and run the code the ADC will first return 1023 (correct value since its supposed to be saturated) then the next time I run it it will return 512 then 255. It seems to mostly return these three values no matter what the actual time difference is.

    Its really weird and fustrating. Does this sound like a problem with the ADC? CTMU? Upper guardband rail?

    I'm going to try with a new PIC tomorrow

    Thanks again