|
/**************************************************************************************
程序名稱:DS18B20檢測(cè)溫度
程序功能:下載程序后,8位七段數(shù)碼管前四位顯示當(dāng)前溫度,后四位顯示濕度(濕度未完成暫時(shí)顯示為H00.0)
溫度超過80,或小于10攝氏度,蜂鳴器響
使用平臺(tái):STC15W/IAP15W
硬件連線:JP10>>J12, JP11>>J16, P3.6>>J8,
DS18B20:DQ>>P3.1, Vcc>>3.3V, GND
時(shí)間:2019/4/24 23:20
作者:璃落彼岸
***************************************************************************************/
#include "STC15.h"
#include "intrins.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit FMQ=P4^2;
sbit DQ = P3^1;
bit ReadTempFlag;
u8 code SEG_Code[]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,//num 0~9
0x7f,0xc6,0x89};//symble “.”,“C”,"H"
//0x88,0x83,,0xa1,0x86,0x8e,0xff
u8 code Bit_Code[]=
{0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//bit 0~7
/************************************
get Temp and HR from sensors
*************************************/
u8 Temp_HR[8]={0,0,0,11,12,0,0,0};//00.0C H00.0
/**************************************
Declare global function
******************************************/
void Pin_Mode(void);
void SEG7_1Bit_Display(u8 num,u8 Position);
void Beep(void);
void Delay_nms(u16 nms);
void delayms(u16 xms);
void delay_xus(u8 n);
u8 Init(void);
u8 readByte();
void writeByte(u8 dat);
float getTmpValue(void);
void transform(long v);
void main(void)
{
u8 i=0;
// u8 j;
Pin_Mode();
Init();//Initialize DS18B20
while(1)
{
transform(getTmpValue()); //get and transfer temp_value
for(j=0;j<200;j++)//穩(wěn)定顯示,約1s刷機(jī)一次
{
SEG7_1Bit_Display(Temp_HR[i++],8-i);
Delay_nms(2);
i=i%8;
}
if(Temp_HR[0]>8)//溫度大于80
{
Beep();Delay_nms(3);
}
else if(Temp_HR[0]<1)//溫度小于10
{
Beep();Delay_nms(3);
}
}
}
/**************************************
Initialize Pin
******************************************/
void Pin_Mode(void)
{
P0M1 = 0x00;P0M0 = 0x00;
P2M1 = 0x00;P2M0 = 0x00;
P3M1 = 0x00;P3M0 = 0x00;
P4M1 = 0x00;P4M0 = 0x00;
}
void SEG7_1Bit_Display(u8 num,u8 Position)
{
P2=0xff;
P0=~SEG_Code[num];
P2=Bit_Code[Position];
if(Position==6||Position==1)//在8位數(shù)碼管第2、7位添加小數(shù)點(diǎn)
{
P0=P0+0x80;
}
}
void Beep(void)
{
u8 i;
for(i=0;i<10;i++)
{
delay_xus(500);
FMQ=~FMQ;
}
FMQ=1;
}
/*****s_delay*****/
void Delay_nms(u16 nms) //65535nms
{
u16 i,t;
for(i=0;i<nms;i++)
{
for(t=0;t<1080;t++);
}
}
/*****ms_delay***
void delayms(u16 xms)
{
u16 i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
*/
/*****us_delay****/
void delay_xus(u8 n)
{
while(n--)
{
_nop_();
_nop_();
}
}
/**************************************
get Temp from DS18B20
Time_delay is serious!
******************************************/
/*****DS18B20初始化函數(shù)*****/
u8 Init(void)
{
u16 CONT_1 = 0;
u8 Flag_1 = 1;
u8 Status = 0x00;
DQ = 1;
DQ = 0;
delay_xus(495);//495us
DQ = 1;
while((DQ != 0)&&(Flag_1 == 1)) //wait for the response of ds18B20,avoid overtime
{
CONT_1++;
delay_xus(10); //60us
if(CONT_1 > 8000)Flag_1 = 0;
Status = DQ;
}
delay_xus(100); //240us
DQ = 1;
return Status; //initial
}
/*****讀一個(gè)字節(jié)數(shù)據(jù)函數(shù)*****/
u8 readByte()
{
u16 i;
u8 Value = 0x00;
DQ = 1;
delay_xus(10);
for(i=1;i<=8;i++)
{
Value >>= 1;
DQ = 0;
delay_xus(1);
DQ = 1;
delay_xus(1);
if(DQ == 1)
Value |= 0x80;
delay_xus(60);
}
return Value;
}
/*****寫一個(gè)字節(jié)數(shù)據(jù)函數(shù)*****/
void writeByte(u8 dat)
{
u16 j;
for(j=1;j<=8;j++)
{
if((dat & 0x01))
{
DQ = 0;
delay_xus(1);
DQ = 1;
delay_xus(60);
}
else
{
DQ = 0;
delay_xus(60);
DQ = 1;
delay_xus(1);
}
dat>>=1;
}
}
/*****得到溫度值函數(shù)*****/
float getTmpValue(void)
{
u8 low,high;
u16 temp;
float fValue;
Init();
writeByte(0xcc);
writeByte(0x44);
Init();
writeByte(0xcc);
writeByte(0xbe);
low = readByte();
high = readByte();
if(high&0xFC)
{
ReadTempFlag = 1;
temp = ((high<<8)|low);
temp = ~temp + 1;
}
else
{
ReadTempFlag = 0;
temp = ((high<<8)|low);
}
fValue = temp*0.0625;
temp = fValue*10+0.5; //>0 +0.5;<0 -0.5
fValue = temp+0.05;
return fValue;
}
/*****溫度轉(zhuǎn)換函數(shù)*****/
void transform(long v)
{
v = v%1000;
Temp_HR[0] = v/100; //ten
v = v%100;
Temp_HR[1] = v/10; //個(gè)位
v = v%10;
Temp_HR[2] = v/1; //小數(shù)位
}
|
-
a3.jpg
(3.3 MB, 下載次數(shù): 53)
下載附件
2019-4-24 23:35 上傳
實(shí)驗(yàn)現(xiàn)象
評(píng)分
-
查看全部評(píng)分
|