用STM8怎样编写温度采集显示系统程序?

用STM8怎样编写温度采集显示系统程序?,第1张

#include "REG51.H"

#include "INTRINS.H"

typedef unsigned char BYTE

sbit DQ = P3^3//DS18B20的数据口位P3.3

BYTE TPH //存放温橡睁度值的高字节

BYTE TPL //存放温森信度值的低字节

void DelayXus(BYTE n)

void DS18B20_Reset()

void DS18B20_WriteByte(BYTE dat)

BYTE DS18B20_ReadByte()

void main()

{

DS18B20_Reset() //设备复位

DS18B20_WriteByte(0xCC) //跳过ROM命令

DS18B20_WriteByte(0x44) //开始转换命令

while (!DQ) //等待转换完成

DS18B20_Reset() //设备复位

DS18B20_WriteByte(0xCC) //跳过ROM命令

DS18B20_WriteByte(0xBE) //读暂梁春岁存存储器命令

TPL = DS18B20_ReadByte() //读温度低字节

TPH = DS18B20_ReadByte() //读温度高字节

while (1)

}

这是我自己用过的DS18B20的程序,有什么问题可以百度橡陆祥HI我,我这个是把温度显示到电脑上了,如果你要显示到LCD上的话可以在里面梁搏加一悉伍些子程序。

#include<reg52.h>

#include<stdio.h>

#define uint unsigned int

#define uchar unsigned char

sbit ds=P1^0

bit flag

uchar count_t0

float f_temp

void delay(uint z)

{

uint x,y

for(x=zx>0x--)

for(y=122y>0y--)

}

void init() // 串口初始化

{

TMOD=0x21

SCON=0x50

TH0=0x4c

TL0=0x00

TH1=0xf3

TL1=0xf3

EA=1

ET0=1

TR0=1

TR1=1

}

void timer0() interrupt 1

{

TH0=0x4c

TL0=0x00

if(++count_t0>=20)

{

count_t0=0

flag=1

}

}

void dsreset()

{

uint i

ds=0

i=103

while(i>0)

i--

ds=1

i=4

while(i>0)

i--

}

bit read_bit()

{

uint i

bit dat

ds=0i++

ds=1i++i++

dat=ds

i=8

while(i>0)

i--

return dat

}

uchar read_byte()

{

uchar i,j,dat

dat=0

for(i=1i<=8i++)

{

j=read_bit()

dat=(j<<7)|(dat>>1)

}

return dat

}

void write_byte(uchar dat)

{

uint i

uchar j

bit testb

for(j=1j<=8j++)

{

testb=dat&0x01

dat=dat>>1

if(testb)

{

ds=0

i++i++

ds=1

i=8

while(i>0)

i--

}

else

{

ds=0

i=8

while(i>0)

i--

ds=1

i++i++

}

}

}

void begin_change()

{

dsreset()

delay(1)

write_byte(0xcc)

write_byte(0x44)

}

float get_temp()

{

uchar a,b

uint temp

float f_temp

dsreset()

delay(1)

write_byte(0xcc)

write_byte(0xbe)

a=read_byte()

b=read_byte()

temp=b

temp<<=8

temp=temp|a

f_temp=temp*0.0625

temp=f_temp*10+0.5

f_temp=f_temp+0.05

return f_temp

}

void main()

{

init()

while(1)

{

if(flag==1)

{

flag=0

begin_change()

TI=1

printf("The tempeature is %f\n",get_temp())

while(!TI)

TI=0

}

}

}

回答1:

用串口前卖咐请初始化串口。

回答2:

看数据定义

float tp//设定为单精度

display((uint) tp) //将单精度浮点型小数转换成无符号整型,这里会丢弃小数部分数据,但对tp本身无影响

send_char((uchar) tp)//将单精度浮点型小数转换成无符号字符型,尼玛还不是丢掉了小数部分。然后发送了个8位的tp过去。

回答3:

t1=(a&0x0f)*0.0625//取小数部分

t=((b<<4)|(a>>4))//取整数部分

//尼玛你非得要这样表示小数?难道你就不可以用2个uchar接收小数和整数,然后去发送?

if(t>128)

{

t=t+1

}

t=t+t1

//上面是不是这个意思如果整数部分大于128就让整数部分加1.如中高纯果不是那么就让整数部分加上小数部分。你的意思是不是大于128就不要小数了,小于128就要个小数。

解决方案先给我分我再给你附上念氏代码


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12375044.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存