#51单片机采集温湿度
刚开始学单片机,简单了解了一下数码管及串口通信,想要采集温湿度数据
无法正确采集数据,如下
请各位帮忙看看代码问题出在哪里
#include<reg52.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit ds=P2^2;
uint temp;
float f_temp;
void delay(uint s)
{
uint i,j;
for(i=s;i>0;i--)
for(j=110;j>0;j--);
}
void dsreset(void)
{
uint i;
ds=0;
i=103;
while(i>0)
i--;
ds=1;
i=4;
while(i>0)
i--;
}
bit tempreadbit(void)
{
uint i;
bit dat;
ds=0;
i++;
ds=1;
i++;
i++;
dat=ds;
i=8;
while(i>0)
i--;
return(dat);
}
uchar tempread(void)
{
uchar i,j,dat;
dat = 0;
for(i=1;i<=8;i++)
{
j=tempreadbit();
dat=(j<<7)|(dat>>1);
}
return(dat);
}
void tempwritebyte(uchar dat)
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb)
{
ds=0;
i++;
ds=1;
i=8;
while(i>0)
i--;
}
else
{
ds=0;
i=8;
while(i>0)
i--;
ds=1;
i++;
i++;
}
}
}
void tempchange(void)
{
dsreset();
delay(1);
tempwritebyte(0xcc);
tempwritebyte(0x44);
}
uint get_temp()
{
uchar a,b;
tempchange();
dsreset();
tempwritebyte(0xcc);
tempwritebyte(0xbe);
a=tempread();
b=tempread();
temp=b;
temp<<=8;
temp=temp|a;
f_temp=temp*0.0625;
temp=f_temp*10+0.5;
f_temp=f_temp+0.05;
}
void init_com(void)
{
TMOD=0x20;
PCON=0x00;
SCON=0x50;
TH1=0xfd;
TL1=0xfd;
TR1=1;
}
void comm(char *parr)
{
do
{
SBUF=*parr++;
while(!TI);
TI=0;
}
while(*parr);
}
void main()
{
uchar buff[4];
init_com();
while(1)
{
get_temp();
sprintf(buff,"%f",f_temp);
delay(100);
comm(buff);
}
}
谢谢各位帮助!