请高手看一下那程序,超声波传感器为什么测不了距离呢
请教一下高手,为什么这个写的这个程序用超声波只能显示刚开始的距离,请帮忙改一下,谢谢了#include "STC15F2K.H"
#include <intrins.h>
//引脚定义
sbit RX =P1 ^ 2;
sbit TX =P1 ^ 3;
sbit CS =P4 ^ 5; //片选 高电平有效 单片LCD使用时可固定高电平
sbit SID =P5 ^ 5; //数据
sbit SCLK=P5 ^ 4; //时钟
sbit LCD_PSB= P4 ^ 1;
sbit LCD_K =P4 ^ 2;
sbit LCD_L =P4 ^ 4;
#define LCD_data P0
void Write_char(bit start, unsigned char ddata);
void Delaynms(unsigned int di);
void Lcd_init(void);
//void Disp_img(unsigned char *img);
void LCD_Write_string(unsigned char X,unsigned char Y,unsigned char *s);
void LCD_set_xy( unsigned char x, unsigned char y );
unsigned char code num[]={"0123456789 :.-"};
unsigned char code waves[]={"超声波测距系统"};
unsigned char code znwk[] ={"智能微控工作室"};
unsigned char code CM[] ={"M"};
static unsigned char DisNum = 0;
unsigned inttime=0;
unsignedlong S=0;
bitflag =0;
unsigned char disbuff ={ 0,0,0,0,};
/******************************************************************************/
void Lcd_init(void) //初始化LCD
{
LCD_K=0;
LCD_L=1;
LCD_PSB=1;
// Delaynms(2); //启动等待,等LCM讲入工作状态
CS=1;
Write_char(0,0x30);//8 位介面,基本指令集
Write_char(0,0x0c);//显示打开,光标关,反白关
Write_char(0,0x01);//清屏,将DDRAM的地址计数器归零
}
/******************************************************************************/
void Write_char(bit start, unsigned char ddata) //写指令或数据
{
CS=start;
SID=0;
SCLK=0;
P0=ddata;
Delaynms(5);
SCLK=1;
Delaynms(5);
SCLK=0;
}
/******************************************************************************/
void Delaynms(unsigned int di) //延时
{
unsigned int da,db;
for(da=0;da<di;da++)
for(db=0;db<10;db++);
}
/******************************************************************************/
void Clr_Scr(void)//清屏函数
{
Write_char(0,0x01);
}
/******************************************************************************/
void LCD_set_xy( unsigned char x, unsigned char y )
{ //设置LCD显示的起始位置,X为行,Y为列
unsigned char address;
switch(x)
{
case 0: address = 0x80 + y; break;
case 1: address = 0x80 + y; break;
case 2: address = 0x90 + y; break;
case 3: address = 0x88 + y; break;
case 4: address = 0x98 + y; break;
default:address = 0x80 + y; break;
}
Write_char(0, address);
}
/******************************************************************************/
void LCD_Write_string(unsigned char X,unsigned char Y,unsigned char *s)
{ // 中英文字符串显示函数
LCD_set_xy( X, Y );
while (*s)
{
Write_char( 1, *s );
s ++;
Delaynms(1);
}
}
/******************************************************************************/
void LCD_Write_number(unsigned char s)// 数字显示函数
{
Write_char(1,num);
Delaynms(5);
}
/******************************************************************************/
void Lcd_Mark2(void)
{
Clr_Scr();//清屏
LCD_Write_string(1,0,znwk);//
LCD_Write_string(2,0,waves);//
LCD_Write_string(3,7,CM);//
}
//5ms延时
void Delay5Ms(void)
{
unsigned int TempCyc = 5552;
while(TempCyc--);
}
//400ms延时
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
};
}
/********************************************************/
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=time*1.87/100; //算出来是CM 11。0592M晶振
if(flag==1) //超出测量
{
flag=0;
LCD_set_xy( 3, 4 );
LCD_Write_number(13);
LCD_Write_number(12);
LCD_set_xy( 3, 5 );
LCD_Write_number(13);
LCD_Write_number(13);
}
else
{
disbuff=S%1000/100;
disbuff=S%1000%100/10;
disbuff=S%1000%10 %10;
LCD_set_xy( 3, 4 );
LCD_Write_number(disbuff);
LCD_Write_number(12);
LCD_set_xy( 3, 5 );
LCD_Write_number(disbuff);
LCD_Write_number(disbuff);
}
}
/********************************************************/
void delayms(unsigned int ms)
{
unsigned char i=100,j;
for(;ms;ms--)
{
while(--i)
{
j=10;
while(--j);
}
}
}
/********************************************************/
void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围
{
flag=1; //中断溢出标志
}
/********************************************************/
voidStartModule() //T1中断用来扫描数码管和计800MS启动模块
{
TX=1; //800MS启动一次模块
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TX=0;
}
/********************************************************/
void main(void)
{
void Delay400Ms(void);
Lcd_init(); //设置液晶显示器
Clr_Scr(); //清屏
Lcd_init(); //从图形显示状态下返回
Lcd_Mark2();
while(1)
{
TMOD=0x01; //设T0为方式1,GATE=1
TH0=0;
TL0=0;
TR0=1;
EA=1;
while(1)
{
StartModule();
while(!RX); //当RX为零时等待
TR0=1; //开启计数
while(RX); //当RX为1计数并等待
TR0=0; //关闭计数
Conut(); //计算
delayms(80); //80MS
}
}
}
检查输出到模块讯号、模块输入讯号、或模块电压、等 devcang 发表于 2012-10-23 17:26 static/image/common/back.gif
检查输出到模块讯号、模块输入讯号、或模块电压、等
我拔了超声波的正电,他会有测量,但他的测量不准 把 StartModule();函数丢在中断服务函数里面去定时60-80MS发射一次 就可以了
你用的是STC的15系列的那个片子,咋搞到的?
jqfsjt 发表于 2012-10-24 19:36 static/image/common/back.gif
你用的是STC的15系列的那个片子,咋搞到的?
网上买的那单片机啊 超声波没做过!是什么
页:
[1]