[评测&赛事] 【瑞萨RA × Zephyr评测】DS18B20 串口温度计

[复制链接]
16|0
无垠的广袤 发表于 2026-4-24 20:01 | 显示全部楼层 |阅读模式
, , , ,

【瑞萨RA × Zephyr评测】DS18B20 串口温度计

本文介绍了瑞萨 FPB-RA6E2 开发板使用 Zephyr 开发环境和 VS Code 实现 DS18B20 传感器获取环境温度并串口打印的项目设计。

项目介绍

  • 准备工作:硬件连接、开发环境搭建等;
  • 工程代码:流程图、关键代码、配置文件等;
  • 编译上传:工程编译、固件上传等流程;
  • 效果演示:使用串口调试工具获取 DS18B20 温度数据。

硬件连接

DS18B20 FPB-RA6E2
VCC 3V3
DATA P500(Arduino D4 引脚)
GND GND

实物连接

hardware_ds18b20.jpg

环境搭建

流程图

flowchart_ds18b20_print.jpg

工程代码

打开 .\Zephyr\ds18b20\src\main.c 主程序文件,代码如下

/*
 * Copyright (c) 2022 Thomas Stranger
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/sensor.h>

/*
 * Get a device structure from a devicetree node with compatible
 * "maxim,ds18b20". (If there are multiple, just pick one.)
 */
static const struct device *get_ds18b20_device(void)
{
	const struct device *const dev = DEVICE_DT_GET_ANY(maxim_ds18b20);

	if (dev == NULL) {
		/* No such node, or the node does not have status "okay". */
		printk("\nError: no device found.\n");
		return NULL;
	}

	if (!device_is_ready(dev)) {
		printk("\nError: Device \"%s\" is not ready; "
		       "check the driver initialization logs for errors.\n",
		       dev->name);
		return NULL;
	}

	printk("Found device \"%s\", getting sensor data\n", dev->name);
	return dev;
}

int main(void)
{
	const struct device *dev = get_ds18b20_device();
	int res;

	if (dev == NULL) {
		return 0;
	}

	while (true) {
		struct sensor_value temp;

		res = sensor_sample_fetch(dev);
		if (res != 0) {
			printk("sample_fetch() failed: %d\n", res);
			return res;
		}

		res = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
		if (res != 0) {
			printk("channel_get() failed: %d\n", res);
			return res;
		}

		printk("Temp: %d.%06d\n", temp.val1, temp.val2);
		k_sleep(K_MSEC(2000));
	}

	return 0;
}

保存代码。

配置文件

新建 ...\Zephyr\ds18b20\boards\fpb_ra6e2.overlay 文件,并添加如下代码

/ {
	w1_master_0: w1_master {
		compatible = "zephyr,w1-gpio";
		gpios = <&ioport5 0 GPIO_PULL_UP>;
		status = "okay";
	};
};

&ioport5 {
	status = "okay";
};

保存代码。

编译上传

代码和配置完成后,执行工程编译和固件上传指令。

工程编译

进入 zephyr 所在文件夹,构建目标工程

ds18b20_build.jpg

固件上传

待编译完成,连接开发板,执行指令

ds18b20_upload.jpg

效果演示

使用 MobaXterm 软件连接 J-LINK 虚拟串口,波特率115200,可获取 DS18B20 采集的环境温湿度信息;

ds18b20_print.jpg

总结

本文介绍了瑞萨 FPB-RA6E2 开发板使用 Zephyr 开发环境结合 VS Code 驱动 DS18B20 传感器获取环境温度并串口打印的项目设计,为相关产品的快速开发和应用设计提供了参考。

您需要登录后才可以回帖 登录 | 注册

本版积分规则

41

主题

101

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部
0