|
请教一下高手,为什么这个写的这个程序用超声波只能显示刚开始的距离,请帮忙改一下,谢谢了
#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 int time=0;
unsigned long S=0;
bit flag =0;
unsigned char disbuff[4] ={ 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[s]);
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[1]=S%1000/100;
disbuff[2]=S%1000%100/10;
disbuff[3]=S%1000%10 %10;
LCD_set_xy( 3, 4 );
LCD_Write_number(disbuff[1]);
LCD_Write_number(12);
LCD_set_xy( 3, 5 );
LCD_Write_number(disbuff[2]);
LCD_Write_number(disbuff[3]);
}
}
/********************************************************/
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; //中断溢出标志
}
/********************************************************/
void StartModule() //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
}
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|