msp430时钟/定时器收集的例程汇总

MSP430的时钟模块由DCOCTL, BCSCTL1, BCSCTL2, IE1, IFG1这五个寄存器来确定,具体的功能如下所示:
(1)DCOCTL:  DCO控制寄存器,地址为56H,初始值为60H

//         7            6             5           4           3             2           1            0

//     DCO2   DCO1   DCO0   MOD4  MOD3   MOD2   MOD1   MOD0

//

//      DCO0~DCO2: DCO Select Bit,定义了8种频率之一,而频率由注入直流发生器的电流定义

//      MOD0~MOD4: Modulation Bit,频率的微调
(2)BCSCTL1(ACLK): Basic Clock System Control 1,地址为58H,初始值为84H

//          7             6         5            4           3              2              1            0

//     XT2OFF   XTS   DIVA1   DIVA0   XT5V     RSEL2    RSEL1    RSEL0

//

//    RSEL2~RSEL0: 选择某个内部电阻以决定标称频率(0最低,7最高)

//    XT5V: 1,该比特未用,必须选择复位

//    DIVA0~DIVA1: 选择ACLK的分频系数。DIVA=0,1,2,3(DIVA_0,DIVA_1...),ACLK的分频系数分别为:1,2,4,8。例: BCSCTL1 |= DIVA_2; //对ACLK进行2分频

//    XTS: 选择LFXT1工作在低频晶体模式(XTS=0)还是高频晶体模式(XTS=1)

//    XT2OFF: 控制XT2振荡器的开启(XT2OFF=0)与关闭(XT2OFF=1)

//

//  BCSCTL1的设置:初始值为84H,使用XT2振荡器,控制XT2振荡器的开启(XT2OFF=0)与关闭(XT2OFF=1)

    BCSCTL1 &= ~XT2OFF;//清OSCOFF/XT2

    do

    {

      IFG1 &= ~OFIFG;//清OFIFG

      OSC_Delay = 255;

       while(OSC_Delay --);//延时等待

     }

     while(IFG1 & OFIFG);//直到OFIFG=0为止

 

// RSEL2~RSEL0:选择某个内部电阻以决定标称频率(0最低,7最高)

    BCSCTL1 |= RSEL0 + RSEL1 + RSEL2;// XT2on,max RSEL  
(3)BCSCTL2(SMCLK,MCLK): Basic Clock System Control 2,地址为58H,初始值为00H

//          7             6              5            4            3           2            1            0

//     SELM1   SELM0   DIVM1   DIVM0   SELS   DIVS1   DIVS0   DCOR

//

//   DCOR: Enable External Resister,0—选择内部电阻,1—选择外部电阻

//   DIVS0~DIVS1: DIVS=0,1,2,3,对应SMCLK的分频因子为1,2,4,8

//   SELS: 选择SMCLK的时钟源,0:DCOCLK,1:XT2CLK/LFXTCLK

//   DIVM0~DIVM1:选择MCLK的分频因子,DIVM=0,1,2,3,对应MCLK的分频因子为1,2,4,8

//   SELM0~SELM1:选择MCLK的时钟源,0,1:DCOCLK,2:XT2CLK,3:LFXT1CLK

//

// BCSCTL2的设置:初始值为00H,设置BCSCTL2,选定MCLK和SMCLK的时钟源XT2,并可以设置其分频因子

//注意:ACLK只能来源于LFXT1,可以在BCSCTL1里设置ACLK的分频,就是说ACLK最大只能为32768Hz(XIN 与 XOUT间接32.768KHz晶振) 

  //设置SMCLK的分频因子,DIVS0~DIVS1:DIVS=0,1,2,3,对应SMCLK的分频因子为1,2,4,8

  //BCSCTL2 = DIVS_0;

  //BCSCTL2 = DIVS_1;

  //BCSCTL2 = DIVS_2;

  //BCSCTL2 = DIVS_3;

 

  //设置MCLK的分频因子,DIVM0~DIVM1:DIVM=0,1,2,3,对应MCLK的分频因子为1,2,4,8

  //BCSCTL2 = DIVM_0;

  //BCSCTL2 = DIVM_1;

  //BCSCTL2 = DIVM_2;

  //BCSCTL2 = DIVM_3;

 

  //BCSCTL2:设置三个时钟源分别选择什么振荡器

 

  //SELM0~SELM1:选择MCLK的时钟源,0,1:DCOCLK,2:XT2CLK,3:LFXT1CLK

  //选择 MCLK 时钟源为XT2,

  //BCSCTL2 = SELM_2 ;

 

  //SELS:选择SMCLK的时钟源,0:DCOCLK,1:XT2CLK/LFXTCLK

  //选择 SMCLK 时钟源为XT2

  //BCSCTL2 = SELS ;

 

  //选择MCLK 与 SMCLK为XT2

  BCSCTL2 = SELM_2 + SELS;

 
(4)IE1,Interrupt Enable Register 1

//       7      6     5      4      3      2      1      0

//                                                     OFIE

//      7~2 and 0 : These bits may be used by other modules

//      OFIE: Oscillator fault interrupt enable.  0---Interrupt not enabled

//                                                                           1---Interrupt enabled
(5)IEG1,Interrupt Flag Register 1

//       7      6     5      4      3      2      1      0

//                                                     OFIFG

//     7~2 and 0 : These bits may be used by other modules

//     OFIE: Oscillator fault interrupt flag.  0 No interrupt pending

//                                                                     1 Interrupt pending

//

在PUC信号后,默认情况下由DCOCLK作MCLK与SMCLK的时钟信号,由于DCOCTL初始值为60H,根据需要可将MCLK的时钟源另外设置为LFXT1或者XT2,设置顺序如下:

//(1)清OSCOFF/XT2

//(2)清OFIFG

//(3)延时等待至少50us

//(4)再次检查OFIFG,如果仍置位,则重复(1)~(4)步,直到OFIFG=0为止

//(5)设置BCSCTL2的相应SELM

示例:

 /*---------------------------------------------------------------*-
* 函数名称: void Clock_Mclk_XT2 ( void ) 
* 参    数:
* 返    回:
* 函数功能: 主时钟设置为 XT2
*
*
* 说     明:
-*----------------------------------------------------------------*/
void Clock_Mclk_XT2 ( void )  
{
    volatile unsigned int i;
    WDTCTL = WDTPW + WDTHOLD;                 //关开门狗
   
//    P5DIR |= 0x10;                            // P5.4= 输出
//    P5SEL |= 0x10;                            // P5.4= MCLK模式 
    BCSCTL1 &= ~XT2OFF;                       // XT2= HF XTAL
    do
    {
        IFG1 &= ~OFIFG;                        // 清除 OSCFault 位
        for (i = 0xFF; i > 0; i--);            // 标志位设置延时
    }
    while ((IFG1 & OFIFG));                   // OSCFault 位是否设置成功

    BCSCTL2 |= SELM_2;                        // MCLK= XT2 (safe)
}

/*---------------------------------------------------------------*-
* 函数名称:
* 参    数:
* 返    回:
* 函数功能:
*
*
* 说     明:子时钟设置为 XT2未分频
-*----------------------------------------------------------------*/
void Clock_Smclk_XT2 ( void )
{
    BCSCTL2 |= SELS;
}

 
————————————————
原文链接:https://blog.csdn.net/xiaoyu_lixing/article/details/6711944

16 位定时器 A 模块头文件定义

/************************************************************ 
* Timer A3 
***********************************************************/ 
#define TAIV_ (0x012E) /* Timer A Interrupt Vector Word */
READ_ONLY DEFW( TAIV , TAIV_) 
#define TACTL_ (0x0160) /* Timer A Control */
DEFW( TACTL , TACTL_) 
#define TACCTL0_ (0x0162) /* Timer A Capture/Compare Control 0 */
DEFW( TACCTL0 , TACCTL0_) 
#define TACCTL1_ (0x0164) /* Timer A Capture/Compare Control 1 */
DEFW( TACCTL1 , TACCTL1_) 
#define TACCTL2_ (0x0166) /* Timer A Capture/Compare Control 2 */
DEFW( TACCTL2 , TACCTL2_) 
#define TAR_ (0x0170) /* Timer A */
DEFW( TAR , TAR_) 
#define TACCR0_ (0x0172) /* Timer A Capture/Compare 0 */
DEFW( TACCR0 , TACCR0_) 
#define TACCR1_ (0x0174) /* Timer A Capture/Compare 1 */
DEFW( TACCR1 , TACCR1_) 
#define TACCR2_ (0x0176) /* Timer A Capture/Compare 2 */
DEFW( TACCR2 , TACCR2_) 
/* Alternate register names */ 
#define CCTL0 TACCTL0 /* Timer A Capture/Compare Control 0 */
#define CCTL1 TACCTL1 /* Timer A Capture/Compare Control 1 */
#define CCTL2 TACCTL2 /* Timer A Capture/Compare Control 2 */
#define CCR0 TACCR0 /* Timer A Capture/Compare 0 */
#define CCR1 TACCR1 /* Timer A Capture/Compare 1 */
#define CCR2 TACCR2 /* Timer A Capture/Compare 2 */
#define CCTL0_ TACCTL0_ /* Timer A Capture/Compare Control 0 */
#define CCTL1_ TACCTL1_ /* Timer A Capture/Compare Control 1 */
#define CCTL2_ TACCTL2_ /* Timer A Capture/Compare Control 2 */
#define CCR0_ TACCR0_ /* Timer A Capture/Compare 0 */
#define CCR1_ TACCR1_ /* Timer A Capture/Compare 1 */
#define CCR2_ TACCR2_ /* Timer A Capture/Compare 2 */
#define TASSEL2 (0x0400) /* unused */ /* to distinguish from USART SSELx */
#define TASSEL1 (0x0200) /* Timer A clock source select 0 */
#define TASSEL0 (0x0100) /* Timer A clock source select 1 */
#define ID1 (0x0080) /* Timer A clock input devider 1 */
#define ID0 (0x0040) /* Timer A clock input devider 0 */
#define MC1 (0x0020) /* Timer A mode control 1 */
#define MC0 (0x0010) /* Timer A mode control 0 */
#define TACLR (0x0004) /* Timer A counter clear */
#define TAIE (0x0002) /* Timer A counter interrupt enable */
#define TAIFG (0x0001) /* Timer A counter interrupt flag */
#define MC_0 (0*0x10u) /* Timer A mode control: 0 - Stop */
#define MC_1 (1*0x10u) /* Timer A mode control: 1 - Up to CCR0 */
#define MC_2 (2*0x10u) /* Timer A mode control: 2 - Continous up */
#define MC_3 (3*0x10u) /* Timer A mode control: 3 - Up/Down */
#define ID_0 (0*0x40u) /* Timer A input divider: 0 - /1 */
#define ID_1 (1*0x40u) /* Timer A input divider: 1 - /2 */
#define ID_2 (2*0x40u) /* Timer A input divider: 2 - /4 */
#define ID_3 (3*0x40u) /* Timer A input divider: 3 - /8 */
#define TASSEL_0 (0*0x100u) /* Timer A clock source select: 0 - TACLK */
#define TASSEL_1 (1*0x100u) /* Timer A clock source select: 1 - ACLK */
#define TASSEL_2 (2*0x100u) /* Timer A clock source select: 2 - SMCLK */
#define TASSEL_3 (3*0x100u) /* Timer A clock source select: 3 - INCLK */
#define CM1 (0x8000) /* Capture mode 1 */
#define CM0 (0x4000) /* Capture mode 0 */
#define CCIS1 (0x2000) /* Capture input select 1 */
65
#define CCIS0 (0x1000) /* Capture input select 0 */
#define SCS (0x0800) /* Capture sychronize */
#define SCCI (0x0400) /* Latched capture signal (read) */
#define CAP (0x0100) /* Capture mode: 1 /Compare mode : 0 */
#define OUTMOD2 (0x0080) /* Output mode 2 */
#define OUTMOD1 (0x0040) /* Output mode 1 */
#define OUTMOD0 (0x0020) /* Output mode 0 */
#define CCIE (0x0010) /* Capture/compare interrupt enable */
#define CCI (0x0008) /* Capture input signal (read) */
#define OUT (0x0004) /* PWM Output signal if output mode 0 */
#define COV (0x0002) /* Capture/compare overflow flag */
#define CCIFG (0x0001) /* Capture/compare interrupt flag */
#define OUTMOD_0 (0*0x20u) /* PWM output mode: 0 - output only */
#define OUTMOD_1 (1*0x20u) /* PWM output mode: 1 - set */
#define OUTMOD_2 (2*0x20u) /* PWM output mode: 2 - PWM toggle/reset */
#define OUTMOD_3 (3*0x20u) /* PWM output mode: 3 - PWM set/reset */
#define OUTMOD_4 (4*0x20u) /* PWM output mode: 4 - toggle */
#define OUTMOD_5 (5*0x20u) /* PWM output mode: 5 - Reset */
#define OUTMOD_6 (6*0x20u) /* PWM output mode: 6 - PWM toggle/set */
#define OUTMOD_7 (7*0x20u) /* PWM output mode: 7 - PWM reset/set */
#define CCIS_0 (0*0x1000u) /* Capture input select: 0 - CCIxA */
#define CCIS_1 (1*0x1000u) /* Capture input select: 1 - CCIxB */
#define CCIS_2 (2*0x1000u) /* Capture input select: 2 - GND */
#define CCIS_3 (3*0x1000u) /* Capture input select: 3 - Vcc */
#define CM_0 (0*0x4000u) /* Capture mode: 0 - disabled */
#define CM_1 (1*0x4000u) /* Capture mode: 1 - pos. edge */
#define CM_2 (2*0x4000u) /* Capture mode: 1 - neg. edge */
#define CM_3 (3*0x4000u) /* Capture mode: 1 - both edges */

--------------------------------------------------------------------
原文链接:https://blog.csdn.net/Aaaaace/article/details/78840806?spm=1001.2014.3001.5501

基本时钟系统头文件定义

/************************************************************ 
* Basic Clock Module 
************************************************************/ 
#define DCOCTL_  (0x0056) /* DCOCTL 的定义 */
DEFC( DCOCTL , DCOCTL_) 
#define BCSCTL1_  (0x0057) /* BCSCTL1 的定义 */ 
DEFC( BCSCTL1 , BCSCTL1_) 
#define BCSCTL2_  (0x0058) /* BCSCTL2 的定义 */
DEFC( BCSCTL2 , BCSCTL2_)
/* DCOCTL 的位定义 */ 
#define MOD0 (0x01) /* Modulation Bit 0 */
#define MOD1 (0x02) /* Modulation Bit 1 */
#define MOD2 (0x04) /* Modulation Bit 2 */
#define MOD3 (0x08) /* Modulation Bit 3 */
#define MOD4 (0x10) /* Modulation Bit 4 */
#define DCO0 (0x20) /* DCO Select Bit 0 */
#define DCO1 (0x40) /* DCO Select Bit 1 */
#define DCO2 (0x80) /* DCO Select Bit 2 */
/* BCSCTL1 的位定义 */ 
#define RSEL0 (0x01) /* Resistor Select Bit 0 */
#define RSEL1 (0x02) /* Resistor Select Bit 1 */
#define RSEL2 (0x04) /* Resistor Select Bit 2 */
#define XT5V (0x08) /* XT5V should always be reset */
#define DIVA0 (0x10) /* ACLK Divider 0 */
#define DIVA1 (0x20) /* ACLK Divider 1 */
#define XTS (0x40) /* LFXTCLK 0:Low Freq. / 1: High Freq. */
#define XT2OFF (0x80) /* Enable XT2CLK */
/* BCSCTL1 的 DIVA 的功能定义 */ 
#define DIVA_0 (0x00) /* ACLK Divider 0: /1 */
#define DIVA_1 (0x10) /* ACLK Divider 1: /2 */
#define DIVA_2 (0x20) /* ACLK Divider 2: /4 */
#define DIVA_3 (0x30) /* ACLK Divider 3: /8 */
/* BCSCTL2 的位定义 */ 
#define DCOR (0x01) /* Enable External Resistor : 1 */
#define DIVS0 (0x02) /* SMCLK Divider 0 */
#define DIVS1 (0x04) /* SMCLK Divider 1 */
#define SELS (0x08) /* SMCLK Source Select 0:DCOCLK / 1:XT2CLK/LFXTCLK 
#define DIVM0 (0x10) /* MCLK Divider 0 */
#define DIVM1 (0x20) /* MCLK Divider 1 */
#define SELM0 (0x40) /* MCLK Source Select 0 */
#define SELM1 (0x80) /* MCLK Source Select 1 */
/* BCSCTL1 的 DIVS 的功能定义 */
#define DIVS_0 (0x00) /* SMCLK Divider 0: /1 */
#define DIVS_1 (0x02) /* SMCLK Divider 1: /2 */
#define DIVS_2 (0x04) /* SMCLK Divider 2: /4 */
#define DIVS_3 (0x06) /* SMCLK Divider 3: /8 */
/* BCSCTL1 的 DIVM 的功能定义 */
#define DIVM_0 (0x00) /* MCLK Divider 0: /1 */
#define DIVM_1 (0x10) /* MCLK Divider 1: /2 */
#define DIVM_2 (0x20) /* MCLK Divider 2: /4 */
#define DIVM_3 (0x30) /* MCLK Divider 3: /8 */
/* BCSCTL1 的 SELM 的功能定义 */
#define SELM_0 (0x00)
/* MCLK Source Select 0: DCOCLK */
#define SELM_1 (0x40) /* MCLK Source Select 1: DCOCLK */
#define SELM_2 (0x80) /* MCLK Source Select 2: XT2CLK/LFXTCLK */
#define SELM_3 (0xC0) /* MCLK Source Select 3: LFXTCLK 
------------------------------------------------------------------
原文地址:https://blog.csdn.net/Aaaaace/article/details/78835510?spm=1001.2014.3001.5501

时钟配置例程

// 设 ACLK=MCLK=LFXT1=HF,将 MCLK 通过 P5.4 输出
#include<msp430x16x.h>
void main(void)
{
unsigned int i;
WDCTL = WDTPW + WDTHOLD; // 停看门狗
P5DIR |= 0x10; // P5.4 输出
P5SEL |= 0x10; // P5.4 = MCLK
BCSCTL1 |= XTS; // ACLK = LFXT1 = HF 模式
do
{
IFG1 &= ~OFIFG; // 清除振荡器失效标志
for(i = 0Xff;i > 0;i--); // 稳定时间
}
while((IFG1 & OFIFG) != 0); // 如果振荡器失效标志存在
BCSCTL2 |= SELM1 + SELM0; // MCLK = LFXT1
While(1){_NOP();}
}
// 时钟设置函数
// 系统时钟设定
// DCO 设置为 3030KHz
// ACLK 为 LFXT1(低频模式)
// MCLK 为 XT2CLK
// SMLCK 为 XT2CLK
void BCSInit (void)
{
DCOCTL = 0x60 + 0x00;
BCSCTL1 = DIVA_0 + 0x07;
BCSCTL2 = SELM_2 + DIVM_0 + SELS + DIVS_0;
}
----------------------------------------------------------------------
原文地址:https://blog.csdn.net/weixin_29547033/article/details/106143869
 // BCSCTL1的设置:初始值为84H

//使用XT2振荡器

  //控制XT2振荡器的开启(XT2OFF=0)与关闭(XT2OFF=1)

  BCSCTL1 &= ~XT2OFF;//清OSCOFF/XT2

  do

  {

    IFG1 &= ~OFIFG;//清OFIFG

    OSC_Delay = 255;

    while(OSC_Delay --);//延时等待

  }

  while(IFG1 & OFIFG);//直到OFIFG=0为止

 

  //RSEL2~RSEL0:选择某个内部电阻以决定标称频率(0最低,7最高)

  BCSCTL1 |= RSEL0 + RSEL1 + RSEL2;// XT2on,max RSEL

 

  //选择ACLK的分频系数:DIVA=0,1,2,3,ACLK的分频系数分别为:1,2,4,8

  //BCSCTL1 |= DIVA_2;//对ACLK进行2分频

 

//(3)BCSCTL2(SMCLK,MCLK):Basic Clock System Control 2,地址为58H,初始值为00H

//       7       6      5     4     3     2     1     0

//     SELM1 SELM0 DIVM1 DIVM0 SELS DIVS1 DIVS0 DCOR

//

//   DCOR:Enable External Resister,0—选择内部电阻,1—选择外部电阻

//   DIVS0~DIVS1:DIVS=0,1,2,3,对应SMCLK的分频因子为1,2,4,8

//   SELS:选择SMCLK的时钟源,0:DCOCLK,1:XT2CLK/LFXTCLK

//   DIVM0~DIVM1:选择MCLK的分频因子,DIVM=0,1,2,3,对应MCLK的分频因子为1,2,4,8

//   SELM0~SELM1:选择MCLK的时钟源,0,1:DCOCLK,2:XT2CLK,3:LFXT1CLK

//

// BCSCTL2的设置:初始值为00H

//设置BCSCTL2,选定MCLK和SMCLK的时钟源XT2,并可以设置其分频因子

  //注意:ACLK只能来源于LFXT1,可以在BCSCTL1里设置ACLK的分频,就是说ACLK最大只能为32768Hz(XIN 与XOUT间接32.768KHz晶振)

 

  //DCOR一般设置为默认值

 

  //设置SMCLK的分频因子,DIVS0~DIVS1:DIVS=0,1,2,3,对应SMCLK的分频因子为1,2,4,8

  //BCSCTL2 = DIVS_0;

  //BCSCTL2 = DIVS_1;

  //BCSCTL2 = DIVS_2;

  //BCSCTL2 = DIVS_3;

 

  //设置MCLK的分频因子,DIVM0~DIVM1:DIVM=0,1,2,3,对应MCLK的分频因子为1,2,4,8

  //BCSCTL2 = DIVM_0;

  //BCSCTL2 = DIVM_1;

  //BCSCTL2 = DIVM_2;

  //BCSCTL2 = DIVM_3;

 

  //BCSCTL2:设置三个时钟源分别选择什么振荡器

 

  //SELM0~SELM1:选择MCLK的时钟源,0,1:DCOCLK,2:XT2CLK,3:LFXT1CLK

  //选择 MCLK 时钟源为XT2,

  //BCSCTL2 = SELM_2 ;

 

  //SELS:选择SMCLK的时钟源,0:DCOCLK,1:XT2CLK/LFXTCLK

  //选择 SMCLK 时钟源为XT2

  //BCSCTL2 = SELS ;

 

  //选择MCLK 与 SMCLK为XT2

  BCSCTL2 = SELM_2 + SELS;

 

//(4)IE1,Interrupt Enable Register 1

//       7     6    5     4     3     2     1     0

//                                         OFIE

//     7~2 and 0 : These bits may be used by other modules

//     OFIE:Oscillator fault interrupt enable. 0---Interrupt not enabled

//                                    1---Interrupt enabled

//(5)IEG1,Interrupt Flag Register 1

//       7     6    5     4     3     2     1     0

//                                        OFIFG

//     7~2 and 0 : These bits may be used by other modules

//     OFIE:Oscillator fault interrupt flag. 0 No interrupt pending

//                                  1 Interrupt pending

//

在PUC信号后,默认情况下由DCOCLK作MCLK与SMCLK的时钟信号,由于DCOCTL初始值为60H,根据需要可将MCLK的时钟源另外设置为LFXT1或者XT2,设置顺序如下:

//(1)清OSCOFF/XT2

//(2)清OFIFG

//(3)延时等待至少50us

//(4)再次检查OFIFG,如果仍置位,则重复(1)~(4)步,直到OFIFG=0为止

//(5)设置BCSCTL2的相应SELM
------------------------------------------------------------
原文地址:https://blog.csdn.net/blank_king/article/details/46543813?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.control
/*************************************
//系统时钟初始化,外部8M晶振
//*************************************
void Clock_Init()
{
  uchar i;
  BCSCTL1&=~XT2OFF;                 //打开XT2振荡器
  BCSCTL2|=SELM1+SELS;              //MCLK为8MHZ,SMCLK为8MHZ
  do{
    IFG1&=~OFIFG;                   //清楚振荡器错误标志
    for(i=0;i<100;i++)
       _NOP();
  }
  while((IFG1&OFIFG)!=0);           //如果标志位1,则继续循环等待
  IFG1&=~OFIFG;
}

--------------------------------------------------------------------
原文地址:https://blog.csdn.net/qq_43313294/article/details/105799858?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-5.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-5.control
详细的看了这几个寄存器后,感觉是不是还是搞不懂是吧。没关系很正常,下面我们就来看DCO是如何配置:
1.0 设置BCSCTL2寄存器中的DCOR来选择是外部电阻还是内部电阻,以确定一个基准频率。
2.0 通过BCSCTL1寄存器的RSELx来进行分频,确定时钟频率。
3.0 通过DCOCTL寄存器中DCOx在标称频率基础上分段粗调,选择频率。
4.0 通过DCOCTL寄存器中MODx的值对频率进行细调,选择DCOx与DCOx+1之间的频率。
例子:
DCOCTL初始值为60H,即DCOCTL |= DCO1 + DCO2;
DCOCTL |= DCO0 + DCO1 + DCO2;// Max DCO
//MOD0~MOD4:Modulation Bit,频率的微调一般保持默认即可
//系统默认情况下,RSELx=4

下面我们再来看如何配置外面高速时钟:
由于在PUC信号后,默认情况下由DCOCLK作MCLK与SMCLK的时钟信号,DCOCTL初始值为60H,频率比较低,所有需要高速运行的则要将MCLK的时钟源另外设置为LFXT1或者XT2,设置的一般顺序如下:
1.0 清OSCOFF/XT2
2.0 清OFIFG
3.0 延时等待至少50us
4.0 再次检查OFIFG,如果仍置位,则重复(1)~(4)步,直到OFIFG=0为止
5.0 设置BCSCTL2的相应SELM

例子:一般的初始化程序
void Clock_Init(void)
{
    unsigned int i;
    BCSCTL1=0x00//XT2开启,LFXTCLK为低频模式,ACLK分频为0
    do
    {
        IFG1&=~OFIFG;
        for(i=0x20;i>0;i--);
    }
    while((IFG1&OFIFG)==OFIFG);//当OSCFault=1 即晶振不起振则等待
    BCSCTL2=0X00;
    BCSCTL2|=SELM1;//MCLK 时钟为XT2,
    BCSCTL2|=SELS;//SMCLK时钟为XT2
}
这个例子我为什么说一般的初始化咧,有个位置就是在判断晶振状态的while语句,如果外部晶振的真的有问题那程序是不是就一直死在此地方?我就想问下,内部没有问题为何不切换到内部?虽然内部时钟比较慢,但是我系统好歹可以运行,所以在我写的程序里都加入了时钟状态判断时间,下面就是我用的初始化,当然这只是个用法,大家如果还有什么更好的方法可以贴出来,分享分享

//系统时钟初始化
//MCLK=16MHz
//SMCLK=8MHz
//ACLK=32.678KHz
void Clock_Init(void)
{
        unsigned int time = 10000;//外部时钟在这个时间内如果还是不能起振则选择内部时钟
        
        BCSCTL1 &= 0x00;//开启XT2振荡器,LFXT1工作在低频晶体模式,ACLK的分频系数0
        do
        {
                IFG1 &= ~OFIFG;//清OFIFG标记
                __delay_cycles(100);//在外部晶振还没有起振时,时钟来源于内部大概1MHz的DCO
        }
        while((IFG1 & OFIFG) && time--);//查询时钟切换成功
        if(time == 0)//外部时钟有问题
        {
                BCSCTL1 |= XT2OFF;//关闭XT2振荡器
        }
        else//切换成功
        {
                BCSCTL2 &= 0x00;
                BCSCTL2 |= SELM1+SELS+DIVS0;//MCLK与SMCLK的时钟源为XT2,SMCLK的分频系数2 
        }                
}
————————————————
版权声明:本文为CSDN博主「电子缘科技」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/mlidsdf/article/details/36257391
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值