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.

Configuring Main Clock to run off 32KHz Crystal

Other Parts Discussed in Thread: MSP430G2553

Hello, I have soldered the provided crystal to the launchpad sockets. Here is my code for configuring it to be my main clock:

#include <msp430.h>

void main(void)
{
    WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

    BCSCTL1 &= ~XTS;
    BCSCTL3 |= XCAP_3;
    BCSCTL3 |= LFXT1S_0;
    BCSCTL2 |= SELM_3;
    BCSCTL2 |= DIVM_0;

    P1SEL &= ~BIT5;
    P1DIR |= BIT5;

    while(1)
    {
        P1OUT ^= BIT5;
        __delay_cycles(32768);
    }
    
}


However this is not working at all. The LED is flashing way too rapidly. What can I do to fix this?

Thanks.

  • Hi Sufyan,

    What MSP430 are you using? Based on you mentioning Launchpad and seeing that your code uses the BCS module, I'm guessing you are using one of the G2xx devices?

    I'm guessing what is happening might be that the crystal has not settled before you are switching it to source MCLK, or the crystal is not running properly for some reason. Your code should check for  the fault flags to clear to ensure the crystal is running before switching it to source MCLK - as you can see in the 2xx user's guide www.ti.com/lit/pdf/slau144 secion 5.2.7 BCS+ Fail-Safe Operation, you'll see that if LFXT1 is sourcing MCLK and a crystal fault is detected, then the MCLK will default back to running off of the DCO regardless of the SELMx setting - this is likely why your LED is blinking too fast.

    Section 5.2.7.1 in the user's guide www.ti.com/lit/pdf/slau144 discusses the correct procedure to follow to check all fault flags etc if you are sourcing MCLK from a crystal.

    Regards,

    Katie

  • Hey, thanks for replying. Yes I'm using the MSP430G2553.

    I am having trouble understanding this whole fault-clearing procedure. Here's what I've done, trying to follow what the user guide suggested.

    IFG1 &= ~OFIFG;
        __delay_cycles(100);
    
        if(IFG1&0x02)
        	IFG1 &= ~OFIFG;
    
        __delay_cycles(100);

    I put this right after my clock configuration code. Then comes the LED toggling. The results are the same as last time...the LED is still blinking rapidly.

  • Hi Sufyan,

    If I modify your code this is what works on my board:

        BCSCTL1 &= ~XTS;
        BCSCTL3 |= XCAP_3;
        BCSCTL3 |= LFXT1S_0;
    
        //crystal fault checking
        do
        {
           IFG1 &= ~OFIFG;
           __delay_cycles(100);
        }
        while(IFG1 & OFIFG);
    
        BCSCTL2 |= SELM_3;
        BCSCTL2 |= DIVM_0;

    This is based off of the steps listed in the user's guide www.ti.com/lit/pdf/slau144 section 5.2.7.1 Sourcing MCLK from a Crystal:

    "

    The sequence to switch the MCLK source from the DCO clock to the crystal clock(LFXT1CLK or XT2CLK) is:

    1. Turn on the crystal oscillator and select the appropriate mode

    2. Clear the OFIFG flag

    3. Wait at least 50 μs

    4. Test OFIFG, and repeat steps 2 through 4 until OFIFG remains cleared.

    "

    Main principles:

    •  You want to do the fault checking after setting up the crystal, but BEFORE setting MCLK to source from the crystal.
    • You want to continually check in a loop until the flag has cleared and stays clear through the delay

    Regards,

    Katie

  • Thank you so much it works perfectly. It turns out the crystal was soldered incorrectly. After I fixed it this code configured it.

    Thanks.

  • Independent of the SELM setting, MCLK will only switch away from the DCO if the new clock is running (see the ’fail-safe operation’ chapter in the users guide). As long as the fault bit for XT1 is set (and it won’t clear by itself), MCLK will not switch to XT1, no matter what you select with SELM.
    Same for SMLCK or ACLK, with one exception: For ACLK, trying to switch ACLK to LFXT1 while the fault flag is not clear will result ACLK to switch to REFO, not DCO.
    Once all (!) fault flags are clear and OFIFG has been cleared, all clock switch to the configured sources.

**Attention** This is a public forum