This tutorial explains how to have an accurate timing by using the processor's peripheral crystals.

 In this tutorial, we are going to use some predefined functions in order to use clocks. We will not explain how these functions work, we are just going to use them as they are. These functions are in 2 files called F5659Utils.h and .c. We will also take this opportunity to introduce Types.h which contains short hand versions of some very common variable types, and HardwrareConfig.h which is (guess what!) a hardware configuration file.

Overview of the program

As said in previously, the default clock rate of MSP430 is around 1 MHz. Therefore it can run as is without any other configuration at that rate. However, when we want to use a faster clock (24 MHz in this example), we have to use a higher voltage. This will be the first item of the program. Set the processor core voltage to the maximum value in order to use the maximum possible clock. This function is SetCoreVoltage, and there are 4 predefined values, VCORE14 (for 1.4V), to VCORE19 (for 1.9V). We will first set VCORE19.

Next, we have to actually increase the frequency. This is done by FLL (frequency locked loop). The DCO is divided, then compared to the RTC crystal clock. With this method, we can have accurate timing with an accuracy of a crystal clock (around 20 ~ 50 ppm depending on the crystal).Once these 2 fonctions have been called, the software is pretty much the same as earlier, as shown below.

 

--------------------

#include <msp430F5659.h>
#include "F5659Utils.h"

int main(void) {
    WDTCTL = WDTPW + WDTHOLD;               //    Stop WDT
    SetCoreVoltage(VCORE19);                //    Set the core to 1.9V
    SetFLL(24);                             //    Set SMCLK to 24 MHz
    P2DIR |= 0x03;                          //    P2.0 output + P2.1 output (oscillo)
    TA0CCTL0 = CCIE;                        //    CCR0 interrupt enabled
    TA0CCR0 = 16383;
    TA0CTL = TASSEL_1 + MC_1 + TACLR;       //    ACLK, upmode, clear TAR
    __bis_SR_register(LPM0_bits + GIE);     //    Enter LPM0, enable interrupts
}

// Timer0 A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR(void) {
    P2OUT |= 0x02;
    P2OUT ^= 0x01;                          // Toggle P2.0
    P2OUT &= ~0x02;
}

--------------------

As for the timer, we are using ACLK (which is the real-time clock) with a timer value of 16383. This means that every 16384 clock cycles, the timer will return to 0 and generate an interrupt. 16384 is precisely half of the clock frequency, therefore changing the polarity twice a second will result in a signal of exactly 1Hz.

This time, we can check that the timing became quite accurate. The port 2 bit 1 period is exactly 1 second.

AccurateClock

 

 

 

 

 

 

 

 

 

 

 

This time, we could make an accurate timing by using the real-time clock crystal.

In the next tutorial, we are going to explain another functionality of MSP430's timers, which is PWM (for Pulse Width Modulation).

 

Download the C code

A summary of the first tutorials has been added as a downloadable zip file here.