ACLK (32768 Hz) to generate a one second timer and toggles an LED

How to Write a C Program to Real Time Clock (One Second Timer) to Use ACLK (32768 Hz) to generate a one second timer and toggles an LED in C Programming Language ?



Solution:
/* Real Time Clock (One Second Timer)
 * Description: Uses ACLK (32768 Hz) to generate a one second timer and toggles an LED.
 */


#include <msp430.h>

int main(void)
{

  WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

  //16Mhz
  if (CALBC1_16MHZ==0xFF) // If calibration constant erased
  {
  while(1); // do not load, trap CPU!!
  }
  DCOCTL = 0; // Select lowest DCOx and MODx settings
  BCSCTL1 = CALBC1_16MHZ; // Set range
  DCOCTL = CALDCO_16MHZ; // Set DCO step + modulation

  P1DIR |= BIT0;     // P1.0 output

  CCTL0 = CCIE;              // CCR0 interrupt enabled
  CCR0 = 32768;
  TACTL = TASSEL_1 + MC_2;   // ACLK, contmode

  _enable_interrupts();      // enable interrupts

}

// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
  P1OUT ^= BIT0; // toggle P1.0
  CCR0 += 32768; // extend timer 1s
}


Learn More :