程序如下:
#include <msp430.h>
#include "stdint.h"
/*
* P1.6 ->测频输入端
*/
uint16_t freq = 0;
uint8_t printflag = 0;
uint32_t SMCLKfreq = 0;
/*
* functon: void enableXT2(void)
* brief: 使能XT2外接晶振
* @para: none
* @return: none
* @comment:F5529开发板外接XT2晶振为4MHz
*/
void enableXT2(void)
{
/*设置XT2端口为第二功能*/
P5SEL |= BIT2 + BIT3;
/*设置XT2驱动强度为最低*/
UCSCTL6 = UCSCTL6 & ~(XT2DRIVE_3) | XT2DRIVE_0;
/*打开XT2*/
UCSCTL6 &= ~XT2OFF;
/*清空XT2错误标志位*/
UCSCTL7 &= ~XT2OFFG;
/*循环等待XT2稳定*/
while(UCSCTL7 & XT2OFFG)//如果错误标志位仍然存在
{
__delay_cycles(1000);//等待一段时间
UCSCTL7 &= ~XT2OFFG;//清除XT2错误标志位
SFRIFG1 &= ~OFIFG;//清除时钟错误标志位
}
}
/*
* @fn: void InitSystemClock(void)
* @brief: 初始化系统时钟
* @para: none
* @return: none
* @comment:初始化系统时钟
*/
void InitSystemClock(void)
{
enableXT2();
/*切换MCLK的时钟源为XT2*/
UCSCTL4 = UCSCTL4 & ~SELM_7 | SELM_5;
/*切换SMCLK的时钟源为XT2*/
UCSCTL4 = UCSCTL4 & ~SELS_7 | SELS_5;
UCSCTL5 |=DIVS_2;//设置SMCLK的分频系数为4
}
/*
* @fn: void InitTimeTick1s(void)
* @brief: 初始化TA0产生1s中断
* @para: none
* @return: none
* @comment:使用TA0产生1s中断
*/
void InitTimeTick1s(void)
{
/*设置时钟源为ACLK*/
//TA1CTL |= TASSEL_1;
/*设置时钟源为SMCLK*/
TA0CTL |= TASSEL_2;
/*设置工作模式为Up Mode*/
TA0CTL |= MC_1;
/*设置定时间隔*/
TA0CCR0 = 49999;//50ms
/*开启TAIFG中断*/
TA0CTL |= TAIE;
}
/*
* @fn: void InitFreqMeasure(void)
* @brief: 初始化TA1用于脉冲计数
* @para: none
* @return: none
* @comment:利用TA1可以外部输入时钟的特点进行测频
*/
void InitFreqMeasure(void)
{
/*使能TACLK输入,P1.6*/
P1SEL |= BIT6;
P1DIR &= ~BIT6;
/*设置时钟源为TACLK*/
TA1CTL |= TASSEL_0;
/*设置工作模式为Up*/
TA1CTL |= MC_1;
/*设置定时间隔*/
TA1CCR0 = 0xFFFF;
}
/*
* main.c
*/
uint8_t flag = 0;
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
InitSystemClock();
InitFreqMeasure();
InitTimeTick1s();
/*设置P1.0为输出*/
P1DIR |= BIT0;
P1OUT &= ~BIT0;
P2SEL |= BIT2;
P2DIR |= BIT2;
P7SEL |= BIT7;
P7DIR |= BIT7;
P4SEL |= BIT3;
P4DIR |= BIT3;
/*打开全局中断*/
__bis_SR_register(GIE);
while(1)
{
if(printflag)
{
printflag = 0;
}
}
return 0;
}
#pragma vector = TIMER0_A1_VECTOR
__interrupt void Time_Tick(void)
{
static uint8_t cnt = 0;
__bis_SR_register(GIE);//允许中断嵌套
switch(TA0IV)
{
case 0x02://捕捉比较中断1
break;
case 0x04://捕捉比较中断2
break;
case 0x0E://溢出中断
cnt ++;
if(cnt == 20)
{
P1OUT ^= BIT0;
freq = TA1R;
TA1R = 0;
cnt = 0;
printflag = 1;
}
break;
default:
break;
}
}
Susan Yang:
已在下面帖子内回复
https://e2echina.ti.com/question_answer/microcontrollers/msp430/f/55/t/189311