Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
humid_sht.c
Go to the documentation of this file.
1 
9 #include "std.h"
10 #include "LPC21xx.h"
11 #include "mcu_periph/uart.h"
12 #include "messages.h"
14 #include "humid_sht.h"
15 
16 #include "led.h"
17 
18 #ifndef DOWNLINK_DEVICE
19 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
20 #endif
21 
26 
29 void s_transstart(void);
30 void s_connectionreset(void);
31 uint8_t s_read_statusreg(uint8_t *p_value, uint8_t *p_checksum);
33 uint8_t s_measure(uint16_t *p_value, uint8_t *p_checksum, uint8_t mode);
35 uint8_t s_read_measure(uint16_t *p_value, uint8_t *p_checksum);
36 void calc_sht(uint16_t hum, uint16_t tem, float *fhum ,float *ftem);
37 uint8_t humid_sht_reset( void );
38 
39 
41 {
42  uint8_t i, error=0;
43 
44  for (i=0x80;i>0;i/=2) //shift bit for masking
45  {
46  if (i & value) DATA_SET; //masking value with i , write to SENSI-BUS
47  else DATA_CLR;
48  SCK_SET; //clk for SENSI-BUS
49  SCK_SET;SCK_SET;SCK_SET; //pulswith approx. 5 us
50 // _nop_();_nop_();_nop_(); //pulswith approx. 5 us
51  SCK_CLR;
52  }
53  DATA_SET; //release DATA-line
54  SCK_SET; //clk #9 for ack
55  error=DATA_IN; //check ack (DATA will be pulled down by SHT11)
56  SCK_CLR;
57 
58  return error; //error=1 in case of no acknowledge
59 }
60 
62 {
63  uint8_t i,val=0;
64 
65  DATA_SET; //release DATA-line
66  for (i=0x80;i>0;i/=2) //shift bit for masking
67  {
68  SCK_SET; //clk for SENSI-BUS
69  if (DATA_IN) val=(val | i); //read bit
70  SCK_CLR;
71  }
72 
73  if (ack) DATA_CLR; //in case of "ack==1" pull down DATA-Line
74  SCK_SET; //clk #9 for ack
75  SCK_SET;SCK_SET;SCK_SET; //pulswith approx. 5 us
76 // _nop_();_nop_();_nop_(); //pulswith approx. 5 us
77  SCK_CLR;
78  DATA_SET; //release DATA-line
79  return val;
80 }
81 
82 void s_transstart(void)
83 {
84 // generates a transmission start
85 // _____ ________
86 // DATA: |_______|
87 // ___ ___
88 // SCK : ___| |___| |______
89 
90  DATA_SET; SCK_CLR; //Initial state
91 SCK_CLR;// _nop_();
92  SCK_SET;
93 SCK_SET;// _nop_();
94  DATA_CLR;
95 DATA_CLR;// _nop_();
96  SCK_CLR;
97 SCK_CLR;SCK_CLR;SCK_CLR; // _nop_();_nop_();_nop_();
98  SCK_SET;
99 SCK_SET;// _nop_();
100  DATA_SET;
101 DATA_SET;// _nop_();
102  SCK_CLR;
103 }
104 
106 {
107 // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
108 // _____________________________________________________ ________
109 // DATA: |_______|
110 // _ _ _ _ _ _ _ _ _ ___ ___
111 // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
112 
113  uint8_t i;
114 
115  DATA_SET; SCK_CLR; //Initial state
116  for(i=0;i<9;i++) //9 SCK cycles
117  {
118  SCK_SET;
119  SCK_CLR;
120  }
121  s_transstart(); //transmission start
122 }
123 
124 uint8_t s_read_statusreg(uint8_t *p_value, uint8_t *p_checksum)
125 {
126 // reads the status register with checksum (8-bit)
127  uint8_t error=0;
128 
129  s_transstart(); //transmission start
130  error=s_write_byte(STATUS_REG_R); //send command to sensor
131  *p_value=s_read_byte(ACK); //read status register (8-bit)
132  *p_checksum=s_read_byte(noACK); //read checksum (8-bit)
133  return error; //error=1 in case of no response form the sensor
134 }
135 
137 {
138 // writes the status register with checksum (8-bit)
139  uint8_t error=0;
140 
141  s_transstart(); //transmission start
142  error+=s_write_byte(STATUS_REG_W);//send command to sensor
143  error+=s_write_byte(*p_value); //send value of status register
144  return error; //error>=1 in case of no response form the sensor
145 }
146 
147 uint8_t s_measure(uint16_t *p_value, uint8_t *p_checksum, uint8_t mode)
148 {
149 // makes a measurement (humidity/temperature) with checksum
150  uint8_t error=0;
151  uint32_t i;
152 
153  s_transstart(); //transmission start
154  switch(mode){ //send command to sensor
155  case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
156  case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
157  default : break;
158  }
159  for (i=0;i<6665535;i++) if(DATA_IN==0) break; //wait until sensor has finished the measurement
160  if(DATA_IN) error+=1; // or timeout (~2 sec.) is reached
161  *(p_value) = s_read_byte(ACK) << 8; //read the first byte (MSB)
162  *(p_value) |= s_read_byte(ACK); //read the second byte (LSB)
163  *p_checksum = s_read_byte(noACK); //read checksum
164 
165  return error;
166 }
167 
169 {
170 // makes a measurement (humidity/temperature) with checksum
171  uint8_t error=0;
172 
173  s_transstart(); //transmission start
174  switch(mode){ //send command to sensor
175  case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
176  case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
177  default : break;
178  }
179 
180  return error;
181 }
182 
183 uint8_t s_read_measure(uint16_t *p_value, uint8_t *p_checksum)
184 {
185 // reads a measurement (humidity/temperature) with checksum
186  uint8_t error=0;
187 
188  if(DATA_IN) error+=1; //still busy?
189  *(p_value) = s_read_byte(ACK) << 8; //read the first byte (MSB)
190  *(p_value) |= s_read_byte(ACK); //read the second byte (LSB)
191  *p_checksum = s_read_byte(noACK); //read checksum
192 
193  return error;
194 }
195 
196 void calc_sht(uint16_t hum, uint16_t tem, float *fhum ,float *ftem)
197 {
198 // calculates temperature [ C] and humidity [%RH]
199 // input : humi [Ticks] (12 bit)
200 // temp [Ticks] (14 bit)
201 // output: humi [%RH]
202 // temp [ C]
203 
204  const float C1 =-4.0; // for 12 Bit
205  const float C2 = 0.0405; // for 12 Bit
206  const float C3 =-0.0000028; // for 12 Bit
207  const float T1 = 0.01; // for 14 Bit @ 5V
208  const float T2 = 0.00008; // for 14 Bit @ 5V
209  float rh; // rh: Humidity [Ticks] 12 Bit
210  float t; // t: Temperature [Ticks] 14 Bit
211  float rh_lin; // rh_lin: Humidity linear
212  float rh_true; // rh_true: Temperature compensated humidity
213  float t_C; // t_C : Temperature [ C]
214 
215  rh = (float)hum; //converts integer to float
216  t = (float)tem; //converts integer to float
217 
218  t_C=t*0.01 - 39.66; //calc. Temperature from ticks to [°C] @ 3.5V
219  rh_lin=C3*rh*rh + C2*rh + C1; //calc. Humidity from ticks to [%RH]
220  rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. Temperature compensated humidity [%RH]
221  if(rh_true>100)rh_true=100; //cut if the value is outside of
222  if(rh_true<0.1)rh_true=0.1; //the physical possible range
223  *ftem = t_C; //return temperature [ C]
224  *fhum = rh_true; //return humidity[%RH]
225 }
226 
228 {
229 // resets the sensor by a softreset
230  uint8_t error=0;
231 
232  s_connectionreset(); //reset communication
233  error+=s_write_byte(RESET); //send RESET-command to sensor
234 
235  return error; //error=1 in case of no response form the sensor
236 }
237 
238 void humid_sht_init( void )
239 {
240  /* Configure DAT/SCL pin as GPIO */
241 
242 #if (DAT_PIN<16)
243  PINSEL0 &= ~(_BV(DAT_PIN*2)|_BV(DAT_PIN*2+1));
244 #else
245  PINSEL1 &= ~(_BV((DAT_PIN-16)*2)|_BV((DAT_PIN-16)*2+1));
246 #endif
247 
248 #if (SCK_PIN<16)
249  PINSEL0 &= ~(_BV(SCK_PIN*2)|_BV(SCK_PIN*2+1));
250 #else
251  PINSEL1 &= ~(_BV((SCK_PIN-16)*2)|_BV((SCK_PIN-16)*2+1));
252 #endif
253 
254  IO0DIR &= ~(_BV(DAT_PIN));
255  IO0CLR = _BV(DAT_PIN);
256  IO0DIR = _BV(SCK_PIN);
257  IO0CLR = _BV(SCK_PIN);
258 
261 }
262 
263 #if 0
264 void humid_sht_periodic_orig(void)
265 {
266  uint8_t error=0, checksum;
267 
269 
270  error += s_measure(&humidsht, &checksum, HUMI); //measure humidity
271  error += s_measure(&tempsht, &checksum, TEMP); //measure temperature
272 
273  if (error != 0)
274  {
275  s_connectionreset(); //in case of an error: connection reset
276  }
277  else
278  {
279  calc_sht(humidsht, tempsht, &fhumidsht, &ftempsht); //calculate humidity, temperature
281  }
282 }
283 #endif
284 
285 void humid_sht_periodic(void) {
286  uint8_t error=0, checksum;
287 
288  if (humid_sht_status == SHT_IDLE) {
289  /* init humidity read */
293  }
295  /* get data */
296  error += s_read_measure(&humidsht, &checksum);
297 
298  if (error != 0) {
300  s_start_measure(HUMI); //restart
301  LED_TOGGLE(2);
302  }
303  else {
304  error += s_start_measure(TEMP);
306  }
307  }
308  else if (humid_sht_status == SHT_MEASURING_TEMP) {
309  /* get data */
310  error += s_read_measure(&tempsht, &checksum);
311 
312  if (error != 0) {
314  s_start_measure(TEMP); //restart
315  LED_TOGGLE(2);
316  }
317  else {
323  DOWNLINK_SEND_SHT_STATUS(DefaultChannel, DefaultDevice, &humidsht, &tempsht, &fhumidsht, &ftempsht);
325  }
326  }
327 }
unsigned short uint16_t
Definition: types.h:16
#define DATA_CLR
Definition: humid_sht.h:47
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
void humid_sht_init(void)
Definition: humid_sht.c:238
void humid_sht_periodic(void)
Definition: humid_sht.c:285
#define SHT_MEASURING_HUMID
Definition: humid_sht.h:59
#define DAT_PIN
Definition: humid_sht.h:13
#define DATA_IN
Definition: humid_sht.h:49
#define HUMI
Definition: humid_sht.h:28
#define STATUS_REG_W
Definition: humid_sht.h:32
uint8_t s_write_byte(uint8_t value)
Definition: humid_sht.c:40
uint16_t humidsht
Definition: humid_sht.c:22
#define SHT_IDLE
Definition: humid_sht.h:58
#define STATUS_REG_R
Definition: humid_sht.h:34
#define FALSE
Definition: imu_chimu.h:141
#define SCK_PIN
Definition: humid_sht.h:20
uint8_t s_write_statusreg(uint8_t *p_value)
Definition: humid_sht.c:136
#define noACK
Definition: humid_sht.h:25
#define SCK_SET
Definition: humid_sht.h:54
uint8_t s_read_measure(uint16_t *p_value, uint8_t *p_checksum)
Definition: humid_sht.c:183
#define SHT_MEASURING_TEMP
Definition: humid_sht.h:60
uint8_t humid_sht_status
Definition: humid_sht.c:25
uint16_t val[TCOUPLE_NB]
unsigned long uint32_t
Definition: types.h:18
uint8_t s_read_byte(uint8_t ack)
Definition: humid_sht.c:61
uint8_t s_start_measure(uint8_t mode)
Definition: humid_sht.c:168
#define MEASURE_HUMI
Definition: humid_sht.h:38
uint8_t humid_sht_reset(void)
Definition: humid_sht.c:227
#define IO0CLR
Definition: LPC21xx.h:304
#define LED_TOGGLE(i)
Definition: led_hw.h:30
void s_transstart(void)
Definition: humid_sht.c:82
#define TRUE
Definition: imu_chimu.h:144
#define IO0DIR
Definition: LPC21xx.h:303
#define PINSEL0
Definition: LPC21xx.h:315
void calc_sht(uint16_t hum, uint16_t tem, float *fhum, float *ftem)
Definition: humid_sht.c:196
#define MEASURE_TEMP
Definition: humid_sht.h:36
unsigned char uint8_t
Definition: types.h:14
bool_t humid_sht_available
Definition: humid_sht.c:24
uint8_t s_read_statusreg(uint8_t *p_value, uint8_t *p_checksum)
Definition: humid_sht.c:124
#define TEMP
Definition: humid_sht.h:27
#define PINSEL1
Definition: LPC21xx.h:316
#define SCK_CLR
Definition: humid_sht.h:56
#define ACK
Definition: humid_sht.h:26
float ftempsht
Definition: humid_sht.c:23
arch independent LED (Light Emitting Diodes) API
void s_connectionreset(void)
Definition: humid_sht.c:105
#define RESET
Definition: humid_sht.h:40
uint8_t s_measure(uint16_t *p_value, uint8_t *p_checksum, uint8_t mode)
Definition: humid_sht.c:147
float fhumidsht
Definition: humid_sht.c:23
uint16_t tempsht
Definition: humid_sht.c:22
#define DATA_SET
Definition: humid_sht.h:45