[活动专区] 【N32G430开发板试用】串口读取环境传感器模块数据

[复制链接]
1953|14
 楼主| dql2015 发表于 2022-8-15 19:08 | 显示全部楼层 |阅读模式
本帖最后由 dql2015 于 2022-8-15 19:10 编辑

@安小芯


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

SGP40主要参数如下:
bb1362807e1d1b31104f946ef0991989.png
SHT40主要参数如下:
a0adabf4c4e3705b1b9fc07b719cb284.png
SVM40模块内置了一颗STM32G0微控制器,通过I2C接口连接2颗传感器,Sensirion自定义了通信协议,模块对外提供串口和I2C接口,模块结构如图所示:
13b75fbe115fd83aee100ae6f9e4fdfc.png
模块提供I2C和UART两种接口,本文采用UART接口,模块引脚含义如图所示:
捕获.PNG
svm40官方提供了串口驱动程序参考,只需要匹配串口初始化、发送数据、读取数据、微秒级延时四个接口就行了,具体就是填充sensirion_uart_hal.c文件里面的驱动接口。
代码.PNG
串口发送数据:
  1. int16_t sensirion_uart_hal_tx(uint16_t data_len, const uint8_t* data)
  2. {
  3.         for(int i=0;i<data_len;i++)
  4.         {
  5.           USART_Data_Send(UART4, data[i]);
  6.     while (USART_Flag_Status_Get(UART4, USART_FLAG_TXDE) == RESET);
  7.         }
  8.     return data_len;
  9. }


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

  9. }


0.PNG



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

  31. #include <stdio.h>  // printf

  32. #include "sensirion_common.h"
  33. #include "sensirion_uart_hal.h"
  34. #include "svm40_uart.h"

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

  39. int svm40_test(void)
  40. {
  41.     int16_t error = 0;

  42.     error = sensirion_uart_hal_init();
  43.     if (error) {
  44.         printf("Error initializing UART: %i\n", error);
  45.         return error;
  46.     }
  47.                
  48.                 error = svm40_device_reset();
  49.                     if (error) {
  50.         printf("Error executing svm40_device_reset(): %i\n", error);
  51.         return error;
  52.     }

  53.     uint8_t serial_number[32];
  54.     uint8_t serial_number_size = 32;
  55.     error = svm40_get_serial_number(&serial_number[0], serial_number_size);
  56.     if (error) {
  57.         printf("Error executing svm40_get_serial_number(): %i\n", error);
  58.     } else {
  59.         printf("Serial number: %s\n", serial_number);
  60.     }

  61.     uint8_t product_type[32];
  62.     uint8_t product_type_size = 32;
  63.     error = svm40_get_product_type(&product_type[0], product_type_size);
  64.     if (error) {
  65.         printf("Error executing svm40_get_product_type(): %i\n", error);
  66.     } else {
  67.         printf("Product type: %s\n", product_type);
  68.     }

  69.     uint8_t product_name[32];
  70.     uint8_t product_name_size = 32;
  71.     error = svm40_get_product_name(&product_name[0], product_name_size);
  72.     if (error) {
  73.         printf("Error executing svm40_get_product_name(): %i\n", error);
  74.     } else {
  75.         printf("Product name: %s\n", product_name);
  76.     }

  77.     uint8_t firmware_major;
  78.     uint8_t firmware_minor;
  79.     bool firmware_debug;
  80.     uint8_t hardware_major;
  81.     uint8_t hardware_minor;
  82.     uint8_t protocol_major;
  83.     uint8_t protocol_minor;
  84.     error = svm40_get_version(&firmware_major, &firmware_minor, &firmware_debug,
  85.                               &hardware_major, &hardware_minor, &protocol_major,
  86.                               &protocol_minor);
  87.     if (error) {
  88.         printf("Error executing svm40_get_version(): %i\n", error);
  89.     } else {
  90.         printf("Firmware: %i.%i Debug: %i\n", firmware_major, firmware_minor,
  91.                firmware_debug);
  92.         printf("Hardware: %i.%i\n", hardware_major, hardware_minor);
  93.         printf("Protocol: %i.%i\n", protocol_major, protocol_minor);
  94.     }

  95.     if (firmware_major < 2) {
  96.         printf("Your SVM40 firmware is out of date!\n");
  97.     } else {
  98.         uint8_t t_offset_buffer[2];
  99.         uint8_t t_offset_size = 2;
  100.         error = svm40_get_temperature_offset_for_rht_measurements(
  101.             &t_offset_buffer[0], t_offset_size);
  102.         int16_t t_offset =
  103.             sensirion_common_bytes_to_int16_t(&t_offset_buffer[0]);
  104.         if (error) {
  105.             printf("Error executing "
  106.                    "svm40_get_temperature_offset_for_rht_measurements(): %i\n",
  107.                    error);
  108.         } else {
  109.             printf("Temperature Offset: %i ticks\n", t_offset);
  110.         }
  111.     }

  112.     // Start Measurement
  113.     error = svm40_start_continuous_measurement();
  114.     if (error) {
  115.         printf("Error executing svm40_start_continuous_measurement(): %i\n",
  116.                error);
  117.     }

  118.     for (;;) {
  119.         // Read Measurement
  120.         sensirion_uart_hal_sleep_usec(1000000);
  121.         int16_t voc_index;
  122.         int16_t humidity;
  123.         int16_t temperature;
  124.         error = svm40_read_measured_values_as_integers(&voc_index, &humidity,
  125.                                                        &temperature);
  126.         if (error)
  127.                                 {
  128.             printf("Error executing svm40_read_measured_values_as_integers(): ""%i\n",error);
  129.         }
  130.                                 else
  131.                                 {
  132.             printf("Voc index:%.2f Temperature:%.2f Humidity:%.2f\n", voc_index/10.0,temperature/200.0,humidity/100.0);
  133.         }
  134.     }

  135.     error = svm40_stop_measurement();
  136.     if (error) {
  137.         printf("Error executing svm40_stop_measurement(): %i\n", error);
  138.     }

  139.     return 0;
  140. }
模块接到串口4:PB0、PB1
IMG_20220814_235747.jpg
日志结果:
结果.PNG

代码:
uart_svm40_test.zip (406.43 KB, 下载次数: 19)
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 | 显示全部楼层

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的吗              
您需要登录后才可以回帖 登录 | 注册

本版积分规则

104

主题

384

帖子

8

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