|
請(qǐng)教一下大神,我用STC15W408AS+DS18B20, 想實(shí)現(xiàn)串口輸出文本類型的溫度數(shù)值,類似28.50這樣的溫度數(shù)值,用下面這個(gè)代碼需要怎么樣改,感謝。
#ifndef _DS18B20_H
#define _DS18B20_H
#include "reg51.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit DQ = P3^2; //DS1820數(shù)據(jù)線IO口
bit nflag; //溫度正負(fù)標(biāo)志
/*****************************************************
延時(shí)函數(shù):延時(shí)1us
******************************************************/
void delayus(uint i)
{
while(i--);
}
/*****************************************************
功能: 延時(shí),最小單位為ms(晶振為11.0592
計(jì)算方法:一個(gè)for循環(huán) = 8個(gè)指令周期 = 8*12個(gè)機(jī)器周期
= 8*12*1/11.0592 = 0.0086ms
xms = 0.0086 * 110 = 0.946 ms
******************************************************/
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
/*****************************************************
DS18B20復(fù)位函數(shù):
1 數(shù)據(jù)線拉低,延時(shí)480us
2 數(shù)據(jù)線拉高,延時(shí)480us
******************************************************/
void ds1820rst()
{
DQ = 1; //初始狀態(tài)
delayus(4);
DQ = 0; //拉低
delayus(480); //延時(shí)480us
DQ = 1; //拉高
delayus(480);
}
/****************************************************
18b20讀數(shù)據(jù):由低位到高位,重復(fù)八次,讀一個(gè)字節(jié)
1 數(shù)據(jù)線拉低,延時(shí)4us
2 數(shù)據(jù)線拉高,延時(shí)10us
3 讀數(shù)據(jù)線狀態(tài)一位,處理數(shù)據(jù),延時(shí)45us
4 重復(fù)步驟1-3
*****************************************************/
uchar ds1820rd()
{
uchar i = 0;
uchar dat = 0;
for(i=8;i>0;i--)
{
DQ = 0; //拉低數(shù)據(jù)線
delayus(4);
dat>>=1; //空出第一位
DQ = 1; //拉高數(shù)據(jù)線
delayus(10);
if(DQ) //若為高電平,則最高位置一
dat|=0x80;
delayus(45);//延時(shí)45us
}
return(dat);
}
/**************************************************
18b20寫數(shù)據(jù):由低位到高位,重復(fù)八次,寫一個(gè)字節(jié)
1 數(shù)據(jù)線拉低,延時(shí)15us
2 送一位數(shù)據(jù)到數(shù)據(jù)線,延時(shí)60us
3 數(shù)據(jù)線拉高
4 重復(fù)步驟1-3
**************************************************/
void ds1820wr(uchar wdata)
{
uchar i = 0;
for (i=8; i>0; i--)
{
DQ = 0; //數(shù)據(jù)線拉低
delayus(15);
DQ = wdata&0x01; //將數(shù)據(jù)最低位送入數(shù)據(jù)線
delayus(60); //延時(shí)10us
DQ = 1; //數(shù)據(jù)線拉高
wdata>>=1; //字節(jié)右移一位
}
}
/*****************************************************
讀溫度并轉(zhuǎn)換,根據(jù)RAM命令表寫命令
0xcc: 不讀取序列地址
0x44: 啟動(dòng)溫度轉(zhuǎn)換
0xbe: 讀取溫度
******************************************************/
uint read_temp()
{
uchar TL,TH; //TL:低八位 TH:高八位
uchar wd; //最終溫度的絕對(duì)值
ds1820rst(); //先復(fù)位
ds1820wr(0xcc); //不讀取64位地址,因?yàn)榭偩上只有一個(gè)DB1820,單片工作
ds1820wr(0x44); //啟動(dòng)溫度轉(zhuǎn)換
delayus(125);
ds1820rst();
ds1820wr(0xcc); //跳過(guò)讀序列號(hào)
ds1820wr(0xbe); //讀取溫度
TL = ds1820rd(); //讀低八位
TH = ds1820rd(); //讀高八位
wd = TL/16+TH*16;
// tvalue = TH; //tvalue為16位
// tvalue = (tvalue<<8)+TL;
// if((tvalue&0xf800) == 0xf800) //判斷負(fù)溫度
// {
// tvalue=~tvalue+1; //補(bǔ)碼=反碼+1
// nflag = 0; //置標(biāo)志位為0
// }
// else
// nflag = 1;
//
// wd = tvalue*0.625; //計(jì)算溫度值
return(wd); //返回溫度值
}
/*****************************************************
串口初始化函數(shù):
******************************************************/
void init()
{
P1M0 = 0;P1M1 = 0;
SCON = 0x40;
T2L = 0xe0;
T2H = 0xfe;
AUXR = 0x14;
AUXR |= 0x01;
TI = 1;
}
/*****************************************************
串口通訊函數(shù):發(fā)送兩個(gè)字節(jié),首字節(jié)為正負(fù)標(biāo)志位,
第二個(gè)字節(jié)為溫度值
******************************************************/
void comm(uchar temper)
{
// while(!TI == 1);
// {
// TI = 0;
// SBUF = nflag;
// }
while(!TI == 1);
{
TI = 0;
SBUF = temper;
}
}
#endif
void main()
{
uchar temp; //臨時(shí)變量存儲(chǔ)溫度
init(); //串口的初始化
while(1)
{
temp=read_temp(); //讀取溫度
delayms(500); //延時(shí)0.5s
comm(temp); //發(fā)送至上位機(jī)
}
}
|
評(píng)分
-
查看全部評(píng)分
|