first
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* SPL06 使用示例 —— 周期读取气压/温度并换算海拔。
|
||||
*
|
||||
* 验证标准(海平面附近):
|
||||
* 气压 ~95-105 kPa,温度接近室温,手指按压传感器气压明显上升。
|
||||
*/
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <math.h>
|
||||
|
||||
LOG_MODULE_REGISTER(app, LOG_LEVEL_INF);
|
||||
|
||||
/* 标准海平面气压,用于海拔换算;精确做法是用起飞点气压做 p0 */
|
||||
#define SEA_LEVEL_PRESS_KPA 101.325f
|
||||
|
||||
static const struct device *const baro = DEVICE_DT_GET(DT_NODELABEL(spl06));
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if (!device_is_ready(baro)) {
|
||||
LOG_ERR("SPL06 not ready");
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
struct sensor_value press, temp;
|
||||
|
||||
if (sensor_sample_fetch(baro) < 0) {
|
||||
LOG_ERR("fetch failed");
|
||||
continue;
|
||||
}
|
||||
if (sensor_channel_get(baro, SENSOR_CHAN_PRESS, &press) < 0 ||
|
||||
sensor_channel_get(baro, SENSOR_CHAN_AMBIENT_TEMP, &temp) < 0) {
|
||||
LOG_ERR("channel_get failed");
|
||||
continue;
|
||||
}
|
||||
|
||||
const float p = press.val1 + press.val2 / 1e6f; /* kPa */
|
||||
const float t = temp.val1 + temp.val2 / 1e6f; /* degC */
|
||||
const float alt = 44330.0f *
|
||||
(1.0f - powf(p / SEA_LEVEL_PRESS_KPA, 0.1903f));
|
||||
|
||||
LOG_INF("P = %.3f kPa T = %.2f C alt = %.1f m",
|
||||
(double)p, (double)t, (double)alt);
|
||||
|
||||
k_sleep(K_MSEC(200));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user