📄 shtxx_self.c
字号:
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
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)
{
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_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
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 init_uart()
//----------------------------------------------------------------------------------
//9600 bps @ 11.059 MHz
{SCON = 0x52;
TMOD = 0x20;
TCON = 0x69;
TH1 = 0xfd;
}
//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [癈] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [癈]
{ const float C1=-4.0; // for 12 Bit
const float C2=+0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=+0.01; // for 14 Bit @ 5V
const float T2=+0.00008; // for 14 Bit @ 5V
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [癈]
t_C=t*0.01 - 40; //calc. temperature from ticks to [癈]
rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*p_temperature=t_C; //return temperature [癈]
*p_humidity=rh_true; //return humidity[%RH]
}
//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [癈]
// output: dew point [癈]
{ float logEx,dew_point;
logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
}
void cal_temp_humi(void) //计算温度和湿度
{
value humi_val,temp_val;
unsigned char error,checksum;
unsigned int i = 0;
// init_uart();
s_connectionreset();
// while(1)
// {
error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
if(error!=0) s_connectionreset(); //in case of an error: connection reset
else
{
humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
// temp_s = (uint)temp_val.f;
// humi_s = (uint)humi_val.f;
b_data.temp = (uint)temp_val.f;
b_data.humi = (uint)humi_val.f;
// dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
// printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n",temp_val.f,humi_val.f,dew_point);
}
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
// for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!)
//-----------------------------------------------------------------------------------
}
void cal_basic(void)
{
lcd_read_time();
cal_temp_humi();
}
void main()
{
/*
P0 = 0XFF;
P1 = 0XFF;
P2 = 0XFF;
P3 = 0XFF;
P0 = 0X00;
P1 = 0X00;
P2 = 0X00;
P3 = 0X00;
*/
uchar i = 0;
uchar v = 0;
uchar keydata = 0;
uint dat_ad = 0;
uchar num_ad = 0;
float dat_ad_xian = 0;
lcd_off();
lcd_on();
lcd_ini();
// lcd_wrc(0x80);
/*
for(i=0;i<60;i++)
{
lcd_wrd(0x00+i);
}
*/
delay(100);
lcd_add(1,1);
for(i=0;i<4;i++)
{
lcd_dis_ch(0xb5,0xa1+i);
}
// lcd_dis_ch(0xb5,0xa1);
// lcd_dis_ch(0xb5,0xa1);
// lcd_dis_ch(0xb5,0xa1);
delay(200);
// i=lcd_read_ac();
// i=lcd_read_ac();
i=lcd_read_ac();
// delay(200);
lcd_dis_num(i);
lcd_dis_num(12458);
// I2C
WP = 0;
// write to rom
/*
lcd_add(3,1);
i = ISendStr_16(0xa0,0x0000,19);
lcd_wrd(0x30+i);
*/
/*
lcd_add(4,1);
for(v=0x0000;v<8;v++)
{
i = 0;
while(i == 0)
{
i = ISendStr_16(0xa0,v,3*v);
}
lcd_wrd(0x30+(uchar)i);
}
*/
// read rom set address
/*
lcd_add(3,1);
for(v=0x0000;v<8;v++)
{
i = IRcvStr_16(0xa0,v);
// lcd_wrc(0x88+2*v);
lcd_dis_num(i);
}
*/
// set_time(30,13,21,1,12,3,7); //时钟设置sec, min, hr, dy, dt, mn, yr
//ad中断相关
/*
// IE = IE&0X81; //1000 0001
EA = 0;
EX0 = 1;
// TCON
IT0 = 1;
*/
/*
lcd_add(4,1);
lcd_dis_num_1(12589);
lcd_add(4,2);
lcd_dis_num_2(12589);
lcd_add(4,4);
lcd_dis_num_3(12589);
*/
while(1)
{
// cal_temp_humi();
cal_basic(); ////计算温度和湿度年月日
lcd_add(1,1);
lcd_dis_num_2(20);
lcd_dis_num_2((uint)b_data.year);
lcd_dis_ch(0xc4,0xea); //uchar h,uchar l);
lcd_wrd(0x20);
lcd_wrd(0x20);
lcd_add(1,5);
lcd_dis_num_2((uint)b_data.month);
lcd_dis_ch(0xd4,0xc2);
lcd_add(1,7);
lcd_dis_num_2((uint)b_data.date);
lcd_dis_ch(0xc8,0xd5);
lcd_add(2,1);
lcd_dis_ch(0xd0,0xc7);
lcd_dis_ch(0xc6,0xda);
lcd_dis_num_2((uint)b_data.day);
lcd_add(2,5);
lcd_dis_num_2((uint)b_data.hour);
lcd_wrd(0x3a);
// lcd_add(2,6);
lcd_dis_num_2((uint)b_data.minute);
lcd_wrd(0x3a);
// lcd_add(2,8);
lcd_dis_num_2((uint)b_data.second);
lcd_add(3,1);
lcd_dis_ch(0xca,0xaa);
lcd_dis_ch(0xb6,0xc8);
lcd_wrd(0x3a);
lcd_add(3,4);
lcd_dis_num_2((uint)b_data.humi);
// lcd_wrd(0x25);
lcd_dis_ch(0xa3,0xa5);
lcd_add(3,7);
lcd_dis_ch(0xc7,0xeb);
lcd_dis_ch(0xb0,0xb4);
lcd_add(4,1);
lcd_dis_ch(0xce,0xc2);
lcd_dis_ch(0xb6,0xc8);
lcd_wrd(0x3a);
lcd_add(4,4);
lcd_dis_num_2((uint)b_data.temp);
lcd_dis_ch(0xa1,0xe6);
lcd_add(4,7);
lcd_dis_ch(0xc8,0xa1);
lcd_dis_ch(0xcf,0xfa);
// ldelay(200);
ldelay(200);
// lcd_dis_time(); //时钟显示函数
//测试n次
// i = ad_ce_n();
// ldelay(80);
/*
//ad测试
dat_ad = ad_ce();
lcd_en();
lcd_add(2,1);
lcd_dis_num(dat_ad);
dat_ad_xian = (float)(dat_ad*1.28/2.048);
lcd_dis_num((uint)dat_ad_xian);
ldelay(80);
/*
//ad读
if(sta == 0)
{
P0 = 0XFF;
delay(50);
if(sta == 0)
{
// P0 = 0XFF;
lben = 0;
hben = 1;
ad_en();
_nop_();
i = P0;
ce = 1;
_nop_();
hben = 0;
lben = 1;
ad_en();
_nop_();
v = P0&0x0f;
lcd_en();
lcd_add(2,1);
lcd_dis_num(v);
lcd_dis_num(i);
dat_ad = v*256+i;
lcd_dis_num(dat_ad);
dat_ad_xian = (float)(dat_ad*1.28/2.048);
// dat_ad_xian = (float)(dat_ad/2.048);
lcd_dis_num((uint)dat_ad_xian);
// v = P0&0X30;
// v = v>>4;
// lcd_dis_num(v);
ldelay(80);
}
}
//ad读结束
*/
/* keydata = key_data();
lcd_add(4,7);
switch(keydata)
{
case 0:
{
v = 3;
lcd_dis_num(v);
break;
}
case 1:
{
v = 4;
lcd_dis_num(v);
break;
}
case 2:
{
v = 5;
lcd_dis_num(v);
break;
}
case 3:
{
v = 6;
lcd_dis_num(v);
break;
}
case 4:
{
v = 2;
lcd_dis_num(v);
break;
}
case 5:
{
v = 7;
lcd_dis_num(v);
break;
}
case 6:
{
v = 8;
lcd_dis_num(v);
break;
}
case 7:
{
v = 9;
lcd_dis_num(v);
break;
}
case 8:
{
v = 1;
lcd_dis_num(v);
break;
}
case 9:
{
v = 10;
lcd_dis_num(v);
break;
}
case 10:
{
v = 11;
lcd_dis_num(v);
break;
}
case 11:
{
v = 12;
lcd_dis_num(v);
break;
}
case 12:
{
v = 0;
lcd_dis_num(v);
break;
}
case 13:
{
v = 13;
lcd_dis_num(v);
break;
}
case 14:
{
v = 14;
lcd_dis_num(v);
break;
}
case 15:
{
v = 15;
lcd_dis_num(v);
break;
}
default:
{
v = 16;
lcd_dis_num(v);
break;
}
}
*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -