用C51调试HC—SR04,LCD1602显示出现乱码 求指教
时间:10-02
整理:3721RD
点击:
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit Trig=P1^0;
sbit Echo=P1^1;
sbit rs=P2^5;
sbit rw=P2^6;
sbit e=P2^7;
uint i, flag=0;
uint time=0;
unsigned long s=0;
uchar disbuff[]="0,0,0,0";
void delay_50us(uint t)
{
uchar j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
void StartModule() //启动模块
{
Trig=1;
delay_50us(1);
Trig=0;
}
void write_com(uchar com) //写指令
{
e=0;
rs=0;
rw=0;
P0=com;
delay_50us(100);
e=1;
delay_50us(100);
e=0;
}
void write_data(uchar dat) // 写数据
{
e=0;
rs=1;
rw=0;
P0=dat;
delay_50us(100);
e=1;
delay_50us(100);
e=0;
}
void init(void) //1602的初始化
{
delay_50us(300);
write_com(0x38);
delay_50us(100);
write_com(0x38);
delay_50us(100);
write_com(0x38);
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x06);
write_com(0x0c);
}
void main()
{
init();
TMOD=0x01; //选择定时器方式
TH0=0;
TL0=0;
EA=1; //开中断
ET0=1;
while(1)
{
StartModule();
while(!Echo)
TR0=1; // 开始计数
while(Echo)
TR0=0; //停止计数
Count(); // 计算
}
}
void time0() interrupt 1
{
flag=1;
}
void Count() //计算函数
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
s=(time*1.7)/100; //算出厘米
if((s<9999)||(flag=1))
{
flag=0;
disbuff[0]=s/1000;
disbuff[1]=s%1000/100;
disbuff[2]=s%1000%100/10;
disbuff[3]=s%1000%100%10;
}
write_com(0x80+0x01);
for(i=0;i<4;i++)
{
write_data(disbuff);
delay_50us(10);
}
while(1);
}
求大神帮忙看一下,怎么就出现乱码了呢? 纠结中啊
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit Trig=P1^0;
sbit Echo=P1^1;
sbit rs=P2^5;
sbit rw=P2^6;
sbit e=P2^7;
uint i, flag=0;
uint time=0;
unsigned long s=0;
uchar disbuff[]="0,0,0,0";
void delay_50us(uint t)
{
uchar j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
void StartModule() //启动模块
{
Trig=1;
delay_50us(1);
Trig=0;
}
void write_com(uchar com) //写指令
{
e=0;
rs=0;
rw=0;
P0=com;
delay_50us(100);
e=1;
delay_50us(100);
e=0;
}
void write_data(uchar dat) // 写数据
{
e=0;
rs=1;
rw=0;
P0=dat;
delay_50us(100);
e=1;
delay_50us(100);
e=0;
}
void init(void) //1602的初始化
{
delay_50us(300);
write_com(0x38);
delay_50us(100);
write_com(0x38);
delay_50us(100);
write_com(0x38);
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x06);
write_com(0x0c);
}
void main()
{
init();
TMOD=0x01; //选择定时器方式
TH0=0;
TL0=0;
EA=1; //开中断
ET0=1;
while(1)
{
StartModule();
while(!Echo)
TR0=1; // 开始计数
while(Echo)
TR0=0; //停止计数
Count(); // 计算
}
}
void time0() interrupt 1
{
flag=1;
}
void Count() //计算函数
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
s=(time*1.7)/100; //算出厘米
if((s<9999)||(flag=1))
{
flag=0;
disbuff[0]=s/1000;
disbuff[1]=s%1000/100;
disbuff[2]=s%1000%100/10;
disbuff[3]=s%1000%100%10;
}
write_com(0x80+0x01);
for(i=0;i<4;i++)
{
write_data(disbuff);
delay_50us(10);
}
while(1);
}
求大神帮忙看一下,怎么就出现乱码了呢? 纠结中啊