|
本帖最后由 heicad 于 2016-8-15 13:55 編輯
用GP2D12傳感器,來進行激光,紅外線測距,用51單片機,ADC0809進行數模轉換
自己親手做的,親測可用.
0.png (17.04 KB, 下載次數: 139)
下載附件
2016-8-15 13:50 上傳
0.png (349.4 KB, 下載次數: 130)
下載附件
2016-8-15 13:51 上傳
附:PROTEUS仿真電路文件,KEIL C 語言源代碼,- #include <reg52.h>
- #include <intrins.h> //包含NOP空指令函數_nop_
- #include<stdio.h>
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- typedef bit BOOL;
- #define uchar unsigned char
- #define uint unsigned int
-
- sbit eoc=P3^0;
- sbit oe =P3^1;
- sbit st =P3^2;
- sbit clk=P3^6;
- sbit rs=P0^7;
- sbit rw=P0^6;
- sbit ep=P0^5;
- uchar code table[]="0123456789";
- uchar code tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x0,0x0};//數碼管共陰級顯示0~9
- uint ad_0809,a1,a2,a3;
- uchar out;
- void delaynms(uint x);
- void display();
- void ad0809();
- void delayms(uint x)
- {
- uint i,j;
- for(i=x;i>0;i--)
- for(j=110;j>0;j--);
- }
- void delay (int m)
- {
- unsigned char i,j;
- for (i=0;i<m;i++)
- for (j=0;j<253;j++);
- }
- BOOL lcd_bz()
- {
- BOOL result;
- rs=0; // 讀忙信號
- rw=1;
- ep=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- result = (BOOL)(P2&0x80);
- ep=0;
- result ;
- }
- lcd_wcmd (BYTE cmd)
- {
- while (lcd_bz());
- rs=0;
- rw=0;
- ep=0;
- _nop_();
- _nop_();
- P2=cmd ;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ep=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ep=0;
- }
- lcd_pos (BYTE pos)
- {
- lcd_wcmd (pos|0x80);
- }
- lcd_wdat (BYTE dat)
- {
- while (lcd_bz());
- rs=1;
- rw=0;
- ep=0;
- _nop_();
- _nop_();
- P2=dat ;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ep=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ep=0;
- }
- lcd_init ()
- {
- lcd_wcmd (0x38);
- delay (1);
- lcd_wcmd (0x0c);
- delay (1);
- lcd_wcmd (0x06);
- delay (1);
- lcd_wcmd (0x01);
- delay (1);
- }
- void main()
- {
- lcd_init ();
- delay (10);
-
- TMOD=0X01;
- TH0=(65536-2)/256;
- TL0=(65536-2)%256;
- EA=1;
- ET0=1;
- TR0=1;
- while(1)
- {
- ad0809();
- ad_0809=out;
- display();
- lcd_pos (7);
- lcd_wdat(table[a1]);
- lcd_pos (8);
- lcd_wdat(table[a2]);
- lcd_pos (9);
- lcd_wdat(table[a3]);
- }
- }
- void T0_time()interrupt 1
- {
- TH0=(65536-2)/256;
- TL0=(65536-2)%256;
- clk=~clk;
- }
- void delaynms(uint x)
- {
- while(x-->0)
-
- {
- unsigned char k;
- for(k=10;k>0;k--);
- }
- }
- void display()
- {
- a1=(ad_0809*49/25)/100; //the highest bit
- a2=(ad_0809*49/25)/10%10;
- a3=(ad_0809*49/25)%100%10;
- }
- void ad0809()
- {
- P1=0xff;//input
- oe=0;
- st=0;
- st=1;//清0
- st=0;//啟動
- delaynms(1);
- while(!eoc); //eoc等于零的話,在這里等待直到eoc=1,結束循環,向下執行
- oe=1;
- out=P1;
- oe=0;
- }
復制代碼
0.png (81.45 KB, 下載次數: 126)
下載附件
2016-8-15 13:50 上傳
|
|