#ifndef __TOU_H__ #define __TOU_H__
#include
//#include
#define uchar unsigned char 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.数据端口
/******** DS1602函数声明 ********/ void LCD_Initial();
void GotoXY(unsigned char x, unsigned char y); void Print(unsigned char *str);
void LCD_Write(bit style, unsigned char input);
/******** SHT10函数声明 ********/ 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);
#endif
/***********************************************************************************************************************************************************/
//SHT10程序(SHT10.c):
//#include
#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_softreset(void)
// resets the sensor by a softreset { unsigned char error=0; s_connectionreset(); //启动连接复位 error+=s_write_byte(RESET); //发送复位命令 return error; //error=1 通讯错误 }
/*读状态寄存器
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum) { 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)