[MSP-EXP430F5529LP] msp430软件模拟fft测频率

[复制链接]

2

主题

2

帖子

54

积分

一粒轻沙

Rank: 1

积分
54
楼主
查看: 2823回复: 0 发表于 2022-7-13 10:24:23   只看该作者
#include "driverlib.h"
uin**_t num,num1;//记录第一次捕获中断和第二次捕捉中断寄存器值
int b=0,a,a1,sum,n;//b:记录溢出中断个数。a,a1:记录两次捕捉中断时,之前的溢出中断个数。sum:记录所测信号一个周期计数器记了多少个数。n:做第一次和第二次的捕捉中断判断。
#define TIMER_PERIOD 999
#define DUTY_CYCLE  350

void main (void)
{
    //Stop WDT
    WDT_A_hold(WDT_A_BASE);

    GPIO_setAsOutputPin(GPIO_PORT_P1,GPIO_PIN0);//p1.0用于观察捕捉中断
    GPIO_setAsOutputPin(GPIO_PORT_P4,GPIO_PIN7);//p4.7用于观察溢出中断,1.0和4.7都为板子上两个led灯,以便于后期验错。

    GPIO_setAsPeripheralModuleFunctionInputPin (GPIO_PORT_P2,GPIO_PIN0);//p2.0为测频率输入口
    GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P1,GPIO_PIN4);//p1.4设置的是pwm波的输出,用于自检,后期可以删除

    Timer_A_outputPWMParam param1 = {0};//设置p1.4输出pwm波
    param1.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
    param1.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
    param1.timerPeriod = TIMER_PERIOD;
    param1.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_3;
    param1.compareOutputMode = TIMER_A_OUTPUTMODE_RESET_SET;
    param1.dutyCycle = DUTY_CYCLE;
    Timer_A_outputPWM(TIMER_A0_BASE, &param1);

    Timer_A_initContinuousModeParam param = {0};//设置timer1计数器为连续模式,开启溢出中断
    param.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
    param.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
    param.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_ENABLE;
    param.timerClear = TIMER_A_DO_CLEAR;
    param.startTimer = false;
    Timer_A_initContinuousMode(TIMER_A1_BASE, &param);

    Timer_A_clearCaptureCompareInterrupt(TIMER_A1_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_1);//清理一波寄存器值,不清理也没什么大问题

    Timer_A_initCaptureModeParam initComp1Param = {0};//设置TA1.1为捕捉模式,下降沿捕捉
    initComp1Param.captureRegister = TIMER_A_CAPTURECOMPARE_REGISTER_1;
    initComp1Param.captureMode = TIMER_A_CAPTUREMODE_FALLING_EDGE;
    initComp1Param.captureInputSelect = TIMER_A_CAPTURE_INPUTSELECT_CCIxA;
    initComp1Param.synchronizeCaptureSource = TIMER_A_CAPTURE_SYNCHRONOUS;
    initComp1Param.captureInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
    initComp1Param.captureOutputMode=TIMER_A_OUTPUTMODE_OUTBITVALUE;
    Timer_A_initCaptureMode(TIMER_A1_BASE, &initComp1Param);

    Timer_A_startCounter( TIMER_A1_BASE,TIMER_A_CONTINUOUS_MODE );//开始计数

     __bis_SR_register(LPM0_bits + GIE);
     __no_operation();
}


#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=TIMER1_A1_VECTOR
__interrupt
#elif defined(__GNUC__)
__attribute__((interrupt(TIMER1_A1_VECTOR)))//对于中断向量的选择,后期如有需要的话,我可以在出一期讲一下函数中每个参数的选择原理。
#endif
void TIMER1_A1_ISR (void)
{

    switch ( __even_in_range(TA1IV,14) ){
        case  2:
        GPIO_toggleOutputOnPin(GPIO_PORT_P1,GPIO_PIN0);
       if(n==0)
       {num=Timer_A_getCounterValue (TIMER_A1_BASE);
          a=b;n++;}
       else
       {num1=Timer_A_getCounterValue (TIMER_A1_BASE);
               a1=b;n=0;
       sum=(a1-a)*65535+num1-num;}
        break;
        case 14:
            GPIO_toggleOutputOnPin(GPIO_PORT_P4,GPIO_PIN7);
            b++;
            break;
        default: break;
}
}
快速回复 返回顶部 返回列表