将DS18B20数据发送至上位机

DS18B20数据上传至上位机

#include<reg52.h>
#include<intrins.h>
#include<stdio.h>
#define uint  unsigned int 
#define uchar unsigned char 

sbit dsdq=P2^2;

void UartInit(void)		//9600bps@11.0592MHz
{
    SCON=0x40; //配置串行口方式1, REN禁止
	  TMOD &=0X0F;
	  TMOD |=0X20; //配置定时器1为8位自动重载
	  TH1=0xfd; //波特率设置 253
	  TL1=0xfd;
	  TR1=1;//打开定时计数器1;
}

void sendByte(char data_msg)
{
  SBUF=data_msg;
  while(!TI);
  TI=0;//发送中断请求标志位  来代替瞎延时
}

void sendString(char *str)
{
  while(*str!='\0')
	{
	   sendByte(*str);
		str++;
	}

}

char putchar (char c) //实现printf函数发送功能
{
	SBUF=c;
  while (!TI);
  TI = 0;
  return c;
}

void Delay5us()
{
	_nop_();
}
void Delayxus(uchar xus)//延时任意us
{
	while(--xus);
}
void DelayMs(uint c)
{
	uint a,b;
	for(a=c;a>0;a--)
		for(b=115;b>0;b--);
}

void Ds18B20_Reset()//DS18B20的复位
{
	dsdq=1;
	Delayxus(1);
	dsdq=0;
	Delayxus(250);
	dsdq=1;
	Delayxus(250);	
}
void Ds18B20_WriteByte(uchar dat)//写一个字节
{
	uchar i;
	bit dstemp;
	for(i=0;i<8;i++)
	{
		dat=dat>>1;
		dstemp=CY;
		if(dstemp)//发送的1
		{
			dsdq=1;
			Delayxus(1);
			dsdq=0;
			Delayxus(1);
			dsdq=1;	
			Delayxus(30);
		}
		else//发送的0
		{
			dsdq=1;
			Delayxus(1);
			dsdq=0;
			Delayxus(30);
		}
	}
}
uchar Ds18B20_ReadByte()//读一个字节
{
	uchar i,k,dat;
	for(i=0;i<8;i++)
	{
		dsdq=1;
		Delayxus(1);
		dsdq=0;
		Delayxus(1);
		dsdq=1;
		k=dsdq;
		dat=(k<<7)|(dat>>1);
		Delayxus(20);
	}
	return dat;
}
int Ds18B20_Temper()//实现一次温度的采集
{
	uchar tem1,tem2;
	int temper;
	Ds18B20_Reset();
	Ds18B20_WriteByte(0xcc);
	Ds18B20_WriteByte(0x44);
	Ds18B20_Reset();
	Ds18B20_WriteByte(0xcc);
	Ds18B20_WriteByte(0xbe);
	tem1=Ds18B20_ReadByte();
	tem2=Ds18B20_ReadByte();
	temper=tem2;
	temper=(temper<<8)|tem1;
	return temper;
}

void main ()
{
float tmp;
	int j;
	UartInit();
	while(1)
	{
		DelayMs(1000);
     tmp=Ds18B20_Temper()*0.0625;//23.458
	  	j=(tmp*10+0.5)*10;//
		printf("温度为%d%d.%d%d℃\r\n",j/1000,j%1000/100,j%100/10,j%10);
	}
}

上位机图片演示

在这里插入图片描述


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值