45 lines
995 B
C
45 lines
995 B
C
#include <zephyr/kernel.h>
|
|
#include <zephyr/device.h>
|
|
#include <zephyr/devicetree.h>
|
|
#include <zephyr/drivers/sensor.h>
|
|
#include <zephyr/logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(adc_dt_example, LOG_LEVEL_DBG);
|
|
|
|
/* Get the voltage divider device */
|
|
#define VOLTAGE_DIVIDER_NODE DT_NODELABEL(vdd_sense)
|
|
|
|
int main(void)
|
|
{
|
|
const struct device *vdd_dev = DEVICE_DT_GET(VOLTAGE_DIVIDER_NODE);
|
|
struct sensor_value val;
|
|
int err;
|
|
|
|
if (!device_is_ready(vdd_dev)) {
|
|
LOG_ERR("Voltage divider device not ready");
|
|
return 0;
|
|
}
|
|
|
|
LOG_INF("Voltage divider device ready!");
|
|
|
|
while (1) {
|
|
err = sensor_sample_fetch(vdd_dev);
|
|
if (err < 0) {
|
|
LOG_ERR("Could not fetch sample (%d)", err);
|
|
k_sleep(K_MSEC(1000));
|
|
continue;
|
|
}
|
|
|
|
err = sensor_channel_get(vdd_dev, SENSOR_CHAN_VOLTAGE, &val);
|
|
if (err < 0) {
|
|
LOG_ERR("Could not get channel (%d)", err);
|
|
k_sleep(K_MSEC(1000));
|
|
continue;
|
|
}
|
|
|
|
LOG_INF("Voltage reading: %d.%06d V", val.val1, val.val2);
|
|
|
|
k_sleep(K_MSEC(1000));
|
|
}
|
|
return 0;
|
|
} |