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
And my main loop is the following
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
}
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
}
}