void SetVcoreUp (unsigned int level)
{
// Open PMM registers for write
PMMCTL0_H = PMMPW_H; // Set SVS/SVM high side new level
SVSMHCTL = SVSHE + SVSHRVL0 * level + SVMHE + SVSMHRRL0 * level;// Set SVM low side to new level
SVSMLCTL = SVSLE + SVMLE + SVSMLRRL0 * level; // Wait till SVM is settled
while ((PMMIFG & SVSMLDLYIFG) == 0); // Clear already set flags
PMMIFG &= ~(SVMLVLRIFG + SVMLIFG); // Set VCore to new level
PMMCTL0_L = PMMCOREV0 * level; // Wait till new level reached
if ((PMMIFG & SVMLIFG))
while ((PMMIFG & SVMLVLRIFG) == 0); // Set SVS/SVM low side to new level
SVSMLCTL = SVSLE + SVSLRVL0 * level + SVMLE + SVSMLRRL0 * level;// Lock PMM registers for write access
PMMCTL0_H = 0x00;
}
void TIME(void)
{
SetVcoreUp (0x01);
SetVcoreUp (0x02);
SetVcoreUp (0x03);
UCSCTL3 = SELREF_2; // Set DCO FLL reference = REFO
UCSCTL4 |= SELA_2; // Set ACLK = REFO
__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
UCSCTL1 = DCORSEL_7; // Select DCO range 50MHz operation
UCSCTL2 = FLLD_1 + 762; // Set DCO Multiplier for 25MHz
// (N + 1) * FLLRef = Fdco// (762 + 1) * 32768 = 25MHz // Set FLL Div = fDCOCLK/2
__bic_SR_register(SCG0); // Enable the FLL control loop
// Worst-case settling time for the DCO when the DCO range bits have been
// changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
// UG for optimization.
// 32 x 32 x 25 MHz / 32,768 Hz ~ 780k MCLK cycles for DCO to settle
__delay_cycles(782000);
// Loop until XT1,XT2 & DCO stabilizes - In this case only DCO has to stabilize
do
{
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG); // Clear XT2,XT1,DCO fault flags
SFRIFG1 &= ~OFIFG; // Clear fault flags
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
}
// DCO时钟16MHz, used as MCLK and SMCLK frequency
// 该函数第1个参数为系统工作频率 16384KHz/1024=16MHz,第2个参数为 工作频率/DCO参考频率=16*1024*1024/32768=512
#define FRQ_DIV 1
//UCS_initFLLSettle(16384/FRQ_DIV , 512/FRQ_DIV);
//! Initializes the XT2 crystal oscillator, which supports crystal frequencies
//! between 4 MHz and 32 MHz, depending on the selected drive strength. Loops
//! until all oscillator fault flags are cleared, with no timeout. See the
//! device-specific data sheet for appropriate drive settings.
UCS_clockSignalInit(UCS_MCLK,UCS_XT2CLK_SELECT ,UCS_CLOCK_DIVIDER_1);
{
// Open PMM registers for write
PMMCTL0_H = PMMPW_H; // Set SVS/SVM high side new level
SVSMHCTL = SVSHE + SVSHRVL0 * level + SVMHE + SVSMHRRL0 * level;// Set SVM low side to new level
SVSMLCTL = SVSLE + SVMLE + SVSMLRRL0 * level; // Wait till SVM is settled
while ((PMMIFG & SVSMLDLYIFG) == 0); // Clear already set flags
PMMIFG &= ~(SVMLVLRIFG + SVMLIFG); // Set VCore to new level
PMMCTL0_L = PMMCOREV0 * level; // Wait till new level reached
if ((PMMIFG & SVMLIFG))
while ((PMMIFG & SVMLVLRIFG) == 0); // Set SVS/SVM low side to new level
SVSMLCTL = SVSLE + SVSLRVL0 * level + SVMLE + SVSMLRRL0 * level;// Lock PMM registers for write access
PMMCTL0_H = 0x00;
}
void TIME(void)
{
SetVcoreUp (0x01);
SetVcoreUp (0x02);
SetVcoreUp (0x03);
UCSCTL3 = SELREF_2; // Set DCO FLL reference = REFO
UCSCTL4 |= SELA_2; // Set ACLK = REFO
__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
UCSCTL1 = DCORSEL_7; // Select DCO range 50MHz operation
UCSCTL2 = FLLD_1 + 762; // Set DCO Multiplier for 25MHz
// (N + 1) * FLLRef = Fdco// (762 + 1) * 32768 = 25MHz // Set FLL Div = fDCOCLK/2
__bic_SR_register(SCG0); // Enable the FLL control loop
// Worst-case settling time for the DCO when the DCO range bits have been
// changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
// UG for optimization.
// 32 x 32 x 25 MHz / 32,768 Hz ~ 780k MCLK cycles for DCO to settle
__delay_cycles(782000);
// Loop until XT1,XT2 & DCO stabilizes - In this case only DCO has to stabilize
do
{
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG); // Clear XT2,XT1,DCO fault flags
SFRIFG1 &= ~OFIFG; // Clear fault flags
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
}
void sysclk_init(void)
{
/* Using 22.1148MHz for XT2 */
P5SEL |= BIT2 + BIT3; // Port select XT2
UCSCTL6 &= ~XT2OFF; // Enable XT2
UCSCTL3 |= SELREF_2; // FLLref = REFO
UCSCTL4 |= SELA__REFOCLK; // ACLK=REFO(32.768kHz),SMCLK=DCO,MCLK=DCO
do
{ // Clear XT2,XT1,DCO fault flags
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + XT1HFOFFG + DCOFFG);
SFRIFG1 &= ~OFIFG; // Clear fault flags
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
UCSCTL6 &= ~(XT2DRIVE0 + XT2DRIVE1); // Decrease XT2 Drive according to expected frequency
UCSCTL6 |= XT2DRIVE_2; // XT2 Drive Level mode: 2, XT2: 16MHz~24MHz
UCSCTL4 |= SELS_5 + SELM_5; // SMCLK=MCLK=XT2
}
void initClock(void)
{
// 初始化P7.0(#13)和P7.1(#14)为复用功能, XT1 外部时钟晶体接线
GPIO_setAsPeripheralModuleFunctionInputPin( GPIO_PORT_P7 , GPIO_PIN0 | GPIO_PIN1 );
// 启动XT1
//Initializes the XT1 crystal oscillator with no timeout
//In case of failure, code hangs here.
//For time-out instead of code hang use UCS_LFXT1StartWithTimeout()
UCS_LFXT1Start( UCS_XT1_DRIVE0 , UCS_XCAP_3);
// DCO参考时钟选择XT1,选择了默认参数
//UCS_clockSignalInit( UCS_FLLREF, UCS_XT1CLK_SELECT , UCS_CLOCK_DIVIDER_1 );
// 初始化P5.2(#89)和P5.3(#90)为复用功能, XT2 外部时钟晶体接线
GPIO_setAsPeripheralModuleFunctionInputPin( GPIO_PORT_P5 , GPIO_PIN2 | GPIO_PIN3 );
// 启动XT2
UCS_XT2Start( UCS_XT2DRIVE_24MHZ_32MHZ );
// DCO参考时钟选择XT1,选择了默认参数
//UCS_clockSignalInit( UCS_FLLREF, UCS_XT1CLK_SELECT , UCS_CLOCK_DIVIDER_1 );
// ACLK参考时钟源选择REFO, 32768Hz
//UCS_clockSignalInit(UCS_ACLK, UCS_REFOCLK_SELECT,UCS_CLOCK_DIVIDER_1);
// DCO参考时钟源选择REFO,32768Hz
//UCS_clockSignalInit(UCS_FLLREF, UCS_REFOCLK_SELECT , UCS_CLOCK_DIVIDER_1);
// DCO时钟16MHz, used as MCLK and SMCLK frequency
// 该函数第1个参数为系统工作频率 16384KHz/1024=16MHz,第2个参数为 工作频率/DCO参考频率=16*1024*1024/32768=512
#define FRQ_DIV 1
//UCS_initFLLSettle(16384/FRQ_DIV , 512/FRQ_DIV);
//! Initializes the XT2 crystal oscillator, which supports crystal frequencies
//! between 4 MHz and 32 MHz, depending on the selected drive strength. Loops
//! until all oscillator fault flags are cleared, with no timeout. See the
//! device-specific data sheet for appropriate drive settings.
UCS_clockSignalInit(UCS_MCLK,UCS_XT2CLK_SELECT ,UCS_CLOCK_DIVIDER_1);
//SMCLK参考时钟源选择DCO,16MHz/4=4MHz,4*1024*1024=4194304Hz
//UCS_clockSignalInit(UCS_SMCLK,UCS_DCOCLK_SELECT,UCS_CLOCK_DIVIDER_4);
UCS_clockSignalInit(UCS_SMCLK,UCS_XT2CLK_SELECT,UCS_CLOCK_DIVIDER_4);
//延时0.5秒, 让时钟源稳定
delay_ms(500);
UCS_setExternalClockSource(32768,MCLK_FRQ);
cvMCLK = UCS_getMCLK();
cvSMCLK = UCS_getSMCLK();
cvACLK = UCS_getACLK();
}
一周热门 更多>