admin 管理员组

文章数量: 887006

MSP430F5529时钟、定时器配置及串口初始化(基于库函数)

默认时钟

        默认情况下使用DOC数字控制振荡器,产生信号提供给MCLK时钟源,MCLK分频后提供给SMCLK(默认情况下不分频)。故默认情况下SMCLK频率为1048576Hz,即为1MHZ。

直接配置TIM1

    Timer_A_initUpModeParam initContParam = {0};initContParam.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;initContParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;initContParam.timerPeriod = 50000;initContParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_ENABLE;initContParam.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE;initContParam.timerClear = TIMER_A_DO_CLEAR;initContParam.startTimer = false;Timer_A_initUpMode(TIMER_A1_BASE, &initContParam);Timer_A_clearTimerInterrupt (TIMER_A1_BASE);

此时使用的SMCLK时钟源为1MHZ,即1秒计1000000个数。将定时器A初始化成增计数模式,并使能中断,定时器开始时从1计数到50000(最大计数65535)时进入中断,同时计数值清零,需要在中断服务函数中额外清除中断标志位。此频率下每50000/1000000=0.05秒进入一次中断。

配置时钟频率(以配置为8MHZ为例)

#define UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ   8000
#define UCS_MCLK_FLLREF_RATIO   244uint32_t clockValue = 0;
uint16_t status;void clk_Init ()
{PMM_setVCore(PMM_CORE_LEVEL_1);//Set DCO FLL reference = REFO   将REFO设置为DCO FLL参照源UCS_initClockSignal(UCS_FLLREF,UCS_REFOCLK_SELECT,UCS_CLOCK_DIVIDER_1);//Set ACLK = REFO                 REFO向ACLK提供信号UCS_initClockSignal(UCS_ACLK,UCS_REFOCLK_SELECT,UCS_CLOCK_DIVIDER_1);//Set Ratio and Desired MCLK Frequency  and initialize DCOUCS_initFLLSettle(UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ,//8000   设置需要的MCLK频率8000KHZUCS_MCLK_FLLREF_RATIO//244                 DOC参数,REF参考源是32.786KHZ,DOC参数=目标频率(8000000)/32786=244);// Enable global oscillator fault flagSFR_clearInterrupt(SFR_OSCILLATOR_FAULT_INTERRUPT);SFR_enableInterrupt(SFR_OSCILLATOR_FAULT_INTERRUPT);// Enable global interrupt__bis_SR_register(GIE);}#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=UNMI_VECTOR
__interrupt
#elif defined(__GNUC__)
__attribute__((interrupt(UNMI_VECTOR)))
#endif
void NMI_ISR(void)
{do {// If it still can't clear the oscillator fault flags after the timeout,// trap and wait here.status = UCS_clearAllOscFlagsWithTimeout(1000);} while(status != 0);
}

REF为内部低频参考源,需要配置两个参数--要配置的目标频率和锁相环参数(即代码最开始的两个宏定义)。

其中函数UCS_initFLLSettle (uint16_t fsystem, uint16_t ratio); 目的:将 dco 初始化为一个频率,该频率是参考频率和 fll 的乘积。

需要配置时钟频率8MHZ,故目标频率为8000KHZ。REF参考源频率为32.786KHZ。

故锁相环参数为8000/32.786=244(如需配置频率为4MHZ,则锁相环参数=4000/32.786=122。)

此时再按照上面的代码配置TIMA1,则SMCLK频率为8MHZ,每50000/8000000=0.00625秒进入一次中断。


串口初始化

工作于1MHZ时

void uartinit1()
{GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P4,GPIO_PIN5);GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P4, GPIO_PIN4);USCI_A_UART_initParam param1 = {0};param1.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;param1.clockPrescalar = 6;param1.firstModReg = 13;param1.secondModReg = 0;param1.parity = USCI_A_UART_NO_PARITY;param1.msborLsbFirst = USCI_A_UART_LSB_FIRST;param1.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;param1.uartMode = USCI_A_UART_MODE;param1.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;if (STATUS_FAIL == USCI_A_UART_init(USCI_A1_BASE, &param1)){return;}//Enable UART module for operationUSCI_A_UART_enable(USCI_A1_BASE);}

配置波特率为9600时,SMCLK=1048576Hz,需要的分频系数=(1048576/ 16)/9600=6.826。
故param1.clockPrescalar等于整数部分6,param1.firstModReg为0.826*16=13.216,四舍五入为13。

工作于8MHZ时

void UARTInit()
{GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P4,GPIO_PIN5+GPIO_PIN4);USCI_A_UART_initParam param1 = {0};param1.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;param1.clockPrescalar = 52;param1.firstModReg = 1;param1.secondModReg = 0;param1.parity = USCI_A_UART_NO_PARITY;param1.msborLsbFirst = USCI_A_UART_LSB_FIRST;param1.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;param1.uartMode = USCI_A_UART_MODE;param1.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;if (STATUS_FAIL == USCI_A_UART_init(USCI_A1_BASE, &param1)){return;}USCI_A_UART_enable(USCI_A1_BASE);
}

配置波特率为9600时,SMCLK=8000000Hz,需要的分频系数=(8000000/ 16)/9600=52.083。
故param1.clockPrescalar等于整数部分52,param1.firstModReg为0.083*16=1.328,四舍五入为1。

本文标签: MSP430F5529时钟定时器配置及串口初始化(基于库函数)