MSP430 - Low Frequency Xtal

Okay, get out the soldering iron and solder the 32768Hz crystal to the SMD pads provided at pins 18 and 19 of the MSP430G2553 chip.








Our goal is to run with the lowest current drain and still keep accurate time.

Here is our program modified to use the crystal oscillator.

Code:
// Timer example with 32768Hz crystal
// 2013.04.27 - MrChips

#include "io430.h"

#define ON 1
#define OFF 0

#define LED1 P1OUT_bit.P0
#define LED2 P1OUT_bit.P6

void init(void)
{

  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;

  P1OUT = 0x00;
  P1DIR = 0xFF;

  // Set up 32768Hz crystal
  BCSCTL1 |= DIVA_3;    // divide by 8
  BCSCTL3 |= XCAP_3;    // select 12pF caps

  // initialize Timer0_A
  TA0CCR0 = 512; // was 62500;  // set up terminal count
  TA0CTL = TASSEL_1 + ID_3 + MC_1; // configure and start timer

  // enable interrupts
  TA0CCTL0_bit.CCIE = 1;   // enable timer interrupts
  __enable_interrupt();    // set GIE in SR
}

#pragma vector = TIMER0_A0_VECTOR
__interrupt void myTimerISR(void)
{
  LED1 = ~LED1;
}

void main( void )
{
  init();
  
  while (1)
  {
    // any code goes here
  }
  
}
Some explanations are in order.

1) The crystal oscillator runs at 32768Hz. We want to use the lowest frequency we can get from ACLK (auxiliary clock). So we divide this by 8 (the largest divider provided for us on the chip). This gives a frequency of 4096Hz for ACLK.

Note here that I have introduced a new operation in C.
On power on the DIVA bits in BCSCTL1 are 00.
If we wish to set DIVA bits we can use the bit-wise OR operator as follows

BCSCTL1 = BCSCTL1 | DIVA_3;

Another way of writing this is:

BCSCTL1 |= DIVA_3;

Note that the OR operator only works in this case because the original bits were zero.

There are safer ways of doing this but we will keep it simple for now.

2) Normally, we need a pair of load capacitors on the pins of the crystal for proper operation. On the MSP430 you can enable three different internal load capacitors. We select the 12pF caps using the instruction

BCSCTL3 |= XCAP_3;


3) We configure TA0CTL to select ACLK by specifying TASSEL_1.

We had already chosen a divide by 8 prescaler to clock Timer0_A. This brings the timer clock to 512Hz.

4) We change the TA0CCR0 terminal count to 512 so that we get a timer interrupt after 512 clock pulses, i.e. after one second.

We leave the rest of the code as is for now.

In the next blog post, I will show how to program for low power operation.

Blog entry information

Author
MrChips
Views
2,056
Comments
2
Last update

More entries in General

More entries from MrChips

Share this entry

Top