本帖最后由 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)
|
此文章已获得独家原创/原创奖标签,著作权归21ic所有,未经允许禁止转载。
共1人点赞
|