Note: Lower versions of Keil software cannot be compiled successfully because they cannot handle floating-point operations.
#include
#include
#define uint unsigned int
#define uchar unsigned char
#define noACK 0
#define ACK 1
#define STATUS_REG_W 0x06
#define STATUS_REG_R 0x07
#define MEASURE_TEMP 0x03
#define MEASURE_HUMI 0x05
#define RESET 0x1e
enum {TEMP,HUMI};
typedef union //Defines a shared type
{
unsigned int i;
float f;
} value;
sbit lcdrs=P2^0;
sbit lcdrw=P2^1;
sbit lcden=P2^2;
sbit SCK = P1^0;
sbit DATA = P1^1;
uchar table2[]="SHT11 temperature and humidity detection";
uchar table3[]="Temperature: ℃";
uchar table4[]="Humidity is:";
uchar table5[]=".";
uchar wendu[6];
uchar shidu[6];
void delay(int z)
{
int x,y;
for(x=z;x>0;x--)
for(y=125;y>0;y--);
}
void delay_50us(uint t)
{
uint j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
void delay_50ms(uint t)
{
uint j;
for(;t>0;t--)
for(j=6245;j>0;j--);
}
void write_12864com(uchar com)
{
lcdrs=0;
lcdrw=0;
delay_50us(1);
P0=com;
lcden=1;
delay_50us(10);
lcden=0;
delay_50us(2);
}
void write_dat(uchar dat)
{
lcdrs=1;
lcdrw=0;
delay_50us(1);
P0=dat;
lcden=1;
delay_50us(10);
lcden=0;
delay_50us(2);
}
void init12864lcd(void)
{
delay_50ms(2);
write_12864com(0x30);
delay_50us(4);
write_12864com(0x30);
delay_50us(4);
write_12864com(0x0f);
delay_50us(4);
write_12864com(0x01);
delay_50us(240);
write_12864com(0x06);
delay_50us(10);
write_12864com(0x0c);
delay_50us(10);
}
void display1(void)
{
uchar i;
write_12864com(0x80);
for(i=0;i<18;i++)
{
write_dat(table2[i]);
delay_50us(1);
}
}
void display2(void)
{
uchar i;
write_12864com(0x90);
for(i=0;i<18;i++)
{
write_dat(table3[i]);
delay_50us(1);
}
}
void display3(void)
{
uchar i;
write_12864com(0x88);
for(i=0;i<8;i++)
{
write_dat(table4[i]);
delay_50us(1);
}
}
void displaywendu(void)
{
uchar i;
write_12864com(0x94);
for(i=0;i<3;i++)
{
write_dat(wendu[i]);
delay_50us(1);
}
for(i=0;i<1;i++)
{
write_dat(table5[i]);
delay_50us(1);
}
for(i=4;i<5;i++)
{
write_dat(wendu[i]);
delay_50us(1);
}
}
void displayshidu(void)
{
uchar i;
write_12864com(0x8C);
for(i=0;i<3;i++)
{
write_dat(shidu[i]);
delay_50us(1);
}
for(i=0;i<1;i++)
{
write_dat(table5[i]);
delay_50us(1);
}
for(i=4;i<5;i++)
{
write_dat(shidu[i]);
delay_50us(1);
}
}
//Write byte program
char s_write_byte(unsigned char value)
{
unsigned char i,error=0;
for (i=0x80;i>0;i>>=1) //The high bit is 1, and the circular right shift
{
if (i&value) DATA=1; // AND the number to be sent, the result is the bit to be sent
else DATA=0;
SCK=1;
_nop_();_nop_();_nop_(); //Delay 3us
SCK=0;
}
DATA=1; //Release data line
SCK=1;
error=DATA; //Check the response signal to confirm that the communication is normal
_nop_();_nop_();_nop_();
SCK=0;
DATA=1;
return error; //error=1 Communication error
}
//Read byte program
char s_read_byte(unsigned char ack)
{
unsigned char i,val=0;
DATA=1; //Release data line
for(i=0x80;i>0;i>>=1) //The high bit is 1, and the loop moves right
{
SCK=1;
if(DATA) val=(val|i); //Read the value of a data line
SCK=0;
}
DATA=!ack; //If it is verification, end the communication after reading;
SCK=1;
_nop_();_nop_();_nop_(); //Delay 3us
SCK=0;
_nop_();_nop_();_nop_();
DATA=1; //Release data line
return val;
}
//Start the transfer
void s_transstart(void)
{
DATA=1; SCK=0; //Prepare
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
_nop_();_nop_();_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}
//Reset the connection
void s_connectionreset(void)
{
unsigned char i;
DATA=1; SCK=0; //Prepare
for(i=0;i<9;i++) //DATA remains high, SCK clock triggers 9 times, sending starts transmission, communication is reset
{
SCK=1;
SCK=0;
}
s_transstart(); //Start transmission
}
//Soft reset procedure
char s_softreset(void)
{
unsigned char error=0;
s_connectionreset(); //Start connection reset
error+=s_write_byte(RESET); //Send reset command
return error; //error=1 Communication error
}
//Temperature and humidity measurement
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
unsigned int i;
s_transstart(); //Start transmission
switch(mode) //Select the send command
{
case TEMP : error+=s_write_byte(MEASURE_TEMP); break; //Measure temperature
case HUMI : error+=s_write_byte(MEASURE_HUMI); break; //Measure humidity
default : break;
}
for (i=0;i<65535;i++) if(DATA==0) break; //Wait for the measurement to end
if(DATA) error+=1; // If the data line is not pulled low for a long time, it means the measurement is wrong
*(p_value) =s_read_byte(ACK); //Read the first byte, high byte (MSB)
*(p_value+1)=s_read_byte(ACK); //Read the second byte, low byte (LSB)
*p_checksum =s_read_byte(noACK); //read CRC checksum
return error; // error=1 Communication error
}
// Temperature and humidity value scale conversion and temperature compensation
void calc_sth10(float *p_humidity, float *p_temperature)
{
const float C1=-4.0; // 12-bit humidity accuracy correction formula
const float C2=+0.0405; // 12-bit humidity accuracy correction formula
const float C3=-0.0000028; // 12-bit humidity accuracy correction formula
const float T1=+0.01; // 14-bit temperature accuracy 5V condition correction formula
const float T2=+0.00008; // 14-bit temperature accuracy 5V condition correction formula
float rh=*p_humidity; // rh: 12-bit humidity
float t=*p_temperature; // t: 14-bit temperature
float rh_lin; // rh_lin: humidity linear value
float rh_true; // rh_true: humidity true value
float t_C; // t_C : temperature in °C
t_C=t*0.01 - 40; //compensation temperature
rh_lin=C3*rh*rh + C2*rh + C1; //Relative humidity nonlinear compensation
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //Relative humidity temperature dependence compensation
if(rh_true>100)rh_true=100; //maximum humidity correction
if(rh_true<0.1)rh_true=0.1; //minimum humidity correction
*p_temperature=t_C; //Return temperature result
*p_humidity=rh_true; //Return humidity result
}
void main(void)
{
unsigned int temp,humi;
value humi_val,temp_val; //Define two communities, one for humidity and one for temperature
unsigned char error; //Used to check if an error occurs
unsigned char checksum; //CRC
init12864lcd();
display1();
display2();
display3();
s_connectionreset(); //Start connection reset
while(1)
{
error=0; //Initialize error=0, that is, no error
error+=s_measure((unsigned char*)&temp_val.i,&checksum,TEMP); //temperature measurement
error+=s_measure((unsigned char*)&humi_val.i,&checksum,HUMI); //humidity measurement
if(error!=0) s_connectionreset(); ////If an error occurs, the system is reset
else
{
humi_val.f=(float)humi_val.i; //Convert to floating point number
temp_val.f=(float)temp_val.i; //Convert to floating point number
calc_sth10(&humi_val.f,&temp_val.f); //Correction of relative humidity and temperature
temp=temp_val.f*10;
humi=humi_val.f*10;
wendu[0]=temp/1000+'0'; //temperature in hundreds
wendu[1]=temp00/100+'0'; //Temperature tens digit
wendu[2]=temp0/10+'0'; //Temperature units
wendu[3]=0x2E; //decimal point
wendu[4]=temp+'0'; //The first decimal place of temperature
displaywendu();
shidu[0]=humi/1000+'0'; //humidity in hundreds
shidu[1]=humi00/100+'0'; //humidity tens digit
shidu[2]=humi0/10+'0'; //Humidity units
shidu[3]=0x2E; //decimal point
shidu[4]=humi+'0'; //The first decimal place of humidity
displayshidu();
}
delay(800); //Wait long enough for the next conversion
}
}