-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
96 lines (77 loc) · 2.99 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2023 Alexey Talts
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "stdio.h"
#include "driver/i2c.h"
#include "stdlib.h"
#include "freertos/FreeRTOS.h"
#include "ms5xxx.h"
#include "pin.h"
/****************************************************************************************
*
* This is a sample demo code to get temperature and pressure readings from 2 sensors:
* - MS5611 and
* - MS5803_01BA
*
* In my case both sensors are wired to the same I2C bus and use default addresses:
* - MS5803 = 0x76,
* - MS5611 = 0x77.
*
* The MS5xxx library supports both sensors. Currently MS5803_01BA version supported.
* If needed, anyone can modify MS5xxx.H and MS5xxx.C files to add calibration routines
* as per corresponding datasheets of other sensor versions.
*
*****************************************************************************************/
void app_main(){
MS5xxx_HANDLE *ptrMs5803, *ptrMs5611;
int32_t temp_5611, press_5611, temp_5803, press_5803;
printf("MS5611 / MS5803_01BA sensor demo project\n"); fflush(0);
// NOTE: calling fflush(0) after printf() is just my common practice in the FreeRTOS mulitask environment.
// Otherwise due to task switching and buffering the debug info might not be purged on time
while(1){
// Allocating resources
printf("Starting MS5611 sensor driver... ");
if(!(ptrMs5611=ms5xxx_DriverCreate(MS5xxx_TYPE_5611, I2C_NUM_0, 0x77, PIN_SDA, PIN_SCL, 400000))){
printf("FAILED\n"); fflush(0);
// ...
// ...
return;
}
else{
printf("OK\n");
}
printf("Starting MS5801 sensor driver... ");
if(!(ptrMs5803=ms5xxx_DriverCreate(MS5xxx_TYPE_5803, I2C_NUM_0, 0x76, PIN_SDA, PIN_SCL, 400000))){
printf("FAILED\n"); fflush(0);
// ...
// ...
return;
}
else{
printf("OK\n");
}
fflush(0);
// going through 10 cycles
for(uint8_t i = 0; i < 10; i++){
// get data from both sensors
ms5xxx_getSensorData(ptrMs5611, MS5xxx_RESOLUTION_512, &temp_5611, &press_5611);
ms5xxx_getSensorData(ptrMs5803, MS5xxx_RESOLUTION_512, &temp_5803, &press_5803);
// display human-readable values in float format
printf("Temperature, C = %.02f (%.02f). Pressure, mbar = %.02f (%.02f)\n", (float) temp_5611/100, (float) temp_5803/100, (float)press_5611/100, (float)press_5803/100); fflush(0);
vTaskDelay(1000); // delay 1000 ticks. On 1000Hz system it gives 1 sec delay
}
// releasing the resources allocated
ms5xxx_DriverDelete(ptrMs5611);
ms5xxx_DriverDelete(ptrMs5803);
}
}