N76E003 超声波测距

#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"

#define uint unsigned int
#define uchar unsigned  char
#define NOP() {_nop_();_nop_();_nop_();_nop_();}


sbit Trig=P0^5;//测距触发引脚
long int temp,t;
int i=0;

/************************************************************************************************************
* 外部中断0服务程序
************************************************************************************************************/
void int0() interrupt 0
{
	t=(TH0*256+TL0);//计算高电平持续的时间,上升沿到来时候开始计时,下降沿到来进入外部中断,关闭计时器,停止计时
	TH0=0;
	TL0=0;
}

void main (void)
{
		Set_All_GPIO_Quasi_Mode;
		P00_Input_Mode;//将P00设置为输入模式
		P00 = 1;
		Trig=0;//为低电平
		TMOD=0x19;
		TR0=1;//启动定时器0
		EX0=1;//使能部中断0
		IT0=1;//外部中断0下降沿触发
		set_EA;//开启总中断
    while(1)
    {
			Trig=1;
			NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
			Trig=0;
			temp=t*0.17;//得到距离
    }
}

跟尺子出来的数据差不多
N76E003 超声波测距
疑惑与不解:回顾下以前的博文,觉得直接使用N76E003单片机自带的输入捕获功能就可实现对超声波模块信号宽度测量,但是测出来的数据误差很大,明明原理跟外部中断差不多的呀,还发现用3.3V驱动模块,用逻辑分析仪可以得到比较好的波形,但是单片机读取的数据就是不对,还是用5V供电才能得到一个相对准确的数据。

相关文章:

  • 2022-01-02
  • 2022-01-02
  • 2022-01-02
  • 2021-11-18
  • 2021-12-29
  • 2021-12-29
  • 2022-01-02
猜你喜欢
  • 2021-12-06
  • 2021-09-06
  • 2021-06-27
  • 2021-10-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案