為什么DS18B20在LCD12864上顯示,溫度一直是0°C,求大佬們幫忙看看,謝謝了
單片機源程序如下:
#include "DS18B20.h"
#include<intrins.h>
uchar flag_temper = 0;
//****************************************************
//DS18B20延時函數(shù)
//****************************************************
void DS18B20_Delay(uint n )
{
uint i;
for(i = 0 ; i < n ; i++ );
}
//****************************************************
//DS18B20寫1字節(jié)
//****************************************************
void DS18B20_Write_Byte( uchar dat)
{
uchar i;
for( i = 0 ; i < 8 ; i++ )
{
DS18B20_DQ = 0;
_nop_(); //延時>1us
_nop_();
DS18B20_DQ = dat&0x01; //先寫低位
dat >>= 1;
DS18B20_Delay(6); //延時60~120us
DS18B20_DQ = 1; //釋放總線
_nop_(); //延時>1us
_nop_();
}
}
//****************************************************
//DS18B20讀1字節(jié)
//****************************************************
uchar DS18B20_Read_Byte( )
{
uchar dat,i;
for( i = 0 ; i < 8 ; i++ )
{
DS18B20_DQ = 0;
_nop_(); //延時>1us
_nop_();
DS18B20_DQ = 1; //釋放總線
_nop_(); //延時>1us
_nop_();
dat >>= 1;
if( DS18B20_DQ == 1)
{
dat |= 0X80;
}
else
{
dat &= 0x7f;
}
DS18B20_Delay(6); //延時60~120us
}
return dat;
}
//****************************************************
//DS18B20初始化
//****************************************************
bit DS18B20_Init()
{
bit Flag_exist = 1;
DS18B20_DQ = 1; //釋放總線
_nop_(); //延時>1us
_nop_();
DS18B20_DQ = 0;
DS18B20_Delay(50); //延時480~960us
DS18B20_DQ = 1; //釋放總線
DS18B20_Delay(3); //延時15~60us
Flag_exist = DS18B20_DQ;
DS18B20_Delay(10); //延時60~240us
DS18B20_DQ = 1; //釋放總線
return Flag_exist;
}
//**********************************************************
//讀取溫度函數(shù),返回溫度的絕對值,并標注flag_temper,flag_temper=1表示負,flag_temper=0表示正
//**********************************************************
uint Get_temp(void) //讀取溫度值
{
float tt;
uchar a,b;
uint temp;
if( DS18B20_Init() == 0 ) //初始化
{
DS18B20_Write_Byte(0xcc); //忽略ROM指令
DS18B20_Write_Byte(0x44); //溫度轉(zhuǎn)換指令
if( DS18B20_Init() == 0 ) //初始化
{
DS18B20_Write_Byte(0xcc); //忽略ROM指令
DS18B20_Write_Byte(0xbe); //讀暫存器指令
a = DS18B20_Read_Byte(); //讀取到的第一個字節(jié)為溫度LSB
b = DS18B20_Read_Byte(); //讀取到的第一個字節(jié)為溫度MSB
temp = b; //先把高八位有效數(shù)據(jù)賦于temp
temp <<= 8; //把以上8位數(shù)據(jù)從temp低八位移到高八位
temp = temp|a; //兩字節(jié)合成一個整型變量
if(temp>0xfff)
{
flag_temper=1; //溫度為負數(shù)
temp=(~temp)+1;
}
else
{
flag_temper=0; //溫度為正或者0
}
tt = temp*0.0625; //得到真實十進制溫度值
//因為DS18B20可以精確到0.0625度
//所以讀回數(shù)據(jù)的最低位代表的是0.0625度
temp = tt*100; //放大百倍
//這樣做的目的將小數(shù)點也轉(zhuǎn)換為可顯示數(shù)字
}
}
return temp;
}
#include"lcd12864.h"
/*******************************************************************************
* 函 數(shù) 名 : LCD12864_Delay1ms
* 函數(shù)功能 : 延時1MS
* 輸 入 : c
* 輸 出 : 無
*******************************************************************************/
void LCD12864_Delay1ms(uint c)
{
uint j,k;
for(j=c; j>0; j--)
for(k=114; k>0; k--);
}
/*******************************************************************************
* 函 數(shù) 名 : LCD12864_Busy
* 函數(shù)功能 : 檢測LCD是否忙
* 輸 入 : 無
* 輸 出 : 1或0(1表示不忙,0表示忙)
*******************************************************************************/
uchar LCD12864_Busy(void)
{
uchar i = 0;
LCD12864_RS = 0; //選擇命令
LCD12864_RW = 1; //選擇讀取
LCD12864_EN = 1;
LCD12864_Delay1ms(1);
while((LCD12864_DATAPORT & 0x80) == 0x80) //檢測讀取到的值
{
i++;
if(i > 100)
{
LCD12864_EN = 0;
return 0; //超過等待時間返回0表示失敗
}
}
LCD12864_EN = 0;
return 1;
}
/*******************************************************************************
* 函 數(shù) 名 : LCD12864_WriteCmd
* 函數(shù)功能 : 寫命令
* 輸 入 : cmd
* 輸 出 : 無
*******************************************************************************/
void LCD12864_WriteCmd(uchar cmd)
{
uchar i;
i = 0;
while( LCD12864_Busy() == 0)
{
LCD12864_Delay1ms(1);
i++;
if( i>100)
{
return; //超過等待退出
}
}
LCD12864_RS = 0; //選擇命令
LCD12864_RW = 0; //選擇寫入
LCD12864_EN = 0; //初始化使能端
LCD12864_DATAPORT = cmd; //放置數(shù)據(jù)
LCD12864_EN = 1; //寫時序
LCD12864_Delay1ms(5);
LCD12864_EN = 0;
}
/*******************************************************************************
* 函 數(shù) 名 : LCD12864_WriteData
* 函數(shù)功能 : 寫數(shù)據(jù)
* 輸 入 : dat
* 輸 出 : 無
*******************************************************************************/
void LCD12864_WriteData(uchar dat)
{
uchar i;
i = 0;
while( LCD12864_Busy() == 0)
{
LCD12864_Delay1ms(1);
i++;
if( i>100)
{
return; //超過等待退出
}
}
LCD12864_RS = 1; //選擇數(shù)據(jù)
LCD12864_RW = 0; //選擇寫入
LCD12864_EN = 0; //初始化使能端
LCD12864_DATAPORT = dat; //放置數(shù)據(jù)
LCD12864_EN = 1; //寫時序
LCD12864_Delay1ms(5);
LCD12864_EN = 0;
}
/*******************************************************************************
* 函 數(shù) 名 : LCD12864_ReadData
* 函數(shù)功能 : 讀取數(shù)據(jù)
* 輸 入 : 無
* 輸 出 : 讀取到的8位數(shù)據(jù)
*******************************************************************************/
#ifdef LCD12864_PICTURE
uchar LCD12864_ReadData(void)
{
uchar i, readValue;
i = 0;
while( LCD12864_Busy() == 0)
{
LCD12864_Delay1ms(1);
i++;
if( i>100)
{
return 0; //超過等待退出
}
}
LCD12864_RS = 1; //選擇命令
LCD12864_RW = 1;
LCD12864_EN = 0;
LCD12864_Delay1ms(1); //等待
LCD12864_EN = 1;
LCD12864_Delay1ms(1);
readValue = LCD12864_DATAPORT;
LCD12864_EN = 0;
return readValue;
}
#endif
/*******************************************************************************
* 函 數(shù) 名 : LCD12864_Init
* 函數(shù)功能 : 初始化LCD12864
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void LCD12864_Init()
{
LCD12864_PSB = 1; //選擇并行輸入
LCD12864_RST = 1; //復位
LCD12864_WriteCmd(0x30); //選擇基本指令操作
LCD12864_WriteCmd(0x0c); //顯示開,關(guān)光標
LCD12864_WriteCmd(0x01); //清除LCD12864的顯示內(nèi)容
}
/*******************************************************************************
* 函 數(shù) 名 : LCD12864_ClearScreen
* 函數(shù)功能 : 在畫圖模式下,LCD12864的01H命令不能清屏,所以要自己寫一個清
* * 屏函數(shù)
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
#ifdef LCD12864_PICTURE
void LCD12864_ClearScreen(void)
{
uchar i,j;
LCD12864_WriteCmd(0x34); //開啟拓展指令集
for(i=0;i<32;i++) //因為LCD有縱坐標32格所以寫三十二次
{
LCD12864_WriteCmd(0x80+i); //先寫入縱坐標Y的值
LCD12864_WriteCmd(0x80); //再寫入橫坐標X的值
for(j=0;j<32;j++) //橫坐標有16位,每位寫入兩個字節(jié)的的數(shù)據(jù),也
{ //就寫入32次以為當寫入兩個字節(jié)之后橫坐標會自
LCD12864_WriteData(0xFF); //動加1,所以就不用再次寫入地址了。
}
}
LCD12864_WriteCmd(0x36); //0x36擴展指令里面打開繪圖顯示
LCD12864_WriteCmd(0x30); //恢復基本指令集
}
#endif
/*******************************************************************************
* 函 數(shù) 名 : LCD12864_SetWindow
* 函數(shù)功能 : 設置在基本指令模式下設置顯示坐標。注意:x是設置行,y是設置列
* 輸 入 : x, y
* 輸 出 : 無
*******************************************************************************/
void LCD12864_SetWindow(uchar x, uchar y)
{
uchar pos;
if(x == 0) // 第一行的地址是80H
{
x = 0x80;
}
else if(x == 1) //第二行的地址是90H
{
x = 0x90;
}
else if(x == 2) //第三行的地址是88H
{
x = 0x88;
}
else if(x == 3)
{
x = 0x98;
}
pos = x + y;
LCD12864_WriteCmd(pos);
}
|