📄 sht91_sample_code.lst
字号:
C51 COMPILER V7.20 SHT91_SAMPLE_CODE 04/30/2009 00:50:56 PAGE 1
C51 COMPILER V7.20, COMPILATION OF MODULE SHT91_SAMPLE_CODE
OBJECT MODULE PLACED IN D:\QCXS_D~1\CXSY_W~1\PROJECT\SOURCE~1\V1.0\SHT91_SAMPLE_CODE.OBJ
COMPILER INVOKED BY: D:\programs\keil\C51\BIN\C51.EXE D:\QCXS_D~1\CXSY_W~1\PROJECT\SOURCE~1\V1.0\SHT91_SAMPLE_CODE.C DB
-SB OE
line level source
1 /****************************************************************************
2 * Filename: SHT91_Sample_code
3 * Fi
4 * Prozessor: 80C51 Family
5 * Compiler: Keil
6 * Author: Edit by QCX,Originally from www.humidity.cn
7 *************************************************************************/
8
9
10 //-------------------------------------------------------------------------
11
12 //--------------------------------------------------------------------------
13 char s_write_byte(unsigned char value)
14 //--------------------------------------------------------------------------
15 //writes a byte on the Sensibu and checks the acknowledge
16 {
17 1 unsigned char i,error=0;
18 1 for (i=0x80;i>0;i/=2) //shift bit for masking
19 1 {
20 2 if(i & value) DATA=1; //masking value with i,write every bit of value
*** ERROR C202 IN LINE 20 OF D:\QCXS_D~1\CXSY_W~1\PROJECT\SOURCE~1\V1.0\SHT91_SAMPLE_CODE.C: 'DATA': undefined identifie
-r
21 2 else DATA=0; //to SENSI-BUS
*** ERROR C202 IN LINE 21 OF D:\QCXS_D~1\CXSY_W~1\PROJECT\SOURCE~1\V1.0\SHT91_SAMPLE_CODE.C: 'DATA': undefined identifie
-r
22 2 SCK=1; //clk for SENSI-BUS
*** ERROR C202 IN LINE 22 OF D:\QCXS_D~1\CXSY_W~1\PROJECT\SOURCE~1\V1.0\SHT91_SAMPLE_CODE.C: 'SCK': undefined identifier
23 2 _nop_ (); //The frequancy of the clk should be less than
24 2 _nop_ (); //4k Hz
25 2 _nop_ ();
26 2 SCK=0;
27 2 }
28 1 DATA=1;
29 1 SCK=1;
30 1 error=DATA;
31 1 SCK=0;
32 1 return error; //error=1 in case of no acknowledge
33 1 }
34
35 //-----------------------------------------------------------------------
36 char s_read_byte(unsigned char ack)
37 //-----------------------------------------------------------------------
38 //func: read a byte from Sens-bus and gives an acknowledge
39 {
40 1 unsigned char i,val=0;
41 1 for(i=0x80;i>0;i/=2)
42 1 {
43 2 SCK=1;
44 2 if (DATA) val=(val | i);
45 2 SCK=0;
46 2 }
47 1 DATA= !ack; //in case of "ack==1" pull down DATA-Line
48 1 SCK=1;
49 1 _nop_();
C51 COMPILER V7.20 SHT91_SAMPLE_CODE 04/30/2009 00:50:56 PAGE 2
50 1 _nop_(); _nop_();
51 1 DATA=1;
52 1 return val;
53 1 }
54
55 //-------------------------------------------------------------------------
56 void s_transstart(void)
57 //-------------------------------------------------------------------------
58 //func: generate a transmission start signal
59 {
60 1 DATA=1;
61 1 SCK=0; //initial state
62 1 _nop_();
63 1 SCK=1;
64 1 _nop_();
65 1 DATA=0;
66 1 _nop_();
67 1 SCK=0;
68 1 _nop_();
69 1 _nop_();
70 1 _nop_();
71 1 SCK=1;
72 1 _nop_();
73 1 DATA=1;
74 1 _nop_();
75 1 SCK=0; // refer to the pdf from SENSIRION(start transmission)
76 1 }
77
78 //------------------------------------------------------------------------------
79 void s_connectionreset(void)
80 //------------------------------------------------------------------------------
81 //func: communication reset:DATA-line and at least 9 sck cycles followed by transstart
82 {
83 1 unsigned char i;
84 1 DATA=1;
85 1 SCK=0;
86 1 for(i=0;i<9;i++)
87 1 {
88 2 SCK=1;
89 2 SCK=0;
90 2 }
91 1 s_transstart();
92 1 }
93
94 //--------------------------------------------------------------------------------
95 char s_softreset(void)
96 //--------------------------------------------------------------------------------
97 //func: reset the sensor by a softreset
98 {
99 1 unsigned char error=0;
100 1 s_connectionreset(); //reset communication
101 1 error+=s_write_byte(RESET); //send RESET -command to sensor
102 1 return error; //sensor=1 in case of no response from sensor
103 1 }
104
105 //-----------------------------------------------------------------------------------
106 char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
107 //-----------------------------------------------------------------------------------
108 //func: read the status register with checksum (8 bit)
109 {
110 1 unsigned char error=0;
111 1 s_transstart(); //transmission start
C51 COMPILER V7.20 SHT91_SAMPLE_CODE 04/30/2009 00:50:56 PAGE 3
112 1 error=s_write_byte(STATUS_REG_R); //send command to sensor
113 1 *p_value=s_read_byte(ACK); //read status register(8 bit)
114 1 *p_checksum=s_read_byte(noACK); //read checksum (8 bit)
115 1 return error; //error=1 in case of no response from sensor
116 1 }
117
118 //----------------------------------------------------------------------------------------
119 char s_write_statusreg(unsigned char *p_value)
120 //----------------------------------------------------------------------------------------
121 //func: writes the status register with checksum(8 bit)
122 {
123 1 unsigned char error=0;
124 1 s_transstart(); //transmission start
125 1 error+=s_write_byte(STATUS_REG_W); //send command to sensor
126 1 error+=s_write_byte(*p_value); //send value of status register
127 1 return error; //error >= 1 in case of no response from sensor
128 1 }
129
130 //-----------------------------------------------------------------------------------------
131 char s_measure(unsigned char *p_value,unsigned char *p_checksum,unsigned char mode)
132 //-----------------------------------------------------------------------------------------
133 //func: make a measurement(temperature or humity) with checksum
134 {
135 1 unsigned char error=0;
136 1 unsigned int i;
137 1 s_transstart(); //start transmission
138 1 switch(mode) //send command to sensor
139 1 {
140 2 case TEMP: error+=s_write_byte(MEASURE_TEMP);break;
141 2 case HUMI: error+=s_write_byte(MEASURE_HUMI);break;
142 2 default: break;
143 2 }
144 1 for(i=0;i<65535;i++)
145 1 if(DATA==0) break;
146 1 if(DATA==1) error+=1;
147 1 *(p_value)=s_read_byte(ACK); //read the first byte
148 1 *(p_value+1) = s_read_byte(ACK); //read the second byte
149 1 *p_checksum = s_read_byte(noACK); //read checksum
150 1 return error;
151 1 }
152
153 //-----------------------------------------------------------------------------------------------
154 void calc_sth(float *p_humidity,float *p_temperature)
155 //-----------------------------------------------------------------------------------------------
156 //func: calculates temperature and humidity
157 {
158 1 const float C1=-4.0;
159 1 const float C2=0.0405;
160 1 const float C3=-0.0000028;
161 1 const float T1=0.01;
162 1 const float T2=0.00008;
163 1 float rh=*p_humidity;
164 1 float t=*p_temperature;
165 1 float rh_lin;
166 1 float rh_ture;
167 1 float t_C;
168 1
169 1 t_C=t*0.01 - 40;
170 1 rh_lin=C3*rh*rh + C2*rh + C1;
171 1 rh_ture=(t_C-25)*(T1+T2*rh)+rh_lin;
172 1 if(rh_ture > 100) rh_ture=100;
173 1 if(rh_ture < 0.1) rh_ture=0.1;
C51 COMPILER V7.20 SHT91_SAMPLE_CODE 04/30/2009 00:50:56 PAGE 4
174 1
175 1 *p_temperature=t_C;
176 1 *p_humidity=rh_ture;
177 1 }
178
179 //--------------------------------------------------------------------------------------------------
180 float calc_dewpoint(float h,float t)
181 //----------------------------------------------------------------------------------------------------
182 //func: calculates the dewpoint
183 //input: humidity[%RH] , temperature[C]
184 //output: dew point
185 {
186 1 float k,dew_point;
187 1 k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);
188 1 dew_point = 243.12 * k/(17.62-k);
189 1 return dew_point;
190 1 }
191
192 //------------------------------------------------------------------------------------------------------
193 void main()
194 //------------------------------------------------------------------------------------------------------
195 //sample program that shows how to use SHT** sensor to measure humidity/temperature
196 //1.connection reset
197 //2.measure humidity and temperature
198 //3.calculate humidity and temperature
199 //4.calculate dew_point
200 //5.print temperature , humidity , dew point
201 {
202 1 value humi_val,temp_val;
203 1 float dew_point;
204 1 unsigned char error,checksum;
205 1 unsigned int i;
206 1
207 1 s_connectionreset();
208 1 while(1)
209 1 {
210 2 error=0;
211 2 error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
212 2 error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
213 2 if(error != 0) s_connectionreset;
214 2 else
215 2 {
216 3 humi_val.f=(float)humi_val.i; //convert integer to float
217 3 temp_val.f=(float)temp_val.i;
218 3 calc_sth(&humi_val.f,&temp_val.f); //calculate humidity and temperature
219 3 dew_point=calc_dewpoint(humi_val.f,temp_val.f); // calculate dewpoint
220 3 printf("temp:%5.1fC humi:%5.1f %% dewpoint:%5.1fC\n",temp_val.f,humi_val.f,dew_point);
221 3 }
222 2 //---------------------wait approximiatelly 0.8s to avoid heating up sensor--------------------
223 2 for(i=0;i<4000;i++) ;
224 2 //------------------------------------------------------------------------------------------------
225 2 }
226 1 }
227
228
229
230
231
232
233
C51 COMPILATION COMPLETE. 0 WARNING(S), 3 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -