I should also point out that the stabilization test in that is not optimized, in part because it's nice to see the blinking to ensure that the board is functioning. A better one for real code would be:
do {
IFG1 &= ~OFIFG;
/* Need at least 50usec delay; boot MCLK is ~800kHz */
__delay_cycles(50);
} while (IFG1 & OFIFG);
The magic "50usec" came from the 2xx manual description of the Basic Clock System. Other clock peripherals might require a different delay. Don't make this too short, or you might prematurely pass the stabilization check.
I've found that most MSP430s in the 2xx and 5xx/6xx families use port pins rather than dedicated pins for XIN/XOUT. The manual and/or datasheet indicates that you must select the peripheral function for those pins in order for the crystal function to affect the clock system, even when the board powers-up with the crystal as the clock source. On the LaunchPad ValueLine processors XIN and XOUT are P2.6 and P2.7. Some TI examples do not show or mention this requirement.
A close review of the data sheet is necessary to determine whether it's sufficient to set PxSEL for XIN or it it's also necessary to set it for XOUT; and whether the corresponding PxDIR bits also must be explicitly set or if this is done for you.
The code below comes with test430 (demos/launchpad/lpcrystal) and is what I use on the launchpad for verifying that a crystal works. (The MSP430G2231 data sheet in the schematics for P2.6 and P2.7 suggests that it may be necessary to also set P2DIR=BIT7, though in practice what's below works for me, and it may be that the pin is automatically set based on P2SEL.)
/** Test whether crystal works, and bring out clock signals.
*
* Launchpad: ACLK on P1.0, SMCLK on P1.4. The green LED flashes
* while the application tests the stability of the crystal. If
* functional, the green LED is lit solid and ACLK should be 32768Hz.
* If an oscillator fault is present after testing, the green LED is
* turned off and ACLK is switched to be VLOCLK.
*
* NOTE: This application sends ACLK to P1.0, and SMCLK to P1.4, to
* allow external measurement. Because P1.0 is also attached to the
* red LED, the red LED being lit is not significant.
*
*/
#include
void main (void)
{
test430_initialize();
#if __MSP430G2211__ || __MSP430G2231__ || __MSP430G2553__
P1SEL |= BIT0 | BIT4;
P1DIR |= BIT0 | BIT4;
/* See whether the crystal is populated and functional. Note:
* Crystal stabilization takes a certain period, perhaps hundreds of
* milliseconds. */
BCSCTL3 &= ~LFXT1S_3;
P2SEL |= BIT6 | BIT7;
{
unsigned int ctr = 100;
while (ctr--) {
IFG1 = 0;
__delay_cycles(100000);
test430_led_green(-1);
}
}
if (IFG1 & OFIFG) {
/* No functional crystal; fall back to drive ACLK from VLOCLK */
test430_led_green(0);
P2SEL &= ~(BIT6 | BIT7);
BCSCTL3 |= LFXT1S_2;
} else {
test430_led_green(1);
}
#if ! TEST430_INHIBIT_PRINTF
printf("BCS+: DCOCTL=%#04x CTL1=%#04x CTL2=%#04x CTL3=%#04x\n",
DCOCTL, BCSCTL1, BCSCTL2, BCSCTL3);
puts("Find ACLK on P1.0 and SMCLK on P1.4");
#endif
#else
#warning No map to clock pins
#endif
while (1) {
__bis_status_register(LPM0_bits);
}
test430_finalize();
}