数字控制振荡器(Numerically Controlled Oscillator)
什麼是数字控制振荡器?
NCO是一个非常好用的功能,可藉由程控产生任意频率的方波, 且不会造成主频变动. 数字控制振荡器的好處
1.产生最快24Mhz的频率组合 2.可输出给外部使用 3.可当成M0的追频函数
NCO溢出频率
(NCO输出频率需 < NCO输入频率/2 且 < APB频率/4)
代码简介 - #include "MG32x02z_APB_DRV.h"
- //*** <<< Use Configuration Wizard in Context Menu >>> ***
- // ----------------------------------------------------------------------------
- // <o0> NCO input clock source select.
- // <0=> CK_IHRCO
- // <1=> CL_PLL
- // <2=> CK_APB
- // <3=> CK_LS
- // <4=> TM00_TRGO
- // <5=> NCO_CK0 (input pin)
- #define NCOClock 0
- 选择NCO的时钟来源
- // ----------------------------------------------------------------------------
- // <o0> NCO output mode <0=>FDC mode <1=>PFM mode
- #define NCOOutputMode 0
- 选择NCO的输出模式
- FDC模式是固定占空比模式
- PFM模式是脉冲频率模式
- // ----------------------------------------------------------------------------
- // <o0> Inverse NCO output <1=>ENABLE <0=>DISABLE
- #define InverseNCO 0
- 设置NCO的输出反相
- // ------------------------------------------------------------------------
- // <o0> input range from 0 to 1048575 (2^20 - 1)
- #define NCOINCVal 1000
- NC0的输入范围,根据前面公式设置该值,获得想要的频率
- // ----------------------------------------------------------------------------
- // <o0> APB NCO PFM width select (only support PFM mode)
- // <0=> 1 CK_NCOn clock period
- // <1=> 2 CK_NCOn clock period
- // <2=> 4 CK_NCOn clock period
- // <3=> 8 CK_NCOn clock period
- // <4=> 16 CK_NCOn clock period
- // <5=> 32 CK_NCOn clock period
- // <6=> 64 CK_NCOn clock period
- // <7=> 128 CK_NCOn clock period
- #define NCO_PFMWidth 7
- 设置NCO PFM输出模式下的脉冲宽度
- //*** <<< end of configuration section >>> ***
- /**
- *******************************************************************************
- * [url=home.php?mod=space&uid=247401]@brief[/url] Initial NCO peripheral.
- * [url=home.php?mod=space&uid=1543424]@Details[/url] Initial NCO with start it.
- * [url=home.php?mod=space&uid=266161]@return[/url] None
- *******************************************************************************
- */
- void Sample_APB_NCO(void)
- {
-
- // make sure :
-
- //===Set CSC init====
- //MG32x02z_CSC_Init.h(Configuration Wizard)
- //Select CK_HS source = CK_IHRCO
- //Select IHRCO = 12M
- //Select CK_MAIN Source = CK_HS
- //Configure PLL->Select APB Prescaler = CK_MAIN/1
- //Configure Peripheral On Mode Clock->APB = Enable
- 在初始化之前确保设置好了想要的时钟
- //==Set GPIO init
- //NCO_P0 pin config:
- //MG32x02z_GPIO_Init.h(Configuration Wizard)->Use GPIOB->PB3
- //GPIO port initial is 0xFFFF
- //PB3 mode is PPO
- //PB3 function NCO_P0
- 确保相应的IO口设置到了相应的模式和功能复用。
- // ------------------------------------------------------------------------
- // initial NCO
- APB_NCOClockSource_Select((APB_NCOInputClockSrcDef) NCOClock);
- APB_NCOOutputMode_Select((APB_NCOOutputMode) NCOOutputMode);
- APB_NCOInverseOutput_Cmd((FunctionalState) InverseNCO);
-
- APB_NCOPFMWidth_Select((APB_NCOPFMWidtrhSelDef) NCO_PFMWidth);
-
- APB_NCOSetINC(NCOINCVal);
- APB_NCOSetACC(999);
-
- APB_NCO_Cmd(ENABLE);
-
- 初始化NCO的步骤:
- 1.选择输入时钟
- 2.选择输出模式
- 3.选择是否进行反相
- 4.设置PFM宽度(若使用PFM模式)
- 5.设置好输入比较值和计数值
- 6.使能输出。
- return;
- }
|