#include <reg52.h>
#include "1602.h"
#include "dht.h"
//定义标识
volatile bit FlagStartRH = 0; //开始温湿度转换标志
//定义温湿度传感器用外部变量
extern U8 U8FLAG,k;
extern U8 U8count,U8temp;
extern U8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
extern U8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
extern U8 U8comdata;
extern U8 count, count_r;
U16 temp;
S16 temperature, humidity;
//定义变量
U16 RHCounter;
//定时器0初始化
void Timer0_Init()
{
ET0 = 1; //允许定时器0中断
TMOD = 1; //定时器工作方式选择
TL0 = 0x06;
TH0 = 0xf8; //定时器赋予初值
TR0 = 1; //启动定时器
}
//定时器0中断
void Timer0_ISR (void) interrupt 1 using 0
{
TL0 = 0x06;
TH0 = 0xf8; //定时器赋予初值
//每2秒钟启动一次温湿度转换
RHCounter ++;
if (RHCounter >= 1000)
{
FlagStartRH = 1;
RHCounter = 0;
}
}
/********************************************************************
* 名称 : Main()
* 功能 : 主函数
***********************************************************************/
void main()
{
U16 i, j, testnum;
EA = 0;
Timer0_Init(); //定时器0初始化
EA = 1;
L1602_init();
L1602_string(1,1," Welcome to T&H ");
L1602_string(2,1,"Detection System! ");
//延时
for (i=0;i<1000;i++)
for (j=0;j<1000;j++)
{;}
//清屏
L1602_string(1,1," ");
L1602_string(2,1," ");
L1602_string(1,1,"Temperature: C");
L1602_string(2,1,"Humidity: %");
while(1)
{
//温湿度转换标志检查
if (FlagStartRH == 1)
{
TR0 = 0;
testnum = RH();
FlagStartRH = 0;
TR0 = 1;
//读出温湿度,只取整数部分
humidity = U8RH_data_H;
temperature = U8T_data_H;
//显示温湿度
L1602_int(1,12,temperature);
L1602_int(2,12,humidity);
}
}
}
#ifndef _DHT_H_
#define _DHT_H_
#include <reg52.h>
typedef unsigned char U8; /* defined for unsigned 8-bits integer variable 无符号8位整型变量 */
typedef signed char S8; /* defined for signed 8-bits integer variable 有符号8位整型变量 */
typedef unsigned int U16; /* defined for unsigned 16-bits integer variable 无符号16位整型变量 */
typedef signed int S16; /* defined for signed 16-bits integer variable 有符号16位整型变量 */
typedef unsigned long U32; /* defined for unsigned 32-bits integer variable 无符号32位整型变量 */
typedef signed long S32; /* defined for signed 32-bits integer variable 有符号32位整型变量 */
typedef float F32; /* single precision floating point variable (32bits) 单精度浮点数(32位长度) */
typedef double F64; /* double precision floating point variable (64bits) 双精度浮点数(64位长度) */
sbit DATA = P1^7;
void Delay1(U16 );
void Delay_10us(void);
void COM(void);
U8 RH(void);
#endif
|