|
程序架构
一般在系统初始化关闭看门狗后要配置系统时钟,配置步骤为:
1、打开晶振;
2、等待晶振起振。清除OFIFG,延时,判断OFIFG是否为0,为0则晶振正常起振,退出判断;
3、选择MCLK/SMCLK时钟源;
uchar iq0;
BCSCTL1&=~XT2OFF; //打开XT2振荡器
do
{
IFG1 &= ~OFIFG; // 清除振荡器失效标志
for (iq0 = 0xFF; iq0 > 0; iq0--); // 延时,等待XT2起振
}
while ((IFG1 & OFIFG) != 0); // 判断XT2是否起振
BCSCTL2 =SELM_2+SELS; //选择MCLK、SMCLK为XT2
2.2 细节描述
对于DCO可以通过配置电阻和DCO得到不同的频率。电阻可配置片内或片外(DCOR一般片内),片内电阻有8中选择(RSELX),DCO有8中选择(DCOX)。
3、相关寄存器
1、DCOCTL
DCOx Bits
7-5
DCO frequency select. These bits select which of the eight discrete DCO
frequencies of the RSELx setting is selected.
2、BCSCTL0
XT2OFF Bit 7 XT2 off. This bit turns off the XT2 oscillator
0 XT2 is on
1 XT2 is off if it is not used for MCLK or SMCLK.
RSELx Bits
2-0
Resistor Select. The internal resistor is selected in eight different steps.
The value of the resistor defines the nominal frequency. The lowest
nominal frequency is selected by setting RSELx=0.
3、BCSCTL0
SELMx Bits
7-6
Select MCLK. These bits select the MCLK source.
00 DCOCLK
01 DCOCLK
10 XT2CLK when XT2 oscillator present on-chip. LFXT1CLK when XT2
oscillator not present on-chip.
11 LFXT1CLK
SELS Bit 3 Select SMCLK. This bit selects the SMCLK source.
0 DCOCLK
1 XT2CLK when XT2 oscillator present on-chip. LFXT1CLK when XT2
oscillator not present on-chip.
DCOR Bit 0 DCO resistor select
0 Internal resistor
1 External resistor
4、实例
4.1 配置MCLK/SMCLK
见本节2.1。
4.2配置DCO
void main(void)
{
WDTCTL = WDTPW +WDTHOLD; // Stop Watchdog Timer
DCOCTL = DCO0 + DCO1 + DCO2; // Max DCO
BCSCTL1 = RSEL0 + RSEL1 + RSEL2; // XT2on, max RSEL
BCSCTL2 |= SELS; // SMCLK = XT2
P5DIR |= 0x70; // P5.6,5,4 outputs
P5SEL |= 0x70; // P5.6,5,5 options
while(1)
{
}
}
实测DCO最低128K,最高4.58M。
|
|