msp430输入捕获实现超声波测距

一.chaoshengbo.h

#ifndef __CHAOSHENGBO_H
#define __CHAOSHENGBO_H

#include "driverlib.h"

void chaoshengbo_Init(void);
uint32_t chaoshengbo_GetDistance(void);
void chaoshengbo_test (void);

#endif

二.chaoshengbo.c

//模块接线:ECHO----p2.5     TRIG----p3.7
//注意:超声波模块一定要给3.3v电源,不能给5v,因为msp430的捕获点评引脚容忍不了5v,可能会烧坏它的GPIO

#include "chaoshengbo.h"

#define TRIG_port  GPIO_PORT_P3
#define TRIG_pin    GPIO_PIN7
#define MCLK_IN_HZ      25000000
#define delay_us(x)     __delay_cycles((MCLK_IN_HZ/1000000*(x)))
#define delay_ms(x)     __delay_cycles((MCLK_IN_HZ/1000*(x)))

uint32_t Sign_Counts;
uint8_t finish_flag=0;

//此函数使用TIMER_A2的比较寄存器2即p2.5来捕获超声波模块返回的脉宽
void chaoshengbo_Init(void)
{
    //初始化定时器为递增计数模式,计数频率=25M
        Timer_A_initContinuousModeParam htim = {0};
        htim.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;//定时器时钟频率25M
        htim.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;//不分频
        htim.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_ENABLE;
        htim.timerClear = TIMER_A_DO_CLEAR;
        htim.startTimer = true;
        Timer_A_initContinuousMode(TIMER_A2_BASE, &htim);

        //初始化定时器为输入捕获模式
        GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P2, GPIO_PIN5);
        Timer_A_initCaptureModeParam capture_htim = {0};
        capture_htim.captureRegister = TIMER_A_CAPTURECOMPARE_REGISTER_2;
        capture_htim.captureMode = TIMER_A_CAPTUREMODE_RISING_AND_FALLING_EDGE;
        capture_htim.captureInputSelect = TIMER_A_CAPTURE_INPUTSELECT_CCIxA;
        capture_htim.synchronizeCaptureSource = TIMER_A_CAPTURE_SYNCHRONOUS;
        capture_htim.captureInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
        capture_htim.captureOutputMode = TIMER_A_OUTPUTMODE_OUTBITVALUE;
        Timer_A_initCaptureMode(TIMER_A2_BASE,&capture_htim);

        //初始化ECHO引脚
        GPIO_setAsOutputPin(TRIG_port,TRIG_pin);
        GPIO_setOutputLowOnPin(TRIG_port,TRIG_pin);
}

uint32_t chaoshengbo_GetDistance(void)
{
    GPIO_setOutputHighOnPin(TRIG_port,TRIG_pin);
    delay_us(10);
    GPIO_setOutputLowOnPin(TRIG_port,TRIG_pin);
    while(!finish_flag){}
    finish_flag=0;

    return Sign_Counts/25*0.0343/2;
}

//信号高电平检测中断函数逻辑
#pragma vector=TIMER2_A1_VECTOR
__interrupt
void TIMER2_A1_ISR (void)
{
    static uint16_t Overflow_Times = 0;
    static uint16_t Sign_Begin = 0, Sign_End = 0;

    switch(TA2IV)
    {
        case TA2IV_TACCR2:
            if(GPIO_getInputPinValue(GPIO_PORT_P2,GPIO_PIN5))
            {
                Sign_Begin = Timer_A_getCaptureCompareCount(TIMER_A2_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_2);
            }
            else
            {
                finish_flag=1;//标记成功捕获一次脉宽
                Sign_End = Timer_A_getCaptureCompareCount(TIMER_A2_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_2);
                if(!Overflow_Times)
                    Sign_Counts = Sign_End - Sign_Begin;
                else
                {
                    Sign_Counts = (uint32_t)65536 * Overflow_Times + Sign_End - Sign_Begin;
                    Overflow_Times = 0;
                }
            }
            Timer_A_clearCaptureCompareInterrupt(TIMER_A2_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_2);
            break;
        case TA2IV_TAIFG:
            if(GPIO_getInputPinValue(GPIO_PORT_P2,GPIO_PIN5))
            {
                ++Overflow_Times;
            }
            else
                Overflow_Times = 0;
            Timer_A_clearTimerInterrupt(TIMER_A2_BASE);
            break;
        default:
            break;
    }
}

#include "my_uart.h"
void chaoshengbo_test (void)
{
    UART_Init(USCI_A1_BASE,115200);
    chaoshengbo_Init();

    while(1)
    {
        UART_printf(USCI_A1_BASE,"%d\r\n",chaoshengbo_GetDistance());
        UART_printf(USCI_A1_BASE,"I love IU\r\n");
        delay_ms(500);
    }
}

你可能感兴趣的:(单片机,嵌入式硬件,算法)