MSP430G2333: DCO calibration with 32KHz watch crytal

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Hi guys

I am using a msp430g2333, and trying to calibrate the DCO with the 32KHz watch crystal as of a TI app note. But my code doesn't seem to do anything. What did I do wrong? Any advice is appreciated.

app note: http://www.ti.com/lit/an/slaa336a/slaa336a.pdf

Code:
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

    // test code
    while(1){
        LED2_HIGH();
        delay_ms(1000);
        LED2_LOW();
        delay_ms(1000);

    }
}
Code:
/*
* DOCCalibrate.h
*
*  Created on: 2/06/2017
*      Author: james
*/

#ifndef SRC_DCOCALIBRATE_H_
#define SRC_DCOCALIBRATE_H_

#include <msp430.h>
#include <stdbool.h>
#include <stdint.h>

//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

/*
*  calibrate the DCO in msp430 with a watch crystal.
*  A watch crystal must be connected. This will use
*  TimerA in capture mode. And this function will
*  reset TimerA.
*
*  ref: http://www.ti.com/lit/an/slaa336a/slaa336a.pdf
*
*  @param: data - one of the pre-define value above
*  @param: isStable - when true, the function will
*          wait for the 32k crystal to be stablized
*          first before calibartion.
*/

void DCOCalibrate(unsigned int data);    // calibrate DOC


#endif /* SRC_DCOCALIBRATE_H_ */
Code:
/*
* DOCCalibrate.c
*
*  Created on: 2/06/2017
*      Author: james
*/

#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 + CCIE;     // 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
}
 

MrChips

Joined Oct 2, 2009
30,708
Is the 32768Hz xtal properly installed with proper loading capacitors?
Is this on a proper PCB board or is it on a demo or solderless breadboard?
Is the MCU properly configured and running on the 32768Hz xtal?
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Doh!

The problem is my own code (again!!! Arrr!!)

The BCSCTL1 register, bit 0 - 3 is for DCO turning, while bit 4 - 7 is for something else. At the last line of my code:
Code:
    BCSCTL1 = Old_BCSCTL1;              // restore register
I intend to restore the value in bit 4 - 7 only, but instead I restored the whole register, including bit 0 - 3. So my calibration works, but my last line of code restored the calibration back to where it was. Double Doh!!

ref:http://www.ti.com/lit/ug/slau144j/slau144j.pdf, page 283, section 5.3.2
Capture.PNG
 
Top