源代码:
#include<reg52.h>
#include <intrins.h>
#include <math.h> //Keil library
#include <stdio.h> //Keil library
#define uchar unsigned char
typedef union //定义共同类型
{ unsigned int i; //i表示测量得到的温湿度数据(int 形式保存的数据)
float f; //f表示测量得到的温湿度数据(float 形式保存的数据)
} value;
enum {TEMP,HUMI};
sbit DATA = P1^1;
sbit SCK = P1^0;
sbit RS = P2^0;
sbit RW = P2^1;
sbit E = P2^2;
sfr DBPort = 0x80; //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口
/******** 1602函数声明 ********/
void LCD_Initial();
void GotoXY(unsigned char x, unsigned char y);
void Print(unsigned char *str);
void LCD_Write(bit style, unsigned char input);
/******** SHT11函数声明 ********/
void s_connectionreset(void);
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);
/***********************************************************************************************************************************************************/
//SHT11程序
#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/=2) //shift bit for masking 高位为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)
// generates a tr