lib_SHT11
Dependents: lib_SHT11_example libs_YSI_example
lib_SHT11.cpp@0:6b657f74b4e4, 2017-06-06 (annotated)
- Committer:
- YSI
- Date:
- Tue Jun 06 14:08:27 2017 +0000
- Revision:
- 0:6b657f74b4e4
lib_SHT11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YSI | 0:6b657f74b4e4 | 1 | /** SHT11 class |
YSI | 0:6b657f74b4e4 | 2 | * |
YSI | 0:6b657f74b4e4 | 3 | * @purpose library for SHT11 humidity and temperature sensor |
YSI | 0:6b657f74b4e4 | 4 | * |
YSI | 0:6b657f74b4e4 | 5 | * Utilisée pour mesurer la temperature et l'humidite |
YSI | 0:6b657f74b4e4 | 6 | * |
YSI | 0:6b657f74b4e4 | 7 | * https://www.sensirion.com/products/humidity-sensor/ |
YSI | 0:6b657f74b4e4 | 8 | * |
YSI | 0:6b657f74b4e4 | 9 | * Copyright (c) 2012, cstyles (http://mbed.org) |
YSI | 0:6b657f74b4e4 | 10 | * |
YSI | 0:6b657f74b4e4 | 11 | * Exemple: |
YSI | 0:6b657f74b4e4 | 12 | * @code |
YSI | 0:6b657f74b4e4 | 13 | * #include "mbed.h" |
YSI | 0:6b657f74b4e4 | 14 | * #include "lib_SHT11.h" |
YSI | 0:6b657f74b4e4 | 15 | * |
YSI | 0:6b657f74b4e4 | 16 | * SHT11 capteur_HT; |
YSI | 0:6b657f74b4e4 | 17 | * //SHT11 capteur_HT(p30, p29, 2.0); //data, sck, periode_cycle_mesure |
YSI | 0:6b657f74b4e4 | 18 | * Serial pc(USBTX, USBRX); |
YSI | 0:6b657f74b4e4 | 19 | * |
YSI | 0:6b657f74b4e4 | 20 | * int main() |
YSI | 0:6b657f74b4e4 | 21 | * { |
YSI | 0:6b657f74b4e4 | 22 | * float Temperature_mesuree=0.0, Humidite_mesuree=0.0; |
YSI | 0:6b657f74b4e4 | 23 | * |
YSI | 0:6b657f74b4e4 | 24 | * while(1) |
YSI | 0:6b657f74b4e4 | 25 | * { |
YSI | 0:6b657f74b4e4 | 26 | * if((Temperature_mesuree != capteur_HT.lire_T()) || (Humidite_mesuree != capteur_HT.lire_H())) |
YSI | 0:6b657f74b4e4 | 27 | * { |
YSI | 0:6b657f74b4e4 | 28 | * capteur_HT.lire_T_H(&Temperature_mesuree, &Humidite_mesuree); |
YSI | 0:6b657f74b4e4 | 29 | * pc.printf("T=%f\tH=%f\r\n", Temperature_mesuree, Humidite_mesuree); |
YSI | 0:6b657f74b4e4 | 30 | * } |
YSI | 0:6b657f74b4e4 | 31 | * } |
YSI | 0:6b657f74b4e4 | 32 | * } |
YSI | 0:6b657f74b4e4 | 33 | * @endcode |
YSI | 0:6b657f74b4e4 | 34 | * @file lib_SHT11.cpp |
YSI | 0:6b657f74b4e4 | 35 | * @date Jun 2012 |
YSI | 0:6b657f74b4e4 | 36 | * @author Yannic Simon |
YSI | 0:6b657f74b4e4 | 37 | */ |
YSI | 0:6b657f74b4e4 | 38 | |
YSI | 0:6b657f74b4e4 | 39 | #include "lib_SHT11.h" |
YSI | 0:6b657f74b4e4 | 40 | |
YSI | 0:6b657f74b4e4 | 41 | |
YSI | 0:6b657f74b4e4 | 42 | SHT11::SHT11(PinName pin_DATA, PinName pin_SCK, float T_C_mesure) : DATA_HT(pin_DATA), SCK_HT(pin_SCK), Temps_cycle_mesure(T_C_mesure) |
YSI | 0:6b657f74b4e4 | 43 | { |
YSI | 0:6b657f74b4e4 | 44 | if(Temps_cycle_mesure < 2) Temps_cycle_mesure = 2; |
YSI | 0:6b657f74b4e4 | 45 | init(); |
YSI | 0:6b657f74b4e4 | 46 | } |
YSI | 0:6b657f74b4e4 | 47 | |
YSI | 0:6b657f74b4e4 | 48 | void SHT11::start(void) |
YSI | 0:6b657f74b4e4 | 49 | { |
YSI | 0:6b657f74b4e4 | 50 | DATA_HT.output(); |
YSI | 0:6b657f74b4e4 | 51 | DATA_HT=1; |
YSI | 0:6b657f74b4e4 | 52 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 53 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 54 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 55 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 56 | DATA_HT=0; |
YSI | 0:6b657f74b4e4 | 57 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 58 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 59 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 60 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 61 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 62 | DATA_HT=1; |
YSI | 0:6b657f74b4e4 | 63 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 64 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 65 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 66 | DATA_HT=0; |
YSI | 0:6b657f74b4e4 | 67 | } |
YSI | 0:6b657f74b4e4 | 68 | |
YSI | 0:6b657f74b4e4 | 69 | void SHT11::changer_timer_cycle(float new_Temps_cycle_mesure) |
YSI | 0:6b657f74b4e4 | 70 | { |
YSI | 0:6b657f74b4e4 | 71 | if(new_Temps_cycle_mesure < 2) Temps_cycle_mesure = 2; |
YSI | 0:6b657f74b4e4 | 72 | else Temps_cycle_mesure = new_Temps_cycle_mesure; |
YSI | 0:6b657f74b4e4 | 73 | if(etat_cycle) desactiver(); init(); |
YSI | 0:6b657f74b4e4 | 74 | } |
YSI | 0:6b657f74b4e4 | 75 | |
YSI | 0:6b657f74b4e4 | 76 | void SHT11::init(void) |
YSI | 0:6b657f74b4e4 | 77 | { |
YSI | 0:6b657f74b4e4 | 78 | // ____________________________________________ |
YSI | 0:6b657f74b4e4 | 79 | // DATA_HT |__ |
YSI | 0:6b657f74b4e4 | 80 | // __ __ __ __ __ __ __ __ __ |
YSI | 0:6b657f74b4e4 | 81 | // SCK_HT _| |_| |_| |_| |_| |_| |_| |_| |_| |_ |
YSI | 0:6b657f74b4e4 | 82 | unsigned char i; |
YSI | 0:6b657f74b4e4 | 83 | DATA_HT.output(); |
YSI | 0:6b657f74b4e4 | 84 | DATA_HT=1; |
YSI | 0:6b657f74b4e4 | 85 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 86 | for (i=0;i<9;i++) |
YSI | 0:6b657f74b4e4 | 87 | { |
YSI | 0:6b657f74b4e4 | 88 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 89 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 90 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 91 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 92 | } |
YSI | 0:6b657f74b4e4 | 93 | etat_sht11=0; |
YSI | 0:6b657f74b4e4 | 94 | etat_cycle=1; |
YSI | 0:6b657f74b4e4 | 95 | Timer_cycle_mesure.attach(callback(this, &SHT11::gestion_HT), Temps_cycle_mesure); |
YSI | 0:6b657f74b4e4 | 96 | } |
YSI | 0:6b657f74b4e4 | 97 | |
YSI | 0:6b657f74b4e4 | 98 | void SHT11::desactiver(void) |
YSI | 0:6b657f74b4e4 | 99 | { |
YSI | 0:6b657f74b4e4 | 100 | etat_cycle=0; |
YSI | 0:6b657f74b4e4 | 101 | Timer_cycle_mesure.detach(); |
YSI | 0:6b657f74b4e4 | 102 | } |
YSI | 0:6b657f74b4e4 | 103 | |
YSI | 0:6b657f74b4e4 | 104 | void SHT11::demande_mesure(short octet_de_commande) |
YSI | 0:6b657f74b4e4 | 105 | { |
YSI | 0:6b657f74b4e4 | 106 | unsigned int i; |
YSI | 0:6b657f74b4e4 | 107 | |
YSI | 0:6b657f74b4e4 | 108 | DATA_HT.output(); |
YSI | 0:6b657f74b4e4 | 109 | for(i=0;i<8;i++) |
YSI | 0:6b657f74b4e4 | 110 | { |
YSI | 0:6b657f74b4e4 | 111 | if ((octet_de_commande)&(0x80)) DATA_HT=1; |
YSI | 0:6b657f74b4e4 | 112 | else DATA_HT=0; |
YSI | 0:6b657f74b4e4 | 113 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 114 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 115 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 116 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 117 | octet_de_commande=octet_de_commande*2; |
YSI | 0:6b657f74b4e4 | 118 | } |
YSI | 0:6b657f74b4e4 | 119 | DATA_HT.input(); |
YSI | 0:6b657f74b4e4 | 120 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 121 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 122 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 123 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 124 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 125 | DATA_HT.output(); |
YSI | 0:6b657f74b4e4 | 126 | DATA_HT=1; |
YSI | 0:6b657f74b4e4 | 127 | DATA_HT.input(); |
YSI | 0:6b657f74b4e4 | 128 | } |
YSI | 0:6b657f74b4e4 | 129 | |
YSI | 0:6b657f74b4e4 | 130 | void SHT11::mesure_T(void) |
YSI | 0:6b657f74b4e4 | 131 | { |
YSI | 0:6b657f74b4e4 | 132 | start(); |
YSI | 0:6b657f74b4e4 | 133 | demande_mesure(3); |
YSI | 0:6b657f74b4e4 | 134 | } |
YSI | 0:6b657f74b4e4 | 135 | |
YSI | 0:6b657f74b4e4 | 136 | void SHT11::mesure_H(void) |
YSI | 0:6b657f74b4e4 | 137 | { |
YSI | 0:6b657f74b4e4 | 138 | start(); |
YSI | 0:6b657f74b4e4 | 139 | demande_mesure(5); |
YSI | 0:6b657f74b4e4 | 140 | } |
YSI | 0:6b657f74b4e4 | 141 | |
YSI | 0:6b657f74b4e4 | 142 | void SHT11::calcul_Temperature_Humidite(void) |
YSI | 0:6b657f74b4e4 | 143 | { |
YSI | 0:6b657f74b4e4 | 144 | Temperature_mesuree=(Temperature*0.01-40); |
YSI | 0:6b657f74b4e4 | 145 | Humidite_mesuree=((float)Temperature_mesuree-25.0)*(0.01+(0.00008*Humidite))-4+0.0405*Humidite-0.0000028*Humidite*Humidite; |
YSI | 0:6b657f74b4e4 | 146 | } |
YSI | 0:6b657f74b4e4 | 147 | |
YSI | 0:6b657f74b4e4 | 148 | void SHT11::reception_mesure_T(void) |
YSI | 0:6b657f74b4e4 | 149 | { |
YSI | 0:6b657f74b4e4 | 150 | unsigned int i; |
YSI | 0:6b657f74b4e4 | 151 | Temperature=0; |
YSI | 0:6b657f74b4e4 | 152 | |
YSI | 0:6b657f74b4e4 | 153 | DATA_HT.input(); |
YSI | 0:6b657f74b4e4 | 154 | for (i=0;i<8;i++) |
YSI | 0:6b657f74b4e4 | 155 | { |
YSI | 0:6b657f74b4e4 | 156 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 157 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 158 | Temperature *=2; |
YSI | 0:6b657f74b4e4 | 159 | if (DATA_HT==1) Temperature++; |
YSI | 0:6b657f74b4e4 | 160 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 161 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 162 | } |
YSI | 0:6b657f74b4e4 | 163 | DATA_HT.output(); |
YSI | 0:6b657f74b4e4 | 164 | DATA_HT=0; |
YSI | 0:6b657f74b4e4 | 165 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 166 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 167 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 168 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 169 | DATA_HT.input(); |
YSI | 0:6b657f74b4e4 | 170 | for (i=0;i<8;i++) |
YSI | 0:6b657f74b4e4 | 171 | { |
YSI | 0:6b657f74b4e4 | 172 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 173 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 174 | Temperature *=2; |
YSI | 0:6b657f74b4e4 | 175 | if (DATA_HT==1) Temperature++; |
YSI | 0:6b657f74b4e4 | 176 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 177 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 178 | } |
YSI | 0:6b657f74b4e4 | 179 | DATA_HT.output(); |
YSI | 0:6b657f74b4e4 | 180 | DATA_HT=1; |
YSI | 0:6b657f74b4e4 | 181 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 182 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 183 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 184 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 185 | } |
YSI | 0:6b657f74b4e4 | 186 | |
YSI | 0:6b657f74b4e4 | 187 | void SHT11::reception_mesure_H(void) |
YSI | 0:6b657f74b4e4 | 188 | { |
YSI | 0:6b657f74b4e4 | 189 | unsigned int i; |
YSI | 0:6b657f74b4e4 | 190 | Humidite=0; |
YSI | 0:6b657f74b4e4 | 191 | |
YSI | 0:6b657f74b4e4 | 192 | DATA_HT.input(); |
YSI | 0:6b657f74b4e4 | 193 | for (i=0;i<8;i++) |
YSI | 0:6b657f74b4e4 | 194 | { |
YSI | 0:6b657f74b4e4 | 195 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 196 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 197 | Humidite *=2; |
YSI | 0:6b657f74b4e4 | 198 | if (DATA_HT==1) Humidite++; |
YSI | 0:6b657f74b4e4 | 199 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 200 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 201 | } |
YSI | 0:6b657f74b4e4 | 202 | DATA_HT.output(); |
YSI | 0:6b657f74b4e4 | 203 | DATA_HT=0; |
YSI | 0:6b657f74b4e4 | 204 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 205 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 206 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 207 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 208 | DATA_HT.input(); |
YSI | 0:6b657f74b4e4 | 209 | for (i=0;i<8;i++) |
YSI | 0:6b657f74b4e4 | 210 | { |
YSI | 0:6b657f74b4e4 | 211 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 212 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 213 | Humidite *=2; |
YSI | 0:6b657f74b4e4 | 214 | if (DATA_HT==1) Humidite++; |
YSI | 0:6b657f74b4e4 | 215 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 216 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 217 | } |
YSI | 0:6b657f74b4e4 | 218 | DATA_HT.output(); |
YSI | 0:6b657f74b4e4 | 219 | DATA_HT=1; |
YSI | 0:6b657f74b4e4 | 220 | SCK_HT=1; |
YSI | 0:6b657f74b4e4 | 221 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 222 | SCK_HT=0; |
YSI | 0:6b657f74b4e4 | 223 | wait_us(1); |
YSI | 0:6b657f74b4e4 | 224 | } |
YSI | 0:6b657f74b4e4 | 225 | |
YSI | 0:6b657f74b4e4 | 226 | void SHT11::gestion_HT(void) |
YSI | 0:6b657f74b4e4 | 227 | { |
YSI | 0:6b657f74b4e4 | 228 | switch (etat_sht11) |
YSI | 0:6b657f74b4e4 | 229 | { |
YSI | 0:6b657f74b4e4 | 230 | case 0: |
YSI | 0:6b657f74b4e4 | 231 | Timer_cycle_mesure.detach(); |
YSI | 0:6b657f74b4e4 | 232 | mesure_T(); |
YSI | 0:6b657f74b4e4 | 233 | etat_sht11=1; |
YSI | 0:6b657f74b4e4 | 234 | Timer_cycle_mesure.attach_us(callback(this, &SHT11::gestion_HT), 10000); |
YSI | 0:6b657f74b4e4 | 235 | break; |
YSI | 0:6b657f74b4e4 | 236 | |
YSI | 0:6b657f74b4e4 | 237 | case 1: |
YSI | 0:6b657f74b4e4 | 238 | DATA_HT.input(); |
YSI | 0:6b657f74b4e4 | 239 | if (DATA_HT==0) |
YSI | 0:6b657f74b4e4 | 240 | { |
YSI | 0:6b657f74b4e4 | 241 | Timer_cycle_mesure.detach(); |
YSI | 0:6b657f74b4e4 | 242 | etat_sht11=2; |
YSI | 0:6b657f74b4e4 | 243 | Timer_cycle_mesure.attach_us(callback(this, &SHT11::gestion_HT), 1); |
YSI | 0:6b657f74b4e4 | 244 | } |
YSI | 0:6b657f74b4e4 | 245 | break; |
YSI | 0:6b657f74b4e4 | 246 | |
YSI | 0:6b657f74b4e4 | 247 | case 2: |
YSI | 0:6b657f74b4e4 | 248 | Timer_cycle_mesure.detach(); |
YSI | 0:6b657f74b4e4 | 249 | reception_mesure_T(); |
YSI | 0:6b657f74b4e4 | 250 | etat_sht11=3; |
YSI | 0:6b657f74b4e4 | 251 | Timer_cycle_mesure.attach_us(callback(this, &SHT11::gestion_HT), 1); |
YSI | 0:6b657f74b4e4 | 252 | break; |
YSI | 0:6b657f74b4e4 | 253 | |
YSI | 0:6b657f74b4e4 | 254 | case 3: |
YSI | 0:6b657f74b4e4 | 255 | Timer_cycle_mesure.detach(); |
YSI | 0:6b657f74b4e4 | 256 | mesure_H(); |
YSI | 0:6b657f74b4e4 | 257 | etat_sht11=4; |
YSI | 0:6b657f74b4e4 | 258 | Timer_cycle_mesure.attach_us(callback(this, &SHT11::gestion_HT), 10000); |
YSI | 0:6b657f74b4e4 | 259 | break; |
YSI | 0:6b657f74b4e4 | 260 | |
YSI | 0:6b657f74b4e4 | 261 | case 4: |
YSI | 0:6b657f74b4e4 | 262 | DATA_HT.input(); |
YSI | 0:6b657f74b4e4 | 263 | if (DATA_HT==0) |
YSI | 0:6b657f74b4e4 | 264 | { |
YSI | 0:6b657f74b4e4 | 265 | Timer_cycle_mesure.detach(); |
YSI | 0:6b657f74b4e4 | 266 | etat_sht11=5; |
YSI | 0:6b657f74b4e4 | 267 | Timer_cycle_mesure.attach_us(callback(this, &SHT11::gestion_HT), 1); |
YSI | 0:6b657f74b4e4 | 268 | } |
YSI | 0:6b657f74b4e4 | 269 | break; |
YSI | 0:6b657f74b4e4 | 270 | |
YSI | 0:6b657f74b4e4 | 271 | case 5: |
YSI | 0:6b657f74b4e4 | 272 | Timer_cycle_mesure.detach(); |
YSI | 0:6b657f74b4e4 | 273 | reception_mesure_H(); |
YSI | 0:6b657f74b4e4 | 274 | calcul_Temperature_Humidite(); |
YSI | 0:6b657f74b4e4 | 275 | etat_sht11=0; |
YSI | 0:6b657f74b4e4 | 276 | Timer_cycle_mesure.attach(callback(this, &SHT11::gestion_HT), Temps_cycle_mesure); |
YSI | 0:6b657f74b4e4 | 277 | break; |
YSI | 0:6b657f74b4e4 | 278 | |
YSI | 0:6b657f74b4e4 | 279 | default: |
YSI | 0:6b657f74b4e4 | 280 | etat_sht11=0; |
YSI | 0:6b657f74b4e4 | 281 | break; |
YSI | 0:6b657f74b4e4 | 282 | } |
YSI | 0:6b657f74b4e4 | 283 | } |
YSI | 0:6b657f74b4e4 | 284 | |
YSI | 0:6b657f74b4e4 | 285 | float SHT11::lire_T(void) |
YSI | 0:6b657f74b4e4 | 286 | { |
YSI | 0:6b657f74b4e4 | 287 | return Temperature_mesuree; |
YSI | 0:6b657f74b4e4 | 288 | } |
YSI | 0:6b657f74b4e4 | 289 | |
YSI | 0:6b657f74b4e4 | 290 | float SHT11::lire_H(void) |
YSI | 0:6b657f74b4e4 | 291 | { |
YSI | 0:6b657f74b4e4 | 292 | return Humidite_mesuree; |
YSI | 0:6b657f74b4e4 | 293 | } |
YSI | 0:6b657f74b4e4 | 294 | |
YSI | 0:6b657f74b4e4 | 295 | void SHT11::lire_T_H(float *pointeur_Temperature, float *pointeur_Humidite) |
YSI | 0:6b657f74b4e4 | 296 | { |
YSI | 0:6b657f74b4e4 | 297 | *pointeur_Temperature = Temperature_mesuree; |
YSI | 0:6b657f74b4e4 | 298 | *pointeur_Humidite = Humidite_mesuree; |
YSI | 0:6b657f74b4e4 | 299 | } |