MSP-EXP430F5529LPPWM库函数+时钟配置
[复制链接]
要实现P2.0口输出10kHz的PWM,这也是应用中电机控制的常用工作频率。要输出准确的频率,了解清楚各个时钟是非常必要的。
首先明确思路,430中有三个时钟:辅助时钟ACLK,频率较低,软件选作各个外围模块的时钟信号,一般用于低速外设;系统主时钟MCLK,频率较高,主要用于CPU和系统,类似于主频;子系统时钟SMCLK,主要用于高速外设模块。这里我们利用TIMER_A产生PWM,选择SMCLK作为模块的时钟源,因此SMCLK的设置就是关键。
LaunchPad为430的XT2外接了一个4MHz的时钟源,T1外接了一个32.768kHz的时钟源,本实验的目标除了输出10kHz的PWM,还要将ACLK配置为32.768kHz,MCLK配置为24MHz,SMCLK配置为4MHz。
整体代码如下:
//*****************************************************************************
#include "driverlib.h"
//*****************************************************************************
//
//Target frequency for MCLK in kHz
//
//*****************************************************************************
#define UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ 24000
//*****************************************************************************
//
//MCLK/FLLRef Ratio
//
//*****************************************************************************
#define UCS_MCLK_FLLREF_RATIO 6
//*****************************************************************************
//
//Variable to store current Clock values
//
//*****************************************************************************
uint32_t clockValue = 0;
//*****************************************************************************
//
//Variable to store status of Oscillator fault flags
//
//*****************************************************************************
//*****************************************************************************
//
//XT1 Crystal Frequency being used
//
//*****************************************************************************
#define UCS_XT1_CRYSTAL_FREQUENCY 32768
//*****************************************************************************
//
//XT2 Crystal Frequency being used
//
//*****************************************************************************
#define UCS_XT2_CRYSTAL_FREQUENCY 4000000
//*****************************************************************************
//
//Desired Timeout for XT1 initialization
//
//*****************************************************************************
#define UCS_XT1_TIMEOUT 50000
#define TIMER_PERIOD 399
#define DUTY_CYCLE 100
#define UCS_XT2_TIMEOUT 50000
uint16_t status;
uint8_t returnValue = 0;
void main (void)
{
//Stop WDT
WDT_A_hold(WDT_A_BASE);
//Set VCore = 1 for 12MHz clock
PMM_setVCore(PMM_CORE_LEVEL_1);//主频提高后,VCore电压也需要随之配置
//Initializes the XT1 and XT2 crystal frequencies being used
UCS_setExternalClockSource(UCS_XT1_CRYSTAL_FREQUENCY,UCS_XT2_CRYSTAL_FREQUENCY);//设置外部时钟源的频率,没什么实际设定
//Initialize XT1. Returns STATUS_SUCCESS if initializes successfully
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5,GPIO_PIN4 + GPIO_PIN5);//XT1口不作为普通IO
returnValue = UCS_turnOnLFXT1WithTimeout(UCS_XT1_DRIVE_0,UCS_XCAP_3,UCS_XT1_TIMEOUT);//启动XT1
//Startup HF XT2 crystal Port select XT2
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5,GPIO_PIN2 + GPIO_PIN3);//XT2口不作为普通IO
//Initialize XT2. Returns STATUS_SUCCESS if initializes successfully
returnValue = UCS_turnOnXT2WithTimeout(UCS_XT2_DRIVE_4MHZ_8MHZ,UCS_XT2_TIMEOUT);//启动XT2
//Set DCO FLL reference = REFO
UCS_initClockSignal(UCS_FLLREF,UCS_XT2CLK_SELECT,UCS_CLOCK_DIVIDER_1);//XT2作为FLL参考
//Set Ratio and Desired MCLK Frequency and initialize DCO
UCS_initFLLSettle(UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ,UCS_MCLK_FLLREF_RATIO);//MCLK设置为24MHz
//Set ACLK = REFO
UCS_initClockSignal(UCS_ACLK,UCS_REFOCLK_SELECT,UCS_CLOCK_DIVIDER_1);//ACLK设置为32.768kHz
UCS_initClockSignal(UCS_SMCLK,UCS_XT2CLK_SELECT,UCS_CLOCK_DIVIDER_1);//SMCLK设置为4MHz
//P2.0 as PWM output
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2,GPIO_PIN0);
//Generate PWM - Timer runs in Up mode
Timer_A_outputPWMParam param = {0};
param.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
param.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
param.timerPeriod = TIMER_PERIOD;
param.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_1;
param.compareOutputMode = TIMER_A_OUTPUTMODE_RESET_SET;
param.dutyCycle = DUTY_CYCLE;
Timer_A_outputPWM(TIMER_A1_BASE, ¶m);
// Enable global interrupt
__bis_SR_register(GIE);
//Verify if the Clock settings are as expected
clockValue = UCS_getMCLK();
clockValue = UCS_getACLK();
clockValue = UCS_getSMCLK();
//Loop in place
while (1) ;
}
运行结果如下,输出占空比25%,10kHz的方波。
|