Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
humid_sht.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2014 The Paparazzi team
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
31 #include "std.h"
32 #include "mcu_periph/gpio.h"
33 #include "mcu_periph/uart.h"
34 #include "messages.h"
36 #include "humid_sht.h"
37 
38 //#include "led.h"
39 
40 #define noACK 0
41 #define ACK 1
42 #define TEMP 0
43 #define HUMI 1
44 
45 //adr command r/w
46 //000 0011 0
47 #define STATUS_REG_W 0x06
48 //000 0011 1
49 #define STATUS_REG_R 0x07
50 //000 0001 1
51 #define MEASURE_TEMP 0x03
52 //000 0010 1
53 #define MEASURE_HUMI 0x05
54 //000 1111 0
55 #define RESET 0x1e
56 
57 
58 #define SHT_IDLE 0
59 #define SHT_MEASURING_HUMID 1
60 #define SHT_MEASURING_TEMP 2
61 
62 #if !defined SHT_DAT_GPIO || !defined SHT_SCK_GPIO
63 #error You need to define SHT_DAT_GPIO and SHT_SCK_GPIO
64 #endif
65 
67 #define DATA_SET gpio_setup_input(SHT_DAT_GPIO)
68 #define DATA_CLR gpio_setup_output(SHT_DAT_GPIO)
70 #define DATA_IN gpio_get(SHT_DAT_GPIO)
72 
74 #define SCK_SET gpio_set(SHT_SCK_GPIO)
75 #define SCK_CLR gpio_clear(SHT_SCK_GPIO)
77 
78 
83 
86 void s_transstart(void);
87 void s_connectionreset(void);
88 uint8_t s_read_statusreg(uint8_t *p_value, uint8_t *p_checksum);
90 uint8_t s_measure(uint16_t *p_value, uint8_t *p_checksum, uint8_t mode);
92 uint8_t s_read_measure(uint16_t *p_value, uint8_t *p_checksum);
93 void calc_sht(uint16_t hum, uint16_t tem, float *fhum ,float *ftem);
94 uint8_t humid_sht_reset( void );
95 
96 
98 {
99  uint8_t i, error=0;
100 
101  for (i=0x80;i>0;i/=2) //shift bit for masking
102  {
103  if (i & value) DATA_SET; //masking value with i , write to SENSI-BUS
104  else DATA_CLR;
105  SCK_SET; //clk for SENSI-BUS
106  SCK_SET;SCK_SET;SCK_SET; //pulswith approx. 5 us
107  // _nop_();_nop_();_nop_(); //pulswith approx. 5 us
108  SCK_CLR;
109  }
110  DATA_SET; //release DATA-line
111  SCK_SET; //clk #9 for ack
112  error=DATA_IN; //check ack (DATA will be pulled down by SHT11)
113  SCK_CLR;
114 
115  return error; //error=1 in case of no acknowledge
116 }
117 
119 {
120  uint8_t i,val=0;
121 
122  DATA_SET; //release DATA-line
123  for (i=0x80;i>0;i/=2) //shift bit for masking
124  {
125  SCK_SET; //clk for SENSI-BUS
126  if (DATA_IN) val=(val | i); //read bit
127  SCK_CLR;
128  }
129 
130  if (ack) DATA_CLR; //in case of "ack==1" pull down DATA-Line
131  SCK_SET; //clk #9 for ack
132  SCK_SET;SCK_SET;SCK_SET; //pulswith approx. 5 us
133  // _nop_();_nop_();_nop_(); //pulswith approx. 5 us
134  SCK_CLR;
135  DATA_SET; //release DATA-line
136  return val;
137 }
138 
139 void s_transstart(void)
140 {
141  // generates a transmission start
142  // _____ ________
143  // DATA: |_______|
144  // ___ ___
145  // SCK : ___| |___| |______
146 
147  DATA_SET; SCK_CLR; //Initial state
148  SCK_CLR;// _nop_();
149  SCK_SET;
150  SCK_SET;// _nop_();
151  DATA_CLR;
152  DATA_CLR;// _nop_();
153  SCK_CLR;
154  SCK_CLR;SCK_CLR;SCK_CLR; // _nop_();_nop_();_nop_();
155  SCK_SET;
156  SCK_SET;// _nop_();
157  DATA_SET;
158  DATA_SET;// _nop_();
159  SCK_CLR;
160 }
161 
163 {
164  // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
165  // _____________________________________________________ ________
166  // DATA: |_______|
167  // _ _ _ _ _ _ _ _ _ ___ ___
168  // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
169 
170  uint8_t i;
171 
172  DATA_SET; SCK_CLR; //Initial state
173  for(i=0;i<9;i++) //9 SCK cycles
174  {
175  SCK_SET;
176  SCK_CLR;
177  }
178  s_transstart(); //transmission start
179 }
180 
181 uint8_t s_read_statusreg(uint8_t *p_value, uint8_t *p_checksum)
182 {
183  // reads the status register with checksum (8-bit)
184  uint8_t error=0;
185 
186  s_transstart(); //transmission start
187  error=s_write_byte(STATUS_REG_R); //send command to sensor
188  *p_value=s_read_byte(ACK); //read status register (8-bit)
189  *p_checksum=s_read_byte(noACK); //read checksum (8-bit)
190  return error; //error=1 in case of no response form the sensor
191 }
192 
194 {
195  // writes the status register with checksum (8-bit)
196  uint8_t error=0;
197 
198  s_transstart(); //transmission start
199  error+=s_write_byte(STATUS_REG_W);//send command to sensor
200  error+=s_write_byte(*p_value); //send value of status register
201  return error; //error>=1 in case of no response form the sensor
202 }
203 
204 uint8_t s_measure(uint16_t *p_value, uint8_t *p_checksum, uint8_t mode)
205 {
206  // makes a measurement (humidity/temperature) with checksum
207  uint8_t error=0;
208  uint32_t i;
209 
210  s_transstart(); //transmission start
211  switch(mode){ //send command to sensor
212  case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
213  case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
214  default : break;
215  }
216  for (i=0;i<6665535;i++) if(DATA_IN==0) break; //wait until sensor has finished the measurement
217  if(DATA_IN) error+=1; // or timeout (~2 sec.) is reached
218  *(p_value) = s_read_byte(ACK) << 8; //read the first byte (MSB)
219  *(p_value) |= s_read_byte(ACK); //read the second byte (LSB)
220  *p_checksum = s_read_byte(noACK); //read checksum
221 
222  return error;
223 }
224 
226 {
227  // makes a measurement (humidity/temperature) with checksum
228  uint8_t error=0;
229 
230  s_transstart(); //transmission start
231  switch(mode){ //send command to sensor
232  case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
233  case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
234  default : break;
235  }
236 
237  return error;
238 }
239 
240 uint8_t s_read_measure(uint16_t *p_value, uint8_t *p_checksum)
241 {
242  // reads a measurement (humidity/temperature) with checksum
243  uint8_t error=0;
244 
245  if(DATA_IN) error+=1; //still busy?
246  *(p_value) = s_read_byte(ACK) << 8; //read the first byte (MSB)
247  *(p_value) |= s_read_byte(ACK); //read the second byte (LSB)
248  *p_checksum = s_read_byte(noACK); //read checksum
249 
250  return error;
251 }
252 
253 void calc_sht(uint16_t hum, uint16_t tem, float *fhum ,float *ftem)
254 {
255  // calculates temperature [ C] and humidity [%RH]
256  // input : humi [Ticks] (12 bit)
257  // temp [Ticks] (14 bit)
258  // output: humi [%RH]
259  // temp [ C]
260 
261  const float C1 =-4.0; // for 12 Bit
262  const float C2 = 0.0405; // for 12 Bit
263  const float C3 =-0.0000028; // for 12 Bit
264  const float T1 = 0.01; // for 14 Bit @ 5V
265  const float T2 = 0.00008; // for 14 Bit @ 5V
266  float rh; // rh: Humidity [Ticks] 12 Bit
267  float t; // t: Temperature [Ticks] 14 Bit
268  float rh_lin; // rh_lin: Humidity linear
269  float rh_true; // rh_true: Temperature compensated humidity
270  float t_C; // t_C : Temperature [ C]
271 
272  rh = (float)hum; //converts integer to float
273  t = (float)tem; //converts integer to float
274 
275  t_C=t*0.01 - 39.66; //calc. Temperature from ticks to [°C] @ 3.5V
276  rh_lin=C3*rh*rh + C2*rh + C1; //calc. Humidity from ticks to [%RH]
277  rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. Temperature compensated humidity [%RH]
278  if(rh_true>100)rh_true=100; //cut if the value is outside of
279  if(rh_true<0.1)rh_true=0.1; //the physical possible range
280  *ftem = t_C; //return temperature [ C]
281  *fhum = rh_true; //return humidity[%RH]
282 }
283 
285 {
286  // resets the sensor by a softreset
287  uint8_t error=0;
288 
289  s_connectionreset(); //reset communication
290  error+=s_write_byte(RESET); //send RESET-command to sensor
291 
292  return error; //error=1 in case of no response form the sensor
293 }
294 
295 void humid_sht_init( void )
296 {
297  /* Configure DAT/SCL pin as GPIO */
298  gpio_setup_input(SHT_DAT_GPIO);
299  gpio_setup_output(SHT_SCK_GPIO);
300  gpio_clear(SHT_SCK_GPIO);
301 
302 
305 }
306 
307 void humid_sht_periodic(void) {
308  uint8_t error=0, checksum;
309 
310  if (humid_sht_status == SHT_IDLE) {
311  /* init humidity read */
315  }
317  /* get data */
318  error += s_read_measure(&humidsht, &checksum);
319 
320  if (error != 0) {
322  s_start_measure(HUMI); //restart
323  //LED_TOGGLE(2);
324  }
325  else {
326  error += s_start_measure(TEMP);
328  }
329  }
330  else if (humid_sht_status == SHT_MEASURING_TEMP) {
331  /* get data */
332  error += s_read_measure(&tempsht, &checksum);
333 
334  if (error != 0) {
336  s_start_measure(TEMP); //restart
337  //LED_TOGGLE(2);
338  }
339  else {
345  DOWNLINK_SEND_SHT_STATUS(DefaultChannel, DefaultDevice, &humidsht, &tempsht, &fhumidsht, &ftempsht);
347  }
348  }
349 }
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
void humid_sht_init(void)
Definition: humid_sht.c:295
#define SHT_MEASURING_HUMID
Definition: humid_sht.c:59
void humid_sht_periodic(void)
Definition: humid_sht.c:307
#define DATA_SET
set data pin to input
Definition: humid_sht.c:67
#define SHT_IDLE
Definition: humid_sht.c:58
uint16_t value
Definition: adc_arch.c:585
Some architecture independent helper functions for GPIOs.
#define HUMI
Definition: humid_sht.c:43
#define DATA_CLR
set data pin to output
Definition: humid_sht.c:69
uint8_t s_write_byte(uint8_t value)
Definition: humid_sht.c:97
uint16_t humidsht
Definition: humid_sht.c:79
#define FALSE
Definition: imu_chimu.h:141
#define TEMP
Definition: humid_sht.c:42
uint8_t s_write_statusreg(uint8_t *p_value)
Definition: humid_sht.c:193
uint8_t s_read_measure(uint16_t *p_value, uint8_t *p_checksum)
Definition: humid_sht.c:240
#define STATUS_REG_W
Definition: humid_sht.c:47
#define MEASURE_HUMI
Definition: humid_sht.c:53
uint8_t humid_sht_status
Definition: humid_sht.c:82
uint16_t val[TCOUPLE_NB]
unsigned long uint32_t
Definition: types.h:18
#define SCK_SET
set clock pin to high
Definition: humid_sht.c:74
#define STATUS_REG_R
Definition: humid_sht.c:49
#define MEASURE_TEMP
Definition: humid_sht.c:51
uint8_t s_read_byte(uint8_t ack)
Definition: humid_sht.c:118
uint8_t s_start_measure(uint8_t mode)
Definition: humid_sht.c:225
#define SHT_MEASURING_TEMP
Definition: humid_sht.c:60
uint8_t humid_sht_reset(void)
Definition: humid_sht.c:284
void s_transstart(void)
Definition: humid_sht.c:139
void gpio_clear(uint32_t port, uint16_t pin)
Clear a gpio output to low level.
#define TRUE
Definition: imu_chimu.h:144
SHTxx sensor interface.
void calc_sht(uint16_t hum, uint16_t tem, float *fhum, float *ftem)
Definition: humid_sht.c:253
unsigned char uint8_t
Definition: types.h:14
static void gpio_setup_output(uint32_t port, uint32_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_arch.h:76
bool_t humid_sht_available
Definition: humid_sht.c:81
uint8_t s_read_statusreg(uint8_t *p_value, uint8_t *p_checksum)
Definition: humid_sht.c:181
float ftempsht
Definition: humid_sht.c:80
#define SCK_CLR
set clock pin to low
Definition: humid_sht.c:76
#define noACK
Definition: humid_sht.c:40
#define ACK
Definition: humid_sht.c:41
void s_connectionreset(void)
Definition: humid_sht.c:162
static void gpio_setup_input(uint32_t port, uint32_t gpios)
Setup one or more pins of the given GPIO port as inputs.
Definition: gpio_arch.h:88
uint8_t s_measure(uint16_t *p_value, uint8_t *p_checksum, uint8_t mode)
Definition: humid_sht.c:204
float fhumidsht
Definition: humid_sht.c:80
#define DATA_IN
get data pin
Definition: humid_sht.c:71
uint16_t tempsht
Definition: humid_sht.c:79
#define RESET
Definition: humid_sht.c:55