利用MSP430F5529定时器捕获功能测量信号周期或频率详解

// 这个程序利用TIMER A0生成1KHz的方波,从引脚P1.3输出,送到引脚P7.4, 

// 再利用TIMER B0测量周期。为测试方便,引脚P1.0输出32KHz的ACLK,

// 引脚P2.2输出12MHz的SMCLK.

//  ACLK = LFXT1 = 32kHz; SMCLK = MCLK = 12MHz
//
//                 MSP430F5529
//             ————————-
//         /|\|                    XT1IN|-
//          | |                              | 32kHz
//          –|RST        XT1OUT|-
//            |                              |
//            |            P7.4/TB0.2|<– CCI2A <-|
//            |            P1.3/TA0.2|–> CCR2 –>|
//            |                              |
//            |                              |    MCLK = 12MHz DCO
//            |                       P2.2|–> SMCLK = 12MHz DCO
//            |                       P1.0|–> ACLK = 32kHz LFXT1
//            ————————–
//******************************************************************************
#include "driverlib.h"

uint16_t REdge1, REdge2, Period, InterCount = 0;

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

    // Configure XT1
    GPIO_setAsPeripheralModuleFunctionInputPin(
                          GPIO_PORT_P5,
                          GPIO_PIN4 + GPIO_PIN5
                          );

    UCS_turnOnLFXT1 (UCS_XT1_DRIVE_3,
                    UCS_XCAP_3
                    );

    // Increase Vcore setting to level1 to support fsystem=12MHz
    // NOTE: Change core voltage one level at a time..
    PMM_setVCore (PMM_CORE_LEVEL_1);

    // Initialize DCO to 12MHz
    UCS_initFLLSettle(12000,
                      374);

    // Setup Port Pins
    //P1.0 –> ACLK
    GPIO_setAsPeripheralModuleFunctionOutputPin(
                          GPIO_PORT_P1,
                          GPIO_PIN0
                          );
    //P2.2 –> SMCLK
    GPIO_setAsPeripheralModuleFunctionOutputPin(
                          GPIO_PORT_P2,
                          GPIO_PIN2
                          );

    // Configure ports TB0.2 input and TA0.2 output
    // TB0.2 input
    // TB0.2 option select
    // TA0.2 output
    // TA0.2 option select
    GPIO_setAsPeripheralModuleFunctionOutputPin(
                      GPIO_PORT_P1,
                      GPIO_PIN3
                      );

    GPIO_setAsPeripheralModuleFunctionInputPin(
                        GPIO_PORT_P7,
                        GPIO_PIN4
                        );

    // Configure TA0.2 compare output, 1kHz freq, 50% dutycycle
    Timer_A_initCompareModeParam initCompParam = {0};
    initCompParam.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_2;
    initCompParam.compareInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE;
    initCompParam.compareOutputMode = TIMER_A_OUTPUTMODE_TOGGLE;
    initCompParam.compareValue = 16;
    Timer_A_initCompareMode(TIMER_A0_BASE, &initCompParam);

    Timer_A_initUpModeParam initUpParam = {0};
    initUpParam.clockSource = TIMER_A_CLOCKSOURCE_ACLK;
    initUpParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
    initUpParam.timerPeriod = 16;
    initUpParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
    initUpParam.captureCompareInterruptEnable_CCR0_CCIE =
        TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE;
    initUpParam.timerClear = TIMER_A_DO_CLEAR;
    initUpParam.startTimer = false;
    Timer_A_initUpMode(TIMER_A0_BASE, &initUpParam);

    Timer_A_startCounter(TIMER_A0_BASE,
          TIMER_A_UP_MODE
          );

    Timer_B_initContinuousModeParam initContParam = {0};
    initContParam.clockSource = TIMER_B_CLOCKSOURCE_SMCLK;
    initContParam.clockSourceDivider = TIMER_B_CLOCKSOURCE_DIVIDER_1;
    initContParam.timerInterruptEnable_TBIE = TIMER_B_TBIE_INTERRUPT_DISABLE;
    initContParam.timerClear = TIMER_B_DO_CLEAR;
    Timer_B_initContinuousMode(TIMER_B0_BASE, &initContParam);

    Timer_B_startCounter(TIMER_B0_BASE,
              TIMER_B_CONTINUOUS_MODE
              );

    Timer_B_clearCaptureCompareInterrupt(TIMER_B0_BASE,
        TIMER_B_CAPTURECOMPARE_REGISTER_2);

    Timer_B_initCaptureModeParam initCapParam = {0};
    initCapParam.captureRegister = TIMER_B_CAPTURECOMPARE_REGISTER_2;
    initCapParam.captureMode = TIMER_B_CAPTUREMODE_RISING_EDGE;
    initCapParam.captureInputSelect = TIMER_B_CAPTURE_INPUTSELECT_CCIxA;
    initCapParam.synchronizeCaptureSource = TIMER_B_CAPTURE_SYNCHRONOUS;
    initCapParam.captureInterruptEnable = TIMER_B_CAPTURECOMPARE_INTERRUPT_ENABLE;
    initCapParam.captureOutputMode = TIMER_B_OUTPUTMODE_OUTBITVALUE;
    Timer_B_initCaptureMode(TIMER_B0_BASE, &initCapParam);

    while(1)
    {
      __bis_SR_register(LPM0_bits + GIE);   // Enter LPM0
      __no_operation();                     // For debugger
    }
}

// Timer0_B1 Interrupt Vector
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=TIMER0_B1_VECTOR
__interrupt
#elif defined(__GNUC__)
__attribute__((interrupt(TIMER0_B1_VECTOR)))
#endif
void TIMER0_B1_ISR (void)
{
    switch(TB0IV)
    {
        case 0x04://捕捉比较中断2
            if(InterCount == 0)
            {
                REdge1 = Timer_B_getCaptureCompareCount(TIMER_B0_BASE,
                                                TIMER_B_CAPTURECOMPARE_REGISTER_2);
                InterCount ++;
            }
            else
            {
                REdge2 = Timer_B_getCaptureCompareCount(TIMER_B0_BASE,
                                       TIMER_B_CAPTURECOMPARE_REGISTER_2);
                InterCount = 0;
                Period = (REdge2 – REdge1);
            }
            break;

        default:
            break;
    }
    __bic_SR_register_on_exit(LPM0_bits);      // Exit LPM0
}
 

物联沃分享整理
物联沃-IOTWORD物联网 » 利用MSP430F5529定时器捕获功能测量信号周期或频率详解

发表评论