Test BME and TLC59711 BLE Nano 1.5

Fork of BME280_SPI by Toyomasa Watarai

Committer:
MACRUM
Date:
Mon Mar 13 15:37:22 2017 +0000
Revision:
8:f0faf51d3d4a
Parent:
7:dfd6107f1f92
Child:
9:01d4e22a7012
Change initialize values

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 1:763a4018aaec 1 /**
MACRUM 5:c1f1647004c4 2 ******************************************************************************
MACRUM 6:b91c721722d2 3 * @file BME280_SPI.cpp
MACRUM 5:c1f1647004c4 4 * @author Toyomasa Watarai
MACRUM 5:c1f1647004c4 5 * @version V1.0.0
MACRUM 5:c1f1647004c4 6 * @date 11 March 2017
MACRUM 6:b91c721722d2 7 * @brief BME280_SPI class implementation
MACRUM 5:c1f1647004c4 8 ******************************************************************************
MACRUM 5:c1f1647004c4 9 * @attention
MACRUM 1:763a4018aaec 10 *
MACRUM 7:dfd6107f1f92 11 * Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 7:dfd6107f1f92 12 * you may not use this file except in compliance with the License.
MACRUM 7:dfd6107f1f92 13 * You may obtain a copy of the License at
MACRUM 7:dfd6107f1f92 14 *
MACRUM 7:dfd6107f1f92 15 * http://www.apache.org/licenses/LICENSE-2.0
MACRUM 1:763a4018aaec 16 *
MACRUM 7:dfd6107f1f92 17 * Unless required by applicable law or agreed to in writing, software
MACRUM 7:dfd6107f1f92 18 * distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 7:dfd6107f1f92 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 7:dfd6107f1f92 20 * See the License for the specific language governing permissions and
MACRUM 7:dfd6107f1f92 21 * limitations under the License.
MACRUM 1:763a4018aaec 22 */
MACRUM 1:763a4018aaec 23
MACRUM 0:ade9be832910 24 #include "mbed.h"
MACRUM 6:b91c721722d2 25 #include "BME280_SPI.h"
MACRUM 0:ade9be832910 26
MACRUM 6:b91c721722d2 27 BME280_SPI::BME280_SPI(PinName mosi, PinName miso, PinName sclk, PinName cs)
MACRUM 0:ade9be832910 28 :
MACRUM 6:b91c721722d2 29 _spi(mosi, miso, sclk),
MACRUM 6:b91c721722d2 30 _cs(cs),
MACRUM 0:ade9be832910 31 t_fine(0)
MACRUM 0:ade9be832910 32 {
MACRUM 0:ade9be832910 33 initialize();
MACRUM 0:ade9be832910 34 }
MACRUM 0:ade9be832910 35
MACRUM 6:b91c721722d2 36
MACRUM 6:b91c721722d2 37 BME280_SPI::~BME280_SPI()
MACRUM 0:ade9be832910 38 {
MACRUM 0:ade9be832910 39 }
MACRUM 6:b91c721722d2 40
MACRUM 6:b91c721722d2 41 void BME280_SPI::initialize()
MACRUM 0:ade9be832910 42 {
MACRUM 0:ade9be832910 43 char cmd[18];
MACRUM 6:b91c721722d2 44
MACRUM 6:b91c721722d2 45 _cs = 1;
MACRUM 6:b91c721722d2 46 _spi.format(8, 0); // 8-bit, mode=0
MACRUM 7:dfd6107f1f92 47 _spi.frequency(1000000); // 1MHZ
MACRUM 6:b91c721722d2 48
MACRUM 6:b91c721722d2 49 _cs = 0;
MACRUM 7:dfd6107f1f92 50 _spi.write(0xd0); // chip_id
MACRUM 7:dfd6107f1f92 51 cmd[0] = _spi.write(0); // read chip_id
MACRUM 7:dfd6107f1f92 52 _cs = 1;
MACRUM 7:dfd6107f1f92 53
MACRUM 7:dfd6107f1f92 54 DEBUG_PRINT("chip_id = 0x%x\n", cmd[0]);
MACRUM 7:dfd6107f1f92 55
MACRUM 7:dfd6107f1f92 56 _cs = 0;
MACRUM 7:dfd6107f1f92 57 _spi.write(0xf2 & BME280_SPI_MASK); // ctrl_hum
MACRUM 8:f0faf51d3d4a 58 _spi.write(0x04); // Humidity oversampling x4
MACRUM 6:b91c721722d2 59 _cs = 1;
MACRUM 6:b91c721722d2 60
MACRUM 6:b91c721722d2 61 _cs = 0;
MACRUM 7:dfd6107f1f92 62 _spi.write(0xf4 & BME280_SPI_MASK); // ctrl_meas
MACRUM 8:f0faf51d3d4a 63 _spi.write((4<<5)|(4<<2)|3); // Temparature oversampling x4, Pressure oversampling x4, Normal mode
MACRUM 6:b91c721722d2 64 _cs = 1;
MACRUM 6:b91c721722d2 65
MACRUM 6:b91c721722d2 66 _cs = 0;
MACRUM 7:dfd6107f1f92 67 _spi.write(0xf5 & BME280_SPI_MASK); // config
MACRUM 7:dfd6107f1f92 68 _spi.write(0xa0); // Standby 1000ms, Filter off, 4-wire SPI interface
MACRUM 6:b91c721722d2 69 _cs = 1;
MACRUM 6:b91c721722d2 70
MACRUM 8:f0faf51d3d4a 71 wait(1);
MACRUM 8:f0faf51d3d4a 72
MACRUM 6:b91c721722d2 73 _cs = 0;
MACRUM 6:b91c721722d2 74 _spi.write(0x88); // read dig_T regs
MACRUM 6:b91c721722d2 75 for(int i = 0; i < 6; i++)
MACRUM 6:b91c721722d2 76 cmd[i] = _spi.write(0);
MACRUM 6:b91c721722d2 77 _cs = 1;
MACRUM 6:b91c721722d2 78
MACRUM 0:ade9be832910 79 dig_T1 = (cmd[1] << 8) | cmd[0];
MACRUM 0:ade9be832910 80 dig_T2 = (cmd[3] << 8) | cmd[2];
MACRUM 0:ade9be832910 81 dig_T3 = (cmd[5] << 8) | cmd[4];
MACRUM 6:b91c721722d2 82
MACRUM 0:ade9be832910 83 DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
MACRUM 7:dfd6107f1f92 84 DEBUG_PRINT("dig_T = %d, %d, %d\n", dig_T1, dig_T2, dig_T3);
MACRUM 6:b91c721722d2 85
MACRUM 6:b91c721722d2 86 _cs = 0;
MACRUM 8:f0faf51d3d4a 87 _spi.write(0x8e); // read dig_P regs
MACRUM 6:b91c721722d2 88 for(int i = 0; i < 18; i++)
MACRUM 6:b91c721722d2 89 cmd[i] = _spi.write(0);
MACRUM 6:b91c721722d2 90 _cs = 1;
MACRUM 6:b91c721722d2 91
MACRUM 0:ade9be832910 92 dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
MACRUM 0:ade9be832910 93 dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
MACRUM 0:ade9be832910 94 dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
MACRUM 0:ade9be832910 95 dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
MACRUM 0:ade9be832910 96 dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
MACRUM 0:ade9be832910 97 dig_P6 = (cmd[11] << 8) | cmd[10];
MACRUM 0:ade9be832910 98 dig_P7 = (cmd[13] << 8) | cmd[12];
MACRUM 0:ade9be832910 99 dig_P8 = (cmd[15] << 8) | cmd[14];
MACRUM 0:ade9be832910 100 dig_P9 = (cmd[17] << 8) | cmd[16];
MACRUM 6:b91c721722d2 101
MACRUM 0:ade9be832910 102 DEBUG_PRINT("dig_P = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9);
MACRUM 7:dfd6107f1f92 103 DEBUG_PRINT("dig_P = %d, %d, %d, %d, %d, %d, %d, %d, %d\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9);
MACRUM 6:b91c721722d2 104
MACRUM 6:b91c721722d2 105 _cs = 0;
MACRUM 8:f0faf51d3d4a 106 _spi.write(0xA1); // read dig_H1 reg
MACRUM 6:b91c721722d2 107 cmd[0] = _spi.write(0);
MACRUM 6:b91c721722d2 108 _cs = 1;
MACRUM 6:b91c721722d2 109
MACRUM 6:b91c721722d2 110 _cs = 0;
MACRUM 6:b91c721722d2 111 _spi.write(0xE1); // read dig_H regs
MACRUM 6:b91c721722d2 112 for(int i = 0; i < 7; i++)
MACRUM 6:b91c721722d2 113 cmd[1+i] = _spi.write(0);
MACRUM 6:b91c721722d2 114 _cs = 1;
MACRUM 2:c35f637c28ef 115
MACRUM 0:ade9be832910 116 dig_H1 = cmd[0];
MACRUM 0:ade9be832910 117 dig_H2 = (cmd[2] << 8) | cmd[1];
MACRUM 0:ade9be832910 118 dig_H3 = cmd[3];
MACRUM 0:ade9be832910 119 dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
MACRUM 2:c35f637c28ef 120 dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f);
MACRUM 2:c35f637c28ef 121 dig_H6 = cmd[7];
MACRUM 6:b91c721722d2 122
MACRUM 0:ade9be832910 123 DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6);
MACRUM 7:dfd6107f1f92 124 DEBUG_PRINT("dig_H = %d, %d, %d, %d, %d, %d\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6);
MACRUM 0:ade9be832910 125 }
MACRUM 6:b91c721722d2 126
MACRUM 6:b91c721722d2 127 float BME280_SPI::getTemperature()
MACRUM 0:ade9be832910 128 {
MACRUM 0:ade9be832910 129 uint32_t temp_raw;
MACRUM 0:ade9be832910 130 float tempf;
MACRUM 8:f0faf51d3d4a 131 char cmd[3];
MACRUM 6:b91c721722d2 132
MACRUM 6:b91c721722d2 133 _cs = 0;
MACRUM 6:b91c721722d2 134 _spi.write(0xfa);
MACRUM 6:b91c721722d2 135 for(int i = 0; i < 3; i++)
MACRUM 8:f0faf51d3d4a 136 cmd[i] = _spi.write(0);
MACRUM 6:b91c721722d2 137 _cs = 1;
MACRUM 6:b91c721722d2 138
MACRUM 8:f0faf51d3d4a 139 temp_raw = (cmd[0] << 12) | (cmd[1] << 4) | (cmd[2] >> 4);
MACRUM 6:b91c721722d2 140
MACRUM 0:ade9be832910 141 int32_t temp;
MACRUM 6:b91c721722d2 142
MACRUM 0:ade9be832910 143 temp =
MACRUM 0:ade9be832910 144 (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
MACRUM 0:ade9be832910 145 ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
MACRUM 6:b91c721722d2 146
MACRUM 0:ade9be832910 147 t_fine = temp;
MACRUM 0:ade9be832910 148 temp = (temp * 5 + 128) >> 8;
MACRUM 0:ade9be832910 149 tempf = (float)temp;
MACRUM 6:b91c721722d2 150
MACRUM 0:ade9be832910 151 return (tempf/100.0f);
MACRUM 0:ade9be832910 152 }
MACRUM 6:b91c721722d2 153
MACRUM 6:b91c721722d2 154 float BME280_SPI::getPressure()
MACRUM 0:ade9be832910 155 {
MACRUM 0:ade9be832910 156 uint32_t press_raw;
MACRUM 0:ade9be832910 157 float pressf;
MACRUM 8:f0faf51d3d4a 158 char cmd[3];
MACRUM 6:b91c721722d2 159
MACRUM 6:b91c721722d2 160 _cs = 0;
MACRUM 6:b91c721722d2 161 _spi.write(0xf7); // press_msb
MACRUM 6:b91c721722d2 162 for(int i = 0; i < 3; i++)
MACRUM 8:f0faf51d3d4a 163 cmd[i] = _spi.write(0);
MACRUM 6:b91c721722d2 164 _cs = 1;
MACRUM 6:b91c721722d2 165
MACRUM 8:f0faf51d3d4a 166 press_raw = (cmd[0] << 12) | (cmd[1] << 4) | (cmd[2] >> 4);
MACRUM 6:b91c721722d2 167
MACRUM 0:ade9be832910 168 int32_t var1, var2;
MACRUM 0:ade9be832910 169 uint32_t press;
MACRUM 6:b91c721722d2 170
MACRUM 0:ade9be832910 171 var1 = (t_fine >> 1) - 64000;
MACRUM 0:ade9be832910 172 var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
MACRUM 0:ade9be832910 173 var2 = var2 + ((var1 * dig_P5) << 1);
MACRUM 0:ade9be832910 174 var2 = (var2 >> 2) + (dig_P4 << 16);
MACRUM 0:ade9be832910 175 var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
MACRUM 0:ade9be832910 176 var1 = ((32768 + var1) * dig_P1) >> 15;
MACRUM 0:ade9be832910 177 if (var1 == 0) {
MACRUM 0:ade9be832910 178 return 0;
MACRUM 0:ade9be832910 179 }
MACRUM 0:ade9be832910 180 press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
MACRUM 0:ade9be832910 181 if(press < 0x80000000) {
MACRUM 0:ade9be832910 182 press = (press << 1) / var1;
MACRUM 0:ade9be832910 183 } else {
MACRUM 0:ade9be832910 184 press = (press / var1) * 2;
MACRUM 0:ade9be832910 185 }
MACRUM 0:ade9be832910 186 var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
MACRUM 0:ade9be832910 187 var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
MACRUM 0:ade9be832910 188 press = (press + ((var1 + var2 + dig_P7) >> 4));
MACRUM 6:b91c721722d2 189
MACRUM 0:ade9be832910 190 pressf = (float)press;
MACRUM 0:ade9be832910 191 return (pressf/100.0f);
MACRUM 0:ade9be832910 192 }
MACRUM 6:b91c721722d2 193
MACRUM 6:b91c721722d2 194 float BME280_SPI::getHumidity()
MACRUM 0:ade9be832910 195 {
MACRUM 0:ade9be832910 196 uint32_t hum_raw;
MACRUM 0:ade9be832910 197 float humf;
MACRUM 8:f0faf51d3d4a 198 char cmd[2];
MACRUM 6:b91c721722d2 199
MACRUM 6:b91c721722d2 200 _cs = 0;
MACRUM 6:b91c721722d2 201 _spi.write(0xfd); // hum_msb
MACRUM 6:b91c721722d2 202 for(int i = 0; i < 2; i++)
MACRUM 8:f0faf51d3d4a 203 cmd[i] = _spi.write(0);
MACRUM 6:b91c721722d2 204 _cs = 1;
MACRUM 6:b91c721722d2 205
MACRUM 8:f0faf51d3d4a 206 hum_raw = (cmd[0] << 8) | cmd[1];
MACRUM 6:b91c721722d2 207
MACRUM 0:ade9be832910 208 int32_t v_x1;
MACRUM 6:b91c721722d2 209
MACRUM 0:ade9be832910 210 v_x1 = t_fine - 76800;
MACRUM 0:ade9be832910 211 v_x1 = (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
MACRUM 0:ade9be832910 212 ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
MACRUM 0:ade9be832910 213 (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
MACRUM 0:ade9be832910 214 (int32_t)dig_H2 + 8192) >> 14));
MACRUM 0:ade9be832910 215 v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
MACRUM 0:ade9be832910 216 v_x1 = (v_x1 < 0 ? 0 : v_x1);
MACRUM 0:ade9be832910 217 v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
MACRUM 6:b91c721722d2 218
MACRUM 0:ade9be832910 219 humf = (float)(v_x1 >> 12);
MACRUM 6:b91c721722d2 220
MACRUM 0:ade9be832910 221 return (humf/1024.0f);
MACRUM 0:ade9be832910 222 }