【Proteus仿真】HC-SRF04超声波测距传感器数码管显示
【Proteus仿真】HC-SRF04超声波测距传感器数码管显示
- VSM Studio Proteus仿真演示

- 实验基于VSM Studio代码编译和仿真一体,可以不依赖外部导入Hex文件进行仿真,本实验同时保留Keil工程。
主程序代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| #include<reg52.h> #include<intrins.h> typedef unsigned char uchar; typedef unsigned int uint; sbit Tr=P1^0;//触发信号 sbit Ec=P1^1;//回响信号 void Delay10us(void);//10us延时函数 void Delayms(unsigned int z);//ms延时函数 uint Read\_value(void);//读值函数 void InitTimer0(void);//定时器0初始化 void show(uint Out);//显示函数 void main() { Tr=0;//出发引脚首先拉低 InitTimer0();//初始化定时器0 while(1){ show(Read\_value());//显示距离 Delayms(5); } } /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*读值函数\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ uint Read\_value() { uint result; Tr=1;//触发引脚发出11us的触发信号(至少10us) Delay10us(); Tr=0; while(!Ec);//度过回响信号的低电平 TR0=1;//开启定时器0 while(Ec);//度过回响信号高电平 TR0=0;//关闭定时器0 result=((TH0\*256+TL0)\*0.034)/2; // 距离cm=(时间us \* 速度cm/us)/2 return result + 1; //+1修正补偿 } /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*10us延时函数\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ void Delay10us() { TL0=0xF6; TH0=0xFF; TR0=1; while (TF0==0); TR0=0; TF0=0; } /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*ms延时函数\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ void Delayms(unsigned int z) { uint i,j; for(i=0;i<z;i++) for(j=0;j<112;j++); //延时函数 } /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*定时器0初始化\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ void InitTimer0(void) {// 选择定时器0,并且确定是工作方式1(为了超声波模块测量距离计时用的) TMOD = 0x01; TH0 = 0x00; TL0 = 0x00; TR0 = 0;//先关闭定时器0 } /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*显示函数\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ void Show(uint Out) { char duan[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//段码 P2=0x01;//第四位(个位) Delay10us(); P0=duan[Out%1000%100%10]; Delayms(1); P0=0xff; P2=0x02;//第三位(十位) Delay10us(); P0=duan[Out%1000%100/10]; Delayms(1); P0=0xff; P2=0x04;//第二位(百位) Delay10us(); P0=duan[Out%1000/100]; Delayms(1); P0=0xff; P2=0x08;//第一位(千位) Delay10us(); P0=duan[Out/1000]; Delayms(1); P0=0xff; }
|
仿真资源和程序源码
本实验基于Proteus8.12平台
1 2 3
| 链接:https://pan.baidu.com/s/1kc54kzA6tlY83FIPMqALmg 提取码:ujr2
|
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!