📄 sht10.h
字号:
#include <intrins.h>
#include <stdio.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
uchar a,b,c,d,e,f,g,h;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SHT10 相关控制信号及变量
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
enum {TEMP,HUMI};
sbit DATA = P2^1;
sbit SCK = P2^0;
unsigned char Tem[2];
unsigned char Hum[2];
uchar error;
////////////////////////////////////////////////
//温湿度传感器地址定义
///////////////////////////////////////////////
#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
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SHT10 温湿度读字节子程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char s_write_byte(unsigned char value)
{
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
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SHT10 温湿度写字节子程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char s_read_byte(unsigned char ack)
{
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;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SHT10 温湿度传输其始子程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void s_transstart(void)
{
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;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SHT10 温湿度连接复位子程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void s_connectionreset(void)
{
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
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SHT10 温湿度获取温度或湿度子程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
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 ShowTem(uchar *ptem,uchar *phum)
{
int ntemp,t1,t0,aa;
int nhum;
float flt1;
ntemp= (*ptem)*256+ (*(ptem+1));
t1=(ntemp)/100-40;
t0=(ntemp)%100;
a=t1/10;b=t1%10;
c=t0/10;d=t0%10;
nhum = (*phum)*256+ (*(phum+1));
flt1=0.0405*nhum-0.0000028*nhum*nhum-4;
aa=flt1*100;
e=aa/1000;
f=aa%1000/100;
g=aa%1000%100/10;
h=aa%10;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -