现在项目中,需要使用msp430单片机作为控制端,所以,要先对msp430的时钟源进行了解和分析。
msp430拥有unified clock system (统一时钟系统),
-FLL(锁频环) for frequency stablization
-VLO(内部时钟源)
-内部参考源(REFO)
-32kHZ晶振
-高达32MHz的高频时钟
The clock system is supported by the Unified Clock System (UCS) module that includes support for a 32-kHz watch crystal oscillator (或者时钟XT1低频态 XT1 LF mode), an internal very-low-power low-frequency oscillator (内部低频低功耗振荡器VLO), an internal trimmed low-frequency oscillator (内部平衡低频晶振REFO), an integrated internal digitally controlled oscillator 内部数字控制振荡器(DCO), and a high frequency crystal oscillator (高频晶振XT1 HF mode or XT2). The UCS module is designed to meet the requirements of both low system cost and low power consumption.
The UCS module features digital frequency locked loop (FLL)hardware that, in conjunction with a digital modulator, stabilizes the DCO frequency to a programmable multiple of the selected FLL reference frequency. The internal DCO provides a fast turn-on clock source and stabilizes in less than 5 μs. The UCS module provides the following clock signals:
• Auxiliary clock (ACLK), sourced from a 32-kHz watch crystal, a high-frequency crystal, the internal lowfrequency
oscillator (VLO), the trimmed low-frequency oscillator (REFO), or the internal digitally controlled
oscillator DCO.
• Main clock (MCLK), the system clock used by the CPU. MCLK can be sourced by same sources made
available to ACLK.
• Sub-Main clock (SMCLK), the subsystem clock used by the peripheral modules. SMCLK can be sourced by
same sources made available to ACLK.
• ACLK/n, the buffered output of ACLK, ACLK/2, ACLK/4, ACLK/8, ACLK/16, ACLK/32.
因为不接外部晶振,所以本文主要是就DCO的设置进行研究。
msp430F5438A datasheet上给出的设置是:
可以见,DCORESL的频率调节范围大致如下:
DCORSEL = 0的调节范围约为0.20~0.70MHZ;
DCORSEL= 1的调节范围约为0.36~1.47MHZ;
DCORSEL = 2的调节范围约为0.75~3.17MHZ;
DCORSEL = 3的调节范围约为1.51~6.07MHZ;
DCORSEL = 4的调节范围约为3.2~12.3MHZ;
DCORSEL = 5的调节范围约为6.0~23.7MHZ;
DCORSEL = 6的调节范围约为10.7~39.7MHZ;
DCORSEL = 7的调节范围约为19.6~60MHZ。
可以看出有重叠的部分,理解了这些,就可以理解Ti官方代码了:
if (fsystem <= 630) // fsystem < 0.63MHz UCSCTL1 = DCORSEL_0; else if (fsystem < 1250) // 0.63MHz < fsystem < 1.25MHz UCSCTL1 = DCORSEL_1; else if (fsystem < 2500) // 1.25MHz < fsystem < 2.5MHz UCSCTL1 = DCORSEL_2; else if (fsystem < 5000) // 2.5MHz < fsystem < 5MHz UCSCTL1 = DCORSEL_3; else if (fsystem < 10000) // 5MHz < fsystem < 10MHz UCSCTL1 = DCORSEL_4; else if (fsystem < 20000) // 10MHz < fsystem < 20MHz UCSCTL1 = DCORSEL_5; else if (fsystem < 40000) // 20MHz < fsystem < 40MHz UCSCTL1 = DCORSEL_6; else UCSCTL1 = DCORSEL_7;
DCO模块运行需要参考时钟REFCLK,REFCLK可以来自REFOCLK、XT1CLK和XT2CLK,通过UCSCTL3的SELREF选择,默认使用的XT1CLK,但如果XT1CLK不可用则使用REFOCLK。
DCO模块有两个输出时钟信号,级DCOCLK和DCOCLKDIV,其中,倍频计算公式如下:
DCOCLK = D*(N+1)*(REFCLK/n)
DCOCLKDIV = (N+1)*(REFCLK/n)
其中:
n即REFCLK输入时钟分频,可以通过UCSCTL3中的FLLCLKDIV设定,默认为0,也就是不分频;
D可以通过UCSCTL2中的FLLD来设定,默认为1,也就是2分频;
N可以通过UCSCTL2中的FLLN来设定,默认值为31。
为了验证上述的陈述,可以编写以下代码:
P1DIR |= BIT0;
P1SEL |= BIT0; //1口输出辅助时钟
UCSCTL3 |= SELREF_2; // FLLref = REFO 为32.768K
UCSCTL4 = SELM__DCOCLKDIV + SELS__DCOCLKDIV + SELA__DCOCLKDIV;// 时钟来源:主系统时钟来源DCOCLKDIV;子系统时钟来源DCOCLKDIV;辅助系统时钟来源DCOCLKDIV
UCSCTL5 |= DIVM__1 + DIVS__1 + DIVA__1; // 分频:主系统时钟1分频;子系统时钟1分频;辅助系统时钟1分频
所以由计算,DCOCLKDIV =32*32.768K = 1.048576M,也就是ACLK的时钟源(不分频)
根据测试结果我们可知T接近1ms,所以DCOCLKDIV差不多为1M,符合计算结果。
那么如果我要设置别的频率又该怎么办呢?
比如我现在需要大约5M的频率,那么由计算得知,当N=151时,大致满足要求。
那么代码写为:
P1DIR |= BIT0;
P1SEL |= BIT0; //1口输出辅助时钟
UCSCTL3 |= SELREF_2; // FLLref = REFO 为32.768K
UCSCTL4 = SELM__DCOCLKDIV + SELS__DCOCLKDIV + SELA__DCOCLKDIV;// 时钟来源:主系统时钟来源DCOCLKDIV;子系统时钟来源DCOCLKDIV;辅助系统时钟来源DCOCLKDIV
UCSCTL1 = DCORSEL_4 // 5M < Fdco < 10M
UCSCTL2 |= FLLD__1 + 151; //DCOCLKDIV=5M 为 Fdco/2
UCSCTL5 |= DIVM__1 + DIVS__1 + DIVA__1; // 分频:主系统时钟1分频;子系统时钟1分频;辅助系统时钟1分频
运行,用示波器测P1.0的输出信号。
测出来的信号周期约为200ns,所以输出的ACLK信号就是约为5M,所以实验证实了想法。
以上大致就是MSP430F5438A芯片DCO的部分的全部内容。