i 超声波测距C语言程序-嵌入式MCU技术-电子人社区 -

电子人社区

 找回密码
 立即注册
今日 : 0|主题 : 4177|排名 : 314 
打印 上一主题 下一主题

超声波测距C语言程序

发表于 2019-1-13 13:44:35 | 90350 只看该作者 回帖奖励 |倒序浏览 |阅读模式

[复制链接]
发表于 2019-1-13 13:44:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
电子人社区网讯:   超声波则距是很多电子制作爱好者比较喜欢的制作项目,下面的这款超声波测距C语言程序,超声波测距发送用的是反相器推挽输出,这样可以加大发射功率,超声波测距接收端用的CX20106A这个红外芯片接收40KHZ的方波,这是网上比较经典的模式,按照这个电路是挺容易成功的测量误差的问题 1m内应为mm级、2m内1cm左右、5m的时候3cm左右。下面的这款超声波测距C语言程序程序也挺简单的,用的定时器和外部中断,接收到回波的时候触发外部中断,在中断服务程序中进行处理,不用加注释应该能看懂吧,这款超声波测距C语言程序程序肯定还有很多不足,给初学者用用应该也可以吧。
  #include
  #define unit unsigned int
  #define uchar unsigned char
  sbit fs=P3^0; //发送端;
  sbit h=P2^0;
  sbit l=P2^1;
  sbit m=P2^2;//数码管位选端;
  sbit jieshou=P3^2;
  uchar tab[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x8e};
  uchar u[3]; //显示数组;
  unit count,b,outcomeH,outcomeL,textover;
  void delay(unit a) //延时;
  {
  unit m;
  for(m=0;m
  }
  void delayms(uchar z)
  {
  uchar x,y;
  for(x=0;x
  for(y=110;y>0;y--);
  }
  void tx() //从P3.0发出40KHz的脉冲
  {
  uchar n,p;
  for(n=0;n
  {
  fs=1;
  for(p=0;p
  fs=0;
  for(p=0;p
  fs=0;
  }
  }
  void display(void) //显示;
  {
  for(b=0;b
  {
  l=1;m=1;h=1;
  P1=tab[u[0]];
  m=0;
  delay(10);
  m=1;
  P1=tab[u[1]];
  l=0;
  delay(10);
  l=1;
  P1=tab[u[2]];
  h=0;
  delay(10);
  h=1;
  }
  }
  void rx() interrupt 0 //外部中断0,接收信号
  {
  TR0=0;
  outcomeH=TH0;
  outcomeL=TL0;
  count=outcomeH;
  count
  count=count|outcomeL;
  count/=59;
  本文引用地址: http://www.21ic.com/app/mcu/201812/781491.htm
  if(count>=10)
  {
  u[0]=count;
  u[1]=(count/10);
  u[2]=(count/100);
  display();
  }
  TH0=0;
  TL0=0;
  //textover=1;
  }
  void over()interrupt 1 //T0溢出为无效测量显示FFF;
  {
  u[0]=10;
  u[1]=10;
  u[2]=10;
  display();
  TR0=0;
  }
  void main()
  {
  fs=0;
  delay(8600);
  TH0=0;
  TL0=0;
  TMOD=0x01;
  EA=1;
  ET0=1;
  PT0=1;
  IT0=1;
  IE=0x83;
  while(1)
  {
  delayms(50);
  fs=0;
  jieshou=1;
  tx();
  TR0=1;
  }
  }

来源:网络转载

 

                                                   转载请注明:电子人社区

0条回复
跳转到指定楼层

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|电子人物联网  

Copyright 2013 最新最精彩-社区论坛 版权所有 All Rights Reserved.

QQ|电子人物联网  

GMT+8, 2024-5-2 07:02 , Processed in 0.103687 second(s), 33 queries.

快速回复 返回顶部 返回列表