void init_hcsr04()
{
//初始化引脚
GPIO_setAsOutputPin(TRIG_PORT, TRIG_PIN);
GPIO_setAsInputPinWithPullDownResistor(ECHO_PORT, ECHO_PIN);
// 配置TA3.0定时器
Timer_A_UpModeConfig initUpModeParam;
initUpModeParam.clockSource= TIMER_A_CLOCKSOURCE_SMCLK;
initUpModeParam.clockSourceDivider= TIMER_A_CLOCKSOURCE_DIVIDER_1;
initUpModeParam.timerPeriod= 48000;
initUpModeParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
initUpModeParam.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
initUpModeParam.timerClear= TIMER_A_DO_CLEAR;
Timer_A_initUpMode(TIMER_A3_BASE, &initUpModeParam);
// 配置TA3.0定时器的CCR0比较匹配寄存器
Timer_A_clearCaptureCompareInterrupt(TIMER_A3_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_0);
Timer_A_CompareModeConfig initCompareModeParam;
initCompareModeParam.compareRegister= TIMER_A_CAPTURECOMPARE_REGISTER_0;
initCompareModeParam.compareInterruptEnable= TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
initCompareModeParam.compareOutputMode= TIMER_A_OUTPUTMODE_OUTBITVALUE;
initCompareModeParam.compareValue= 48000;
Timer_A_initCompareMode(TIMER_A3_BASE, &initCompareModeParam);
Timer_A_startCounter(TIMER_A3_BASE, TIMER_A_UP_MODE);
//输出低电平
GPIO_setOutputLowOnPin(TRIG_PORT, TRIG_PIN);
__delay_cycles(10 * 48); // 10us延时
}
float sonar_cm(void)
{
double distance = 0;
GPIO_setOutputHighOnPin(TRIG_PORT, TRIG_PIN);
__delay_cycles(15 * 48);
GPIO_setOutputLowOnPin(TRIG_PORT, TRIG_PIN);
while(GPIO_getInputPinValue(ECHO_PORT, ECHO_PIN) == 0);
// 计时器开始计时
Timer_A_clear(TIMER_A3_BASE);
uint32_t start_time = Timer_A_getCounterValue(TIMER_A3_BASE);
while(GPIO_getInputPinValue(ECHO_PORT, ECHO_PIN) == 1);
uint32_t end_time = Timer_A_getCounterValue(TIMER_A3_BASE);
uint32_t duration = end_time - start_time;
if(duration > 60)
{
distance = duration * 0.034;
distance = duration / (2 * 58);
//
{
//初始化引脚
GPIO_setAsOutputPin(TRIG_PORT, TRIG_PIN);
GPIO_setAsInputPinWithPullDownResistor(ECHO_PORT, ECHO_PIN);
// 配置TA3.0定时器
Timer_A_UpModeConfig initUpModeParam;
initUpModeParam.clockSource= TIMER_A_CLOCKSOURCE_SMCLK;
initUpModeParam.clockSourceDivider= TIMER_A_CLOCKSOURCE_DIVIDER_1;
initUpModeParam.timerPeriod= 48000;
initUpModeParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
initUpModeParam.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
initUpModeParam.timerClear= TIMER_A_DO_CLEAR;
Timer_A_initUpMode(TIMER_A3_BASE, &initUpModeParam);
// 配置TA3.0定时器的CCR0比较匹配寄存器
Timer_A_clearCaptureCompareInterrupt(TIMER_A3_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_0);
Timer_A_CompareModeConfig initCompareModeParam;
initCompareModeParam.compareRegister= TIMER_A_CAPTURECOMPARE_REGISTER_0;
initCompareModeParam.compareInterruptEnable= TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
initCompareModeParam.compareOutputMode= TIMER_A_OUTPUTMODE_OUTBITVALUE;
initCompareModeParam.compareValue= 48000;
Timer_A_initCompareMode(TIMER_A3_BASE, &initCompareModeParam);
Timer_A_startCounter(TIMER_A3_BASE, TIMER_A_UP_MODE);
//输出低电平
GPIO_setOutputLowOnPin(TRIG_PORT, TRIG_PIN);
__delay_cycles(10 * 48); // 10us延时
}
float sonar_cm(void)
{
double distance = 0;
GPIO_setOutputHighOnPin(TRIG_PORT, TRIG_PIN);
__delay_cycles(15 * 48);
GPIO_setOutputLowOnPin(TRIG_PORT, TRIG_PIN);
while(GPIO_getInputPinValue(ECHO_PORT, ECHO_PIN) == 0);
// 计时器开始计时
Timer_A_clear(TIMER_A3_BASE);
uint32_t start_time = Timer_A_getCounterValue(TIMER_A3_BASE);
while(GPIO_getInputPinValue(ECHO_PORT, ECHO_PIN) == 1);
uint32_t end_time = Timer_A_getCounterValue(TIMER_A3_BASE);
uint32_t duration = end_time - start_time;
if(duration > 60)
{
distance = duration * 0.034;
distance = duration / (2 * 58);
//