Hi!

I am using the CTMU of a PIC24FJ64GA106, and I can't seem to calibrate it at all. The CTMUIsrc value always returns as 0, and the CTMUCap always returns as NaN. What could be the problem?

Thanks in advance!

Code:
#include "p24Fxxxx.h"
#include "p24FJ64GA106.h"
#include "adc.h"
#include "comparator.h"
#include "crc.h"
#include "ctmu.h"
#include "i2c.h"
#include "incap.h"
#include "outcompare.h"
#include "pmp.h"
#include "ports.h"
#include "PPS.h"
#include "PwrMgnt.h"
#include "Rtcc.h"
#include "spi.h"
#include "sram.h"
#include "timer.h"
#include "uart.h"
#include "wdt.h"
#include "stdio.h"
#include "stdlib.h"
#include "xc.h"
#include "PIC24F_periph_features.h"

// CONFIG3
#pragma config WPFP = WPFP511           // Write Protection Flash Page Segment Boundary (Highest Page (same as page 42))
#pragma config WPDIS = WPDIS            // Segment Write Protection Disable bit (Segmented code protection disabled)
#pragma config WPCFG = WPCFGDIS         // Configuration Word Code Page Protection Selectbit (Last page(at the top of program memory) and Flash configuration words are not protected)
#pragma config WPEND = WPENDMEM         // Segment Write Protection End Page Select bit (Write Protect from WPFP to the last page of memory)

// CONFIG2
#pragma config POSCMOD = EC             // Primary Oscillator Select (EC Oscillator mode Selected)
#pragma config IOL1WAY = OFF             // IOLOCK One-Way Set Enable bit (Write RP Registers Once)
#pragma config OSCIOFNC = OFF           // Primary Oscillator Output Function (OSCO functions as CLKO (FOSC/2))
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor (Both Clock Switching and Fail-safe Clock Monitor are disabled)
#pragma config FNOSC = PRI              // Oscillator Select (Primary oscillator (XT, HS, EC))
#pragma config IESO = OFF               // Internal External Switch Over Mode (IESO mode (Two-speed start-up)disabled)

// CONFIG1
#pragma config WDTPS = PS1              // Watchdog Timer Postscaler (1:1)
#pragma config FWPSA = PR32             // WDT Prescaler (1:32)
#pragma config WINDIS = OFF             // Watchdog Timer Window (Standard Watchdog timer is enabled(Windowed mode is disabled))
#pragma config FWDTEN = OFF             // Watchdog Timer Enable (Watchdog timer is disabled)
#pragma config ICS = PGx1               // Comm Channel Select (EMUC/EMUD share PGC1/PGD1)
#pragma config GWRP = OFF               // General Code Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF                // General Code Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = OFF             // JTAG Port Enable (JTAG port is disabled)


/**************************************************************************/
//Setup CTMU
/**************************************************************************/


double CTMUISrc=0;
double CTMUCap=0;;

int setupcur(void);
int setupcap(void);
int CurCal(void);
int CapCal(void);
int StartPulse(void);
int TimeMeasure(void);

int main(void)
{
    RCON=0x0000;
    OSCCONL=0x46;
    OSCCONH=0x78;
    //OSCCONH=0x9A;
    //OSCCONH=0x22;
    /*OSCCONbits.CLKLOCK=0;
    OSCCONbits.IOLOCK=1;
    OSCCONbits.LOCK=0;
    OSCCONbits.CF=0;
    OSCCONbits.POSCEN=1;
    OSCCONbits.SOSCEN=1;
    OSCCONbits.OSWEN=0;*/
    CLKDIV=0x0000;
    REFOCON=0x0000;
    CurCal();
    CapCal();
    /*while(1)
    {
        // Step 7: Wait for ADC interrupt
        while(IFS0bits.AD1IF == 0)
        {
            // Steps 8-11
            IFS0bits.AD1IF = 0; // clear the interrupt
            result = ADC1BUF0; // read ADC result
            CTMUCONbits.IDISSEN = 1; // begin manual discharge of cap
            asm("NOP"); // may be required for external caps
            asm("NOP");
            asm("NOP");
            asm("NOP");
            CTMUCONbits.IDISSEN = 0; // stop discharge of cap
            CTMUCON &= ~0x0300; // clear the edge status bits
        }
        
    }*/
    //TimeMeasure();
    int i=0;
    TRISFbits.TRISF0=0;
    LATFbits.LATF0=1;
    while(1)
    {
        LATFbits.LATF0=0;
        for(i=0;i<8100;i++);
        LATFbits.LATF0=1;
        for(i=0;i<8100;i++);
    }
    return(1);
}


int setupcur(void)
{         //CTMUCON - CTMU Control register
        CTMUCON = 0x80FC; //make sure CTMU is disabled
        //CTMU continues to run when emulator is stopped,CTMU continues
        //to run in idle mode,Time Generation mode disabled, Edges are blocked
        //No edge sequence order, Analog current source not grounded, trigger
        //output disabled, Edge2 polarity = positive level, Edge2 source =
        //source 0, Edge1 polarity = positive level, Edge1 source = source 0,
        // Set Edge status bits to zero
        //CTMUICON - CTMU Current Control Register
        CTMUICON = 0x0100; //0.55uA, Nominal - No Adjustment
        //-------------------------------------------------------------------------
        //setupAD converter;
        //-------------------------------------------------------------------------
        TRISB=0x0001; //set channel 0 as an input
        AD1PCFGL=0x0000; //
        AD1PCFGH=0x0000;
        AD1CHS=0x0000; //select the analog channel(0)
        AD1CSSL=0x0001; //
        AD1CON1 = 0x8000; //Turn On A/D Converter, continue in
        // idle mode, Unsigned fractional format, Clear Samp bit to start
        //conversion, Sample when SAMP bit is set, sampling on hold
        AD1CON2 = 0x0000; //VR+ = AVDD, V- = AVSS, Don't scan,
        //always use MUX A inputs
        AD1CON3 = 0x0100; //A/D uses system clock, conversion
        //clock = 1xTcy
        return(1);
}

int setupcap(void)
{
      //CTMUCON - CTMU Control register
        CTMUCON = 0x80FC; //make sure CTMU is disabled
        //CTMU continues to run when emulator is stopped,CTMU continues
        //to run in idle mode,Time Generation mode disabled, Edges are blocked
        //No edge sequence order, Analog current source not grounded, trigger
        //output disabled, Edge2 polarity = positive level, Edge2 source =
        //source 0, Edge1 polarity = positive level, Edge1 source = source 0,
        // Set Edge status bits to zero
        //CTMUICON - CTMU Current Control Register
        CTMUICON = 0x0100; //0.55uA, Nominal - No Adjustment
        //-------------------------------------------------------------------------
        //setupAD converter;
        //-------------------------------------------------------------------------
        TRISB=0x0002; //set channel 1 as an input
        AD1PCFGL=0x0000; //
        AD1PCFGH=0x0000;
        AD1CHS=0x0101; //select the analog channel(1)
        AD1CSSL=0x0002; //
        AD1CON1 = 0x8000; //Turn On A/D Converter, continue in
        // idle mode, Unsigned fractional format, Clear Samp bit to start
        //conversion, Sample when SAMP bit is set, sampling on hold
        AD1CON2 = 0x0000; //VR+ = AVDD, V- = AVSS, Don't scan,
        //always use MUX A inputs
        AD1CON3 = 0x0100; //A/D uses system clock, conversion
        //clock = 1xTcy
        return(1);
}

#define COUNT 500 //@ 8MHz = 125uS.
#define DELAY for(i=0;i
Click image for larger version. 

Name:	tdr.jpg 
Views:	7 
Size:	515.6 KB 
ID:	130437