|
第一次进入测距时,一切正常,然后就出现问题了
1602.c程序应该没问题,在调试第一次时,GetDistance()函数运行正常,然后就无法再次进入GetDistance函数,
用不同值测试了ValueToStr函数(整形数值 转换成字符串,用于液晶显示),显示正常。
调试好几天了,实在调不出来了,求大神帮忙看看。拜谢
/****************************main**********************************/
#include<reg52.h>
#include<intrins.h>
#include"1602.h"
sbit tring = P1^6;
sbit echo = P3^2;
bit flag200ms = 0;
void ValueToStr(unsigned char *str,unsigned int value);
unsigned int GetDistance();
void InitTimer0();
void InitTimer1();
void main()
{
unsigned char str[10];
unsigned int distance;
InitLcd();
InitTimer0();
InitTimer1();
tring = 0;
while(1)
{
if(flag200ms)
{
flag200ms = 0;
distance = GetDistance();
ValueToStr(str,distance);
WriteCmd(0x01);
ShowStr(0,0,"distance mm ");
ShowStr(0,1,str);
}
}
}
void InitTimer1() //初始化定时器1,用于计时200ms
{
EA = 1;
TMOD &= 0x0f;/配置定时器1
TMOD |= 0x10
TH1 = 0xfc;
TL0 = 0x66;
ET1 = 1;
TR1 = 1;
}
void delay() //延时函数
{
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
}
void InitTimer0() //初始化定时器0,便于计时
{
TMOD = 0x01;
TH0 = 0;
TL0 = 0;
TR0 = 0;
}
void SendStart() //发送起始信号
{
tring = 1;
delay();
tring = 0;
}
unsigned int GetDistance() //超声波测距函数
{
float distance=0,time=0; //初始时间距离
SendStart();
while(!echo); //未接收到信号时等待
TR0 = 1; //接收到信号后,跳出等待,开始计时
while(echo); //等待高电平结束
TR0 = 0; //关闭定时器
if(TH0>=0x3e)//距离大于3m无法精确测距,3e为大于3m时间
{
TH0 = 0;
TL0 = 0;
return 0;
}
time = (256*TH0 +TL0)*1.085; //计算时间 单位us
TH0 = 0;
TL0 = 0;
distance = time * 344/1000/2;//计算距离 单位mm
return (unsigned char ) distance;//将距离强制转换成整数后返回
}
void ValueToStr(unsigned char *str,unsigned int value) //把距离转换成字符串,以便于在液晶上显示
{
unsigned char dat[10];
char index;
for(index=0;value!=0;index++)
{
dat[index] = value%10;
value /= 10;
}
index--;
for(;index>=0;index--)
{
*str++ = dat[index]+'0';
}
*str = '\0';
}
void InterruptTimer1()interrupt 3
{
static unsigned int cnt = 0;
TH1 = 0xfc;
TL1 = 0x66;
cnt++;
if(cnt == 200)
{
flag200ms = 1;
cnt = 0;
}
}
/************************1602.c*****************************/
#define _1602_C
#include<reg52.h>
#define LCD_DB P0
sbit LCD_RS = P1^0;
sbit LCD_RW = P1^1;
sbit ENLCD = P1^5;
void LcdReady()
{
unsigned char sta;
LCD_DB = 0xff;//¸ßμçƽ¼ì2a
LCD_RS = 0;
LCD_RW = 1;
do
{
ENLCD = 1;
sta = LCD_DB;
ENLCD = 0;
}while(sta & 0x80);
}
void WriteCmd(unsigned char cmd)
{
LcdReady();
LCD_RS = 0;
LCD_RW = 0;
LCD_DB = cmd;
ENLCD = 1;
ENLCD = 0;
}
void InitLcd()
{
WriteCmd(0x38);
WriteCmd(0x0c);
WriteCmd(0x06);
WriteCmd(0x01);
}
void WriteDat(unsigned char dat)
{
LcdReady();
LCD_RS = 1;
LCD_RW = 0;
LCD_DB = dat;
ENLCD = 1;
ENLCD = 0;
}
void ShowStr(unsigned char x,unsigned char y,unsigned char *ptr)
{
unsigned char addr;
if(y==0)
addr =0x00 + x;
else if(y == 1)
addr = 0x40 +x;
WriteCmd(0x80 | addr);//í¨1yÖ¸áîÂëéèÖÿaê¼Î»ÖÃ
while(*ptr != '\0')
{
WriteDat(*ptr++);
}
}
/*************************1602.h*****************************/
#ifndef _1602_H
#define _1602_H
#ifndef _1602_C
#endif
void ShowStr(unsigned char x,unsigned char y,unsigned char *ptr);
void InitLcd();
void WriteCmd(unsigned char cmd);
#endif
|
|