[分享] 《RT-Thread设备驱动开发指南》读书笔记3一DS18B20传感器应用

chrisrh   2023-9-25 15:04 楼主

 

rtthread最好用的地方就是 ,给我们提供了许多优秀的经过验证的代码,避免我们重复造轮子,加速应用快速开发。


比如配置一个DS18B20温度传感器

打开Setting~

首先在Drivers中打开传感器组件,接着添加软件包,选择DS18B20的软件包,使用最新版本即可

image.png  

添加DS18B20的软件包

image.png  

保存,自动链接,编译,整理错误~

编译后发现对uint8_t和int32_t没有适配,便在“sensor_dallas_ds18b20.h"中添加了#include "stdint.h"头文件

image.png  

修改DS18B20的一线读取IO口,组件中的程序是通过软件模拟一线通信进行的,IO的定义在ds18b20_sample.c

使用RT-Studio基于BluePi生成工程,我随便找了一个A7口,用作DS18B20的读写口;

/* Modify this pin according to the actual wiring situation */
#define DS18B20_DATA_PIN    GET_PIN(A, 7)

image.png  

再次编译,pass,下载至板中:

image.png  

对常用的传感器,生成工程,配置,编译,下载至板中,很是方便;

 

有很多经过验证的优秀代码供我们学习和借鉴。

 


 

程序分析:

首先作为传感器,在sensor.h中,可以明显的看到,rtthread将常用的传感器进行了分类,

同时也对常见的厂家、通信类型,以及传感器的单位也进行了分类

/* Sensor types */

#define RT_SENSOR_CLASS_NONE           (0)
#define RT_SENSOR_CLASS_ACCE           (1)  /* Accelerometer     */
#define RT_SENSOR_CLASS_GYRO           (2)  /* Gyroscope         */
#define RT_SENSOR_CLASS_MAG            (3)  /* Magnetometer      */
#define RT_SENSOR_CLASS_TEMP           (4)  /* Temperature       */
#define RT_SENSOR_CLASS_HUMI           (5)  /* Relative Humidity */
#define RT_SENSOR_CLASS_BARO           (6)  /* Barometer         */
#define RT_SENSOR_CLASS_LIGHT          (7)  /* Ambient light     */
#define RT_SENSOR_CLASS_PROXIMITY      (8)  /* Proximity         */
#define RT_SENSOR_CLASS_HR             (9)  /* Heart Rate        */
#define RT_SENSOR_CLASS_TVOC           (10) /* TVOC Level        */
#define RT_SENSOR_CLASS_NOISE          (11) /* Noise Loudness    */
#define RT_SENSOR_CLASS_STEP           (12) /* Step sensor       */
#define RT_SENSOR_CLASS_FORCE          (13) /* Force sensor      */
#define RT_SENSOR_CLASS_DUST           (14) /* Dust sensor       */
#define RT_SENSOR_CLASS_ECO2           (15) /* eCO2 sensor       */
#define RT_SENSOR_CLASS_GNSS           (16) /* GPS/GNSS sensor   */
#define RT_SENSOR_CLASS_TOF            (17) /* TOF sensor        */
#define RT_SENSOR_CLASS_SPO2           (18) /* SpO2 sensor       */
#define RT_SENSOR_CLASS_IAQ            (19) /* IAQ sensor.       */
#define RT_SENSOR_CLASS_ETOH           (20) /* EtOH sensor.      */
#define RT_SENSOR_CLASS_BP             (21) /* Blood Pressure    */

常见的传感器厂家

/* Sensor vendor types */

#define RT_SENSOR_VENDOR_UNKNOWN       (0)
#define RT_SENSOR_VENDOR_STM           (1)  /* STMicroelectronics */
#define RT_SENSOR_VENDOR_BOSCH         (2)  /* Bosch */
#define RT_SENSOR_VENDOR_INVENSENSE    (3)  /* Invensense */
#define RT_SENSOR_VENDOR_SEMTECH       (4)  /* Semtech */
#define RT_SENSOR_VENDOR_GOERTEK       (5)  /* Goertek */
#define RT_SENSOR_VENDOR_MIRAMEMS      (6)  /* MiraMEMS */
#define RT_SENSOR_VENDOR_DALLAS        (7)  /* Dallas */
#define RT_SENSOR_VENDOR_ASAIR         (8)  /* Aosong */
#define RT_SENSOR_VENDOR_SHARP         (9)  /* Sharp */
#define RT_SENSOR_VENDOR_SENSIRION     (10) /* Sensirion */
#define RT_SENSOR_VENDOR_TI            (11) /* Texas Instruments */
#define RT_SENSOR_VENDOR_PLANTOWER     (12) /* Plantower */
#define RT_SENSOR_VENDOR_AMS           (13) /* ams AG */
#define RT_SENSOR_VENDOR_MAXIM         (14) /* Maxim Integrated */

各传感器常用的标准单位

/* Sensor unit types */

#define  RT_SENSOR_UNIT_NONE           (0)
#define  RT_SENSOR_UNIT_MG             (1)  /* Accelerometer           unit: mG         */
#define  RT_SENSOR_UNIT_MDPS           (2)  /* Gyroscope               unit: mdps       */
#define  RT_SENSOR_UNIT_MGAUSS         (3)  /* Magnetometer            unit: mGauss     */
#define  RT_SENSOR_UNIT_LUX            (4)  /* Ambient light           unit: lux        */
#define  RT_SENSOR_UNIT_CM             (5)  /* Distance                unit: cm         */
#define  RT_SENSOR_UNIT_PA             (6)  /* Barometer               unit: pa         */
#define  RT_SENSOR_UNIT_PERMILLAGE     (7)  /* Relative Humidity       unit: permillage */
#define  RT_SENSOR_UNIT_DCELSIUS       (8)  /* Temperature             unit: dCelsius   */
#define  RT_SENSOR_UNIT_HZ             (9)  /* Frequency               unit: HZ         */
#define  RT_SENSOR_UNIT_ONE            (10) /* Dimensionless quantity  unit: 1          */
#define  RT_SENSOR_UNIT_BPM            (11) /* Heart rate              unit: bpm        */
#define  RT_SENSOR_UNIT_MM             (12) /* Distance                unit: mm         */
#define  RT_SENSOR_UNIT_MN             (13) /* Force                   unit: mN         */
#define  RT_SENSOR_UNIT_PPM            (14) /* Concentration           unit: ppm        */
#define  RT_SENSOR_UNIT_PPB            (15) /* Concentration           unit: ppb        */
#define  RT_SENSOR_UNIT_DMS            (16) /* Coordinates             unit: DMS        */
#define  RT_SENSOR_UNIT_DD             (17) /* Coordinates             unit: DD         */
#define  RT_SENSOR_UNIT_MGM3           (18) /* Concentration           unit: mg/m3      */
#define  RT_SENSOR_UNIT_MMHG           (19) /* Blood Pressure          unit: mmHg       */

常用的通信类型

/* Sensor communication interface types */
#define  RT_SENSOR_INTF_I2C            (1 << 0)
#define  RT_SENSOR_INTF_SPI            (1 << 1)
#define  RT_SENSOR_INTF_UART           (1 << 2)
#define  RT_SENSOR_INTF_ONEWIRE        (1 << 3)

接口函数:

struct rt_sensor_ops
{
    rt_size_t (*fetch_data)(struct rt_sensor_device *sensor, void *buf, rt_size_t len);
    rt_err_t (*control)(struct rt_sensor_device *sensor, int cmd, void *arg);
};

 

帮我们把传感器的共性都进行了分类,以做到通用框架

 


 

在ds18b20_sample.c中,通过“INIT_COMPONENT_EXPORT”对函数“rt_hw_ds18b20_port”初始化,选择传感器数据获取方式,并进行接口对接;

接口函数在sensor_dallas_ds18b20.c中,通过rt_hw_ds18b20_init()完成该传感器类型的确定,接口对接,以及注册到传感器列表;

static int rt_hw_ds18b20_port(void)
{
    struct rt_sensor_config cfg;
    
    cfg.intf.user_data = (void *)DS18B20_DATA_PIN;///* Private data for the sensor. ex. i2c addr,spi cs,control I/O */
    rt_hw_ds18b20_init("ds18b20", &cfg);
    
    return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_hw_ds18b20_port);

在ds18b20_sample.c中,通过“INIT_APP_EXPORT”初始化“ds18b20_read_temp_sample”函数,进行线程创建,将入口函数挂在到调度器上开始调度,若rt_device_find找到注册的传感器,则处理数据并打印输出;

static int ds18b20_read_temp_sample(void)
{
    rt_thread_t ds18b20_thread;

    ds18b20_thread = rt_thread_create("18b20tem",
                                      read_temp_entry,
                                      "temp_ds18b20",
                                      1024,
                                      RT_THREAD_PRIORITY_MAX / 2,
                                      20);
    if (ds18b20_thread != RT_NULL)
    {
        rt_thread_startup(ds18b20_thread);
    }

    return RT_EOK;
}
INIT_APP_EXPORT(ds18b20_read_temp_sample);

  下载到自己的F103小板中~~

image-20230925145952-1.png  

image.png

 

 



使用软件模拟RTC

image.png  

开启配置项,保存便会自动添加至工程,编译使用:

在串口中发送:date - get date and time or set (local timezone) [year month day hour min sec]     获取当前时间;

在串口中发送:date 2023 9 25 15 00 00,即修改rtthread程序内当前时间为2023.9.25.15:00:00,再通过date可以get当前最新时间;

image.png  

 

本帖最后由 chrisrh 于 2023-9-25 15:06 编辑

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复