51温度湿度c语言代码
初三 其它 9085字 558人浏览 猥琐的抱大腿

#ifndef __TOU_H__//如果没有被编译过

#define __TOU_H__//那么标记为编译过

#include<reg52.h>

#include <intrins.h>

#include <math.h>

#include<string.h>

#define uint unsigned int

#define uchar unsigned char

enum {TEMP,HUMI};

sbit DATA = P2^5;//温湿度传感器位定义

sbit SCK = P2^4;

void s_connectionreset(void);/******** SHT11函数声明 ********/

char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode); void calc_sth10(float *p_humidity ,float *p_temperature);

float calc_dewpoint(float h,float t);

#endif//结束#ifndef,如果已经编译过就跳过之间的部分

uchar code table1[]="00:00:00";

uchar code table2[]="00:00:00RH:00.0";

sbit shui1=P1^0;

sbit shui2=P1^1;

sbit shi_s1=P1^2;

sbit shi_s2=P1^3;

sbit shi_s3=P1^4;

sbit led=P1^6;

sbit sound=P1^7;

sbit lcden=P2^2;

sbit lcdrw=P2^1;

sbit lcdrs=P2^0;

sbit relay=P2^3;

sbit s1=P2^7;

uchar data disdata[4];

uchar num,count,s1num;

char shi,fen,miao,shi1,fen1,miao1;

uint bian,i,j;

void delay(uint z)

{

uint x,y;

for(x=z;x>0;x--)

for(y=110;y>0;y--);

}

//SHT11程序(SHT11.c ):

#define noACK 0 //继续传输数据,用于判断是否结束通讯

#define ACK 1 //结束数据传输;

//地址命令读/写

#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)

{

unsigned char i,error=0;

for (i=0x80;i>0;i>>=1) //高位为1,循环右移 {

if (i&value) DATA=1; //和要发送的数相与,结果为发送的位

else DATA=0;

SCK=1;

_nop_();_nop_();_nop_(); //延时3us

SCK=0;

}

DATA=1; //释放数据线

SCK=1;

error=DATA; //检查应答信号,确认通讯正常

_nop_();_nop_();_nop_();

SCK=0;

DATA=1;

return error; //error=1 通讯错误

} //读字节程序

char s_read_byte(unsigned char ack)

{

unsigned char i,val=0;

DATA=1; //释放数据线 for(i=0x80;i>0;i>>=1) //高位为1,循环右移 {

SCK=1;

if(DATA) val=(val|i); //读一位数据线的值

SCK=0;

}

DATA=!ack; //如果是校验,读取完后结束通讯;

SCK=1;

_nop_();_nop_();_nop_(); //延时3us

SCK=0;

_nop_();_nop_();_nop_(); DATA=1; //释放数据线

return val;

}

//启动传输

void s_transstart(void)

{

DATA=1; SCK=0;

_nop_();

SCK=1;

_nop_();

DATA=0;

_nop_();

SCK=0;

_nop_();_nop_();_nop_();

SCK=1;

_nop_();

DATA=1;

_nop_();

SCK=0;

}

void s_connectionreset(void)

{

unsigned char i;

DATA=1; SCK=0; //准备

for(i=0;i<9;i++) //DATA保持高,SCK 时钟触发9次,发送启动传输,通迅即复位

{

SCK=1;

SCK=0;

}

s_transstart(); //启动传输

}

//温湿度测量

char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) // 进行温度或者湿度转换,由参数mode 决定转换内容;

{

unsigned error=0;

unsigned int i;

s_transstart(); //启动传输

switch(mode) //选择发送命令

{

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; //等待测量结束

if(DATA) error+=1; // 如果长时间数据线没有拉低,说明测量错误 *(p_value) =s_read_byte(ACK); //读第一个字节,高字节 (MSB)

*(p_value+1)=s_read_byte(ACK); //读第二个字节,低字节 (LSB)

*p_checksum =s_read_byte(noACK); //read CRC校验码

return error; // error=1 通讯错误

}

//温湿度值标度变换及温度补偿

void calc_sth10(float *p_humidity ,float *p_temperature)

{

const float C1=-4.0; // 12位湿度精度修正公式

const float C2=+0.0405; // 12位湿度精度修正公式

const float C3=-0.0000028; // 12位湿度精度修正公式

const float T1=0.46; // 14位温度精度 5V 条件修正公式

const float T2=+0.00008; // 14位温度精度 5V 条件修正公式

float rh=*p_humidity; // rh: 12位湿度

float t=*p_temperature; // t: 14位温度

float rh_lin; // rh_lin: 湿度 linear 值

float rh_true; // rh_true: 湿度 ture 值

float t_C; // t_C : 温度℃

t_C=t*0.01 - 40; //补偿温度

rh_lin=C3*rh*rh + C2*rh + C1; //相对湿度非线性补偿

rh_true=(t_C-33)*(T1+T2*rh)+rh_lin; //相对湿度对于温度依赖性补偿

if(rh_true>100)rh_true=100; //湿度最大修正

if(rh_true<0.1)rh_true=0.1; //湿度最小修正

*p_temperature=t_C; //返回温度结果

*p_humidity=rh_true; //返回湿度结果

}

//从相对温度和湿度计算露点

float calc_dewpoint(float h,float t)

{

float logEx,dew_point;

logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);

dew_point = (0.66077-logEx)*237.3/(logEx-8.16077);

return dew_point;

}

typedef union //定义共用同类型

{

unsigned int i;

float f;

} value;

void write_com(uchar com)//lcd部分

{

lcdrs=0;

lcdrw=0;

delay(5);

lcden=1;

P0=com;

delay(5);

lcden=0;

}

void write_data(uchar date) {

lcdrs=1;

lcden=1;

delay(5);

P0=date;

delay(5);

lcden=0;

}

void lcd_init()

{

lcdrw=0;

lcden=0;

delay(15);

write_com(0x38); delay(5);

write_com(0x38); delay(5);

write_com(0x38); write_com(0x38); write_com(0x08); write_com(0x01); write_com(0x06); write_com(0x0c); write_com(0x80+1); TMOD=0x11;

TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1;

ET0=1;

TR0=1;

write_com(0x80+1);

for(num=0;num<8;num++) {

write_data(table1[num]); delay(20);

}

write_com(0x80+0x40+1);

for(num=0;num<16;num++)

{

write_data(table2[num]);

delay(20);

}

}

void write_sfm(uchar add,uchar date) {

uchar shi,ge;

shi=date/10;

ge=date%10;

write_com(0x80+0x40+add);

write_data(0x30+shi);

write_data(0x30+ge);

}

void write_shedingsfm(uchar add1,uchar date1) {

uchar shi1,ge1;

shi1=date1/10;

ge1=date1%10;

write_com(0x80+add1);

write_data(0x30+shi1);

write_data(0x30+ge1);

}

void keyscan()

{

if(shi_s1==0)

{

delay(5);

if(shi_s1==0)

{

s1num++;

while(!shi_s1);

}

}

if(s1num==1)

{

write_com(0x80+0x40+8);

write_com(0x0f);

}

if(s1num==2)

{

write_com(0x80+0x40+5); write_com(0x0f); }

if(s1num==3)

{

write_com(0x80+0x40+2); write_com(0x0f); }

if(s1num==4)

{TR0=1;

write_com(0x80+8); write_com(0x0f); }

if(s1num==5)

{

write_com(0x80+5); write_com(0x0f); }

if(s1num==6)

{

write_com(0x80+2); write_com(0x0f); }

if(s1num==7)

{

s1num=0; write_com(0x0c); }

if(s1num!=0)

{

if(shi_s2==0)

{

delay(5);

if(shi_s2==0)

{

while (!shi_s2); if(s1num==1) {

miao++; if(miao==60) {

miao=0; }

write_sfm(7,miao);

write_com(0x80+0x40+8);

}

if(s1num==2)

{

fen++;

if(fen==60)

{

fen=0;

}

write_sfm(4,fen);

write_com(0x80+0x40+5);

}

if(s1num==3)

{

shi++;

if(shi==24)

{

shi=0;

}

write_sfm(1,shi);

write_com(0x80+0x40+2); }

if(s1num==4)

{

TR0=1;

miao1++;

if(miao1==60)

{

miao1=0;

}

write_shedingsfm(7,miao1); write_com(0x80+8);

}

if(s1num==5)

{

fen1++;

if(fen1==60)

{

fen1=0;

}

write_shedingsfm(4,fen1);

write_com(0x80+5);

}

if(s1num==6)

{

shi1++;

if(shi1==24)

{

shi1=0;

}

write_shedingsfm(1,shi1); write_com(0x80+2); }

}

}

}

if(shi_s3==0)

{

delay(5);

if(shi_s3==0)

{

while (!shi_s3);

if(s1num==1)

{

miao--;

if(miao==-1)

{

miao=59;

}

write_sfm(7,miao);

write_com(0x80+0x40+8); }

if(s1num==2)

{

fen--;

if(fen==-1)

{

fen=59;

}

write_sfm(4,fen);

write_com(0x80+0x40+5); }

if(s1num==3)

{

shi--;

if(shi==-1)

{

shi=23;

}

write_sfm(1,shi);

write_com(0x80+0x40+2); }

if(s1num==4)

{

TR0=1;

miao1--;

if(miao1==-1)

{

miao1=59;

}

write_shedingsfm(7,miao1); write_com(0x80+8); }

if(s1num==5)

{

fen1--;

if(fen1==-1)

{

fen1=59;

}

write_shedingsfm(4,fen1); write_com(0x80+5); }

if(s1num==6)

{

shi1--;

if(shi1==-1)

{

shi1=23;

}

write_shedingsfm(1,shi1); write_com(0x80+2); }

}

}

}

void baojing()

{

if(shui1==0)

{

led=~led;

delay(150);

{

for(i=0;i<10;i++)

{

sound=0;

delay(20);

sound=1;

delay(20);

}

}

}

if(shui2==0)

relay=1;

}

void lcd_dingshi()

{

keyscan();

if(count==20)

{

count=0;

miao++;

if(miao==60)

{

miao=0;

fen++;

if(fen==60) {

fen=0; shi++; if(shi==24) {

shi=0; }

write_sfm(1,shi); }

write_sfm(4,fen); }

write_sfm(7,miao);

}

}

void main()

{

unsigned int temp,humi;

value humi_val,temp_val; //定义两个共同体,一个用于湿度,一个用于温度 float dew_point; //用于记录露点值

unsigned char error; //用于检验是否出现错误

unsigned char checksum; //CRC

uchar n; n=0;

relay=1;

lcd_init();

P3=0;

led=0;

sound=1;

while(1)

{

if(s1==0)//if语句内为关系运算符为==

{

delay(20);

if(s1==0)

{

n++;

while(!s1);

}

}

if(n==5)

n=0;

switch(n)//使用多分支选择语句

{

case 1:P3=0x10;bian=500; //第一个LED 亮

break;

case 2:P3=0x20;bian=450; //第二个LED 亮

break;

case 3:P3=0x40;bian=550; //第三个LED 亮

break;

case 4:P3=0x80;bian=600; //第四个LED 亮

break;

default:P3=0x00;

}

lcd_dingshi();

if((shi==shi1)&&(fen==fen1)&&(miao==miao1))

{

relay=0;

}

baojing();

if(miao==10)

{

s_connectionreset(); //启动连接复位

error=0; //初始化error=0,即没有错误

error+=s_measure((unsigned char*)&temp_val.i,&checksum,TEMP); //温度测量 error+=s_measure((unsigned char*)&humi_val.i,&checksum,HUMI); //湿度测量 if(error!=0) s_connectionreset(); ////如果发生错误,系统复位 humi_val.f=(float)humi_val.i; //转换为浮点数 temp_val.f=(float)temp_val.i; //转换为浮点数

calc_sth10(&humi_val.f,&temp_val.f); //修正相对湿度及温度 dew_point=calc_dewpoint(humi_val.f,temp_val.f); //计算e dew_point temp=temp_val.f*10;

humi=humi_val.f*10;

disdata[0]=humi%1000/100+0x30;

disdata[1]=humi%100/10+0x30;

disdata[2]=humi%10+0x30;

write_com(0x80+0x40+12);

write_data(disdata[0]);

write_com(0x80+0x40+13);

write_data(disdata[1]);

write_com(0x80+0x40+14);

write_data(0x2e);

write_com(0x80+0x40+15);

write_data(disdata[2]);

miao++;

if(miao==60)

{

miao=0;

fen++;

if(fen==60)

{

fen=0;

shi++;

if(shi==24)

{

shi=0;

}

write_sfm(1,shi);

}

write_sfm(4,fen);

}

write_sfm(7,miao);

}

if(P3!=0x00) {

if(bian>humi){relay=0;} else{relay=1;} }

if(shui2==0){relay=1;}

}

}

void timer0() interrupt 1 {

TH0=(65536-50000)/256; TL0=(65536-50000)%256; count++;

}