dql2015 发表于 2022-8-15 19:08

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

本帖最后由 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);
    while (USART_Flag_Status_Get(UART4, USART_FLAG_TXDE) == RESET);
      }
    return data_len;
}

串口接收数据基于中断:
extern __IO int RxCounter;
extern uint8_t RxBuffer;
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;
void UART4_IRQHandler(void)
{
    if (USART_Interrupt_Status_Get(UART4, USART_INT_RXDNE) != RESET)
    {
      RxBuffer = 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;
    uint8_t serial_number_size = 32;
    error = svm40_get_serial_number(&serial_number, 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;
    uint8_t product_type_size = 32;
    error = svm40_get_product_type(&product_type, 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;
    uint8_t product_name_size = 32;
    error = svm40_get_product_name(&product_name, 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;
      uint8_t t_offset_size = 2;
      error = svm40_get_temperature_offset_for_rht_measurements(
            &t_offset_buffer, t_offset_size);
      int16_t t_offset =
            sensirion_common_bytes_to_int16_t(&t_offset_buffer);
      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

日志结果:


代码:

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

这个使用的模块自己做的吗   

janewood 发表于 2022-8-18 19:25

sgp30不是iic的吗   

lzbf 发表于 2022-8-18 19:52

串口传感器吗   

febgxu 发表于 2022-8-20 13:56

多传感器还是485协议好。

dql2015 发表于 2022-8-24 12:32

janewood 发表于 2022-8-18 19:25
sgp30不是iic的吗

sgp40,模块上面有个小mcu

macpherson 发表于 2022-12-3 13:14

SGP40可以通过iic读取吧?

dql2015 发表于 2022-12-4 16:26

macpherson 发表于 2022-12-3 13:14
SGP40可以通过iic读取吧?

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

jkl21 发表于 2022-12-4 20:19

SVM40是什么传感器?            

tabmone 发表于 2022-12-4 21:31

使用N32G430和wifi模块,实现物联网设计。

beacherblack 发表于 2022-12-4 22:01

N32G430做这个的速度快吗?

kmzuaz 发表于 2022-12-6 14:30

现在有很多的 传感器可以直接使用的。

maudlu 发表于 2022-12-6 15:51

这个串口可以使用dma的吗            
页: [1]
查看完整版本: 【N32G430开发板试用】串口读取环境传感器模块数据