fet140_fll_01.c


1
//******************************************************************************
2
//  MSP-FET430P140 Demo - Basic Clock, Implement Auto RSEL SW FLL
3
//
4
//  Description: Set DCO clock to (Delta)*(4096) using software FLL. DCO clock
5
//  is output on P5.5 as SMCLK.  DCO clock, which is the selected SMCLK source
6
//  for timer_A is integrated over LFXT1/8 (4096) until SMCLK is is equal
7
//  to Delta.  CCR2 captures ACLK.  To use Set_DCO Timer_A must be
8
//  operating in continous mode.  Watch crystal for ACLK is required for
9
//  this example.  Delta must be kept in a range that allows possible
10
//  DCO speeds.  Minimum Delta must ensure that Set_DCO loop
11
//  can complete within capture interval. Maximum delta can be calculated be
12
//  f(DCOx7) / 4096.  f(DCOx7) can be found in device specific datasheet.
13
//  ACLK = LFXT1/8 = 32768/8, MCLK = SMCLK = target DCO
14
//  //* External watch crystal installed on XIN XOUT is required for ACLK *//  
15
//
16
//           MSP430F149
17
//         ---------------
18
//     /|\|            XIN|-
19
//      | |               | 32kHz
20
//      --|RST        XOUT|-
21
//        |               |
22
//        |           P5.5|--> SMLCK = target DCO
23
//        |           P5.6|--> ALCK = 4096
24
//
25
//
26
//  M. Buccini
27
//  Texas Instruments Inc.
28
//  Feb 2005
29
//  Built with CCE Version: 3.2.0 and IAR Embedded Workbench Version: 3.21A
30
//******************************************************************************
31
32
#include  <msp430x14x.h>
33
34
void Set_DCO (void);
35
36
void main(void)
37
{
38
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
39
  P5DIR |= 0x60;                            // P5.5,6 output
40
  P5SEL |= 0x60;                            // P5.5,6 SMCLK, ACLK output
41
  Set_DCO();                                // Set DCO
42
43
  while (1);
44
}
45
46
//------------------------------------------------------------------------------
47
void Set_DCO (void)                         // Set DCO to selected frequency
48
//------------------------------------------------------------------------------
49
{
50
//#define DELTA 900                           // target DCO = DELTA*(4096) = 3686400
51
#define DELTA 256                           // target DCO = DELTA*(4096) = 1048576
52
//#define DELTA 70                            // target DCO = DELTA*(4096) = 286720
53
  unsigned int Compare, Oldcapture = 0;
54
55
  BCSCTL1 |= DIVA_3;                        // ACLK= LFXT1CLK/8
56
  CCTL2 = CM_1 + CCIS_1 + CAP;              // CAP, ACLK
57
  TACTL = TASSEL_2 + MC_2 + TACLR;          // SMCLK, cont-mode, clear
58
59
  while (1)
60
  {
61
    while (!(CCIFG & CCTL2));               // Wait until capture occured
62
    CCTL2 &= ~CCIFG;                        // Capture occured, clear flag
63
    Compare = CCR2;                         // Get current captured SMCLK
64
    Compare = Compare - Oldcapture;         // SMCLK difference
65
    Oldcapture = CCR2;                      // Save current captured SMCLK
66
67
    if (DELTA == Compare) break;            // If equal, leave "while(1)"
68
    else if (DELTA < Compare)               // DCO is too fast, slow it down
69
    {
70
      DCOCTL--;
71
      if (DCOCTL == 0xFF)
72
      {
73
        if (!(BCSCTL1 == (XT2OFF + DIVA_3)))
74
        BCSCTL1--;                          // Did DCO roll under?, Sel lower RSEL
75
      }
76
    }
77
    else
78
    {
79
      DCOCTL++;
80
      if (DCOCTL == 0x00)
81
        {
82
          if (!(BCSCTL1 == (XT2OFF + DIVA_3 + 0x07)))
83
          BCSCTL1++;                        // Did DCO roll over? Sel higher RSEL
84
        }
85
    }
86
  }
87
  CCTL2 = 0;                                // Stop CCR2
88
  TACTL = 0;                                // Stop Timer_A
89
}