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@7:90753ca349c8, 2020-05-18 (annotated)
- Committer:
- christodoulos
- Date:
- Mon May 18 10:33:49 2020 +0000
- Revision:
- 7:90753ca349c8
- Parent:
- 6:10523c9e9b68
BME680 low level code
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 | 7:90753ca349c8 | 9 | int BMEaddr=0xEC; //8bit slave BMEaddress |
| christodoulos | 6:10523c9e9b68 | 10 | |
| christodoulos | 6:10523c9e9b68 | 11 | char wHum[2]; |
| christodoulos | 6:10523c9e9b68 | 12 | wHum[0]=0x72; //humidity register |
| christodoulos | 6:10523c9e9b68 | 13 | wHum[1]=0x1;// |
| christodoulos | 6:10523c9e9b68 | 14 | char rHum[1]; |
| mbed_official | 0:b3b93997a0a6 | 15 | |
| christodoulos | 7:90753ca349c8 | 16 | bme.write(BMEaddr,wHum,2); |
| christodoulos | 7:90753ca349c8 | 17 | bme.read(BMEaddr,rHum,1); |
| christodoulos | 7:90753ca349c8 | 18 | //ble.printf("Hum: %x\n",rHum[0]); |
| christodoulos | 6:10523c9e9b68 | 19 | wait(0.1); |
| christodoulos | 7:90753ca349c8 | 20 | |
| christodoulos | 6:10523c9e9b68 | 21 | char wPres[2]; |
| christodoulos | 6:10523c9e9b68 | 22 | wPres[0]=0x74;//measurement register |
| christodoulos | 6:10523c9e9b68 | 23 | wPres[1]=0x54; //01010100 (kept the temperature) |
| christodoulos | 6:10523c9e9b68 | 24 | char rPres[1]; |
| christodoulos | 6:10523c9e9b68 | 25 | |
| christodoulos | 7:90753ca349c8 | 26 | bme.write(BMEaddr,wPres,2); |
| christodoulos | 7:90753ca349c8 | 27 | bme.read(BMEaddr,rPres,1); |
| christodoulos | 7:90753ca349c8 | 28 | //ble.printf("Temp and pres: %x\n",rPres[0]); |
| christodoulos | 6:10523c9e9b68 | 29 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 30 | |
| christodoulos | 6:10523c9e9b68 | 31 | char wWait[2]; |
| christodoulos | 6:10523c9e9b68 | 32 | wWait[0]=0x64;//gas wait 0 register |
| christodoulos | 6:10523c9e9b68 | 33 | wWait[1]=0x59; //100 ms |
| christodoulos | 6:10523c9e9b68 | 34 | char rWait[1]; |
| christodoulos | 6:10523c9e9b68 | 35 | |
| christodoulos | 7:90753ca349c8 | 36 | bme.write(BMEaddr,wWait,2); |
| christodoulos | 7:90753ca349c8 | 37 | bme.read(BMEaddr,rWait,1); |
| christodoulos | 7:90753ca349c8 | 38 | //ble.printf("Wait: %x\n",rWait[0]); |
| christodoulos | 6:10523c9e9b68 | 39 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 40 | |
| christodoulos | 6:10523c9e9b68 | 41 | //GET HEAT SETTING PARAMETERS |
| christodoulos | 6:10523c9e9b68 | 42 | char wg1[1]; |
| christodoulos | 6:10523c9e9b68 | 43 | wg1[0]=0xED; |
| christodoulos | 6:10523c9e9b68 | 44 | char rg1[1]; |
| christodoulos | 6:10523c9e9b68 | 45 | char wg2M[1]; |
| christodoulos | 6:10523c9e9b68 | 46 | char wg2L[1]; |
| christodoulos | 6:10523c9e9b68 | 47 | char rg2M[1]; |
| christodoulos | 6:10523c9e9b68 | 48 | char rg2L[1]; |
| christodoulos | 6:10523c9e9b68 | 49 | wg2M[0]=0xEC; |
| christodoulos | 6:10523c9e9b68 | 50 | wg2L[0]=0xEB; |
| christodoulos | 6:10523c9e9b68 | 51 | char wg3[1]; |
| christodoulos | 6:10523c9e9b68 | 52 | wg3[0]=0xEE; |
| christodoulos | 6:10523c9e9b68 | 53 | char rg3[1]; |
| christodoulos | 6:10523c9e9b68 | 54 | char wHR[1]; |
| christodoulos | 6:10523c9e9b68 | 55 | wHR[0]=0x02; |
| christodoulos | 6:10523c9e9b68 | 56 | char rHR[1]; |
| christodoulos | 6:10523c9e9b68 | 57 | char wHV[1]; |
| christodoulos | 6:10523c9e9b68 | 58 | wHV[0]=0x00; |
| christodoulos | 6:10523c9e9b68 | 59 | char rHV[1]; |
| christodoulos | 6:10523c9e9b68 | 60 | |
| christodoulos | 7:90753ca349c8 | 61 | bme.write(BMEaddr,wg1,1); |
| christodoulos | 7:90753ca349c8 | 62 | bme.read(BMEaddr,rg1,1);//get g1 |
| christodoulos | 6:10523c9e9b68 | 63 | int g1=rg1[0]; |
| christodoulos | 7:90753ca349c8 | 64 | //ble.printf("G1 hex: %x, G1 int: %d\n",rg1[0], g1); |
| christodoulos | 6:10523c9e9b68 | 65 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 66 | |
| christodoulos | 7:90753ca349c8 | 67 | bme.write(BMEaddr,wg2M,1); |
| christodoulos | 7:90753ca349c8 | 68 | bme.read(BMEaddr,rg2M,1);//get g2m |
| christodoulos | 6:10523c9e9b68 | 69 | |
| christodoulos | 7:90753ca349c8 | 70 | bme.write(BMEaddr,wg2L,1); |
| christodoulos | 7:90753ca349c8 | 71 | bme.read(BMEaddr,rg2L,1);//get g2l |
| christodoulos | 7:90753ca349c8 | 72 | //ble.printf("G2M hex: %x, G2L hex: %x\n",rg2M[0], rg2L[0]); |
| christodoulos | 6:10523c9e9b68 | 73 | int g2=((rg2M[0]<<8)+rg2L[0]); |
| christodoulos | 7:90753ca349c8 | 74 | //ble.printf("G2 hex: %x, G2 int: %d\n",g2, g2); |
| christodoulos | 6:10523c9e9b68 | 75 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 76 | |
| christodoulos | 7:90753ca349c8 | 77 | bme.write(BMEaddr,wg3,1); |
| christodoulos | 7:90753ca349c8 | 78 | bme.read(BMEaddr,rg3,1);//get g3 |
| christodoulos | 6:10523c9e9b68 | 79 | int g3=rg3[0]; |
| christodoulos | 7:90753ca349c8 | 80 | //ble.printf("G3 hex: %x, G3 int: %d\n",rg3[0],g3); |
| christodoulos | 6:10523c9e9b68 | 81 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 82 | |
| christodoulos | 7:90753ca349c8 | 83 | bme.write(BMEaddr,wHR,1); |
| christodoulos | 7:90753ca349c8 | 84 | bme.read(BMEaddr,rHR,1);//get heating range |
| christodoulos | 6:10523c9e9b68 | 85 | int HR=((rHR[0]<<2)>>6); |
| christodoulos | 7:90753ca349c8 | 86 | //ble.printf("HR hex: %x, HR int: %d\n",rHR[0], HR); |
| christodoulos | 6:10523c9e9b68 | 87 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 88 | |
| christodoulos | 7:90753ca349c8 | 89 | bme.write(BMEaddr,wHV,1); |
| christodoulos | 7:90753ca349c8 | 90 | bme.read(BMEaddr,rHV,1);//get g3 |
| christodoulos | 6:10523c9e9b68 | 91 | int HV=rHV[0]; |
| christodoulos | 7:90753ca349c8 | 92 | //ble.printf("HV hex: %x, HV int: %d\n",rHV[0], HV); |
| christodoulos | 6:10523c9e9b68 | 93 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 94 | |
| christodoulos | 6:10523c9e9b68 | 95 | double var1=(g1/16.0)+49.0; |
| christodoulos | 6:10523c9e9b68 | 96 | double var2= ((g2 / 32768.0) * 0.0005) + 0.00235; |
| christodoulos | 6:10523c9e9b68 | 97 | double var3=g3 / 1024.0; |
| christodoulos | 6:10523c9e9b68 | 98 | double var4 = var1 * (1.0 + (var2 *3000)); //target temp 300c |
| christodoulos | 6:10523c9e9b68 | 99 | double var5= var4 + (var3 * 24.0);//ambient 24c |
| christodoulos | 6:10523c9e9b68 | 100 | uint8_t heat=(3.4 * ((var5 * (4.0 / (4.0 + HR)) * (1.0/(1.0 +(HV * 0.002)))) - 25)); |
| christodoulos | 7:90753ca349c8 | 101 | //ble.printf("Re heat: %d\n",heat); |
| christodoulos | 6:10523c9e9b68 | 102 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 103 | |
| christodoulos | 6:10523c9e9b68 | 104 | //END OF HEAT SETTING PARAMETERS |
| christodoulos | 6:10523c9e9b68 | 105 | |
| christodoulos | 6:10523c9e9b68 | 106 | char wRes[2]; |
| christodoulos | 6:10523c9e9b68 | 107 | wRes[0]=0x5A;//gas wait 0 heat setting |
| christodoulos | 6:10523c9e9b68 | 108 | wRes[1]=0x59; //??? |
| christodoulos | 6:10523c9e9b68 | 109 | char rRes[1]; |
| christodoulos | 6:10523c9e9b68 | 110 | |
| christodoulos | 7:90753ca349c8 | 111 | bme.write(BMEaddr,wRes,2); |
| christodoulos | 7:90753ca349c8 | 112 | bme.read(BMEaddr,rRes,1); |
| christodoulos | 7:90753ca349c8 | 113 | //ble.printf("Heater resistance: %x\n",rRes[0]); |
| christodoulos | 6:10523c9e9b68 | 114 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 115 | |
| christodoulos | 6:10523c9e9b68 | 116 | char wCG[2]; |
| christodoulos | 6:10523c9e9b68 | 117 | wCG[0]=0x71;//control gas register |
| christodoulos | 6:10523c9e9b68 | 118 | wCG[1]=0x0; |
| christodoulos | 6:10523c9e9b68 | 119 | char rCG[1]; |
| christodoulos | 6:10523c9e9b68 | 120 | |
| christodoulos | 7:90753ca349c8 | 121 | bme.write(BMEaddr,wCG,2); |
| christodoulos | 7:90753ca349c8 | 122 | bme.read(BMEaddr,rCG,1); |
| christodoulos | 7:90753ca349c8 | 123 | //ble.printf("Control gas: %x\n",rCG[0]); |
| christodoulos | 6:10523c9e9b68 | 124 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 125 | |
| christodoulos | 6:10523c9e9b68 | 126 | char wRG[2]; |
| christodoulos | 6:10523c9e9b68 | 127 | wRG[0]=0x71;//control gas register |
| christodoulos | 6:10523c9e9b68 | 128 | wRG[1]=0x10; |
| christodoulos | 6:10523c9e9b68 | 129 | char rRG[1]; |
| christodoulos | 6:10523c9e9b68 | 130 | |
| christodoulos | 7:90753ca349c8 | 131 | bme.write(BMEaddr,wRG,2); |
| christodoulos | 7:90753ca349c8 | 132 | bme.read(BMEaddr,rRG,1); |
| christodoulos | 7:90753ca349c8 | 133 | //ble.printf("Run gas: %x\n",rRG[0]); |
| christodoulos | 6:10523c9e9b68 | 134 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 135 | |
| christodoulos | 6:10523c9e9b68 | 136 | |
| mbed_official | 0:b3b93997a0a6 | 137 | |
| christodoulos | 6:10523c9e9b68 | 138 | |
| christodoulos | 6:10523c9e9b68 | 139 | |
| christodoulos | 7:90753ca349c8 | 140 | wPres[1]=0x55; //01010101 (kept the temperature and prssure, enabled measurement |
| christodoulos | 6:10523c9e9b68 | 141 | |
| christodoulos | 6:10523c9e9b68 | 142 | ///GET GAS |
| christodoulos | 6:10523c9e9b68 | 143 | char wGasL[1]; |
| christodoulos | 6:10523c9e9b68 | 144 | wGasL[0]=0x2B; |
| christodoulos | 6:10523c9e9b68 | 145 | char wGasM[1]; |
| christodoulos | 6:10523c9e9b68 | 146 | wGasM[0]=0x2A; |
| christodoulos | 6:10523c9e9b68 | 147 | char rGasL[1]; |
| christodoulos | 6:10523c9e9b68 | 148 | char rGasM[1]; |
| christodoulos | 6:10523c9e9b68 | 149 | |
| christodoulos | 6:10523c9e9b68 | 150 | //GET TEMP |
| christodoulos | 6:10523c9e9b68 | 151 | char wTempL[1]; |
| christodoulos | 6:10523c9e9b68 | 152 | char wTempM[1]; |
| christodoulos | 6:10523c9e9b68 | 153 | wTempL[0]=0x23; |
| christodoulos | 6:10523c9e9b68 | 154 | wTempM[0]=0x22; |
| christodoulos | 6:10523c9e9b68 | 155 | char rTempL[1]; |
| christodoulos | 6:10523c9e9b68 | 156 | char rTempM[1]; |
| christodoulos | 6:10523c9e9b68 | 157 | |
| christodoulos | 6:10523c9e9b68 | 158 | while(1) { |
| christodoulos | 7:90753ca349c8 | 159 | bme.write(BMEaddr,wPres,2); //enables measurement |
| christodoulos | 7:90753ca349c8 | 160 | bme.read(BMEaddr,rPres,1); |
| mbed_official | 0:b3b93997a0a6 | 161 | |
| christodoulos | 7:90753ca349c8 | 162 | bme.write(BMEaddr,wGasL,1); |
| christodoulos | 7:90753ca349c8 | 163 | bme.read(BMEaddr,rGasL,1); //bits 7:6 are significant |
| christodoulos | 7:90753ca349c8 | 164 | bme.write(BMEaddr,wGasM,1); |
| christodoulos | 7:90753ca349c8 | 165 | bme.read(BMEaddr,rGasM,1); //whole byte is significant |
| christodoulos | 6:10523c9e9b68 | 166 | |
| christodoulos | 7:90753ca349c8 | 167 | bme.write(BMEaddr,wTempL,1); |
| christodoulos | 7:90753ca349c8 | 168 | bme.read(BMEaddr,rTempL,1); |
| christodoulos | 7:90753ca349c8 | 169 | bme.write(BMEaddr,wTempM,1); |
| christodoulos | 7:90753ca349c8 | 170 | bme.read(BMEaddr,rTempM,1); |
| christodoulos | 6:10523c9e9b68 | 171 | |
| christodoulos | 6:10523c9e9b68 | 172 | int temp =(rTempM[0]<<8)+rTempL[0]; |
| christodoulos | 6:10523c9e9b68 | 173 | 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 | 174 | |
| christodoulos | 7:90753ca349c8 | 175 | ble.printf("Gas: %i, Temp L: %x, Temp H: %x\n",gas, rTempL[0],rTempM[0]); |
| christodoulos | 6:10523c9e9b68 | 176 | wait(0.1); |
| christodoulos | 6:10523c9e9b68 | 177 | } |
| mbed_official | 0:b3b93997a0a6 | 178 | |
| mbed_official | 0:b3b93997a0a6 | 179 | } |