The acquisition of atmospheric pressure and temperature is also a necessary parameter in the accounting of atmospheric standards, we must know the pressure and temperature in order to calculate the parameters of the standard conditions, we need a can detect both the pressure and the temperature of the components.
1 , hardware overview
MS5837 pressure sensor is a kind of sensor which can be used on the circuit board to detect 10-1200mbar pressure range, the sensitivity is very high, it can detect the pressure change of 0.01mbar in theory, and there is no obvious change in the actual use process.
(1) Hardware Connection
MS5837 uses the I²C bus communication, and the STM32 MCU can realize the i²c communication. The hardware is connected in the following way:
We found that the onenet Kylin was led to the 4th and 5 pins of the J3, but we found the interface on the OLED is also this, so we use this directly, because the power supply is just right, specific location such as the red box:
(3) Register Allocation
MS5837 has only 5 basic commands: RESET, read factory calibration values, data 1 conversion (pressure value data), data 2 conversion (temperature value data) and read ADC conversion results. The specific allocations are as follows:
2 , Software design
Because the address of the MS5837 is fixed, an i²c bus can only hang 1 MS5837 modules. In order to make the program more portable, we do not use the direct manipulation of the hardware when we write the program, but use the function pointer to manipulate it, so we define:
/* Issued instructions to MS5837, instruction format is 1 bytes */
typedef void (*writecommandtoms5837type) (uint8_t deviceaddress,uint8_t command);
/* Read the value of multiple bytes of data from MS5837 */
typedef void (*readbytesfromms5837type) (uint8_t deviceaddress,uint8_t *pdata,uint16_t bytesnum);
The above two function pointers are implemented for hardware-to-read operations. Next we start writing code.
( 1 ) Reset Operation
The data flow for the reset operation is as shown, and only one command can be sent to complete:
/* Reset MS5837 Operation */
void ResetForMs5837 (uint8_t deviceaddress,writecommandtoms5837type WriteCommandToMs5837)
{
uint8_t Command=command_reset;
/* Reset Command Issued */
WriteCommandToMs5837 (Deviceaddress,command);
}
( 2 ) Read the calibration value
Calibration values are factory calibration of the various factors, each device has a difference, is fixed, only need to read once, a total of 6 coefficients, all 16 is an integer. The command to read the coefficients is sent first, then read on it, 1 reads each, and 6 reads at a time. The process data flow is as follows:
/* Read calibration data from MS5837 's Prom */
void Getcalibrationdata (uint8_t deviceaddress,uint16_t *calipara,writecommandtoms5837type WriteCommandToMs5837, Readbytesfromms5837type ReadBytesFromMs5837)
{
/*C1 Pressure Sensitivity */
calipara[0]=readpromfromms5837 (deviceaddress,command_prom_read_c1,writecommandtoms5837,readbytesfromms5837);
/*C2 Pressure Compensation Value */
calipara[1]=readpromfromms5837 (deviceaddress,command_prom_read_c2,writecommandtoms5837,readbytesfromms5837);
/*C3 pressure sensitivity temperature coefficient */
calipara[2]=readpromfromms5837 (deviceaddress,command_prom_read_c3,writecommandtoms5837,readbytesfromms5837);
/*C4 pressure compensation temperature coefficient */
calipara[3]=readpromfromms5837 (deviceaddress,command_prom_read_c4,writecommandtoms5837,readbytesfromms5837);
/*C5 Reference Temperature */
calipara[4]=readpromfromms5837 (deviceaddress,command_prom_read_c5,writecommandtoms5837,readbytesfromms5837);
Temperature coefficient of/*C6 temperature sensor */
calipara[5]=readpromfromms5837 (deviceaddress,command_prom_read_c6,writecommandtoms5837,readbytesfromms5837);
}
( 3 ) Read conversion value
Reading the result value of the conversion is our purpose, can read temperature and pressure of two, but only one read at a time. First Send command to set the acquisition pressure or temperature, and set the accuracy. It then sends the Read command and finally reads the corresponding value. The final physical value is calculated using the calibration factor.
/* Get conversion values, including temperature and pressure */
void Getconversionvalue (uint8_t deviceaddress,float *ppres,float *ptemp,uint16_t *calipara,uint16_t *semaphore, Writecommandtoms5837type Writecommandtoms5837,readbytesfromms5837type ReadBytesFromMs5837)
{
uint16_t Senst1; C1 pressure Sensitivity
uint16_t offt1; C2 Pressure compensation Value
uint16_t TCS; C3 pressure sensitivity temperature coefficient
uint16_t TCO; C4 pressure compensation temperature coefficient
uint16_t Tref; C5 Reference temperature
uint16_t Tempsens; Temperature coefficient of C6 temperature sensor
/* Read calibration data from MS5837 's Prom */
if (*semaphore>0)
{
Getcalibrationdata (deviceaddress,calipara,writecommandtoms5837,readbytesfromms5837);
*semaphore=*semaphore-1;
}
SENST1=CALIPARA[0];
OFFT1=CALIPARA[1];
TCS=CALIPARA[2];
TCO=CALIPARA[3];
TREF=CALIPARA[4];
TEMPSENS=CALIPARA[5];
uint32_t Digitalpressurevalue;
uint32_t Digitaltemperaturevalue;
/* Read the pressure data */
digitalpressurevalue=readconversionfromms5837 (deviceaddress,command_convertd1osr4096,writecommandtoms5837, ReadBytesFromMs5837);
Delayms (20);
/* Read temperature data */
digitaltemperaturevalue=readconversionfromms5837 (deviceaddress,command_convertd2osr4096,writecommandtoms5837, ReadBytesFromMs5837);
/* First-order correction of temperature */
int32_t DT;
int32_t temp;
dt=digitaltemperaturevalue-tref*256;
Temp= (int32_t) (2000+dt*tempsens/pow (2,23));
/* First-order correction of pressure */
int64_t off;
int64_t Sens;
int32_t pres;
Off= (int64_t) (Offt1*pow (2,17) + (TCO*DT)/pow (2,6));
Sens= (int64_t) (Senst1*pow (2,16) + (TCS*DT)/pow (2,7));
Pres= (int32_t) ((Digitalpressurevalue*sens/pow (2,21)-off)/pow (2,15));
/* Second-order correction of temperature and pressure */
int64_t ti=0;
int64_t offi=0;
int64_t sensi=0;
int64_t off2=0;
int64_t sens2=0;
if (temp<2000)
{
Ti= (int64_t) (11*dt*dt/pow (2,35));
Offi= (int64_t) (31* (temp-2000) * (temp-2000)/pow (2,3));
Sensi= (int64_t) (63* (temp-2000) * (temp-2000)/pow (2,5));
Off2=off-offi;
Sens2=sens-sensi;
temp=temp-(int32_t) ti;
Pres= (int32_t) ((Digitalpressurevalue*sens2/pow (2,21)-off2)/pow (2,15));
}
if (( -4000<=temp) && (temp<=8500))
{
*ptemp= (float) temp/100.0;
}
if ((1000<=pres) && (pres<=120000))
{
*ppres= (float) pres/100.0;
}
}
Finally, 2 writecommandtoms5837type (uint8_t deviceaddress,uint8_t command) are implemented when communication is realized in the STM32 I²c interface; readbytesfromms5837type (uint8_t deviceaddress,uint8_t *pdata,uint16_t bytesnum); functions are called, and a replacement platform can only be ported by rewriting the two functions.
3 , results show
After completing the above development, compile the download. We also uploaded data results to onenet, showing the results as follows:
The parameters such as atmospheric pressure, temperature and humidity are shown respectively.
Onenet Kylin seat Application development: acquisition of atmospheric pressure and other environmental parameters