MSP430入门-----总算读懂例程中的用调节DCO频率
2015-06-27 11:54
330 查看
//******************************************************************************
// MSP-FET430P140 Demo - Basic Clock, Implement Auto RSEL SW FLL
//
// Description: Set DCO clock to (Delta)*(4096) using software FLL. DCO clock
// is output on P5.5 as SMCLK. DCO clock, which is the selected SMCLK source
// for timer_A is integrated over LFXT1/8 (4096) until SMCLK is is equal
// to Delta. CCR2 captures ACLK. To use Set_DCO Timer_A must be
// operating in continous mode. Watch crystal for ACLK is required for
// this example. Delta must be kept in a range that allows possible
// DCO speeds. Minimum Delta must ensure that Set_DCO loop
// can complete within capture interval. Maximum delta can be calculated be
// f(DCOx7) / 4096. f(DCOx7) can be found in device specific datasheet.
// ACLK = LFXT1/8 = 32768/8, MCLK = SMCLK = target DCO
// //* External watch crystal installed on XIN XOUT is required for ACLK *//
//
// MSP430F149
// ---------------
// /|\| XIN|-
// | | | 32kHz
// --|RST XOUT|-
// | |
// | P5.5|--> SMLCK = target DCO
// | P5.6|--> ALCK = 4096
//
//
// M. Buccini
// Texas Instruments Inc.
// Feb 2005
// Built with IAR Embedded Workbench Version: 3.21A
//******************************************************************************
#include <msp430x14x.h>
void Set_DCO (void);
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P5DIR |= 0x60; // P5.5,6 output
P5SEL |= 0x60; // P5.5,6 SMCLK, ACLK output
Set_DCO(); // Set DCO
while (1);
}
//------------------------------------------------------------------------------
void Set_DCO (void) // Set DCO to selected frequency
//------------------------------------------------------------------------------
{
//#define DELTA 900 // target DCO = DELTA*(4096) = 3686400
#define DELTA 256 // target DCO = DELTA*(4096) = 1048576//DCO频率
//#define DELTA 70 // target DCO = DELTA*(4096) = 286720
unsigned int Compare, Oldcapture = 0;
BCSCTL1 |= DIVA_3; // ACLK= LFXT1CLK/8
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 + DIVA_3)))//这儿用实际的数值表示可能更明确点!!//减的极限
BCSCTL1--; // Did DCO roll under?, Sel lower RSEL
}
}
else
{
DCOCTL++;
if (DCOCTL == 0x00)
{
if (!(BCSCTL1 == (XT2OFF + DIVA_3 + 0x07)))//加的极限。
BCSCTL1++; // Did DCO roll over? Sel higher RSEL
}
}
}
CCTL2 = 0; // Stop CCR2
TACTL = 0; // Stop Timer_A
}
其中DCOCTL,BCSCTL1的加减需要结合寄存器看。
如果直接写成数值的形式,理解好点。
调节频率,有点类似万用表的电压,先选好档位,再调节。其中BCSCTL1就是档位,而DCOCTL就是说的具体调节。
在每个加减都有溢出情况对于DCOCTL;
BCSCTL1,res0~7,分成八个档位。
// MSP-FET430P140 Demo - Basic Clock, Implement Auto RSEL SW FLL
//
// Description: Set DCO clock to (Delta)*(4096) using software FLL. DCO clock
// is output on P5.5 as SMCLK. DCO clock, which is the selected SMCLK source
// for timer_A is integrated over LFXT1/8 (4096) until SMCLK is is equal
// to Delta. CCR2 captures ACLK. To use Set_DCO Timer_A must be
// operating in continous mode. Watch crystal for ACLK is required for
// this example. Delta must be kept in a range that allows possible
// DCO speeds. Minimum Delta must ensure that Set_DCO loop
// can complete within capture interval. Maximum delta can be calculated be
// f(DCOx7) / 4096. f(DCOx7) can be found in device specific datasheet.
// ACLK = LFXT1/8 = 32768/8, MCLK = SMCLK = target DCO
// //* External watch crystal installed on XIN XOUT is required for ACLK *//
//
// MSP430F149
// ---------------
// /|\| XIN|-
// | | | 32kHz
// --|RST XOUT|-
// | |
// | P5.5|--> SMLCK = target DCO
// | P5.6|--> ALCK = 4096
//
//
// M. Buccini
// Texas Instruments Inc.
// Feb 2005
// Built with IAR Embedded Workbench Version: 3.21A
//******************************************************************************
#include <msp430x14x.h>
void Set_DCO (void);
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P5DIR |= 0x60; // P5.5,6 output
P5SEL |= 0x60; // P5.5,6 SMCLK, ACLK output
Set_DCO(); // Set DCO
while (1);
}
//------------------------------------------------------------------------------
void Set_DCO (void) // Set DCO to selected frequency
//------------------------------------------------------------------------------
{
//#define DELTA 900 // target DCO = DELTA*(4096) = 3686400
#define DELTA 256 // target DCO = DELTA*(4096) = 1048576//DCO频率
//#define DELTA 70 // target DCO = DELTA*(4096) = 286720
unsigned int Compare, Oldcapture = 0;
BCSCTL1 |= DIVA_3; // ACLK= LFXT1CLK/8
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 + DIVA_3)))//这儿用实际的数值表示可能更明确点!!//减的极限
BCSCTL1--; // Did DCO roll under?, Sel lower RSEL
}
}
else
{
DCOCTL++;
if (DCOCTL == 0x00)
{
if (!(BCSCTL1 == (XT2OFF + DIVA_3 + 0x07)))//加的极限。
BCSCTL1++; // Did DCO roll over? Sel higher RSEL
}
}
}
CCTL2 = 0; // Stop CCR2
TACTL = 0; // Stop Timer_A
}
其中DCOCTL,BCSCTL1的加减需要结合寄存器看。
如果直接写成数值的形式,理解好点。
调节频率,有点类似万用表的电压,先选好档位,再调节。其中BCSCTL1就是档位,而DCOCTL就是说的具体调节。
在每个加减都有溢出情况对于DCOCTL;
BCSCTL1,res0~7,分成八个档位。
相关文章推荐
- 公司产品
- 动态顺序表(可分配内存空间)
- openerp如何禁用单击后弹窗,以及使tree view可编辑
- js实现省市选择二级联动
- 洪晃说
- 项目笔记:1.nginx的七层的负载均衡集群
- 菜鸟的mongoDB学习---(五)MongoDB的limit、skip、sort方法
- 欢迎使用CSDN-markdown编辑器
- Java中ArrayList和LinkedList区别
- CMake快速入门
- CodeForces 550C Divisibility by Eight(枚举)
- Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>
- iOS_小问题积累(自定义tableviewcell时覆盖父类的变量出现的问题)
- iOS应用开发:什么是ARC?
- 解决默认安装AndroidX86 2.2以上版本不能上网
- easyui datagrid显示进度条控制操作
- 数据结构推荐的书籍
- picasso图片下载缓存库使用
- 《C/C++专项练习》 — (3)
- 泛型容器单元(Generics.Collections)[3]: TStack<T> 堆栈列表