#include "project.h" //------------------------------------------------------------------------------ void Set_DCO (void) // Set DCO to selected frequency //------------------------------------------------------------------------------ { #define DELTA 125 // target DCO = DELTA*(32768) = 4096000 unsigned int Compare, Oldcapture = 0; CCTL2 = CM_1 + CCIS_1 + CAP; // CAP, ACLK TACTL = TASSEL_2 + MC_2 + TACLR; // SMCLK, cont-mode, clear while (1) { while (!(CCIFG & CCTL2)); // Wait until capture occured CCTL2 &= ~CCIFG; // Capture occured, clear flag Compare = CCR2; // Get current captured SMCLK Compare = Compare - Oldcapture; // SMCLK difference Oldcapture = CCR2; // Save current captured SMCLK if (DELTA == Compare) break; // If equal, leave "while(1)" else if (DELTA < Compare) // DCO is too fast, slow it down { DCOCTL--; if (DCOCTL == 0xFF) { if (!(BCSCTL1 == (XT2OFF))) BCSCTL1--; // Did DCO roll under?, Sel lower RSEL } } else { DCOCTL++; if (DCOCTL == 0x00) { if (!(BCSCTL1 == (XT2OFF + 0x07))) BCSCTL1++; // Did DCO roll over? Sel higher RSEL } } } CCTL2 = 0; // Stop CCR2 TACTL = 0; // Stop Timer_A }