紅外對管大多數(shù)的使用方法都是通過判斷D0口的高低進行循跡,紅外對管除了能用來循跡,還可以進行近距離的測距。測距范圍在10cm左右。
SG7QXI2SQWM5L25~0@H2T.png (95.79 KB, 下載次數(shù): 42)
下載附件
2019-11-27 01:42 上傳
如圖,通過ADC檢測A0口的電壓進行測距,如果想要讓這個值更穩(wěn)定可以通過硬件或者軟件濾波,硬件就是在A0和gnd之間加一個電容,軟件濾波可以用算法,一下是我用的一個還可以的算法。
)DOW{7O5L3LFR`K2C)Y~59U.png (38.45 KB, 下載次數(shù): 32)
下載附件
2019-11-27 01:44 上傳
單片機源程序如下:
- #include "stc15.h"
- #include "intrins.h"
- #include "delayms.h"
- #include "ADC.h"
- #define N 12
- unsigned int value_buf[N];
- unsigned int temp=0;
- unsigned int dat_hight,dat_low;
- unsigned char i=0,j=0;
- extern unsigned char ch;
- int filter() //遞推平均濾波法
- {
- unsigned char count;
- unsigned int sum=0;
- ADC_CONTR=ADC_POWER|ADC_START+ch;
- delayms(10);
- for(i=0;i<N;i++)
- {
- dat_hight=ADC_RES; //取ADC高八位
- dat_hight=dat_hight<<2; //將數(shù)據(jù)左移兩位
- dat_low=ADC_RESL&0x03; //取ADC低兩位
- value_buf[i]=dat_hight|dat_low;;
- ADC_CONTR=ADC_POWER|ADC_START+ch;
- delayms(10);
- }
- sum=0;
- for(i=0;i<N-1;i++)
- for(j=0;j<N-1-i;j++)
- {
- if(value_buf[j]<value_buf[j+1])
- {
- temp=value_buf[j];
- value_buf[j]=value_buf[j+1];
- value_buf[j+1]=temp;
- }
- }
- for(count=2;count<N-2;count++) sum+=value_buf[count];
- return (unsigned int)(sum/(N-4));
- }
復制代碼
- #include "stc15.h"
- #include "ADC.h"
- #include "uart.h"
- #include "stdio.h"
- #include "delayms.h"
- #include "math_algorithm.h"
- #include "oled.h"
- sbit S2=P1^6;
- unsigned int number;
- void main()
- {
- delayms(10);
- ADC_init();
- uart_init(9600);
- OLED_Init();
- OLED_Clear();
- while(1)
- {
- while(S2==0)
- {
- delayms(10); //10ms消抖
- if(S2==0)
- {
- OLED_ShowNum(54,4,filter(),4,32);
- }
- while(!S2); //等待按鍵電平
- }
- }
- }
復制代碼
所有資料51hei提供下載:
紅外對管檢測.zip
(70.32 KB, 下載次數(shù): 68)
2019-11-27 01:45 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|