PIC24EP512GP202 CTMU Software Test

Discussion in 'Embedded Systems and Microcontrollers' started by Mellowed, Sep 19, 2016.

  1. Mellowed

    Thread Starter New Member

    Mar 6, 2016
    10
    2
    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 (Text):
    1. void CtmuTimeConfigManualADC(void)
    2. {
    3. // Step 1 Configure the CTMU
    4. CTMUCON1 = 0x0000; // Disable CTMU
    5. CTMUCON1bits.TGEN = 0; // Disable Time Generation mode
    6. CTMUCON1bits.EDGEN = 0; // Edges are disabled
    7. CTMUICONbits.ITRIM = 0; // Set trim
    8. CTMUICONbits.IRNG = 3; // set base x100
    9.  
    10. ANSELA=0x0003;  // use only AN0-AN1 as analog
    11. ANSELB=0x0000;  
    12. // input pins
    13. _TRISA0 = 0;  // analog AN0
    14. // Step 2 Configure the port
    15. TRISAbits.TRISA0 = 1; // Set channel
    16.  
    17. // Step 3 configure the ADC
    18. AD1CON1 = 0x0000; // Turn off ADC
    19. AD1CON2 = 0x0000; // VR+ = AVDD, V- = AVSS, Don't scan,
    20. AD1CON3 = 0x0000; // ADC uses system clock
    21. AD1CON3bits.ADCS = 8; // conversion clock = 1xTcy
    22.  
    23. AD1CON1bits.ADON = 1;
    24. AD1CHS0bits.CH0SA = 0; // Select AN0
    25. AD1CSSHbits.CSS31 =0;   //I think the errata datasheet says this doesn't work :(
    26.  
    27. CTMUCON1bits.CTMUEN = 1; // Enable the CTMU
    28.  
    29. }
    And my main loop is the following

    Code (Text):
    1. void SoftwareTestCTMU(void)
    2. {
    3.  
    4.     CTMUCON2bits.EDG2STAT = 0;
    5.     CTMUCON2bits.EDG1STAT = 0;
    6.  
    7.     while(1)
    8.     {
    9.      char s[64];
    10.  
    11.         CTMUCON2bits.EDG1STAT = 1;
    12.         delayXus(10);
    13.         CTMUCON2bits.EDG1STAT = 0;
    14.  
    15.      AD1CON1bits.SAMP = 1; // Manual sampling start
    16.     // step 7 ~3000 us delay to charge sample cap
    17.      delayXus(3000);
    18.     // step 8 Convert the sample
    19.     AD1CON1bits.SAMP = 0; // Begin A/D conversion
    20.     while(AD1CON1bits.DONE == 0); // Wait for A/D convert complete
    21.  
    22.     CTMUCON1bits.CTMUEN = 0; // Disable the CTMU
    23.     IFS0bits.AD1IF = 0; // clear the interrupt
    24.     int result = ADC1BUF1; // read ADC result
    25.     int result1 = ADC1BUF0;
    26.     int result2 = ADC1BUF2;
    27.     int result3 = ADC1BUF3;
    28.  
    29.  
    30.  
    31.     sprintf(s,"\r\nDoing Science %02d %02d %02d %02d\r\n",result,result1,result2,result3);
    32.                   PutStrU1(s);
    33.     CTMUCON1bits.IDISSEN = 1; // begin manual discharge of cap
    34.     delay1s();
    35.     CTMUCON1bits.IDISSEN = 0; // stop discharge of cap
    36.  
    37.         CTMUCON1bits.CTMUEN = 1; // Enable the CTMU
    38.     }
    39.  
    40. }  
     
  2. Mellowed

    Thread Starter New Member

    Mar 6, 2016
    10
    2
    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 (Text):
    1. void CtmuTimeConfigAutoADC(void)
    2. {
    3. // Step 1 Configure the CTMU
    4. CTMUCON1 = 0x0000; // Disable CTMU
    5. CTMUCON1bits.TGEN = 0; // Disable Time Generation mode
    6. CTMUCON1bits.EDGEN = 0; // Edges are enabled
    7. CTMUICONbits.ITRIM = 0; // Set trim
    8. CTMUICONbits.IRNG = 2; // set high bit of range
    9. CTMUCON1bits.CTTRIG = 1;
    10.  
    11. ANSELA=0x0003;  // use only AN0-AN1 as analog
    12. ANSELB=0x0000;  // use only AN2 - AN3 (RB0,RB1) as analog
    13. // input pins
    14. TRISAbits.TRISA0 = 1; // Set channel 2
    15. ANSELAbits.ANSA0 = 1; // Make AN0 as analog (Resistor is connected to this pin)
    16.  
    17. // Step 3 configure the ADC
    18. AD1CON1 = 0x0000; // Turn off ADC
    19. AD1CON1bits.SSRCG = 0;
    20. AD1CON1bits.SSRC = 6;
    21. AD1CON2 = 0x0000; // VR+ = AVDD, V- = AVSS, Don't scan,
    22. AD1CON3 = 0x0000; // ADC uses system clock
    23. AD1CON3bits.ADCS = 8; // conversion clock = 1xTcy
    24.  
    25. AD1CON1bits.ASAM = 1;
    26. AD1CON1bits.ADON = 1;
    27. AD1CHS0bits.CH0SA = 0; // Select AN2
    28. AD1CHS0bits.CH0SB = 0; // Select AN2
    29. AD1CHS0bits.CH0NB = 0; // Negative pin is Vrefl
    30. AD1CHS0bits.CH0NA = 0; // Negative pin is Vrefl
    31. AD1CSSHbits.CSS31 =0;
    32. AD1CSSLbits.CSS0 = 0;
    33.  
    34. CTMUCON2 &= ~0x0300; // clear the edge status bits
    35. IFS0bits.AD1IF = 0;
    36. CTMUCON1bits.CTMUEN = 1; // Enable the CTMU
    37.  
    38. }
    Code (Text):
    1. void SoftwareTestCTMUAuto(void)
    2. {
    3. char s[64];
    4.  
    5.     while(1)
    6.     {
    7.    
    8.       //  AD1CON1bits.SAMP = 1;
    9.      
    10.                 LEDB1 = 1;
    11.    //     delay1s();
    12.        // delay1ms();
    13.         delayXus(2);
    14.         LEDB2 = 1;
    15.    
    16.         CTMUCON2bits.EDG1STAT = 1;
    17.         delay1us();
    18.      
    19.         CTMUCON2bits.EDG2STAT = 1;
    20.  
    21.             while(IFS0bits.AD1IF == 0);
    22.             {
    23.  
    24.              
    25.  
    26.                 int result = ADC1BUF1; // read ADC result
    27.                 int result1 = ADC1BUF0;
    28.                 int result2 = ADC1BUF2;
    29.                 int result3 = ADC1BUF3;
    30.  
    31.                 sprintf(s,"\r\nDoing Science %02d %02d %02d %02d Status %02d %02d\r\n",result,result1,result2,IFS0bits.AD1IF,CTMUCON2bits.EDG1STAT,CTMUCON2bits.EDG2STAT);
    32.                               PutStrU1(s);
    33.                 CTMUCON1bits.IDISSEN = 1; // begin manual discharge of cap
    34.                 delay1ms();
    35.                 CTMUCON1bits.IDISSEN = 0; // stop discharge of cap
    36.              
    37.                 CTMUCON2 &= ~0x0300; // clear the edge status bits
    38.                 IFS0bits.AD1IF = 0;
    39.             }      
    40.  
    41.             LEDB2 = 0;
    42.             LEDB1 = 0;
    43.        
    44.     }
    45.  
    46. }  
     
    JohnInTX and nsaspook like this.
Loading...