|
/**************************************************************************************
程序名称:DS18B20检测温度
程序功能:下载程序后,8位七段数码管前四位显示当前温度,后四位显示湿度(湿度未完成暂时显示为H00.0)
温度超过80,或小于10摄氏度,蜂鸣器响
使用平台:STC15W/IAP15W
硬件连线:JP10>>J12, JP11>>J16, P3.6>>J8,
DS18B20:DQ>>P3.1, Vcc>>3.3V, GND
时间: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++)//稳定显示,约1s刷机一次
{
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位数码管第2、7位添加小数点
{
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初始化函数*****/
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
}
/*****读一个字节数据函数*****/
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;
}
/*****写一个字节数据函数*****/
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;
}
}
/*****得到温度值函数*****/
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;
}
/*****温度转换函数*****/
void transform(long v)
{
v = v%1000;
Temp_HR[0] = v/100; //ten
v = v%100;
Temp_HR[1] = v/10; //个位
v = v%10;
Temp_HR[2] = v/1; //小数位
}
|
-
a3.jpg
(3.3 MB, 下载次数: 53)
实验现象
评分
-
查看全部评分
|