PIC24EP512GP202 CTMU Software Test

Status
Not open for further replies.

Thread Starter

Mellowed

Joined Mar 6, 2016
13
Hi Guys,

I'm struggling to get any valid readings from a CTMU module on the PIC24EP512GP202 below is the code i am using :( i struggled to do the current calibration as well and i am wonder if perhaps there is an issue with the way i setup the ADC or perhaps something wrong with the CTMU on this particular chip itself :(

Code:
void CtmuTimeConfigManualADC(void)
{
// Step 1 Configure the CTMU
CTMUCON1 = 0x0000; // Disable CTMU
CTMUCON1bits.TGEN = 0; // Disable Time Generation mode
CTMUCON1bits.EDGEN = 0; // Edges are disabled
CTMUICONbits.ITRIM = 0; // Set trim
CTMUICONbits.IRNG = 3; // set base x100

ANSELA=0x0003;  // use only AN0-AN1 as analog
ANSELB=0x0000;  
// input pins
_TRISA0 = 0;  // analog AN0
// Step 2 Configure the port 
TRISAbits.TRISA0 = 1; // Set channel 

// Step 3 configure the ADC
AD1CON1 = 0x0000; // Turn off ADC
AD1CON2 = 0x0000; // VR+ = AVDD, V- = AVSS, Don't scan,
AD1CON3 = 0x0000; // ADC uses system clock
AD1CON3bits.ADCS = 8; // conversion clock = 1xTcy

AD1CON1bits.ADON = 1;
AD1CHS0bits.CH0SA = 0; // Select AN0
AD1CSSHbits.CSS31 =0;   //I think the errata datasheet says this doesn't work :(

CTMUCON1bits.CTMUEN = 1; // Enable the CTMU

}
And my main loop is the following

Code:
void SoftwareTestCTMU(void)
{

    CTMUCON2bits.EDG2STAT = 0;
    CTMUCON2bits.EDG1STAT = 0;

    while(1)
    {
     char s[64];

        CTMUCON2bits.EDG1STAT = 1;
        delayXus(10);
        CTMUCON2bits.EDG1STAT = 0;

     AD1CON1bits.SAMP = 1; // Manual sampling start
    // step 7 ~3000 us delay to charge sample cap
     delayXus(3000);
    // step 8 Convert the sample
    AD1CON1bits.SAMP = 0; // Begin A/D conversion
    while(AD1CON1bits.DONE == 0); // Wait for A/D convert complete

    CTMUCON1bits.CTMUEN = 0; // Disable the CTMU
    IFS0bits.AD1IF = 0; // clear the interrupt
    int result = ADC1BUF1; // read ADC result
    int result1 = ADC1BUF0;
    int result2 = ADC1BUF2;
    int result3 = ADC1BUF3;



    sprintf(s,"\r\nDoing Science %02d %02d %02d %02d\r\n",result,result1,result2,result3);
                  PutStrU1(s);
    CTMUCON1bits.IDISSEN = 1; // begin manual discharge of cap
    delay1s();
    CTMUCON1bits.IDISSEN = 0; // stop discharge of cap

        CTMUCON1bits.CTMUEN = 1; // Enable the CTMU
    }

}
 

Thread Starter

Mellowed

Joined Mar 6, 2016
13
Just In case anyone ever has the same issue, below was my solution.
I had the following two issues.

  1. The ADC pin connected to the CTMU module needs to be disconnected from any breadboard or connection. (i actually ended up just buying the microstick dev kit and removing the AN0 pin from the breadboard)
  2. There is a fair amount of current leakage whilst waiting for the signal to arrive, but after both edgestats are set high the leakage is not an issue. For my real time measurements i used the timer to count 1ms and discharge the cap and reset the ctmu. This particular chip cannot work with the "CHOSA=31" which is the "open connection".
  3. The best resolution i could achieve has so far been around 200ps with a very stable repeatable measurement. I am however still working on the input buffer circuit to confirm this with external pulses.
My code below was used for software testing of the CTMU, comments are probably all wrong since i was doing a lot of trial and error

Code:
void CtmuTimeConfigAutoADC(void)
{
// Step 1 Configure the CTMU
CTMUCON1 = 0x0000; // Disable CTMU
CTMUCON1bits.TGEN = 0; // Disable Time Generation mode
CTMUCON1bits.EDGEN = 0; // Edges are enabled
CTMUICONbits.ITRIM = 0; // Set trim
CTMUICONbits.IRNG = 2; // set high bit of range
CTMUCON1bits.CTTRIG = 1;

ANSELA=0x0003;  // use only AN0-AN1 as analog
ANSELB=0x0000;  // use only AN2 - AN3 (RB0,RB1) as analog
// input pins
TRISAbits.TRISA0 = 1; // Set channel 2
ANSELAbits.ANSA0 = 1; // Make AN0 as analog (Resistor is connected to this pin)

// Step 3 configure the ADC
AD1CON1 = 0x0000; // Turn off ADC
AD1CON1bits.SSRCG = 0;
AD1CON1bits.SSRC = 6;
AD1CON2 = 0x0000; // VR+ = AVDD, V- = AVSS, Don't scan,
AD1CON3 = 0x0000; // ADC uses system clock
AD1CON3bits.ADCS = 8; // conversion clock = 1xTcy

AD1CON1bits.ASAM = 1;
AD1CON1bits.ADON = 1;
AD1CHS0bits.CH0SA = 0; // Select AN2
AD1CHS0bits.CH0SB = 0; // Select AN2
AD1CHS0bits.CH0NB = 0; // Negative pin is Vrefl
AD1CHS0bits.CH0NA = 0; // Negative pin is Vrefl
AD1CSSHbits.CSS31 =0;
AD1CSSLbits.CSS0 = 0;

CTMUCON2 &= ~0x0300; // clear the edge status bits
IFS0bits.AD1IF = 0;
CTMUCON1bits.CTMUEN = 1; // Enable the CTMU

}
Code:
void SoftwareTestCTMUAuto(void)
{
char s[64];

    while(1)
    {
   
      //  AD1CON1bits.SAMP = 1;
      
                LEDB1 = 1;
   //     delay1s();
       // delay1ms();
        delayXus(2);
        LEDB2 = 1;
   
        CTMUCON2bits.EDG1STAT = 1;
        delay1us();
      
        CTMUCON2bits.EDG2STAT = 1;

            while(IFS0bits.AD1IF == 0);
            {

              

                int result = ADC1BUF1; // read ADC result
                int result1 = ADC1BUF0;
                int result2 = ADC1BUF2;
                int result3 = ADC1BUF3;

                sprintf(s,"\r\nDoing Science %02d %02d %02d %02d Status %02d %02d\r\n",result,result1,result2,IFS0bits.AD1IF,CTMUCON2bits.EDG1STAT,CTMUCON2bits.EDG2STAT);
                              PutStrU1(s);
                CTMUCON1bits.IDISSEN = 1; // begin manual discharge of cap
                delay1ms();
                CTMUCON1bits.IDISSEN = 0; // stop discharge of cap
              
                CTMUCON2 &= ~0x0300; // clear the edge status bits
                IFS0bits.AD1IF = 0;
            }      

            LEDB2 = 0;
            LEDB1 = 0;
        
    }

}
 
Status
Not open for further replies.
Top