|
怎么將18b20的溫度顯示到屏幕上 單片機(jī)用的stc15f2k602
//////////////////////////////////////////////////////////////////////////////////
//本程序只供學(xué)習(xí)使用,未經(jīng)作者許可,不得用于其它任何用途
// 文 件 名 : main.c
// 版 本 號 : v2.0
// 作 者 : HuangKai
// 生成日期 : 2014-0101
// 最近修改 :
// 功能描述 : OLED 4接口演示例程(51系列)
// 說明:
// ----------------------------------------------------------------
// GND 電源地
// VCC 接5V或3.3v電源
// D0 P1^0(SCL)
// D1 P1^1(SDA)
// RES 接P12
// DC 接P13
// CS 接P14
// ----------------------------------------------------------------
// 修改歷史 :
// 日 期 :
//All rights reserved
//******************************************************************************/
#include "STC15Fxxx.H"//調(diào)用stc15f系列頭文件,下載地址: http://pan.baidu.com/s/1eRUbjLS
#include "oled.h"
#include "bmp.h"
#define ds18b20_io P20 //P2.0口作為數(shù)據(jù)讀取接口
void Delay3us(); //22.1184M晶振精確延時30微秒
void Delay6us(); //22.1184M晶振精確延時30微秒
void Delay24us(); //22.1184M晶振精確延時40微秒
void Delay30us(); //22.1184M晶振精確延時40微秒
void Delay300us(); //22.1184M晶振精確延時40微秒
void Delay600us(); //22.1184M晶振精確延時30毫秒
void Delay1000ms(); //22.1184M晶振精確延時1000毫秒
bit ds18b20_init(); //初始化ds18b20
u8 ds18b20_read_one_char(); //從ds18b20讀取一個字節(jié)
void ds18b20_write_one_char(u8 dat);//向ds18b20寫入一個字節(jié)
void ds18b20_ready_read_temp(); //準(zhǔn)備讀取數(shù)據(jù)
u16 ds18b20_read_temp_val(); //讀取數(shù)據(jù),返回溫度值,返回實(shí)際值的100倍,且萬位為符號位,例:12556為-25.56度,2556為25.56度
u8 high,low;
//變量定義
u16 datas=12345;//測試用
float f_temp ;//溫度數(shù)據(jù)
u16 d2,d1,d0,temp;//測試
int main(void)
{
OLED_Init(); //初始化OLED
OLED_Clear();//清屏
ds18b20_io = 1;//18b20先高電平
OLED_DrawBMP(0,0,128,8,BMP1);
delay_ms(1000);
OLED_Clear();
while(1)
{
datas=ds18b20_read_temp_val();//
if(datas/10000 == 1) //如果萬位為1,則表示溫度為負(fù)
f_temp=datas*0.0625; //溫度在寄存器中為12位 分辨率位0.0625°
datas=f_temp*10+0.5; //精確到十分位,四舍五入
d2=datas/100; //顯示數(shù)據(jù):十位
d1=datas%100/10; //顯示數(shù)據(jù):個位
d0=datas%10; //顯示數(shù)據(jù):十分位
Delay1000ms();
OLED_ShowCHinese(0,2,6);//當(dāng)
OLED_ShowCHinese(16,2,7);//前
OLED_ShowCHinese(32,2,8);//溫
OLED_ShowCHinese(48,2,9);//度
OLED_ShowNum(76,2,d2,1,16);
OLED_ShowNum(92,2,d1,1,16);
OLED_ShowNum(108,2,d0,1,16);
OLED_ShowCHinese(112,0,1);//滿電
}
}
//ds18b20初始化
bit ds18b20_init()
{
u8 init_success_tag=0;
ds18b20_io=1;//拉高
Delay6us();//6us
ds18b20_io=0;//拉低
Delay600us();//600us
ds18b20_io=1;//拉高
Delay30us();//30us
init_success_tag=ds18b20_io;
Delay600us();//600us
return init_success_tag;
}
//讀取一個字節(jié)
u8 ds18b20_read_one_char()
{
u8 i=0;
u8 dat=0;
for(i=0;i<8;i++)
{
ds18b20_io=1;//拉高
NOP1();
ds18b20_io=0;//拉低
dat>>=1;
NOP1();
ds18b20_io=1;//拉高
Delay6us();//6us
if(ds18b20_io==1)
dat|=0x80;
else
dat|=0x00;
Delay24us();//24us
}
return (dat);
}
//向傳感器寫入一個字節(jié)
void ds18b20_write_one_char(u8 dat)
{
u8 i=0;
for(i=0;i<8;i++)
{
ds18b20_io=1;//拉高
NOP1();
ds18b20_io=0;//拉低
ds18b20_io=dat&0x01;
Delay30us();//30us
ds18b20_io=1;//拉高
Delay3us();//3us
dat>>=1;
}
Delay6us();
Delay6us();//12us
}
//準(zhǔn)備讀取一個溫度值
void ds18b20_ready_read_temp()
{
ds18b20_init(); //初始化
ds18b20_write_one_char(0xcc);//忽略讀序列號
ds18b20_write_one_char(0x44);//啟動溫度轉(zhuǎn)換
Delay300us(); //300us,等待轉(zhuǎn)換完畢
ds18b20_init(); //初始化
ds18b20_write_one_char(0xcc);//忽略讀序列號
ds18b20_write_one_char(0xbe);//讀取溫度寄存器
}
//讀取數(shù)據(jù),返回溫度值,返回實(shí)際值的100倍,且萬位為符號位,例:12556
u16 ds18b20_read_temp_val()
{
u16 temp_16_bit=0;
u8 temp_L=0;
u8 temp_H=0;
ds18b20_ready_read_temp();
temp_L=ds18b20_read_one_char();//讀取溫度低八位
temp_H=ds18b20_read_one_char();//讀取溫度高八位
if(temp_H>0x7f)
{
temp_L=~temp_L; //補(bǔ)碼轉(zhuǎn)換,取反加一
temp_H=~temp_H+1;
temp_16_bit=100;
}
temp_16_bit=temp_16_bit+temp_H*16+temp_L/16;
temp_16_bit=temp_16_bit*100;
temp_16_bit=temp_16_bit+((temp_L&0x0f)*10/16)*10;
temp_16_bit=temp_16_bit+(temp_L&0x0f)*100/16%10;
return (u16)(temp_16_bit);
}
void Delay3us() //@22.1184MHz
{
unsigned char i;
i = 14;
while (--i);
}
void Delay6us() //@22.1184MHz
{
unsigned char i;
_nop_();
_nop_();
_nop_();
i = 30;
while (--i);
}
void Delay24us() //@22.1184MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 1;
j = 128;
do
{
while (--j);
} while (--i);
}
void Delay30us() //@22.1184MHz
{
unsigned char i, j;
i = 1;
j = 162;
do
{
while (--j);
} while (--i);
}
void Delay300us() //@22.1184MHz
{
unsigned char i, j;
i = 7;
j = 113;
do
{
while (--j);
} while (--i);
}
void Delay600us() //@22.1184MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 13;
j = 229;
do
{
while (--j);
} while (--i);
}
void Delay1000ms() //@22.1184MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 85;
j = 12;
k = 155;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
|
|