STC15F2K60S2系列的单片机,我想做一个超声波测距的,用1602显示,可是程序不知道怎么改,有大神可以编一下吗?
#include
#include "1062.h"
#define VELOCITY_30C 3495
#define VELOCITY_23C 3453
sbit INPUT = P1^6;
sbit OUTPUT = P1^7;
sbit Beep = P5^5 ;
long int distance=0;
uchar table[]=" Welcome to ";
uchar table0[]="wei bao love you ";
uchar table1[]="There's no echo.";
uchar table2[]=" ren jia you ";
uchar table3[]="Distance:";
uchar count;
extern void initLCD();
extern void write_date(uchar date);
extern void write_com(uchar com);
extern void delay(uint x);
void Delay_xMs(unsigned int x)
{
unsigned int i,j;
for(i = 0;i < x;i++ )
{
for(j = 0;j < 3;j++ )
{
;
}
}
}
void Alarm(uchar t)
{
uchar i;
for(i = 0;i < t;i++)
{
Beep = 0;
Delay_xMs(20000);
Beep = 1;
Delay_xMs(20000);
}
}
void delayt(uint x)
{
uchar j;
while(x-- > 0)
{
for(j = 0;j < 125;j++)
{
;
}
}
}
void Init_MCU(void)
{
TMOD = 0x01;
TL0 = 0x66;
TH0 = 0xfc;
ET0 = 1;
EA = 1;
}
void Init_Parameter(void)
{
OUTPUT =1;
INPUT = 1;
count = 0;
distance = 0;
}
void display_char(uchar *point,uchar address)
{
uchar i;
write_com(0x80 + address);
for(i = 0;i < 16; i++)
{
write_date(*point);
point++;
}
}
void display(int number,uchar address)
{
uchar b,c,d,e;
b= (number / 1000);
c= (number / 100) % 10;
d = (number / 10) % 10;
e = number % 10;
write_com(0x80 + address);
write_date(b + 48);
write_date(c + 48);
write_date(d + 48);
write_date(46);
write_date(e + 48);
write_date(99);
write_date(109);
}
void Trig_SuperSonic(void)
{
OUTPUT = 1;
delayt(1);
OUTPUT = 0;
}
void Measure_Distance(void)
{
uchar l;
uint h,y;
TR0 = 1;
while(INPUT)
{
;
}
TR0 = 0;
l = TL0;
h = TH0;
y = (h << 8) + l;
y = y - 0xfc66;//us??
distance = y + 1000 * count;
TL0 = 0x66;
TH0 = 0xfc;
delayt(30);
distance = VELOCITY_30C * distance / 20000;
}
void main(void)
{
initLCD();
Init_MCU();
Init_Parameter();
Alarm(2);
display_char(table,0x00);
display_char(table0,0x40);
Delay_xMs(30000);
display_char(table2,0x00);
display_char(table1,0x40);
while(1)
{
Trig_SuperSonic();
while(INPUT == 0)
{
;
}
Measure_Distance();
display_char(table3,0x40);
display(distance,0x49);
Init_Parameter();
delayt(100);
}
}
void timer0 (void) interrupt 1
{
TF0 = 0;
TL0 = 0x66;
TH0 = 0xfc;
count++;
if(count == 18)
{
TR0 =0;
TL0 = 0x66;
TH0 = 0xfc;
count = 0;
}
}
。先调和测距模块之间的通信。看模块资料。写协议。得到距离数据了。就调1602。把整体流程大概分析一下。进行整合。
要改成什么样,超声波模块的使用、显示部分,这些先把规格书看明白了。