|
#include<reg51.h>
#include"temp.h"
//--定義使用的IO--//
#define GPIO_DIG P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
//--定義全局變量
unsigned char code DIG_CODE[17]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的顯示碼
unsigned char DisplayData[8];
//用來(lái)存放要顯示的8位數(shù)的值
//--聲明全局函數(shù)--//
void LcdDisplay(int);
void DigDisplay();
/*******************************************************************************
* 函 數(shù) 名 : main
* 函數(shù)功能 : 主函數(shù)
* 輸 入 : 無(wú)
* 輸 出 : 無(wú)
*******************************************************************************/
void main()
{
while(1)
{
LcdDisplay(Ds18b20ReadTemp());
}
}
/*******************************************************************************
* 函 數(shù) 名 : LcdDisplay()
* 函數(shù)功能 : LCD顯示讀取到的溫度
* 輸 入 : v
* 輸 出 : 無(wú)
*******************************************************************************/
void LcdDisplay(int temp) //lcd顯示
{
float tp;
if(temp< 0) //當(dāng)溫度值為負(fù)數(shù)
{
DisplayData[0] = 0x40;
//因?yàn)樽x取的溫度是實(shí)際溫度的補(bǔ)碼,所以減1,再取反求出原碼
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
//留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語(yǔ)言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時(shí)候把小數(shù)點(diǎn)
//后面的數(shù)自動(dòng)去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進(jìn)1了,小于0.5的就
//算由?.5,還是在小數(shù)點(diǎn)后面。
}
else
{
DisplayData[0] = 0x00;
tp=temp;//因?yàn)閿?shù)據(jù)處理有小數(shù)點(diǎn)所以將溫度賦給一個(gè)浮點(diǎn)型變量
//如果溫度是正的那么,那么正數(shù)的原碼就是補(bǔ)碼它本身
temp=tp*0.0625*100+0.5;
//留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語(yǔ)言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時(shí)候把小數(shù)點(diǎn)
//后面的數(shù)自動(dòng)去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進(jìn)1了,小于0.5的就
//算加上0.5,還是在小數(shù)點(diǎn)后面。
}
DisplayData[1] = DIG_CODE[temp / 10000];
DisplayData[2] = DIG_CODE[temp % 10000 / 1000];
DisplayData[3] = DIG_CODE[temp % 1000 / 100] | 0x80;
DisplayData[4] = DIG_CODE[temp % 100 / 10];
DisplayData[5] = DIG_CODE[temp % 10];
DigDisplay(); //掃描顯示
}
/*******************************************************************************
* 函 數(shù) 名 : DigDisplay
* 函數(shù)功能 : 使用數(shù)碼管顯示
* 輸 入 : 無(wú)
* 輸 出 : 無(wú)
*******************************************************************************/
void DigDisplay()
{
unsigned char i;
unsigned int j;
for(i=0;i<8;i++)
{
switch(i) //位選,選擇點(diǎn)亮的數(shù)碼管,
{
case(0):
LSA=0;LSB=0;LSC=0; break;//顯示第0位
case(1):
LSA=1;LSB=0;LSC=0; break;//顯示第1位
case(2):
LSA=0;LSB=1;LSC=0; break;//顯示第2位
case(3):
LSA=1;LSB=1;LSC=0; break;//顯示第3位
case(4):
LSA=0;LSB=0;LSC=1; break;//顯示第4位
case(5):
LSA=1;LSB=0;LSC=1; break;//顯示第5位
case(6):
LSA=0;LSB=1;LSC=1; break;//顯示第6位
case(7):
LSA=1;LSB=1;LSC=1; break;//顯示第7位
}
GPIO_DIG=DisplayData[i];//發(fā)送段碼
j=180; //掃描間隔時(shí)間設(shè)定
while(j--);
}
}
#include"temp.h"
/*******************************************************************************
* 函 數(shù) 名 : Delay1ms
* 函數(shù)功能 : 延時(shí)函數(shù)
* 輸 入 : 無(wú)
* 輸 出 : 無(wú)
*******************************************************************************/
void Delay1ms(uint y)
{
uint x;
for( ; y>0; y--)
{
for(x=110; x>0; x--);
}
}
/*******************************************************************************
* 函 數(shù) 名 : Ds18b20Init
* 函數(shù)功能 : 初始化
* 輸 入 : 無(wú)
* 輸 出 : 初始化成功返回1,失敗返回0
*******************************************************************************/
uchar Ds18b20Init()
{
uchar i;
DSPORT = 0; //將總線拉低480us~960us
i = 70;
while(i--);//延時(shí)642us
DSPORT = 1; //然后拉高總線,如果DS18B20做出反應(yīng)會(huì)將在15us~60us后總線拉低
i = 0;
while(DSPORT) //等待DS18B20拉低總線
{
Delay1ms(1);
i++;
if(i>5)//等待>5MS
{
return 0;//初始化失敗
}
}
return 1;//初始化成功
}
/*******************************************************************************
* 函 數(shù) 名 : Ds18b20WriteByte
* 函數(shù)功能 : 向18B20寫(xiě)入一個(gè)字節(jié)
* 輸 入 : com
* 輸 出 : 無(wú)
*******************************************************************************/
void Ds18b20WriteByte(uchar dat)
{
uint i, j;
for(j=0; j<8; j++)
{
DSPORT = 0; //每寫(xiě)入一位數(shù)據(jù)之前先把總線拉低1us
i++;
DSPORT = dat & 0x01; //然后寫(xiě)入一個(gè)數(shù)據(jù),從最低位開(kāi)始
i=6;
while(i--); //延時(shí)68us,持續(xù)時(shí)間最少60us
DSPORT = 1; //然后釋放總線,至少1us給總線恢復(fù)時(shí)間才能接著寫(xiě)入第二個(gè)數(shù)值
dat >>= 1;
}
}
/*******************************************************************************
* 函 數(shù) 名 : Ds18b20ReadByte
* 函數(shù)功能 : 讀取一個(gè)字節(jié)
* 輸 入 : com
* 輸 出 : 無(wú)
*******************************************************************************/
uchar Ds18b20ReadByte()
{
uchar byte, bi;
uint i, j;
for(j=8; j>0; j--)
{
DSPORT = 0;//先將總線拉低1us
i++;
DSPORT = 1;//然后釋放總線
i++;
i++;//延時(shí)6us等待數(shù)據(jù)穩(wěn)定
bi = DSPORT; //讀取數(shù)據(jù),從最低位開(kāi)始讀取
/*將byte左移一位,然后與上右移7位后的bi,注意移動(dòng)之后移掉那位補(bǔ)0。*/
byte = (byte >> 1) | (bi << 7);
i = 4; //讀取完之后等待48us再接著讀取下一個(gè)數(shù)
while(i--);
}
return byte;
}
/*******************************************************************************
* 函 數(shù) 名 : Ds18b20ChangTemp
* 函數(shù)功能 : 讓18b20開(kāi)始轉(zhuǎn)換溫度
* 輸 入 : com
* 輸 出 : 無(wú)
*******************************************************************************/
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳過(guò)ROM操作命令
Ds18b20WriteByte(0x44); //溫度轉(zhuǎn)換命令
// Delay1ms(100); //等待轉(zhuǎn)換成功,而如果你是一直刷著的話,就不用這個(gè)延時(shí)了
}
/*******************************************************************************
* 函 數(shù) 名 : Ds18b20ReadTempCom
* 函數(shù)功能 : 發(fā)送讀取溫度命令
* 輸 入 : com
* 輸 出 : 無(wú)
*******************************************************************************/
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳過(guò)ROM操作命令
Ds18b20WriteByte(0xbe); //發(fā)送讀取溫度命令
}
/*******************************************************************************
* 函 數(shù) 名 : Ds18b20ReadTemp
* 函數(shù)功能 : 讀取溫度
* 輸 入 : com
* 輸 出 : 無(wú)
*******************************************************************************/
int Ds18b20ReadTemp()
{
int temp = 0;
uchar tmh, tml;
Ds18b20ChangTemp(); //先寫(xiě)入轉(zhuǎn)換命令
Ds18b20ReadTempCom(); //然后等待轉(zhuǎn)換完后發(fā)送讀取溫度命令
tml = Ds18b20ReadByte(); //讀取溫度值共16位,先讀低字節(jié)
tmh = Ds18b20ReadByte(); //再讀高字節(jié)
temp = tmh;
temp <<= 8;
temp |= tml;
return temp;
}
|
|