/*

 Test Clk32

 Simple program to test if the Clk32 clock is operating ok

 If operates ok blinks Green LED (changes every second)

 If operates bad fixes Red LED of Green LED blinks at different rate

 02/05/2012: Corrected a confict with P1DIR that was accessed by the

                 WDT Timer Interrupt and the main program.

 */

// Include the register definitions.

// msp430h calls the msp430g2553h, but this last file is

// included also to ease the eclipse error checker

#include <msp430.h>

//#include <msp430g2231.h>

#include <msp430g2553.h>

#include <legacymsp430.h>   // Needed for interrupts

#include "io430masks.h"         // My SET/RESET macros

// Defines --------------------------------------------------

#define LED_RED         BIT0 // Red LED at P1.0            Pulses with the Reed switch

#define LED_GREEN   BIT6 // Green LED at P1.6

// Functions ------------------------------------------------

// Simple delay function

void simpleDelay()

 {

 __delay_cycles(1000);

 }

// Tries to Start of the Clk32 clock

// Uses default value of DIVA_0

void startClk32()

 {

 do

   {

    SET_FIELD(BCSCTL3,XCAP_MASK,XCAP_3); // Set 12.5pF

    CLEAR_FIELD(BCSCTL3,LFXT1S_MASK);         // Set mode 0

    simpleDelay();

   }

   while (BCSCTL3&LFXT1OF); // Loop while faulty

 }

// Configure all the peripherals

void configureAll()

 {

 // Set LEDs as output

 SET_FLAG(P1DIR,LED_RED+LED_GREEN);

 // Red Led while not operating the 32k Clock

 SET_FLAG(P1OUT,LED_RED);

 // If device has port 3 but 20 pins

 #ifdef __MSP430_HAS_PORT3_R__

 P3REN=0xFF;

 #endif

 // Start the 32k clock

 startClk32();

 // If started turn off red led

 RESET_FLAG(P1OUT,LED_RED);

 // Configure WDT as a timer

 WDTCTL=WDT_ADLY_1000;  // t=32768/fACLK=1s

 SET_FLAG(IE1,WDTIE);   // Enable WDT interrupt

 }

int main()        // The main function

 {

 // Disable the watchdog.

 WDTCTL = WDTPW + WDTHOLD;

 // Configure all the peripherals

 configureAll();

 // Enable interrupts

 eint();

 // do forever:

 while (1)

        {

    __delay_cycles(10000);

    dint();

    if (BCSCTL3&LFXT1OF)

                   SET_FLAG(P1OUT,LED_RED);

              else

                   RESET_FLAG(P1OUT,LED_RED);

    eint();

        };

 return 0;

 }

/****************** RSI Functions **********************/

// Watchdog timer interrupt

// Called each second

interrupt(WDT_VECTOR) WDT_ISR(void)

{

 // Don't need to clear any flag

 // Toggle GREEN Led

 P1OUT^=LED_GREEN;

}