求单片机控制温湿度C程序下载地址... 或发邮箱[email protected]

如题所述

#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit DATA = P1^1; //DS18B20接入口
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//共阴极字型码
int temp; //温度值
int ss;//中间的一个变量
int dd;
int j;
uchar data b;//定时器中断次数
uchar data buf[4];//字型显示中间变量
int alarmH=500; //默认报警值
int alarmL=-10;
//定义开关的接入口
sbit k1=P2^5;//+
sbit k2=P2^6;//-
sbit k3=P2^7;//确认
sbit k4=P2^4;
sbit bell=P1^0;
sbit HLight=P1^2;
sbit LLight=P1^3;
sbit Red=P1^6;
sbit Green=P1^7;
bit set=0;
bit Flag=0;
int n;
//函数的声明区
void key_to1();
void key_to2();
void delay(uint);
void key();
void Show();
//函数的定义区
/*延时子函数*/
void delay(uint num)
{
while(num--) ;
}
//DS18b20温度传感器所需函数,分为初始化,读写字节,读取温度4个函数
Init_DS18B20(void) //传感器初始化
{
uchar x=0;
DATA = 1; //DQ复位
delay(10); //稍做延时
DATA = 0; //单片机将DQ拉低
delay(80); //精确延时 大于 480us //450
DATA = 1; //拉高总线
delay(20);
x=DATA; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(30);
}
ReadOneChar(void) //读一个字节
{
uchar i=0;
uchar dat = 0;
for (i=8;i>0;i--)
{
DATA = 0; // 给脉冲信号
dat>>=1;
DATA = 1; // 给脉冲信号
if(DATA)
dat|=0x80;
delay(8);
}
return(dat);
}
WriteOneChar(unsigned char dat)//写一个字节
{
uchar i=0;
for (i=8; i>0; i--)
{
DATA = 0;
DATA = dat&0x01;
delay(10);
DATA = 1;
dat>>=1;
}
delay(8);
}
int ReadTemperature(void) //读取温度
{
uchar a=0;
uchar b=0;
int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();//低位
b=ReadOneChar();//高位
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5;
return(t);
}
void display00() //*********显示负值子函数
{
dd=-(temp-1);
buf[1]=dd/100;
buf[2]=dd/100;
buf[3]=dd%100/10;
buf[0]=dd%10;
//动态显示
for(j=0;j<5;j++)
{
P2=0xff; // 初始灯为灭的
P0=0x00;
P2=0xfd; //显示小数点
P0=0x80; //显示小数点
delay(100);
P2=0xff; // 初始灯为灭的
P0=0x00;
P2=0xf7; //片选LCD1
P0=0x40;
delay(100);
P2=0xff;
P0=0x00;
P2=0xfb; //片选LCD2
P0=table[buf[2]];
delay(100);
P2=0xff;
P0=0x00;
P2=0Xfd; //片选LCD3
P0=table[buf[3]];
delay(100);
P2=0xff;
P0=0x00;
P2=0Xfe;
P0=table[buf[0]]; //片选LCD4
delay(100);
P2=0xff;
}
}
//显示正值子函数
void display()
{
buf[1]=temp/1000;//显示百位
buf[2]=temp/100%10;//显示十位
buf[3]=temp%100/10;//显示个位
buf[0]=temp%10; //第4位 南北秒个位
for(j=0;j<3;j++)
{
P2=0xff; // 初始灯为灭的
P0=0x00;
P2=0xfd; //显示小数点
P0=0x80; //显示小数点
delay(300);
P2=0xff; // 初始灯为灭的
P0=0x00;
P2=0xf7; //片选LCD1
P0=table[buf[1]];
delay(300);
P2=0xff;
P0=0x00;
P2=0xfb; //片选LCD2
P0=table[buf[2]];
delay(300);
P2=0xff;
P0=0x00;
P2=0Xfd; //片选LCD3
P0=table[buf[3]];
delay(300);
P2=0xff;
P0=0x00;
P2=0Xfe;
P0=table[buf[0]]; //片选LCD4
delay(300);
P2=0xff;
}
}
void key()//按键扫描子程序
{ if(k1!=1)
{
delay(20);
if(k1!=1)
{
while(k1!=1)
{ key_to1();
for(n=0;n<8;n++)
Show();
}
}
}
if(k2!=1)
{
delay(20);
if(k2!=1)
{
while(k2!=1)
{ key_to2();
for(n=0;n<8;n++)
Show();
}
}
}
if(k3!=1)
{TR0=1; //复位,开定时
temp=ReadTemperature();
}
if(k4!=1)
{ delay(20);
if(k4!=1)
{ while(k4!=1);
set=!set;
if(set==0)
{ Red=0;Green=1;}
else { Green=0;Red=1;}
}
}
}
void key_to1()
{
TR0=0; //关定时器
temp+=10;
if(temp>=1100)
{temp=-550;}
if(set==0)
{alarmH=temp;}
else {alarmL=temp;}
}
void key_to2()
{
TR0=0;
//关定时器
temp-=10;
if(temp<=-550)
{temp=1100;}
if(set==0)
{ alarmH=temp;}
else { alarmL=temp;}
}
void alarm(void)
{
if(temp>alarmH||temp<alarmL)
{ //bell=1;
//delay(50);
//bell=0;
Flag=1;
}else {Flag=0;}
}
logo()//开机的Logo
{ P0=0x40;
P2=0xf7;
delay(50);
P2=0xfb;
delay(50);
P2=0Xfd;
delay(50);
P2=0Xfe;
delay(50);
P1 = 0xff; //关闭显示
}
void Show()
{ if(temp>=0)
{HLight=1;LLight=0;display();}//显示函数
if(temp<0)
{HLight=0;LLight=1;display00();}
}
void main()
{
TCON=0x01;
TMOD=0X01;
TH0=0XD8;
TL0=0XF0;
EA=1;
ET0=1;
TR0=1;
EX0=1;
for(n=0;n<500;n++)//显示启动LOGo"- - - -"
{bell=0;logo();}
Red=0;
while(1)
{
key();
ss=ReadTemperature();
Show();
alarm();
if(Flag==1)
{bell=!bell;} //蜂鸣器滴滴响
else {bell=0;}
}
}
void time0(void) interrupt 1 using 1 //每隔10ms执行一次此子程序
{TH0=0X56;
TL0=0XDC;
temp=ss;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-01-12
基于SHT11的51单片机程序
#include <AT89s53.h> //Microcontroller specific library, e.g. port definitions
#include <intrins.h> //Keil library (is used for _nop()_ operation)
#include <math.h> //Keil library
#include <stdio.h> //Keil library

typedef union
{ unsigned int i;
float f;
} value;

//----------------------------------------------------------------------------------
// modul-var
//----------------------------------------------------------------------------------
enum {TEMP,HUMI};

#define DATA P1_1
#define SCK P1_0

#define noACK 0
#define ACK 1
//adr command r/w
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0

//----------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{ if (i & value) DATA=1; //masking value with i , write to SENSI-BUS
else DATA=0;
SCK=1; //clk for SENSI-BUS
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SCK=0;
}
DATA=1; //release DATA-line
SCK=1; //clk #9 for ack
error=DATA; //check ack (DATA will be pulled down by SHT11)
SCK=0;
return error; //error=1 in case of no acknowledge
}

//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
unsigned char i,val=0;
DATA=1; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{ SCK=1; //clk for SENSI-BUS
if (DATA) val=(val | i); //read bit
SCK=0;
}
DATA=!ack; //in case of "ack==1" pull down DATA-Line
SCK=1; //clk #9 for ack
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SCK=0;
DATA=1; //release DATA-line
return val;
}

//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
DATA=1; SCK=0; //Initial state
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
_nop_();_nop_();_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}

//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
DATA=1; SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{ SCK=1;
SCK=0;
}
s_transstart(); //transmission start
}

//----------------------------------------------------------------------------------
char s_softreset(void)
//----------------------------------------------------------------------------------
// resets the sensor by a softreset
{
unsigned char error=0;
s_connectionreset(); //reset communication
error+=s_write_byte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}

//----------------------------------------------------------------------------------
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error=s_write_byte(STATUS_REG_R); //send command to sensor
*p_value=s_read_byte(ACK); //read status register (8-bit)
*p_checksum=s_read_byte(noACK); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}

//----------------------------------------------------------------------------------
char s_write_statusreg(unsigned char *p_value)
//----------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error+=s_write_byte(STATUS_REG_W);//send command to sensor
error+=s_write_byte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}

//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
unsigned error=0;
unsigned int i;

s_transstart(); //transmission start
switch(mode){ //send command to sensor
case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
default : break;
}
for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
if(DATA) error+=1; // or timeout (~2 sec.) is reached
*(p_value) =s_read_byte(ACK); //read the first byte (MSB)
*(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
*p_checksum =s_read_byte(noACK); //read checksum
return error;
}

//----------------------------------------------------------------------------------
void init_uart()
//----------------------------------------------------------------------------------
//9600 bps @ 11.059 MHz
{SCON = 0x52;
TMOD = 0x20;
TCON = 0x69;
TH1 = 0xfd;
}

//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [度] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [度]
{ const float C1=-4.0; // for 12 Bit
const float C2=+0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=+0.01; // for 14 Bit @ 5V
const float T2=+0.00008; // for 14 Bit @ 5V

float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature

t_C=t*0.01 - 40; //calc. temperature from ticks
rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range

*p_temperature=t_C; //return temperature
*p_humidity=rh_true; //return humidity[%RH]
}

//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [度]
// output: dew point
{ float logEx,dew_point;
logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
}

//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature
// 4. calculate dew point
// 5. print temperature, humidity, dew point

{ value humi_val,temp_val;
float dew_point;
unsigned char error,checksum;
unsigned int i;

init_uart();
s_connectionreset();
while(1)
{ error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
if(error!=0) s_connectionreset(); //in case of an error: connection reset
else
{ humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n",temp_val.f,humi_val.f,dew_point);
}
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!)
//-----------------------------------------------------------------------------------
}
}
第2个回答  2013-01-12
您的问题大了
温湿度采集就不同的传感器方法是不一样的,当然程序也不会相同。所以您好先说明你手头的传感器是什么?
相似回答