CTMU Second Event Working

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

  1. Odyss3us

    Thread Starter New Member

    Feb 12, 2014
    9
    0
    Hi,

    I'm trying to use the CTMU on a PIC24F16KA302 to measure the time difference between two pulses and I can't get the second edge to register.
    I have it setup so that the first edge triggers on the edge of CTED1 (pin 20 of the 28 pin pic) and the second edge should trigger on the rising edge of CTED2 (pin 23). I can see that the edges on my oscilloscope and everything is fine there, but the PIC never detects edge 2. Any suggestions?

    Here is my code:

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

    //set up ctmu
    CTMUCON1 = 0x0500; //make sure CTMU is disabled
    CTMUCON2 = 0xCCC8;
    CTMUICON = 0x8700; // 55uA, low adjustment

    set up inputs and a/d
    TRISAbits.TRISA6 = 1; //cted1
    TRISBbits.TRISB12 = 1; //cted2
    AD1CHS = 0x1111;
    AD1CSSL = 0x0000;
    AD1CON1 = 0x8000;
    AD1CON2 = 0x0000;
    AD1CON3 = 0x0000;

    //start sampling
    AD1CON1bits.SAMP = 1;

    //enable ctmu
    CTMUCON1bits.EDGEN = 1;
    CTMUCON1bits.CTMUEN = 1;

    // wait for the second edge to happen
    while(!CTMUCON2bits.EDG2);

    // 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);

    My program gets stuck at the line:
    while(!CTMUCON2bits.EDG2);

    Thanks
     
  2. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,912
    2,176
    Have you checked the analog configuration bits for any pins you want for digital input that have analog functions? I think the default is to disable the digital input buffer. Check the ANSA, ANSB, ANSC registers for proper configuration.
     
  3. Odyss3us

    Thread Starter New Member

    Feb 12, 2014
    9
    0
    Should the analog configuration bits be enabled or disabled?
    I assumed that they should be disabled so I didn't set them
     
  4. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,912
    2,176
    They should be disabled on inputs you want to be digital. You will have to clear the bits as I think the reset default for analog capable pins is for them to be set.

    It's usually a good idea to have a chip pin init routine that manually configures all pins to the desired I/O configuration to be sure you don't miss something.
     
    Last edited: Feb 16, 2014
  5. Odyss3us

    Thread Starter New Member

    Feb 12, 2014
    9
    0
    Oh, Ok, I thought that the analog was initially disabled.

    I do have one more question though, for some reason I can't seem to find which ANSA, ANSB, ANSC bits correspond to the pins I'm using.

    The data sheet shows that my PIC has ANSA, ANSB and ANSC but the pin diagram shows only pins that have ANx where some of the values of x don't have matches in ANSA, ANSB or ANSC.

    How do I know which ANSA/B/C bits to clear? I thought that pins ANx were supposed to match up to a bit in ANSA. Is this not correct?

    Thanks again
     
  6. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,912
    2,176
    Look at Table 4-18: Analog Select Register Map
     
    Last edited: Feb 17, 2014
Loading...