Fuckup with I2C

This commit is contained in:
BlubbFish 2019-06-30 19:23:45 +02:00
parent 588e778f10
commit 311b8d2b1b
2 changed files with 258 additions and 218 deletions

View File

@ -24,17 +24,23 @@ namespace BlubbFish.Iot.Thermometer.Librarys {
public void Measure() { public void Measure() {
this._adc_T = (Int32)this.Read20((Byte)Register.BME280_REG_TEMP_DATA); this._adc_T = (Int32)this.Read20((Byte)Register.BME280_REG_TEMP_DATA);
this._adc_P = (Int32)this.Read20((Byte)Register.BME280_REG_PRESS_DATA); this._adc_P = (Int32)this.Read20((Byte)Register.BME280_REG_PRESS_DATA);
this._adc_H = this.Read16((Byte)Register.BME280_REG_HUM_DATA); //this._adc_H = this.Read16((Byte)Register.BME280_REG_HUM_DATA);
} }
public Double GetTemperature() { public Double GetTemperature() {
Int32 adc_T = this._adc_T; Int32 adc_T = this._adc_T;
Int32 var1, var2; //Int32 var1, var2;
var1 = (((adc_T >> 3) - (this._dig_T1 << 1)) * this._dig_T2) >> 11; Double var1, var2;
var1 = (adc_T / 16384.0 - this._dig_T1 / 1024.0) * this._dig_T2;
var2 = ((adc_T / 131072.0 - this._dig_T1 / 8192.0) * (adc_T / 131072.0 - this._dig_T1 / 8192.0)) * this._dig_T3;
this._t_fine = (Int32)(var1 + var2);
return (var1 + var2) / 5120.0;
/*var1 = (((adc_T >> 3) - (this._dig_T1 << 1)) * this._dig_T2) >> 11;
var2 = (((((adc_T >> 4) - this._dig_T1) * ((adc_T >> 4) - this._dig_T1)) >> 12) * this._dig_T3) >> 14; var2 = (((((adc_T >> 4) - this._dig_T1) * ((adc_T >> 4) - this._dig_T1)) >> 12) * this._dig_T3) >> 14;
this._t_fine = var1 + var2; this._t_fine = var1 + var2;
return ((Double)(this._t_fine * 5 + 128 >> 8)) / 100; return ((Double)(this._t_fine * 5 + 128 >> 8)) / 100;*/
/*adc_T >>= 4; /*adc_T >>= 4;
var1 = (((adc_T >> 3) - ((int32_t)(this->_dig_T1 << 1))) * ((int32_t)this->_dig_T2)) >> 11; var1 = (((adc_T >> 3) - ((int32_t)(this->_dig_T1 << 1))) * ((int32_t)this->_dig_T2)) >> 11;
@ -46,7 +52,24 @@ namespace BlubbFish.Iot.Thermometer.Librarys {
public Double GetPressure() { public Double GetPressure() {
Int32 adc_P = this._adc_P; Int32 adc_P = this._adc_P;
Int64 var1, var2, P;
Double var1, var2, p;
var1 = (this._t_fine / 2.0) - 64000.0;
var2 = var1 * var1 * this._dig_P6 / 32768.0;
var2 = var2 + var1 * this._dig_P5 * 2.0;
var2 = (var2 / 4.0) + (this._dig_P4 * 65536.0);
var1 = (this._dig_P3 * var1 * var1 / 524288.0 + this._dig_P2 * var1) / 524288.0;
var1 = (1.0 + var1 / 32768.0) * this._dig_P1;
if(var1 == 0.0) {
return 0; // avoid exception caused by division by zero
}
p = 1048576.0 - adc_P;
p = (p - (var2 / 4096.0)) * 6250.0 / var1;
var1 = this._dig_P9 * p * p / 2147483648.0;
var2 = p * this._dig_P8 / 32768.0;
return (p + (var1 + var2 + this._dig_P7) / 16.0) / 100.0;
/*Int64 var1, var2, P;
var1 = ((Int64)this._t_fine) - 128000; var1 = ((Int64)this._t_fine) - 128000;
var2 = var1 * var1 * this._dig_P6; var2 = var1 * var1 * this._dig_P6;
@ -62,7 +85,8 @@ namespace BlubbFish.Iot.Thermometer.Librarys {
var1 = (this._dig_P9 * (P >> 13) * (P >> 13)) >> 25; var1 = (this._dig_P9 * (P >> 13) * (P >> 13)) >> 25;
var2 = (this._dig_P8 * P) >> 19; var2 = (this._dig_P8 * P) >> 19;
P = ((P + var1 + var2) >> 8) + (((Int64)this._dig_P7) << 4); P = ((P + var1 + var2) >> 8) + (((Int64)this._dig_P7) << 4);
return ((Double)((UInt32)P)) / 25600; return ((Double)((UInt32)P)) / 25600;*/
/*adc_P >>= 4; /*adc_P >>= 4;
var1 = ((int64_t)t_fine) - 128000; var1 = ((int64_t)t_fine) - 128000;
@ -84,7 +108,21 @@ namespace BlubbFish.Iot.Thermometer.Librarys {
public Double GetHumidity() { public Double GetHumidity() {
Int32 adc_H = this._adc_H; Int32 adc_H = this._adc_H;
Int32 H;
Double var_H;
var_H = this._t_fine - 76800.0;
var_H = (adc_H - (this._dig_H4 * 64.0 + this._dig_H5 / 16384.0 * var_H)) * (this._dig_H2 / 65536.0 * (1.0 + this._dig_H6 / 67108864.0 * var_H * (1.0 + this._dig_H3 / 67108864.0 * var_H)));
var_H = var_H * (1.0 - this._dig_H1 * var_H / 524288.0);
return var_H;
return var_H > 100 ? 100 : var_H < 0 ? 0: var_H;
/*if(var_H > 100.0) {
var_H = 100.0;
} else if(var_H < 0.0) {
var_H = 0.0;
}
return var_H;*/
/*Int32 H;
H = this._t_fine - 76800; H = this._t_fine - 76800;
H = ((((adc_H << 14) - (this._dig_H4 << 20) - (this._dig_H5 * H)) + 16384) >> 15) * H = ((((adc_H << 14) - (this._dig_H4 << 20) - (this._dig_H5 * H)) + 16384) >> 15) *
@ -92,7 +130,7 @@ namespace BlubbFish.Iot.Thermometer.Librarys {
H = H - (((((H >> 15) * (H >> 15)) >> 7) * this._dig_H1) >> 4); H = H - (((((H >> 15) * (H >> 15)) >> 7) * this._dig_H1) >> 4);
H = H < 0 ? 0 : H; H = H < 0 ? 0 : H;
H = H > 419430400 ? 419430400 : H; H = H > 419430400 ? 419430400 : H;
return ((Double)(H >> 12)) / 1024; return ((Double)(H >> 12)) / 1024;*/
} }
public void ReadTrimming() { public void ReadTrimming() {
@ -189,7 +227,7 @@ namespace BlubbFish.Iot.Thermometer.Librarys {
; ;
return this->w->read();*/ return this->w->read();*/
private UInt16 Read16(Byte reg) => this.w.ReadAddressWord(reg); private UInt16 Read16LE(Byte reg) => this.w.ReadAddressWord(reg);
/*uint8_t msb, lsb; /*uint8_t msb, lsb;
this->w->beginTransmission(address); this->w->beginTransmission(address);
this->w->write(reg); this->w->write(reg);
@ -201,8 +239,8 @@ namespace BlubbFish.Iot.Thermometer.Librarys {
lsb = this->w->read(); lsb = this->w->read();
return (uint16_t)msb << 8 | lsb;*/ return (uint16_t)msb << 8 | lsb;*/
private UInt16 Read16LE(Byte reg) { private UInt16 Read16(Byte reg) {
UInt16 data = this.Read16(reg); UInt16 data = this.Read16LE(reg);
return (UInt16)((data >> 8) | (data << 8)); return (UInt16)((data >> 8) | (data << 8));
} }

View File

@ -12,12 +12,14 @@ namespace BlubbFish.Iot.Thermometer {
this.tls.Begin(); this.tls.Begin();
this.bme.Begin(); this.bme.Begin();
while(true) { while(true) {
this.tls.Measure(); //this.tls.Measure();
this.bme.Measure(); this.bme.Measure();
Console.WriteLine(this.tls.GetLux()+" lux"); //Console.WriteLine(this.tls.GetLux()+" lux");
Console.WriteLine(this.bme.GetHumidity() + " Hm%");
Console.WriteLine(this.bme.GetPressure() + " mbHp");
Console.WriteLine(this.bme.GetTemperature() + " °C"); Console.WriteLine(this.bme.GetTemperature() + " °C");
Console.WriteLine(this.bme.GetPressure() + " mbHp");
Console.WriteLine(this.bme.GetHumidity() + " Hm%");
System.Threading.Thread.Sleep(1000); System.Threading.Thread.Sleep(1000);
} }
} }