This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430G2333: DCO calibration with 32KHz watch crytal

Part Number: MSP430G2333


Hi there

I am using a msp430g2333, and trying to calibrate the DCO with the 32KHz watch crystal. But my code doesn't seem to do anything. What did I do wrong? thanks

Regards

James

int main(void) {
    initialIOs();

    /* wait for crystal to be stabalized */
    __delay_cycles(1000000);


    DCOCalibrate(DCO_CAL_8MHZ);

	//BCSCTL1 |= CALBC1_8MHZ;				// copy calibate info to the register
	//DCOCTL |= CALDCO_8MHZ;				// copy calibate info to the register

    while(1){
    	LED2_HIGH();
    	delay_ms(1000);
    	LED2_LOW();
    	delay_ms(1000);

    }
}
#include "DCOCalibrate.h"


void DCOCalibrate(unsigned int data)
{
    unsigned int Compare, Oldcapture = 0;
    unsigned char Old_BCSCTL1;
    unsigned int Old_TACCTL0;
    unsigned int Old_TACTL;

    Old_BCSCTL1 = BCSCTL1;				// make a backup
    Old_TACCTL0 = TACCTL0;				// make a backup
    Old_TACTL = TACTL;					// make a backup

    BCSCTL1 |= DIVA_3;                  	// ACLK = LFXT1CLK/8
    TACCTL0 = CM_1 + CCIS_1 + CAP; 	        // CAP, ACLK
    TACTL = TASSEL_2 + MC_2 + TACLR;    	// SMCLK, cont-mode, clear

    while (1) {
        while (!(CCIFG & TACCTL0));     // Wait until capture occured
        TACCTL0 &= ~CCIFG;              // Capture occured, clear flag
        Compare = TACCR0;               // Get current captured SMCLK
        Compare = Compare - Oldcapture; // SMCLK difference
        Oldcapture = TACCR0;            // Save current captured SMCLK

        if (data == Compare){
            break;          			// If equal, leave "while(1)"
        }
        else if (data < Compare) {
            DCOCTL--;                   // DCO is too fast, slow it down
            if (DCOCTL == 0xFF)         // Did DCO roll under?
                if (BCSCTL1 & 0x0f)
                    BCSCTL1--;          // Select lower RSEL
        }
        else {
            DCOCTL++;                   // DCO is too slow, speed it up
            if (DCOCTL == 0x00)         // Did DCO roll over?
                if ((BCSCTL1 & 0x0f) != 0x0f)
                    BCSCTL1++;          // Sel higher RSEL
        }
    }
    TACCTL0 = Old_TACCTL0;              // restore register
    TACTL = Old_TACTL;                  // restore register
    BCSCTL1 = Old_BCSCTL1;              // restore register
}
//DOC calibration constants
#define DCO_CAL_500KHZ				122		// 122 * 4096 = 499.7KHz
#define DCO_CAL_1MHZ				244		// 244 * 4096 = 0.999MHz
#define DCO_CAL_2MHZ				488		// 488 * 4096 = 1.999MHz
#define DCO_CAL_8MHZ				1953	// 1953 * 4096 = 7.999MHz
#define DCO_CAL_12MHZ				2930	// 2930 * 4096 = 12.001MHz

#define DCO_CAL_1843KHZ				450		// 450 * 4096 = 1.843200MHz, good for using uart

**Attention** This is a public forum