📄 sht11.c
字号:
#define wenshidu_c
#include "wsfw.h"
#define SETDATA( ) PORTG|=1<<PG3
#define CLRDATA( ) PORTG&=~(1<<PG3)
#define SETSCK( ) PORTG|=1<<PG4
#define CLRSCK( ) PORTG&=~(1<<PG4)
//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
#ifdef wenshidu_c
//volatile uint shixunhuan;
volatile uchar wendu1;
volatile uchar wendu2;
volatile uchar shidu1;
volatile uchar shidu2;
volatile uchar wenxiao;
volatile uchar shixiao;
volatile uint wenduint;
volatile uint shiduint;
volatile double wenduf1;
volatile double shiduf1;
volatile double wenduf2;
volatile double shiduf2;
volatile double wenduf3;
volatile double shiduf3;
volatile double *weng;
volatile double *shig;
volatile uchar wendu3;
volatile uchar wendu4;
volatile uchar shidu3;
volatile uchar shidu4;
volatile uchar error,checksum;
//volatile uchar tongdao;
volatile double rh_lin;
volatile double rh_true;
volatile double t_C;
#endif
#ifdef wenshidu_c
char s_write_byte(unsigned char value);
char s_read_byte( );
void s_transstart(void);
void s_connectionreset(void);
char s_softreset(void);
char s_read_statusreg( );
char s_write_statusreg(unsigned char *p_value);
char s_measurewen( );
char s_measureshi( );
void calc_sth11( );
void adad();
void SHTyanshi( );
#else
extern char s_write_byte(unsigned char value);
extern char s_read_byte(unsigned char ack);
extern void s_transstart(void);
extern void s_connectionreset(void);
extern char s_softreset(void);
extern char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum);
extern char s_write_statusreg(unsigned char *p_value);
extern void s_measurewen( );
extern void s_measureshi( );
extern void calc_sth11( );
extern void adad();
extern void SHTyanshi( );
#endif
char s_write_byte(unsigned char value)
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2)
{ if (i&value)
{
SETDATA( );
}
else
{
CLRDATA( );
}
SETSCK( );
SHTyanshi( );
CLRSCK( );
}
SETDATA( );
SHTyanshi( );
SETSCK( );
SHTyanshi( );
DDRG&=~(1<<PG3);
abb: if((PING&0x08)==0x00)error+=1;
else goto abb;
DDRG|=(1<<PG3);
CLRSCK( );
SHTyanshi( );
return error;
}
char s_read_byte( )
{
unsigned char i,val=0;
SETDATA( );
SHTyanshi( );
for (i=0x80;i>0;i/=2)
{
SETSCK( );
SHTyanshi( );
DDRG&=~(1<<PG3);
if((PING&0x08)==0x08)val=(val|i);
DDRG|=(1<<PG3);
CLRSCK( );
SHTyanshi( );
}
SHTyanshi( );
CLRDATA( );
SHTyanshi( );
SETSCK( );
SHTyanshi( );
CLRSCK( );
SHTyanshi( );
SETDATA( );
return val;
}
void s_transstart(void)
{
SETDATA( );SHTyanshi( );
CLRSCK( ); SHTyanshi( );
SETSCK( ); SHTyanshi( );
CLRDATA( );SHTyanshi( );
CLRSCK( ); SHTyanshi( );
SETSCK( ); SHTyanshi( );
SETDATA( );SHTyanshi( );
CLRSCK( );
}
void s_connectionreset(void)
{
unsigned char i;
SETDATA( ); SHTyanshi( ); CLRSCK( );
for(i=0;i<9;i++)
{ SETSCK( );
SHTyanshi( );
SHTyanshi( );
CLRSCK( );
}
s_transstart();
}
char s_softreset(void)
{
unsigned char error=0;
s_connectionreset();
error+=s_write_byte(RESET);
return error;
}
char s_read_statusreg( )
{
unsigned char error=0;
s_transstart(); //transmission start
error=s_write_byte(STATUS_REG_R); //send command to sensor
wendu1=s_read_byte( ); //read status register (8-bit)
wenxiao=s_read_byte( ); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}
char s_write_statusreg(unsigned char *p_value)
{
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_measurewen( )
{
unsigned error=0;
unsigned char wan;
s_transstart(); //transmission start
error+=s_write_byte(MEASURE_TEMP);
DDRG&=~(1<<PG3);
abb: if((PING&0x08)==0x00)error+=1;
else goto abb;
DDRG|=(1<<PG3);
wendu1 =s_read_byte( ); //read the first byte (MSB)
wendu2=s_read_byte( ); //read the second byte (LSB)
wenxiao =s_read_byte( ); //read checksum
wenduint=wendu1*256+wendu2;
return error;
}
char s_measureshi( )
{
unsigned error=0;
unsigned char wan;
s_transstart();
error+=s_write_byte(MEASURE_HUMI);
DDRG&=~(1<<PG3);
abb: if((PING&0x08)==0x00)error+=1;
else goto abb;
DDRG|=(1<<PG3);
shidu1 =s_read_byte( ); //read the first byte (MSB)
shidu2=s_read_byte( ); //read the second byte (LSB)
shixiao =s_read_byte( ); //read checksum
shiduint=shidu1*256+shidu2;
//shiduint=65040;
return error;
}
void calc_sth11( )
{
const double C1=-4.0;
const double C2=+0.0405;
const double C3=-0.0000028;
const double T1=+0.01;
const double T2=+0.00008;
t_C=wenduf1*0.01 - 40;
rh_lin=C3*shiduf1*shiduf1 + C2*shiduf1 + C1;
rh_true=(t_C-25)*(T1+T2*shiduf1)+rh_lin;
if(rh_true>100)rh_true=100;
if(rh_true<0.1)rh_true=0.1;
wenduf2=t_C;
shiduf2=rh_true;
}
void adad()
{
s_connectionreset();
error=0;
error+=s_measureshi( );
error+=s_measurewen( );
{
shiduf1=(double)shiduint;
wenduf1=(double)wenduint;
calc_sth11( );
wendu3=(uint)wenduf2;
wenduf3=modf(wenduf2,weng);
wendu4=(uchar)(wenduf3*10);
wendu=wendu3*10+wendu4;
shidu=(uint)shiduf2;
}
}
void SHTyanshi( )
{
asm("nop");asm("nop");asm("nop");asm("nop");
asm("nop");asm("nop");asm("nop");asm("nop");
asm("nop");asm("nop");asm("nop");asm("nop");
asm("nop");asm("nop");asm("nop");asm("nop");
asm("nop");asm("nop");asm("nop");asm("nop");
asm("nop");asm("nop");asm("nop");asm("nop");
asm("nop");asm("nop");asm("nop");asm("nop");
asm("nop");asm("nop");asm("nop");asm("nop");
asm("nop");asm("nop");asm("nop");asm("nop");
asm("nop");asm("nop");asm("nop");asm("nop");
asm("nop");asm("nop");asm("nop");asm("nop");
asm("nop");asm("nop");asm("nop");asm("nop");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -