STM32F103C8T6 Blue pillボードで、BoschのBME280を使って、湿度、気圧、温度センサーを測定し、液晶ディスプレイSC1602に測定値を表示する方法を紹介します。Arduino用ではありません。
Bosch BME280は、小型のセンサーでありながら、湿度、気圧、温度を測定で、マイコンとのI/FもSPIとI2Cが選べます。また、中国AliExpressでは5Vまで使えるようにLDOとI2C の3V-5V変換I/Fがついたボードが300円以下で販売されています。前に紹介したS/W I2Cクラスを使う事にします。こんな感じです。
SC1602もそうですが、Arduinoでは既にライブラリが公開されており、簡単に使うことができます。でも、私はメーカーの仕様書を読んで、自力で実装する事が一つの楽しみなのです。仕様書を読んでプログラムを書いていると自分の頭のトレーニングにもなり、また、デバイスの設計者の考え方に時々「なるほど!」と感心したり、勉強になります。趣味なので、好きなだけ時間をかける事が出来ますからね。
では、SW_I2Cクラスを継承してBME280クラスを定義します。
レジスタが多いので面倒ですけど。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
/* * bme280.h * * Created on: Jul 11, 2019 * Author: http://www.e-momonga.com/honkytonk * Copyright 2019 honkytonk. All right reserved. */ #ifndef BME280_H_ #define BME280_H_ #include "stm32f1xx_hal.h" #include "main.h" #include "sw_i2c.h" #ifdef __cplusplus extern "C" { #endif #ifdef __cplusplus }; #endif typedef int32_t BME280_S32_t; typedef uint32_t BME280_U32_t; typedef int64_t BME280_S64_t; #define BME280_ADDRESS 0b11101100 #define IIC_READ_MASK ~0xfe #define IIC_WRITE_MASK 0xfe #define BME280_SLEEP_MODE 0x00 #define BME280_NORMAL_MODE 0x03 #define BME280_FORCED_MODE 0x01 #define BME280_HUMIDITY_DATA_ADDRESS 0xfd #define BME280_TEMPERATURE_DATA_ADDRESS 0xfa #define BME280_PRESSURE_DATA_ADDRESS 0xf7 #define BME280_CONFIG_ADDRESS 0xf5 #define BME280_CONTROL_MEASURE_ADDRESS 0xf4 #define BME280_STATUS_ADDRESS 0xf3 #define BME280_CONTROL_HUMIDITY_ADDRESS 0xf2 #define BME280_CALIBRATION_26_41_ADDRESS 0xe1 #define BME280_RESET_ADDRESS 0xe0 #define BME280_ID_ADDRESS 0xd0 #define BME280_CALIBRATION_00_25_ADDRESS 0x88 #define BME280_TSB_0_5MS 0b0000 #define BME280_TSB_62_5MS 0b0001 #define BME280_TSB_125MS 0b0010 #define BME280_TSB_250MS 0b0011 #define BME280_TSB_500MS 0b0100 #define BME280_TSB_1000MS 0b0101 #define BME280_TSB_10MS 0b0110 #define BME280_TSB_20MS 0b0111 #define BME280_FILTER_OFF 0b0000 #define BME280_FILTER_2 0b0001 #define BME280_FILTER_4 0b0010 #define BME280_FILTER_8 0b0011 #define BME280_FILTER_16 0b0100 #define BME280_OSRS_SKIP 0b0000 #define BME280_OSRS_x1 0b0001 #define BME280_OSRS_x2 0b0010 #define BME280_OSRS_x4 0b0011 #define BME280_OSRS_x8 0b0100 #define BME280_OSRS_x16 0b0101 #define BME280_MODE_SLEEP 0b0000 #define BME280_MODE_FORCED 0b0001 #define BME280_MODE_NORMAL 0b0011 #define BME280_MEASURING_MASK BIT_03 #define BME280_IM_UPDATE_MASK BIT_00 #define BME280_HANDLER_WAIT 0 #define BME280_HANDLER_RESET_DEVICE 1 #define BME280_HANDLER_INIT_DEVICE 2 #define BME280_HANDLER_MEASURING 3 typedef enum{bme_init = 0, bme_config, bme_measure }bme280_mode; typedef enum{bme_press_msb = 0, bme_press_lsb, bme_press_xlsb, bme_temp_msb, bme_temp_lsb, bme_temp_xlsb, bme_hum_msb, bme_hum_lsb, bme_caldata_size}bme280_cal; class BME280 : SW_I2C { private: uint8_t revid; int32_t temperatureX100; uint32_t pressureX100; uint32_t humidityX100; // t_fine carries fine temperature as global value BME280_S32_t t_fine; uint16_t dig_T1; int16_t dig_T2; int16_t dig_T3; uint16_t dig_P1; int16_t dig_P2; int16_t dig_P3; int16_t dig_P4; int16_t dig_P5; int16_t dig_P6; int16_t dig_P7; int16_t dig_P8; int16_t dig_P9; uint16_t dig_H1; int16_t dig_H2; uint16_t dig_H3; int16_t dig_H4; int16_t dig_H5; int16_t dig_H6; uint8_t hum_data[3]; uint8_t temp_data[3]; uint8_t press_data[3]; uint8_t config_val; uint8_t ctrl_meas_val; uint8_t ctrl_hum_val; uint8_t status_val; uint8_t reset_val; uint8_t id_val; uint8_t mode = 0; // handler mode void readCompensationData(void); void indirectDataWrite(uint8_t address, uint8_t wdata); uint32_t readPressure(void); int32_t readTemperature(void); uint32_t readHumidity(void); void readMeasuredAllData(void); void initPort(void); bool checkDrdy(void); void resetMode(void){mode = 0;} BME280_S32_t BME280_compensate_T_int32(BME280_S32_t adc_T); BME280_U32_t BME280_compensate_P_int64(BME280_S32_t adc_P); BME280_U32_t BME280_compensate_H_int32(BME280_S32_t adc_H); public: BME280(uint8_t slave, gpio sda, gpio scl): SW_I2C(slave, sda, scl){ config_val = 0x00; ctrl_meas_val = 0x00; ctrl_hum_val = 0x00; status_val = 0x00; reset_val = 0x00; id_val = 0x60; mode = 0; // handler mode }; uint8_t getId(void); void resetDevice(void); void resetHandler(void){mode = 0;} uint8_t getHandlerMode(void){return mode;} void setMode(uint8_t mode); void setOverSamplingModeTemperature(uint8_t mode); void setOverSamplingModePressure(uint8_t mode); void setOverSamplingModeHumidity(uint8_t mode); void setDuration(uint8_t t_sb); void setFilterCoefficient(uint8_t filter); void updateRegister(uint8_t address); uint8_t checkStatus(void); uint32_t getPressure(void){return pressureX100;} int32_t getTemperature(void){return temperatureX100;} uint32_t getHumidity(void){return humidityX100;} uint32_t handler(void); }; #endif /* BME280_H_ */ |
では、関数の定義です。長いですねぇ。
途中、キャリブレーションデータを使って測定を補正する所があるのですが、理解できなまま仕様書からコピーしました。なんとなく、それらしい値が測定できているみたいなので良いかみたいな。
温度、湿度、気圧の測定値は小数点以下2桁までを整数で扱うため、100倍した値を返すようにしています。
表示するときは、1/100するようにしてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
/* * bme280.cpp * * Created on: Jul 11, 2019 * Author: http://www.e-momonga.com/honkytonk * Copyright honkytonk. */ #include <stdint.h> //#include <stdio.h> #include "bme280.h" #include "stm32f1xx_it.h" #include "main.h" #include "cmsis_os.h" #ifdef __cplusplus extern "C" { #endif int sprintf( char *apBuf, const char *apFmt, ... ); int tiny_printf( const char *format, ... ); #ifdef __cplusplus }; #endif uint8_t BME280::getId(void) { uint8_t dat = 0; this->burstReadDataBytes(this->getSlaveAddress(), BME280_ID_ADDRESS, &dat, 1); return dat; } void BME280::resetDevice(void) { uint8_t dat = 0xb6; this->burstWriteDataBytes(this->getSlaveAddress(), BME280_RESET_ADDRESS, &dat, 1); } void BME280::setMode(uint8_t mode) { this->ctrl_meas_val &= ~(BIT_00 | BIT_01); this->ctrl_meas_val |= (mode & 0x03); this->burstWriteDataBytes(this->getSlaveAddress(), BME280_CONTROL_MEASURE_ADDRESS, &(this->ctrl_meas_val), 1); } uint8_t BME280::checkStatus(void) { uint8_t dat = 0; this->burstReadDataBytes(this->getSlaveAddress(), BME280_STATUS_ADDRESS, &dat, 1); return dat; } void BME280::setOverSamplingModeTemperature(uint8_t mode) { this->ctrl_meas_val &= ~(BIT_05 | BIT_06 | BIT_07); this->ctrl_meas_val |= (mode & 0x07) << 5; this->burstWriteDataBytes(this->getSlaveAddress(), BME280_CONTROL_MEASURE_ADDRESS, &(this->ctrl_meas_val), 1); } void BME280::setOverSamplingModePressure(uint8_t mode) { this->ctrl_meas_val &= ~(BIT_04 | BIT_03 | BIT_02); this->ctrl_meas_val |= (mode & 0x07) << 2; this->burstWriteDataBytes(this->getSlaveAddress(), BME280_CONTROL_MEASURE_ADDRESS, &(this->ctrl_meas_val), 1); } void BME280::setOverSamplingModeHumidity(uint8_t mode) { this->ctrl_hum_val &= ~(BIT_02 | BIT_01 | BIT_00); this->ctrl_hum_val |= (mode & 0x07); this->burstWriteDataBytes(this->getSlaveAddress(), BME280_CONTROL_HUMIDITY_ADDRESS, &(this->ctrl_hum_val), 1); this->burstWriteDataBytes(this->getSlaveAddress(), BME280_CONTROL_MEASURE_ADDRESS, &(this->ctrl_meas_val), 1); } void BME280::setDuration(uint8_t t_sb) { this->config_val &= ~(BIT_07 | BIT_06 | BIT_05); this->config_val |= (t_sb & 0x07) << 5; this->burstWriteDataBytes(this->getSlaveAddress(), BME280_CONFIG_ADDRESS, &(this->config_val), 1); } void BME280::setFilterCoefficient(uint8_t filter) { this->config_val &= ~(BIT_04 | BIT_03 | BIT_02); this->config_val |= (filter & 0x07) << 2; this->burstWriteDataBytes(this->getSlaveAddress(), BME280_CONFIG_ADDRESS, &(this->config_val), 1); } void BME280::updateRegister(uint8_t address) { uint8_t val = 0; switch(address){ case BME280_CONFIG_ADDRESS: val = this->config_val; break; case BME280_CONTROL_MEASURE_ADDRESS: val = this->ctrl_meas_val; break; case BME280_CONTROL_HUMIDITY_ADDRESS: val = this->ctrl_hum_val; break; default: break; } this->burstWriteDataBytes(this->getSlaveAddress(), address, &val, 1); } int32_t BME280::readTemperature(void) { BME280_S32_t temp = 0; this->burstReadDataBytes(this->getSlaveAddress(), BME280_TEMPERATURE_DATA_ADDRESS, this->temp_data, 3); temp = (int32_t)(((uint32_t)temp_data[0] << 12)|((uint32_t)temp_data[1] << 4)|((uint32_t)temp_data[2] >> 4)); this->temperatureX100 = this->BME280_compensate_T_int32(temp); return this->temperatureX100; } uint32_t BME280::readPressure(void) { BME280_S32_t temp = 0; this->burstReadDataBytes(this->getSlaveAddress(), BME280_PRESSURE_DATA_ADDRESS, this->press_data, 3); temp = (int32_t)(((uint32_t)press_data[0] << 12)|((uint32_t)press_data[1] << 4)|((uint32_t)press_data[2] >> 4)); this->pressureX100 = this->BME280_compensate_P_int64(temp); this->pressureX100 /= 256; return this->pressureX100; } uint32_t BME280::readHumidity(void) { BME280_S32_t temp = 0; this->burstReadDataBytes(this->getSlaveAddress(), BME280_HUMIDITY_DATA_ADDRESS, this->hum_data, 2); temp = (int32_t)((((uint32_t)hum_data[0]) << 8)|(uint32_t)hum_data[1]); this->humidityX100 = this->BME280_compensate_H_int32(temp); this->humidityX100 *= 100; this->humidityX100 /= 1024; return this->humidityX100; } void BME280::readMeasuredAllData(void) { BME280_S32_t temp = 0; uint8_t meas[bme_caldata_size]; this->burstReadDataBytes(this->getSlaveAddress(), BME280_PRESSURE_DATA_ADDRESS, meas, bme_caldata_size); temp = (int32_t)(((uint32_t)meas[bme_temp_msb] << 12)|((uint32_t)meas[bme_temp_lsb] << 4)|((uint32_t)meas[bme_temp_xlsb] >> 4)); this->temperatureX100 = this->BME280_compensate_T_int32(temp); temp = (int32_t)(((uint32_t)meas[bme_press_msb] << 12)|((uint32_t)(meas[bme_press_lsb] << 4))|((uint32_t)(meas[bme_press_xlsb]) >> 4)); this->pressureX100 = this->BME280_compensate_P_int64(temp); this->pressureX100 /= 256; temp = (int32_t)((((uint32_t)meas[bme_hum_msb]) << 8)|(uint32_t)meas[bme_hum_msb]); this->humidityX100 = this->BME280_compensate_H_int32(temp); this->humidityX100 *= 100; this->humidityX100 /= 1024; } void BME280::readCompensationData(void) { uint8_t cdat[2] = {0}; uint8_t slaveAddress = this->getSlaveAddress(); this->burstReadDataBytes(slaveAddress, 0x88, cdat, 2); this->dig_T1 = (uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]; this->burstReadDataBytes(slaveAddress, 0x8A, cdat, 2); this->dig_T2 = (int16_t)((uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]); this->burstReadDataBytes(slaveAddress, 0x8C, cdat, 2); this->dig_T3 = (int16_t)((uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]); this->burstReadDataBytes(slaveAddress, 0x8E, cdat, 2); this->dig_P1 = (uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]; this->burstReadDataBytes(slaveAddress, 0x90, cdat, 2); this->dig_P2 = (int16_t)((uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]); this->burstReadDataBytes(slaveAddress, 0x92, cdat, 2); this->dig_P3 = (int16_t)((uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]); this->burstReadDataBytes(slaveAddress, 0x94, cdat, 2); this->dig_P4 = (int16_t)((uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]); this->burstReadDataBytes(slaveAddress, 0x96, cdat, 2); this->dig_P5 = (int16_t)((uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]); this->burstReadDataBytes(slaveAddress, 0x98, cdat, 2); this->dig_P6 = (int16_t)((uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]); this->burstReadDataBytes(slaveAddress, 0x9A, cdat, 2); this->dig_P7 = (int16_t)((uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]); this->burstReadDataBytes(slaveAddress, 0x9C, cdat, 2); this->dig_P8 = (int16_t)((uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]); this->burstReadDataBytes(slaveAddress, 0x9E, cdat, 2); this->dig_P9 = (int16_t)((uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]); this->burstReadDataBytes(slaveAddress, 0xA1, cdat, 1); this->dig_H1 = (uint16_t)cdat[0]; this->burstReadDataBytes(slaveAddress, 0xE1, cdat, 2); this->dig_H2 = (int16_t)((uint16_t)cdat[1] << 8 | (uint16_t)cdat[0]); this->burstReadDataBytes(slaveAddress, 0xE3, cdat, 1); this->dig_H3 = (uint16_t)cdat[0]; this->burstReadDataBytes(slaveAddress, 0xE4, cdat, 2); this->dig_H4 = (int16_t)((uint16_t)cdat[0] << 4 | ((uint16_t)cdat[1] & 0x0007)); this->burstReadDataBytes(slaveAddress, 0xE5, cdat, 2); this->dig_H5 = 0; this->dig_H5 = cdat[1] << 4 | cdat[0] >> 4; this->burstReadDataBytes(slaveAddress, 0xE7, cdat, 1); this->dig_H6 = cdat[0]; } // Returns temperature in DegC, resolution is 0.01 DegC. Output value of “5123” equals 51.23 DegC. // t_fine carries fine temperature as global value //BME280_S32_t t_fine; BME280_S32_t BME280::BME280_compensate_T_int32(BME280_S32_t adc_T) { BME280_S32_t var1, var2, T; var1 = ((((adc_T >> 3) - ((BME280_S32_t)dig_T1 << 1))) * ((BME280_S32_t)dig_T2)) >> 11; var2 = (((((adc_T >> 4) - ((BME280_S32_t)dig_T1)) * ((adc_T >> 4) - ((BME280_S32_t)dig_T1))) >> 12) * ((BME280_S32_t)dig_T3)) >> 14; t_fine = var1 + var2; T =(t_fine * 5 + 128) >> 8; return T; } // Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 integer bits and 8 fractional bits). // Output value of “24674867” represents 24674867/256 = 96386.2 Pa = 963.862 hPa BME280_U32_t BME280::BME280_compensate_P_int64(BME280_S32_t adc_P) { BME280_S64_t var1, var2, p; var1 = ((BME280_S64_t)t_fine) - 128000; var2 = var1 * var1 * (BME280_S64_t)dig_P6; var2 = var2 + ((var1*(BME280_S64_t)dig_P5)<<17); var2 = var2 + (((BME280_S64_t)dig_P4)<<35); var1 = ((var1 * var1 * (BME280_S64_t)dig_P3)>>8) + ((var1 * (BME280_S64_t)dig_P2)<<12); var1 = (((((BME280_S64_t)1)<<47)+var1))*((BME280_S64_t)dig_P1)>>33; if (var1 == 0){ return 0; // avoid exception caused by division by zero } p = 1048576-adc_P; p = (((p<<31)-var2)*3125)/var1; var1 = (((BME280_S64_t)dig_P9) * (p>>13) * (p>>13)) >> 25; var2 = (((BME280_S64_t)dig_P8) * p) >> 19; p = ((p + var1 + var2) >> 8) + (((BME280_S64_t)dig_P7)<<4); return (BME280_U32_t)p; } // Returns humidity in %RH as unsigned 32 bit integer in Q22.10 format (22 integer and 10 fractional bits). // Output value of “47445” represents 47445/1024 = 46.333 %RH BME280_U32_t BME280::BME280_compensate_H_int32(BME280_S32_t adc_H) { BME280_S32_t v_x1_u32r; v_x1_u32r = (t_fine - ((BME280_S32_t)76800)); v_x1_u32r = (((((adc_H << 14) - (((BME280_S32_t)dig_H4) << 20) - (((BME280_S32_t)dig_H5) * v_x1_u32r)) + ((BME280_S32_t)16384)) >> 15) * (((((((v_x1_u32r * ((BME280_S32_t)dig_H6)) >> 10) * (((v_x1_u32r * ((BME280_S32_t)dig_H3)) >> 11) + ((BME280_S32_t)32768))) >> 10) + ((BME280_S32_t)2097152)) * ((BME280_S32_t)dig_H2) + 8192) >> 14)); v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * ((BME280_S32_t)dig_H1)) >> 4)); v_x1_u32r = (v_x1_u32r < 0 ? 0 : v_x1_u32r); v_x1_u32r = (v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r); return (BME280_U32_t)(v_x1_u32r>>12); } uint32_t BME280::handler(void) { uint32_t msWait; switch(this->mode){ case BME280_HANDLER_WAIT: //Power on wait msWait = 100; mode++; break; case BME280_HANDLER_RESET_DEVICE: //Reset device this->resetDevice(); msWait = 500; this->mode++; break; case BME280_HANDLER_INIT_DEVICE: //Init device //tiny_printf("ID = %x\n",this->getId()); this->setMode(BME280_SLEEP_MODE); this->setOverSamplingModePressure(BME280_OSRS_x8); this->setOverSamplingModeTemperature(BME280_OSRS_x8); this->setOverSamplingModeHumidity(BME280_OSRS_x8); this->setDuration(BME280_TSB_250MS); this->setFilterCoefficient(BME280_FILTER_2); this->readCompensationData(); this->setMode(BME280_NORMAL_MODE); msWait = 1000; this->mode++; break; case BME280_HANDLER_MEASURING: //Read measured data this->readMeasuredAllData(); msWait = 1000; break; } return msWait; } |
BME280::handler()の中でBME280の初期化からパラメータ設定、測定をしています。
各々のシーケンスの後に必要なwait値(1ms単位)を返すようにしていますので、Taskの中でwaitするようにします。もともとFreeRTOSを使わずにHALのみで実装していたのをFreeRTOS対応にしたので、このような格好になってます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
/* * task_bme280.cpp * * Created on: Oct 26, 2019 * Author: http://www.e-momonga.com/honkytonk * Copyright 2019 honkytonk. All right reserved. */ #include "task_bme280.h" #ifdef __cplusplus extern "C" { #endif int sprintf( char *apBuf, const char *apFmt, ... ); int tiny_printf( const char *format, ... ); #ifdef __cplusplus }; #endif extern osMailQId SC1602MailHandle; void TaskBme280(void const * argument) { class BME280 bme280(BME280_ADDRESS, {SDA_GPIO_Port, SDA_Pin}, {SCL_GPIO_Port, SCL_Pin}); sc1602_message_t* message; message = (sc1602_message_t*)osMailAlloc(SC1602MailHandle, osWaitForever); message->command = SC1602_COMMAND_CURSOR_OFF; osMailPut(SC1602MailHandle, message); osDelay(100); message = (sc1602_message_t*)osMailAlloc(SC1602MailHandle, osWaitForever); message->command = SC1602_COMMAND_CLEAR_DISPLAY; osMailPut(SC1602MailHandle, message); osDelay(1000); /* Infinite loop */ uint32_t msWait; while(1){ msWait = bme280.handler(); if(bme280.getHandlerMode() == BME280_HANDLER_MEASURING){ message = (sc1602_message_t*)osMailAlloc(SC1602MailHandle, osWaitForever); if(message){ int32_t t = bme280.getTemperature(); uint32_t h = bme280.getHumidity(); if(h > 9999){h = 9999;} // clip humidity value sprintf((char*)message->message, "%3d.%02d c %2d.%02d %%", (int16_t)(t/100), (int16_t)(t%100), (int16_t)(h/100), (int16_t)(h%100)); message->message[7] = 0x08; // custom font for degree C message->column = 0; message->line = 0; message->command = SC1602_COMMAND_NONE; osMailPut(SC1602MailHandle, message); } message = (sc1602_message_t*)osMailAlloc(SC1602MailHandle, osWaitForever); if(message){ uint32_t p = bme280.getPressure(); sprintf((char*)message->message, "%6d.%02d hPa", (int16_t)(p/100), (int16_t)(p%100)); message->column = 0; message->line = 1; message->command = SC1602_COMMAND_NONE; osMailPut(SC1602MailHandle, message); } } osDelay(msWait); } } #endif |
こちらのTaskから測定値をMailQueueでSC1602のタスクに渡しています。
osMailQId SC1602MailHandleは、前回のtask_sc1602.cppで定義しています。
CMSIS-RTOS(FreeRTOSのwrapper)のタスクやパラメータの設定は、STM32CubeIDEのMXで定義するのがおすすめです。
最後まで読んでいただいた貴方に、STM32F103C8T6 Blue Pillボード用でBME280を使って温度、湿度、気圧を測定し、SC1602に表示するプログラムのバイナリオブジェクトを差し上げます。STM32_RTOS_SC1602.elf
I2Cのポートは、SDA: PB8, SCL PB9に設定しています。
PCF8574(スレーブアドレス 0x27)と、BME280(スレーブアドレス 0x76)用です。その他の設定では動作しません。気温がマイナスになると正しく表示しないかもしれません。
また、Arduino用ではありませんので、このバイナリオブジェクトを書き込むとArduino用のブートローダーが消えてしまいます。復元の方法は当方ではサポートできません。動作しない、誤動作する、本記事および関連記事、バイナリオブジェクトを使用して発生したいかなる被害、損失等についても当方は責任を負いません、また、いかなる保証もしません。自己責任でお使いください。