1 | //******************************************************************************
|
2 | // MSP430F22x4 Demo - DCO Calibration Constants Programmer
|
3 | //
|
4 | //
|
5 | // MSP430F22x4
|
6 | // ---------------
|
7 | // /|\| XIN|-
|
8 | // | | | 32kHz
|
9 | // --|RST XOUT|-
|
10 | // | |
|
11 | // | P1.0|--> LED
|
12 | // | P2.1|--> SMLCK = target DCO
|
13 | //
|
14 | // A. Dannenberg
|
15 | // Texas Instruments Inc.
|
16 | // April 2006
|
17 | // Built with CCE Version: 3.2.0 and IAR Embedded Workbench Version: 3.41A
|
18 | //******************************************************************************
|
19 | #include "msp430x22x4.h"
|
20 |
|
21 | #define DELTA_1MHZ 244 // 244 x 4096Hz = 999.4Hz
|
22 | #define DELTA_8MHZ 1953 // 1953 x 4096Hz = 7.99MHz
|
23 | #define DELTA_12MHZ 2930 // 2930 x 4096Hz = 12.00MHz
|
24 | #define DELTA_16MHZ 3906 // 3906 x 4096Hz = 15.99MHz
|
25 |
|
26 | unsigned char CAL_DATA[8]; // Temp. storage for constants
|
27 | volatile unsigned int i;
|
28 | int j;
|
29 | char *Flash_ptrA; // Segment A pointer
|
30 | void Set_DCO(unsigned int Delta);
|
31 |
|
32 | void main(void)
|
33 | {
|
34 | WDTCTL = WDTPW + WDTHOLD; // Stop WDT
|
35 | for (i = 0; i < 0xfffe; i++); // Delay for XTAL stabilization
|
36 | P1OUT = 0x00; // Clear P1 output latches
|
37 | P1DIR = 0x01; // P1.0 output
|
38 | P2SEL |= 0x06; // P2.1 SMCLK output, P2.2 CCI0B input
|
39 | P2DIR |= 0x02; // P2.1 output
|
40 |
|
41 | j = 0; // Reset pointer
|
42 |
|
43 | Set_DCO(DELTA_16MHZ); // Set DCO and obtain constants
|
44 | CAL_DATA[j++] = DCOCTL;
|
45 | CAL_DATA[j++] = BCSCTL1;
|
46 |
|
47 | Set_DCO(DELTA_12MHZ); // Set DCO and obtain constants
|
48 | CAL_DATA[j++] = DCOCTL;
|
49 | CAL_DATA[j++] = BCSCTL1;
|
50 |
|
51 | Set_DCO(DELTA_8MHZ); // Set DCO and obtain constants
|
52 | CAL_DATA[j++] = DCOCTL;
|
53 | CAL_DATA[j++] = BCSCTL1;
|
54 |
|
55 | Set_DCO(DELTA_1MHZ); // Set DCO and obtain constants
|
56 | CAL_DATA[j++] = DCOCTL;
|
57 | CAL_DATA[j++] = BCSCTL1;
|
58 |
|
59 | Flash_ptrA = (char *)0x10C0; // Point to beginning of seg A
|
60 | FCTL2 = FWKEY + FSSEL0 + FN1; // MCLK/3 for Flash Timing Generator
|
61 | FCTL1 = FWKEY + ERASE; // Set Erase bit
|
62 | FCTL3 = FWKEY + LOCKA; // Clear LOCK & LOCKA bits
|
63 | *Flash_ptrA = 0x00; // Dummy write to erase Flash seg A
|
64 | FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
|
65 | Flash_ptrA = (char *)0x10F8; // Point to beginning of cal consts
|
66 | for (j = 0; j < 8; j++)
|
67 | *Flash_ptrA++ = CAL_DATA[j]; // re-flash DCO calibration data
|
68 | FCTL1 = FWKEY; // Clear WRT bit
|
69 | FCTL3 = FWKEY + LOCKA + LOCK; // Set LOCK & LOCKA bit
|
70 |
|
71 | while (1)
|
72 | {
|
73 | P1OUT ^= 0x01; // Toggle LED
|
74 | for (i = 0; i < 0x4000; i++); // SW Delay
|
75 | }
|
76 | }
|
77 |
|
78 | void Set_DCO(unsigned int Delta) // Set DCO to selected frequency
|
79 | {
|
80 | unsigned int Compare, Oldcapture = 0;
|
81 |
|
82 | BCSCTL1 |= DIVA_3; // ACLK = LFXT1CLK/8
|
83 | TACCTL2 = CM_1 + CCIS_1 + CAP; // CAP, ACLK
|
84 | TACTL = TASSEL_2 + MC_2 + TACLR; // SMCLK, cont-mode, clear
|
85 |
|
86 | while (1)
|
87 | {
|
88 | while (!(CCIFG & TACCTL2)); // Wait until capture occured
|
89 | TACCTL2 &= ~CCIFG; // Capture occured, clear flag
|
90 | Compare = TACCR2; // Get current captured SMCLK
|
91 | Compare = Compare - Oldcapture; // SMCLK difference
|
92 | Oldcapture = TACCR2; // Save current captured SMCLK
|
93 |
|
94 | if (Delta == Compare)
|
95 | break; // If equal, leave "while(1)"
|
96 | else if (Delta < Compare)
|
97 | {
|
98 | DCOCTL--; // DCO is too fast, slow it down
|
99 | if (DCOCTL == 0xFF) // Did DCO roll under?
|
100 | if (BCSCTL1 & 0x0f)
|
101 | BCSCTL1--; // Select lower RSEL
|
102 | }
|
103 | else
|
104 | {
|
105 | DCOCTL++; // DCO is too slow, speed it up
|
106 | if (DCOCTL == 0x00) // Did DCO roll over?
|
107 | if ((BCSCTL1 & 0x0f) != 0x0f)
|
108 | BCSCTL1++; // Sel higher RSEL
|
109 | }
|
110 | }
|
111 | TACCTL2 = 0; // Stop TACCR2
|
112 | TACTL = 0; // Stop Timer_A
|
113 | BCSCTL1 &= ~DIVA_3; // ACLK = LFXT1CLK
|
114 | }
|