找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4219|回复: 0
收起左侧

利用红外对管测距 单片机源程序

[复制链接]
ID:596290 发表于 2019-11-27 01:46 | 显示全部楼层 |阅读模式
红外对管大多数的使用方法都是通过判断D0口的高低进行循迹,红外对管除了能用来循迹,还可以进行近距离的测距。测距范围在10cm左右。 SG7QXI2SQWM5L25~0@H2T.png 如图,通过ADC检测A0口的电压进行测距,如果想要让这个值更稳定可以通过硬件或者软件滤波,硬件就是在A0和gnd之间加一个电容,软件滤波可以用算法,一下是我用的一个还可以的算法。 )DOW{7O5L3LFR`K2C)Y~59U.png

单片机源程序如下:
  1. #include "stc15.h"
  2. #include "intrins.h"
  3. #include "delayms.h"
  4. #include "ADC.h"
  5. #define N 12
  6. unsigned int value_buf[N];
  7. unsigned int temp=0;
  8. unsigned int dat_hight,dat_low;
  9. unsigned char i=0,j=0;
  10. extern unsigned char ch;

  11. int filter()                                //递推平均滤波法
  12. {
  13.         unsigned char count;
  14.         unsigned int sum=0;
  15.         ADC_CONTR=ADC_POWER|ADC_START+ch;
  16.         delayms(10);
  17.         for(i=0;i<N;i++)
  18.         {
  19.                 dat_hight=ADC_RES;                                                                                                                                                                        //取ADC高八位
  20.                 dat_hight=dat_hight<<2;                                                                                                                                                                        //将数据左移两位
  21.                 dat_low=ADC_RESL&0x03;                                                                                                                                                //取ADC低两位
  22.                 value_buf[i]=dat_hight|dat_low;;
  23.                 ADC_CONTR=ADC_POWER|ADC_START+ch;
  24.                 delayms(10);
  25.         }
  26.         sum=0;
  27.         for(i=0;i<N-1;i++)
  28.                 for(j=0;j<N-1-i;j++)
  29.                 {
  30.                         if(value_buf[j]<value_buf[j+1])
  31.                         {
  32.                                 temp=value_buf[j];
  33.                                 value_buf[j]=value_buf[j+1];
  34.                                 value_buf[j+1]=temp;
  35.                         }
  36.                 }       
  37.         for(count=2;count<N-2;count++) sum+=value_buf[count];
  38.         return (unsigned int)(sum/(N-4));
  39. }
复制代码

  1. #include "stc15.h"
  2. #include "ADC.h"
  3. #include "uart.h"
  4. #include "stdio.h"
  5. #include "delayms.h"
  6. #include "math_algorithm.h"
  7. #include "oled.h"

  8. sbit S2=P1^6;

  9. unsigned int number;
  10. void main()
  11. {
  12.         delayms(10);
  13.         ADC_init();
  14.         uart_init(9600);
  15.         OLED_Init();
  16.         OLED_Clear();
  17.         while(1)
  18.         {
  19.                 while(S2==0)
  20.                 {
  21.                  delayms(10);   //10ms消抖
  22.                  if(S2==0)
  23.                  {
  24.                          OLED_ShowNum(54,4,filter(),4,32);
  25.                  }                 
  26.                   while(!S2);  //等待按键电平
  27.                 }               
  28.         }
  29. }
复制代码

所有资料51hei提供下载:
红外对管检测.zip (70.32 KB, 下载次数: 68)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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