打印
[活动专区]

【N32G430开发板试用】串口读取环境传感器模块数据

[复制链接]
750|14
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
dql2015|  楼主 | 2022-8-15 19:08 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 dql2015 于 2022-8-15 19:10 编辑

@安小芯


本文基于N32G430开发板的串口4读取了环境传感器模块SVM40的数据,SVM40是Sensirion基于VOC传感器SGP40和数字温湿度传感器SHT40的传感器模块。

SGP40主要参数如下:

SHT40主要参数如下:

SVM40模块内置了一颗STM32G0微控制器,通过I2C接口连接2颗传感器,Sensirion自定义了通信协议,模块对外提供串口和I2C接口,模块结构如图所示:

模块提供I2C和UART两种接口,本文采用UART接口,模块引脚含义如图所示:

svm40官方提供了串口驱动程序参考,只需要匹配串口初始化、发送数据、读取数据、微秒级延时四个接口就行了,具体就是填充sensirion_uart_hal.c文件里面的驱动接口。

串口发送数据:
int16_t sensirion_uart_hal_tx(uint16_t data_len, const uint8_t* data)
{
        for(int i=0;i<data_len;i++)
        {
          USART_Data_Send(UART4, data[i]);
    while (USART_Flag_Status_Get(UART4, USART_FLAG_TXDE) == RESET);
        }
    return data_len;
}


串口接收数据基于中断:
extern __IO int RxCounter;
extern uint8_t RxBuffer[64];
int16_t sensirion_uart_hal_rx(uint16_t max_data_len, uint8_t* data)
{
        if(RxCounter>0)
        {
                memcpy(data,RxBuffer,max_data_len);
                RxCounter = 0;
        }
        return max_data_len;
}
中断服务函数
extern __IO int RxCounter;
extern uint8_t RxBuffer[64];
void UART4_IRQHandler(void)
{
    if (USART_Interrupt_Status_Get(UART4, USART_INT_RXDNE) != RESET)
    {
        RxBuffer[RxCounter++] = USART_Data_Receive(UART4);
    }

}






测试代码:
/*
* Copyright (c) 2021, Sensirion AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
*   list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
*   this list of conditions and the following disclaimer in the documentation
*   and/or other materials provided with the distribution.
*
* * Neither the name of Sensirion AG nor the names of its
*   contributors may be used to endorse or promote products derived from
*   this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>  // printf

#include "sensirion_common.h"
#include "sensirion_uart_hal.h"
#include "svm40_uart.h"

/* TO USE CONSOLE OUTPUT (printf) YOU MAY NEED TO ADAPT THE
* INCLUDE ABOVE OR DEFINE IT ACCORDING TO YOUR PLATFORM.
* #define printf(...)
*/

int svm40_test(void)
{
    int16_t error = 0;

    error = sensirion_uart_hal_init();
    if (error) {
        printf("Error initializing UART: %i\n", error);
        return error;
    }
               
                error = svm40_device_reset();
                    if (error) {
        printf("Error executing svm40_device_reset(): %i\n", error);
        return error;
    }

    uint8_t serial_number[32];
    uint8_t serial_number_size = 32;
    error = svm40_get_serial_number(&serial_number[0], serial_number_size);
    if (error) {
        printf("Error executing svm40_get_serial_number(): %i\n", error);
    } else {
        printf("Serial number: %s\n", serial_number);
    }

    uint8_t product_type[32];
    uint8_t product_type_size = 32;
    error = svm40_get_product_type(&product_type[0], product_type_size);
    if (error) {
        printf("Error executing svm40_get_product_type(): %i\n", error);
    } else {
        printf("Product type: %s\n", product_type);
    }

    uint8_t product_name[32];
    uint8_t product_name_size = 32;
    error = svm40_get_product_name(&product_name[0], product_name_size);
    if (error) {
        printf("Error executing svm40_get_product_name(): %i\n", error);
    } else {
        printf("Product name: %s\n", product_name);
    }

    uint8_t firmware_major;
    uint8_t firmware_minor;
    bool firmware_debug;
    uint8_t hardware_major;
    uint8_t hardware_minor;
    uint8_t protocol_major;
    uint8_t protocol_minor;
    error = svm40_get_version(&firmware_major, &firmware_minor, &firmware_debug,
                              &hardware_major, &hardware_minor, &protocol_major,
                              &protocol_minor);
    if (error) {
        printf("Error executing svm40_get_version(): %i\n", error);
    } else {
        printf("Firmware: %i.%i Debug: %i\n", firmware_major, firmware_minor,
               firmware_debug);
        printf("Hardware: %i.%i\n", hardware_major, hardware_minor);
        printf("Protocol: %i.%i\n", protocol_major, protocol_minor);
    }

    if (firmware_major < 2) {
        printf("Your SVM40 firmware is out of date!\n");
    } else {
        uint8_t t_offset_buffer[2];
        uint8_t t_offset_size = 2;
        error = svm40_get_temperature_offset_for_rht_measurements(
            &t_offset_buffer[0], t_offset_size);
        int16_t t_offset =
            sensirion_common_bytes_to_int16_t(&t_offset_buffer[0]);
        if (error) {
            printf("Error executing "
                   "svm40_get_temperature_offset_for_rht_measurements(): %i\n",
                   error);
        } else {
            printf("Temperature Offset: %i ticks\n", t_offset);
        }
    }

    // Start Measurement
    error = svm40_start_continuous_measurement();
    if (error) {
        printf("Error executing svm40_start_continuous_measurement(): %i\n",
               error);
    }

    for (;;) {
        // Read Measurement
        sensirion_uart_hal_sleep_usec(1000000);
        int16_t voc_index;
        int16_t humidity;
        int16_t temperature;
        error = svm40_read_measured_values_as_integers(&voc_index, &humidity,
                                                       &temperature);
        if (error)
                                {
            printf("Error executing svm40_read_measured_values_as_integers(): ""%i\n",error);
        }
                                else
                                {
            printf("Voc index:%.2f Temperature:%.2f Humidity:%.2f\n", voc_index/10.0,temperature/200.0,humidity/100.0);
        }
    }

    error = svm40_stop_measurement();
    if (error) {
        printf("Error executing svm40_stop_measurement(): %i\n", error);
    }

    return 0;
}
模块接到串口4:PB0、PB1

日志结果:


代码:
uart_svm40_test.zip (406.43 KB)

使用特权

评论回复
沙发
i1mcu| | 2022-8-17 15:56 | 只看该作者
这个串口模块这么多功能吗   

使用特权

评论回复
板凳
dql2015|  楼主 | 2022-8-18 14:13 | 只看该作者
i1mcu 发表于 2022-8-17 15:56
这个串口模块这么多功能吗

是的,voc,温湿度三合一,精度很高

使用特权

评论回复
地板
backlugin| | 2022-8-18 18:45 | 只看该作者
这个使用的模块自己做的吗   

使用特权

评论回复
5
janewood| | 2022-8-18 19:25 | 只看该作者
sgp30不是iic的吗   

使用特权

评论回复
6
lzbf| | 2022-8-18 19:52 | 只看该作者
串口传感器吗   

使用特权

评论回复
7
febgxu| | 2022-8-20 13:56 | 只看该作者
多传感器还是485协议好。  

使用特权

评论回复
8
dql2015|  楼主 | 2022-8-24 12:32 | 只看该作者

sgp40,模块上面有个小mcu

使用特权

评论回复
9
macpherson| | 2022-12-3 13:14 | 只看该作者
SGP40可以通过iic读取吧?

使用特权

评论回复
10
dql2015|  楼主 | 2022-12-4 16:26 | 只看该作者
macpherson 发表于 2022-12-3 13:14
SGP40可以通过iic读取吧?

可以的,这个模块提供串口和I2C

使用特权

评论回复
11
jkl21| | 2022-12-4 20:19 | 只看该作者
SVM40是什么传感器?              

使用特权

评论回复
12
tabmone| | 2022-12-4 21:31 | 只看该作者
使用N32G430和wifi模块,实现物联网设计。

使用特权

评论回复
13
beacherblack| | 2022-12-4 22:01 | 只看该作者
N32G430做这个的速度快吗?

使用特权

评论回复
14
kmzuaz| | 2022-12-6 14:30 | 只看该作者
现在有很多的 传感器可以直接使用的。

使用特权

评论回复
15
maudlu| | 2022-12-6 15:51 | 只看该作者
这个串口可以使用dma的吗              

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

101

主题

372

帖子

7

粉丝