51_LanQiaoBei/题目/真题/12 第十二届国赛_左岚/第十二届国赛/Driver/ultrasound.c
2025-04-13 01:02:19 +08:00

49 lines
1.0 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <ultrasound.h>
#include "intrins.h"
sbit Tx = P1^0;
sbit Rx = P1^1;
void Delay12us() //@12.000MHz
{
unsigned char i;
_nop_();
_nop_();
i = 38;
while (--i);
}
void Ut_Wave_Init() //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>8<EFBFBD><38>40Mhz<68>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD>ź<EFBFBD>
{
unsigned char i;
for(i=0; i<8; i++)
{
Tx = 1;
Delay12us();
Tx = 0;
Delay12us();
}
}
unsigned char Ut_Wave_Data() //超声波距离读取函数
{
unsigned int time;//时间储存变量
CMOD = 0x00;//配置PCA工作模式
CH = CL = 0;//复位计数值 等待超声波信号发出
Ut_Wave_Init();//发送超声波信号
CR = 1;//开始计时
while((Rx == 1) && (CF == 0));//等待接受返回信号或者定时器溢出
CR = 0;//停止计时
if(CF == 0) //定时器没有溢出
{
time = CH << 8 | CL;//读取当前时间
return (time * 0.017);//返回距离值
}
else
{
CF = 0;//清除溢出标志位
return 0;
}
}