|
自己编写的基于IAR环境下STM32F103单片机读取BME280程序,没有太多的寄存器操作,简单易懂,可以直接调用,上电后只要对IO口初始化,调用函数Bme_ReadDigValue()读取校准寄存器参数并保存,再调用函数Bme_Init()初始化之后就可以用函数Bme_ReadStatus()读取传感器状态判断是否读取数据了,也可不判断传感器状态直接读取数据,读取数据直接调用函数ReadBme280()。
单片机源程序:- void ReadBme280()
- {
- u32 value_ad;
- double var1;
- double var2;
- double var3;
-
- double var4;
- double var5;
- double var6;
-
- double pressure;
- double pressure_min = 30000.0;
- double pressure_max = 110000.0;
-
- double humidity;
- double humidity_min = 0.0;
- double humidity_max = 100.0;
-
- u8 temp[10];
-
- ///////////////// temp /////////////////
- Bme_Read(Adds_RegValueTempStart,temp,3);
- value_ad = ( ((temp[0]<<16)+(temp[1]<<8)+(temp[2]&0xf0))>>4 );
- var1 = ( ((value_ad>>3)-(Bme280.dig_T1<<1))*Bme280.dig_T2 )>>11;
- var2 = (((((value_ad>>4)-Bme280.dig_T1)*((value_ad>>4)-Bme280.dig_T1))>>12)*Bme280.dig_T3)>>14;
- Bme280.t_fine = var1+var2;
- Bme280.temp = ( (Bme280.t_fine*5+128)>>8);
- Bme280.temp = Bme280.temp;
- ///////////////// press /////////////////
- Bme_Read(Adds_RegValuePressStart,temp,3);
- value_ad = ( ((temp[0]<<16)+(temp[1]<<8)+(temp[2]&0xf0))>>4 );
- var1 = ((double)Bme280.t_fine / 2.0) - 64000.0;
- var2 = var1 * var1 * ((double)Bme280.dig_P6) / 32768.0;
- var2 = var2 + var1 * ((double)Bme280.dig_P5) * 2.0;
- var2 = (var2 / 4.0) + (((double)Bme280.dig_P4) * 65536.0);
- var3 = ((double)Bme280.dig_P3) * var1 * var1 / 524288.0;
- var1 = (var3 + ((double)Bme280.dig_P2) * var1) / 524288.0;
- var1 = (1.0 + var1 / 32768.0) * ((double)Bme280.dig_P1);
- if (var1 > (0.0))
- {
- pressure = 1048576.0 - (double)value_ad;
- pressure = (pressure - (var2 / 4096.0)) * 6250.0 / var1;
- var1 = ((double)Bme280.dig_P9) * pressure * pressure / 2147483648.0;
- var2 = pressure * ((double)Bme280.dig_P8) / 32768.0;
- pressure = pressure + (var1 + var2 + ((double)Bme280.dig_P7)) / 16.0;
- if (pressure < pressure_min)
- {
- pressure = pressure_min;
- }
- else if (pressure > pressure_max)
- {
- pressure = pressure_max;
- }
- }
- else
- {
- pressure = pressure_min;
- }
- Bme280.press = pressure;
- Bme280.press = Bme280.press*100;
- ///////////////// humidity /////////////////
- Bme_Read(Adds_RegValueHumStart,temp,2);
- value_ad = ( (temp[0]<<8)+temp[1] );
- var1 = ((double)Bme280.t_fine) - 76800.0;
- var2 = (((double)Bme280.dig_H4) * 64.0 + (((double)Bme280.dig_H5) / 16384.0) * var1);
- var3 = value_ad - var2;
- var4 = ((double)Bme280.dig_H2) / 65536.0;
- var5 = (1.0 + (((double)Bme280.dig_H3) / 67108864.0) * var1);
- var6 = 1.0 + (((double)Bme280.dig_H6) / 67108864.0) * var1 * var5;
- var6 = var3 * var4 * (var5 * var6);
- humidity = var6 * (1.0 - ((double)Bme280.dig_H1) * var6 / 524288.0);
- if (humidity > humidity_max)
- {
- humidity = humidity_max;
- }
- else if (humidity < humidity_min)
- {
- humidity = humidity_min;
- }
- Bme280.hum = humidity*100;// 结果包含2位小数
- }
- void BmeReset()
- {
- u8 temp[50];
- temp[0] = CMD_Bme280Reset;
- Bme_Write(Reg_Bme280Reset,temp,1);
- }
复制代码
下载:
|
|