Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@6:10523c9e9b68, 2020-05-01 (annotated)
- Committer:
- christodoulos
- Date:
- Fri May 01 14:30:25 2020 +0000
- Revision:
- 6:10523c9e9b68
- Parent:
- 5:0c6401d671c6
- Child:
- 7:90753ca349c8
BME680 (used in POCBreath_V2_smd_commercial)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| christodoulos | 6:10523c9e9b68 | 1 | #include "mbed.h" |
| christodoulos | 6:10523c9e9b68 | 2 | |
| christodoulos | 6:10523c9e9b68 | 3 | Serial ble(PC_12, PD_2); |
| christodoulos | 6:10523c9e9b68 | 4 | I2C bme(PC_1,PC_0); |
| christodoulos | 6:10523c9e9b68 | 5 | |
| christodoulos | 6:10523c9e9b68 | 6 | int main() |
| christodoulos | 6:10523c9e9b68 | 7 | { |
| christodoulos | 6:10523c9e9b68 | 8 | ble.baud(9600); |
| christodoulos | 6:10523c9e9b68 | 9 | // char rId[1]; //id read |
| christodoulos | 6:10523c9e9b68 | 10 | int addr=0xEC; //8bit slave address |
| christodoulos | 6:10523c9e9b68 | 11 | |
| christodoulos | 6:10523c9e9b68 | 12 | char wHum[2]; |
| christodoulos | 6:10523c9e9b68 | 13 | wHum[0]=0x72; //humidity register |
| christodoulos | 6:10523c9e9b68 | 14 | wHum[1]=0x1;// |
| christodoulos | 6:10523c9e9b68 | 15 | char rHum[1]; |
| mbed_official | 0:b3b93997a0a6 | 16 | |
| christodoulos | 6:10523c9e9b68 | 17 | bme.write(addr,wHum,2); |
| christodoulos | 6:10523c9e9b68 | 18 | bme.read(addr,rHum,1); |
| christodoulos | 6:10523c9e9b68 | 19 | ble.printf("Hum: %x\n",rHum[0]); |
| christodoulos | 6:10523c9e9b68 | 20 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 21 | |
| christodoulos | 6:10523c9e9b68 | 22 | // char wTemp[2]; |
| christodoulos | 6:10523c9e9b68 | 23 | // wTemp[0]=0x74;//measurement register |
| christodoulos | 6:10523c9e9b68 | 24 | // wTemp[1]=0x40; //01000000 |
| christodoulos | 6:10523c9e9b68 | 25 | // char rTemp[1]; |
| christodoulos | 6:10523c9e9b68 | 26 | // |
| christodoulos | 6:10523c9e9b68 | 27 | // bme.write(addr,wTemp,2); |
| christodoulos | 6:10523c9e9b68 | 28 | // bme.read(addr,rTemp,1); |
| christodoulos | 6:10523c9e9b68 | 29 | // ble.printf("%x\n",rTemp[0]); |
| christodoulos | 6:10523c9e9b68 | 30 | // wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 31 | |
| christodoulos | 6:10523c9e9b68 | 32 | char wPres[2]; |
| christodoulos | 6:10523c9e9b68 | 33 | wPres[0]=0x74;//measurement register |
| christodoulos | 6:10523c9e9b68 | 34 | wPres[1]=0x54; //01010100 (kept the temperature) |
| christodoulos | 6:10523c9e9b68 | 35 | char rPres[1]; |
| christodoulos | 6:10523c9e9b68 | 36 | |
| christodoulos | 6:10523c9e9b68 | 37 | bme.write(addr,wPres,2); |
| christodoulos | 6:10523c9e9b68 | 38 | bme.read(addr,rPres,1); |
| christodoulos | 6:10523c9e9b68 | 39 | ble.printf("Temp and pres: %x\n",rPres[0]); |
| christodoulos | 6:10523c9e9b68 | 40 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 41 | |
| christodoulos | 6:10523c9e9b68 | 42 | char wWait[2]; |
| christodoulos | 6:10523c9e9b68 | 43 | wWait[0]=0x64;//gas wait 0 register |
| christodoulos | 6:10523c9e9b68 | 44 | wWait[1]=0x59; //100 ms |
| christodoulos | 6:10523c9e9b68 | 45 | char rWait[1]; |
| christodoulos | 6:10523c9e9b68 | 46 | |
| christodoulos | 6:10523c9e9b68 | 47 | bme.write(addr,wWait,2); |
| christodoulos | 6:10523c9e9b68 | 48 | bme.read(addr,rWait,1); |
| christodoulos | 6:10523c9e9b68 | 49 | ble.printf("Wait: %x\n",rWait[0]); |
| christodoulos | 6:10523c9e9b68 | 50 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 51 | |
| christodoulos | 6:10523c9e9b68 | 52 | //GET HEAT SETTING PARAMETERS |
| christodoulos | 6:10523c9e9b68 | 53 | char wg1[1]; |
| christodoulos | 6:10523c9e9b68 | 54 | wg1[0]=0xED; |
| christodoulos | 6:10523c9e9b68 | 55 | char rg1[1]; |
| christodoulos | 6:10523c9e9b68 | 56 | char wg2M[1]; |
| christodoulos | 6:10523c9e9b68 | 57 | char wg2L[1]; |
| christodoulos | 6:10523c9e9b68 | 58 | char rg2M[1]; |
| christodoulos | 6:10523c9e9b68 | 59 | char rg2L[1]; |
| christodoulos | 6:10523c9e9b68 | 60 | wg2M[0]=0xEC; |
| christodoulos | 6:10523c9e9b68 | 61 | wg2L[0]=0xEB; |
| christodoulos | 6:10523c9e9b68 | 62 | char wg3[1]; |
| christodoulos | 6:10523c9e9b68 | 63 | wg3[0]=0xEE; |
| christodoulos | 6:10523c9e9b68 | 64 | char rg3[1]; |
| christodoulos | 6:10523c9e9b68 | 65 | char wHR[1]; |
| christodoulos | 6:10523c9e9b68 | 66 | wHR[0]=0x02; |
| christodoulos | 6:10523c9e9b68 | 67 | char rHR[1]; |
| christodoulos | 6:10523c9e9b68 | 68 | char wHV[1]; |
| christodoulos | 6:10523c9e9b68 | 69 | wHV[0]=0x00; |
| christodoulos | 6:10523c9e9b68 | 70 | char rHV[1]; |
| christodoulos | 6:10523c9e9b68 | 71 | |
| christodoulos | 6:10523c9e9b68 | 72 | bme.write(addr,wg1,1); |
| christodoulos | 6:10523c9e9b68 | 73 | bme.read(addr,rg1,1);//get g1 |
| christodoulos | 6:10523c9e9b68 | 74 | int g1=rg1[0]; |
| christodoulos | 6:10523c9e9b68 | 75 | ble.printf("G1 hex: %x, G1 int: %d\n",rg1[0], g1); |
| christodoulos | 6:10523c9e9b68 | 76 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 77 | |
| christodoulos | 6:10523c9e9b68 | 78 | bme.write(addr,wg2M,1); |
| christodoulos | 6:10523c9e9b68 | 79 | bme.read(addr,rg2M,1);//get g2m |
| christodoulos | 6:10523c9e9b68 | 80 | |
| christodoulos | 6:10523c9e9b68 | 81 | bme.write(addr,wg2L,1); |
| christodoulos | 6:10523c9e9b68 | 82 | bme.read(addr,rg2L,1);//get g2l |
| christodoulos | 6:10523c9e9b68 | 83 | ble.printf("G2M hex: %x, G2L hex: %x\n",rg2M[0], rg2L[0]); |
| christodoulos | 6:10523c9e9b68 | 84 | int g2=((rg2M[0]<<8)+rg2L[0]); |
| christodoulos | 6:10523c9e9b68 | 85 | ble.printf("G2 hex: %x, G2 int: %d\n",g2, g2); |
| christodoulos | 6:10523c9e9b68 | 86 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 87 | |
| christodoulos | 6:10523c9e9b68 | 88 | bme.write(addr,wg3,1); |
| christodoulos | 6:10523c9e9b68 | 89 | bme.read(addr,rg3,1);//get g3 |
| christodoulos | 6:10523c9e9b68 | 90 | int g3=rg3[0]; |
| christodoulos | 6:10523c9e9b68 | 91 | ble.printf("G3 hex: %x, G3 int: %d\n",rg3[0],g3); |
| christodoulos | 6:10523c9e9b68 | 92 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 93 | |
| christodoulos | 6:10523c9e9b68 | 94 | bme.write(addr,wHR,1); |
| christodoulos | 6:10523c9e9b68 | 95 | bme.read(addr,rHR,1);//get heating range |
| christodoulos | 6:10523c9e9b68 | 96 | int HR=((rHR[0]<<2)>>6); |
| christodoulos | 6:10523c9e9b68 | 97 | ble.printf("HR hex: %x, HR int: %d\n",rHR[0], HR); |
| christodoulos | 6:10523c9e9b68 | 98 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 99 | |
| christodoulos | 6:10523c9e9b68 | 100 | bme.write(addr,wHV,1); |
| christodoulos | 6:10523c9e9b68 | 101 | bme.read(addr,rHV,1);//get g3 |
| christodoulos | 6:10523c9e9b68 | 102 | int HV=rHV[0]; |
| christodoulos | 6:10523c9e9b68 | 103 | ble.printf("HV hex: %x, HV int: %d\n",rHV[0], HV); |
| christodoulos | 6:10523c9e9b68 | 104 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 105 | |
| christodoulos | 6:10523c9e9b68 | 106 | double var1=(g1/16.0)+49.0; |
| christodoulos | 6:10523c9e9b68 | 107 | double var2= ((g2 / 32768.0) * 0.0005) + 0.00235; |
| christodoulos | 6:10523c9e9b68 | 108 | double var3=g3 / 1024.0; |
| christodoulos | 6:10523c9e9b68 | 109 | double var4 = var1 * (1.0 + (var2 *3000)); //target temp 300c |
| christodoulos | 6:10523c9e9b68 | 110 | double var5= var4 + (var3 * 24.0);//ambient 24c |
| christodoulos | 6:10523c9e9b68 | 111 | uint8_t heat=(3.4 * ((var5 * (4.0 / (4.0 + HR)) * (1.0/(1.0 +(HV * 0.002)))) - 25)); |
| christodoulos | 6:10523c9e9b68 | 112 | ble.printf("Re heat: %d\n",heat); |
| christodoulos | 6:10523c9e9b68 | 113 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 114 | |
| christodoulos | 6:10523c9e9b68 | 115 | //END OF HEAT SETTING PARAMETERS |
| christodoulos | 6:10523c9e9b68 | 116 | |
| christodoulos | 6:10523c9e9b68 | 117 | char wRes[2]; |
| christodoulos | 6:10523c9e9b68 | 118 | wRes[0]=0x5A;//gas wait 0 heat setting |
| christodoulos | 6:10523c9e9b68 | 119 | wRes[1]=0x59; //??? |
| christodoulos | 6:10523c9e9b68 | 120 | char rRes[1]; |
| christodoulos | 6:10523c9e9b68 | 121 | |
| christodoulos | 6:10523c9e9b68 | 122 | bme.write(addr,wRes,2); |
| christodoulos | 6:10523c9e9b68 | 123 | bme.read(addr,rRes,1); |
| christodoulos | 6:10523c9e9b68 | 124 | ble.printf("Heater resistance: %x\n",rRes[0]); |
| christodoulos | 6:10523c9e9b68 | 125 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 126 | |
| christodoulos | 6:10523c9e9b68 | 127 | char wCG[2]; |
| christodoulos | 6:10523c9e9b68 | 128 | wCG[0]=0x71;//control gas register |
| christodoulos | 6:10523c9e9b68 | 129 | wCG[1]=0x0; |
| christodoulos | 6:10523c9e9b68 | 130 | char rCG[1]; |
| christodoulos | 6:10523c9e9b68 | 131 | |
| christodoulos | 6:10523c9e9b68 | 132 | bme.write(addr,wCG,2); |
| christodoulos | 6:10523c9e9b68 | 133 | bme.read(addr,rCG,1); |
| christodoulos | 6:10523c9e9b68 | 134 | ble.printf("Control gas: %x\n",rCG[0]); |
| christodoulos | 6:10523c9e9b68 | 135 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 136 | |
| christodoulos | 6:10523c9e9b68 | 137 | char wRG[2]; |
| christodoulos | 6:10523c9e9b68 | 138 | wRG[0]=0x71;//control gas register |
| christodoulos | 6:10523c9e9b68 | 139 | wRG[1]=0x10; |
| christodoulos | 6:10523c9e9b68 | 140 | char rRG[1]; |
| christodoulos | 6:10523c9e9b68 | 141 | |
| christodoulos | 6:10523c9e9b68 | 142 | bme.write(addr,wRG,2); |
| christodoulos | 6:10523c9e9b68 | 143 | bme.read(addr,rRG,1); |
| christodoulos | 6:10523c9e9b68 | 144 | ble.printf("Run gas: %x\n",rRG[0]); |
| christodoulos | 6:10523c9e9b68 | 145 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 146 | |
| christodoulos | 6:10523c9e9b68 | 147 | |
| mbed_official | 0:b3b93997a0a6 | 148 | |
| christodoulos | 6:10523c9e9b68 | 149 | |
| christodoulos | 6:10523c9e9b68 | 150 | |
| christodoulos | 6:10523c9e9b68 | 151 | wPres[1]=0x55; //01010100 (kept the temperature and prssure, enabled measurement ) |
| christodoulos | 6:10523c9e9b68 | 152 | |
| christodoulos | 6:10523c9e9b68 | 153 | ///GET GAS |
| christodoulos | 6:10523c9e9b68 | 154 | char wGasL[1]; |
| christodoulos | 6:10523c9e9b68 | 155 | wGasL[0]=0x2B; |
| christodoulos | 6:10523c9e9b68 | 156 | char wGasM[1]; |
| christodoulos | 6:10523c9e9b68 | 157 | wGasM[0]=0x2A; |
| christodoulos | 6:10523c9e9b68 | 158 | char rGasL[1]; |
| christodoulos | 6:10523c9e9b68 | 159 | char rGasM[1]; |
| christodoulos | 6:10523c9e9b68 | 160 | |
| christodoulos | 6:10523c9e9b68 | 161 | //GET TEMP |
| christodoulos | 6:10523c9e9b68 | 162 | char wTempL[1]; |
| christodoulos | 6:10523c9e9b68 | 163 | char wTempM[1]; |
| christodoulos | 6:10523c9e9b68 | 164 | wTempL[0]=0x23; |
| christodoulos | 6:10523c9e9b68 | 165 | wTempM[0]=0x22; |
| christodoulos | 6:10523c9e9b68 | 166 | char rTempL[1]; |
| christodoulos | 6:10523c9e9b68 | 167 | char rTempM[1]; |
| christodoulos | 6:10523c9e9b68 | 168 | |
| christodoulos | 6:10523c9e9b68 | 169 | while(1) { |
| christodoulos | 6:10523c9e9b68 | 170 | bme.write(addr,wPres,2); //enables measurement |
| christodoulos | 6:10523c9e9b68 | 171 | bme.read(addr,rPres,1); |
| mbed_official | 0:b3b93997a0a6 | 172 | |
| christodoulos | 6:10523c9e9b68 | 173 | bme.write(addr,wGasL,1); |
| christodoulos | 6:10523c9e9b68 | 174 | bme.read(addr,rGasL,1); //bits 7:6 are significant |
| christodoulos | 6:10523c9e9b68 | 175 | bme.write(addr,wGasM,1); |
| christodoulos | 6:10523c9e9b68 | 176 | bme.read(addr,rGasM,1); //whole byte is significant |
| christodoulos | 6:10523c9e9b68 | 177 | |
| christodoulos | 6:10523c9e9b68 | 178 | bme.write(addr,wTempL,1); |
| christodoulos | 6:10523c9e9b68 | 179 | bme.read(addr,rTempL,1); |
| christodoulos | 6:10523c9e9b68 | 180 | bme.write(addr,wTempM,1); |
| christodoulos | 6:10523c9e9b68 | 181 | bme.read(addr,rTempM,1); |
| christodoulos | 6:10523c9e9b68 | 182 | |
| christodoulos | 6:10523c9e9b68 | 183 | int temp =(rTempM[0]<<8)+rTempL[0]; |
| christodoulos | 6:10523c9e9b68 | 184 | int gas =(rGasM[0]<<2)+(rGasL[0]>>6); //make space on the most of 2 bits to add only the first two bits from the least |
| christodoulos | 6:10523c9e9b68 | 185 | |
| christodoulos | 6:10523c9e9b68 | 186 | ble.printf("Gas: %i, Tem: %i\n",gas, temp); |
| christodoulos | 6:10523c9e9b68 | 187 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 188 | } |
| mbed_official | 0:b3b93997a0a6 | 189 | |
| mbed_official | 0:b3b93997a0a6 | 190 | } |