微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 语音播报超声波测距程序

语音播报超声波测距程序

时间:11-25 来源:互联网 点击:
语音播报超声波测距程序,语音集成电路用ISD4004 SS接P1.2 MOSI接P1.0 MISO接P1.1 SCLK接P1.3。超声波模块用市面上常用的模块。由于超声波属于声波范围,其速度与温度有关,不同温度下超声波在空气中传播速度随温度变化关系:V=331.4+0.61T。所以要是测量结果更加精确需另加温度补偿模块(本实验中采用开发板上的DS18B20温度传感器完成温度补偿)。外围可添加LCD显示(本实验采用开发板1602LCD)和语音播报模块(本实验采用ISD4004语音模块)。

// *********************单片机实验室******************************/
// *功能 : 语音播报超声波测距程序(测量范围:4~80cm)
// *引脚连接 : ISD4004 SS接P1.2 MOSI接P1.0 MISO接P1.1 SCLK接P1.3
// * 超声波模块 TX接P3.1 RX接P3.2 J7接上插冒
// *当前版本 :1.0
// *作者 : 刘松
// ****************************************************************/
#include
#include
#include"lcd.h" // 液晶显示
#include"ds18b20.h" //温度传感器

sbit P3_1=P3^1;
sbit key1=P2^1; //定义按键
sbit SS=P1^2; //以下四行定义ISD4004引脚
sbit MOSI=P1^0;
sbit MISO=P1^1;
sbit SCLK=P1^3;

uchar addr; //语音地址全局变量
uchar s; //语音所对应数字
int VD; //扩大十倍的声速
int D; //测量的距离
int temp; //超声波反射时间

void timer() //初始化计数器
{
TMOD=0x10;
TH1=0;
TL1=0;
EA=1;
EX0=1;
}

void delay25us_40KHz(unsigned char us) //产生方波用于超声波发射
{

TR1=1;
while(us--)
{
P3_1= 0;
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
_nop_();
P3_1= 1;
_nop_();_nop_();
_nop_();_nop_();
}
P3_1= 1;
}

rec() interrupt 0 //计算超声波反射时间
{
TR1=0;
EA=0;
temp=TH1*256+TL1;
}

void delayms(uchar ms) // 延时子程序用于语音播放上电等待
{
uchar j;
while(ms--)
{
for(j = 0; j < 120; j++);
}
}

void delay2(int m) //长延时用于语音播放http://www.jiaoqb.com/
{
int l,j;
for(l=0;l<30001;l++)
for(j=0;j}

////////////////////////////液晶显示子函数//////////////////////////////////
void display()
{
float V;//声速
V=331.4+0.61*T; VD=V*10;
D=temp*V/2000-29;
displaystring(0,0,"Dis=");
displaychar(4,0,(D/100)+0x30);
displaychar(5,0,(D0/10)+0x30);
displaychar(6,0,.);
displaychar(7,0,(D)+0x30);
displaystring(8,0,"cm");
displaystring(0,1,"T=");
displaychar(5,1,(TD)+0x30);
displaychar(4,1,.);
displaychar(3,1,(TD0/10)+0x30);
displaychar(2,1,(TD/100)+0x30);
displaychar(6,1,0xdf);
displaychar(7,1,C);
displaystring(9,1,"V=");
displaychar(11,1,(VD/1000)+0X30);
displaychar(12,1,(VD00/100)+0X30);
displaychar(13,1,(VD0/10)+0X30);
displaychar(14,1,.);
displaychar(15,1,(VD)+0X30);

}
///////////////////////////////////////////////////////////////

////////////////放音部分子程序,放音地址由ADDR决定http://www.jiaoqb.com/

void play(addr)
{
uchar y;
SS=0;
MOSI=0;//发送开始
SCLK=0;
for(y=0;y<8;y++)
{
SCLK=0;
if((0x20>>y)&0x01)MOSI=1; //上电命令
else MOSI=0;
_nop_();
_nop_();
_nop_();
SCLK=1;
_nop_();
_nop_();
_nop_();
}//发送结束
SS=1;//上电结束
delayms(50);
SS=0;
MOSI=0;//发送地址
SCLK=0;
for(y=0;y<16;y++)
{
SCLK=0;
if((addr>>y)&0x01)MOSI=1;
else MOSI=0;
_nop_();
_nop_();
_nop_();
SCLK=1;
_nop_();
_nop_();
_nop_();
}//发送地址结束
MOSI=0;//放音
SCLK=0;
for(y=0;y<8;y++)
{
SCLK=0;
if((0xe0>>y)&0x01)MOSI=1; //指定地址放音命令电子小制作_我爱制作_电子DIY制作套件http://www.5imcu.net/
else MOSI=0;
_nop_();
_nop_();
_nop_();
SCLK=1;
_nop_();
_nop_();
_nop_();
}
SS=1;
SS=0;
MOSI=0;
SCLK=0;
for(y=0;y<8;y++)
{
SCLK=0;
if((0xf0>>y)&0x01)MOSI=1; //忽略地址放音命令(连贯播放后续空间)
else MOSI=0;
_nop_();
_nop_();
_nop_();
SCLK=1;
_nop_();
_nop_();
_nop_();
}
SS=1;
}
///////////////////////////////////////////////////////////////

///////////////////读数字子函数///////////////////////////////
void speaknum()
{
if(s==1) play(0x01);
if(s==2) play(0x0a);
if(s==3) play(0x14);
if(s==4) play(0x1e);
if(s==5) play(0x28);
if(s==6) play(0x32);
if(s==7) play(0x3c);
if(s==8) play(0x46);
if(s==9) play(0x50);
if(s==0) play(0x6e);
}
////////////////////////////////////////////////////////////////


///////////////////读出显示结果子函数////////////////////////////
void read()
{
play(0xdc); //播报“测量距离”这四个字,0xdc是之前记录下4004录这四个字的地址
delay2(50000);
delay2(50000); //延时等待播报完毕电子小制作_我爱制作_电子DIY制作套件
s=D/100; //D是测量得到的数据值,此语句是取数据的最高位
if(s==1) //判断最高位(十位)如果是“1”则不读yi而读shi
{
play(0x5a); //shi的地址
delay2(50000);
}
if(s>1) //如果大于1则读出相应数字后直接在后面加读shi
{
speaknum();
delay2(50000);
play(0x5a);
delay2(50000);
}
s=D0/10; //取次高位(个位)的值
if(s!=0) //次高位不为0则读出相应的值,为0跳过
{
speaknum();
delay2(50000);
}
play(0x64); //读小数点“dian”
delay2(50000);
s=D; //取小数点后一位的值
speaknum();
delay2(50000);
play(0xbe); //播报“cm”limi
delay2(50000);

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top