初学者
10 分钟

使用SHT11和PIC32MZ2048EFH100实现可靠的湿度和温度测量

从环境监测到热管理

SHT1x Click with Flip&Click PIC32MZ

已发布 6月 27, 2024

点击板

SHT1x Click

开发板

Flip&Click PIC32MZ

编译器

NECTO Studio

微控制器单元

PIC32MZ2048EFH100

在环境条件直接影响功能、产品质量或用户舒适度的应用中,这是一种非常宝贵的解决方案。

A

A

硬件概览

它是如何工作的?

SHT1x Click基于Sensirion的湿度和温度传感器SHT11。SHT11是一种强大而可靠的传感器,即使在超出正常范围的条件下,一旦条件稳定,它也可以重新校准自身。该传感器在推荐的正常温度范围内(-20°C至100°C)运行时性能最佳。长期暴露于正常范围之外的条件可能会暂时偏移RH信号。在返回正常温度范围后,传感器将自行缓慢恢复到校准状态。此外,请注意,长时间暴露于极端条件可能会加速老 化。尽管典型的相对湿度分辨率为12位,温度读数为

14位,但SHT11上的两个传感器都无缝地耦合到14位ADC。相对湿度传感器使用独特的电容式传感器元件,而带隙传感器测量温度。SHT11在精密湿度室中单独校准,并且校准系数存储在板载OTP存储器中。SHT1x Click通过mikroBUS™插座上的I2C接口与主机MCU通信,通信速度高达1MHz。SHT1x Click没有复位引脚;如果与传感器的通信中断,可以通过信号 序列重新设置传感器。虽然SHT1x Click不能直接测量露点,但可以使用湿度和温度读数计算露点。这是可

能的,因为同一块单片芯片测量湿度和温度。但是,重要的是要注意,传感器对光不敏感,长时间暴露于强烈的紫外辐射可能会导致外壳随时间的推移而劣化。此Click板可以通过PWR SEL跳线选择3.3V或5V逻辑电压级别进行操作。这样,既支持3.3V又支持5V的MCU可以正确使用通信线路。此外,此Click板配备了一个包含易于使用的函数和示例代码的库,可用作进一步开发的参考。

SHT1x Click hardware overview image

功能概述

开发板

Flip&Click PIC32MZ 是一款紧凑型开发板,设计为一套完整的解决方案,它将 Click 板™的灵活性带给您喜爱的微控制器,使其成为实现您想法的完美入门套件。它配备了一款板载 32 位 PIC32MZ 微控制器,Microchip 的 PIC32MZ2048EFH100,四个 mikroBUS™ 插槽用于 Click 板™连接,两个 USB 连接器,LED 指示灯,按钮,调试器/程序员连接器,以及两个与 Arduino-UNO 引脚兼容的头部。得益于创

新的制造技术,它允许您快速构建具有独特功能和特性的小工具。Flip&Click PIC32MZ 开发套件的每个部分都包含了使同一板块运行最高效的必要组件。此外,还可以选择 Flip&Click PIC32MZ 的编程方式,使用 chipKIT 引导程序(Arduino 风格的开发环境)或我们的 USB HID 引导程序,使用 mikroC、mikroBasic 和 mikroPascal for PIC32。该套件包括一个通过 USB 类型-C(USB-C)连接器的干净且调

节过的电源供应模块。所有 mikroBUS™ 本身支持的 通信方法都在这块板上,包括已经建立良好的 mikroBUS™ 插槽、用户可配置的按钮和 LED 指示灯。Flip&Click PIC32MZ 开发套件允许您在几分钟内创建新的应用程序。它由 Mikroe 软件工具原生支持,得益于大量不同的 Click 板™(超过一千块板),其数量每天都在增长,它涵盖了原型制作的许多方面。

Flip&Click PIC32MZ double image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC32

MCU 内存 (KB)

2048

硅供应商

Microchip

引脚数

100

RAM (字节)

524288

使用的MCU引脚

mikroBUS™映射器

NC
NC
AN
NC
NC
RST
NC
NC
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
NC
NC
INT
NC
NC
TX
NC
NC
RX
I2C Clock
RA2
SCL
I2C Data
RA3
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

SHT1x Click Schematic schematic

一步一步来

项目组装

Flip&Click PIC32MZ front image hardware assembly

从选择您的开发板和Click板™开始。以Flip&Click PIC32MZ作为您的开发板开始。

Flip&Click PIC32MZ front image hardware assembly
GNSS2 Click front image hardware assembly
Prog-cut hardware assembly
Flip&Click PIC32MZ MB1 Access - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
Flip&Click PIC32MZ MCU step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

实时跟踪您的结果

应用程序输出

1. 应用程序输出 - 在调试模式下,“应用程序输出”窗口支持实时数据监控,直接提供执行结果的可视化。请按照提供的教程正确配置环境,以确保数据正确显示。

2. UART 终端 - 使用UART Terminal通过USB to UART converter监视数据传输,实现Click board™与开发系统之间的直接通信。请根据项目需求配置波特率和其他串行设置,以确保正常运行。有关分步设置说明,请参考提供的教程

3. Plot 输出 - Plot功能提供了一种强大的方式来可视化实时传感器数据,使趋势分析、调试和多个数据点的对比变得更加直观。要正确设置,请按照提供的教程,其中包含使用Plot功能显示Click board™读数的分步示例。在代码中使用Plot功能时,请使用以下函数:plot(insert_graph_name, variable_name);。这是一个通用格式,用户需要将“insert_graph_name”替换为实际图表名称,并将“variable_name”替换为要显示的参数。

软件支持

库描述

这个库包含 SHT1x Click 驱动程序的 API。

关键函数:

  • sht1x_output_sda - 设置引脚为输出模式。

  • sht1x_input_sda - 设置引脚为输入模式。

  • sht1x_sda_high - 设置 SDA 高电平的功能。

开源

代码示例

完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio 应用程序代码也可以在MIKROE的GitHub账户中找到。

/*!
 * \file 
 * \brief SHT1x Click example
 * 
 * # Description
 * This click measures temperature and humidity.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initialization driver enables GPIO.
 * 
 * ## Application Task  
 * This example demonstrates the use of SHT1x Click board by measuring 
   temperature and humidity, and displays the results on USART terminal.
 * 
 * 
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "sht1x.h"

// ------------------------------------------------------------------ VARIABLES

static sht1x_t sht1x;
static log_t logger;

static sht1x_cfg_t cfg;

static uint8_t i;
static uint8_t data_val;
static uint8_t err;
static uint8_t msb;
static uint8_t lsb;
static uint8_t checksum;
static uint16_t t;
static uint16_t h;
static float value;
static uint8_t uc_sens_err;
static int16_t int_temp;
static int16_t int_humi;
static float humidity;
static float temperature;

// ------------------------------------------------------ APPLICATION FUNCTIONS

void sht1x_trans_start ( )
{
    sht1x_output_sda ( &sht1x, &cfg );

    sht1x_sda_high( &sht1x );

    sht1x_input_sda ( &sht1x, &cfg );

    sht1x_scl_low( &sht1x );

    Delay_us( 1 );

    sht1x_scl_high( &sht1x );

    Delay_us( 1 );

    sht1x_output_sda ( &sht1x, &cfg );

    sht1x_sda_low( &sht1x );

    Delay_us( 1 );

    sht1x_scl_low( &sht1x );

    Delay_us( 3 );

    sht1x_scl_high( &sht1x );

    Delay_us( 1 );

    sht1x_input_sda ( &sht1x, &cfg );

    Delay_us( 1 );

    sht1x_scl_low( &sht1x );
}

uint8_t sht1x_read_byte( uint8_t ack )
{
    i = 0x80;
    data_val = 0;

    sht1x_output_sda ( &sht1x, &cfg );

    sht1x_sda_high( &sht1x );

    sht1x_input_sda ( &sht1x, &cfg );

    sht1x_scl_low( &sht1x );

    while( i )
    {
        sht1x_scl_high( &sht1x );
        Delay_us( 1 );
        if ( sht1x_get_sda( &sht1x ) == 1 )
        {
            data_val = ( data_val | i );
        }
        sht1x_scl_low( &sht1x );
        Delay_us( 1 );
        i = ( i >> 1 );
    }

    sht1x_output_sda ( &sht1x, &cfg );

    if( ack )
    {
        sht1x_sda_low( &sht1x );
    }
    else
    {
        sht1x_sda_high( &sht1x );
    }

    sht1x_scl_high( &sht1x );
    Delay_us( 3 );
    sht1x_scl_low( &sht1x );
    Delay_us( 1 );

    sht1x_sda_high( &sht1x );

    sht1x_input_sda ( &sht1x, &cfg );

    return data_val;
}

uint8_t sht1x_write_byte( uint8_t value )
{
    i = 0x80;
    err = 0;

    sht1x_output_sda ( &sht1x, &cfg );

    while( i )
    {
        if ( i & value )
        {
            sht1x_sda_high( &sht1x );
        }
        else
        {
            sht1x_sda_low( &sht1x );
        }

        sht1x_scl_high( &sht1x );
        Delay_us( 3 );
        sht1x_scl_low( &sht1x );
        Delay_us( 3 );
        i = ( i >> 1 );
    }

    sht1x_sda_high( &sht1x );
    sht1x_input_sda ( &sht1x, &cfg );

    sht1x_scl_high( &sht1x );
    Delay_us( 3 );
    if ( sht1x_get_sda( &sht1x ) == 1 )
	{
	    err = 1;
	}
    Delay_us( 1 );
    sht1x_scl_low( &sht1x );

    return err;
}

uint8_t sht1x_measure( uint16_t *p_val, uint8_t mode )
{
    i = 0;
    *p_val = 0;
    
    sht1x_trans_start(  );

    if( mode )
    {
        mode = SHT1X_MEAS_HUMI;
    }
    else
    {
        mode = SHT1X_MEAS_TEMP;
    }

    if( sht1x_write_byte( mode ) )
    {
        return( 1 );
    }

    sht1x_input_sda ( &sht1x, &cfg );

    while( i < 240 )
    {
        Delay_ms( 3 );
        if ( sht1x_get_sda( &sht1x ) == 0 )
        {
            i = 0;
            break;
        }
        i++;
    }

    if( i )
    {
        return( 2 );
    }

    msb = sht1x_read_byte( SHT1X_ACK );
    lsb = sht1x_read_byte( SHT1X_ACK );
    checksum = sht1x_read_byte( SHT1X_NACK );

    *p_val = ( msb << 8 ) | lsb ;

    return( 0 );
}

void sht1x_read_results ( float *f_t, float *f_rh )
{
    uc_sens_err = 0;

    uc_sens_err = sht1x_measure( &t, 0 );
    int_temp = ( int16_t )( sht1x_calc_temp( &sht1x, t ) * 10 );

    uc_sens_err = sht1x_measure(&h, 1);
    int_humi = ( int16_t )( sht1x_calc_humi( &sht1x, h, t ) * 10 );

    value = ( float )int_temp;
    *f_t = value / 10;
    value = ( float )int_humi;
    *f_rh = value / 10;
}

uint8_t sht1x_rd_stat_reg ( uint8_t *p_val )
{
    checksum = 0;

    sht1x_trans_start( );
    if( sht1x_write_byte( SHT1X_STAT_REG_R ) )
    {
        return 1;
    }
    *p_val = sht1x_read_byte( SHT1X_ACK );
    checksum = sht1x_read_byte( SHT1X_NACK );

    return 0;
}

uint8_t sht1x_wr_stat_reg ( uint8_t value )
{
    sht1x_trans_start();
    if( sht1x_write_byte( SHT1X_STAT_REG_W ) )
    {
        return 1;
    }
    if( sht1x_write_byte( value ) )
    {
        return 1;
    }

    return 0;
}

void sht1x_connection_reset ( )
{
    sht1x_output_sda ( &sht1x, &cfg );

    sht1x_sda_high( &sht1x );

    sht1x_input_sda ( &sht1x, &cfg );

    sht1x_scl_low( &sht1x );

    for( i = 0; i < 9; i++ )
    {
        sht1x_scl_high( &sht1x );
        Delay_us( 3 );
        sht1x_scl_low( &sht1x );
        Delay_us( 3 );
    }

    sht1x_trans_start( );
}

uint8_t sht1x_soft_reset ( )
{
    sht1x_connection_reset( );

    return ( sht1x_write_byte( SHT1X_SOFT_RESET ) );
}

void application_init ( void )
{
    log_cfg_t log_cfg;
    
    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info(&logger, "---- Application Init ----");

    //  Click initialization.

    sht1x_cfg_setup( &cfg );
    SHT1X_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    sht1x_init( &sht1x, &cfg );
}

void application_task ( void )
{
    sht1x_read_results( &temperature, &humidity );
    log_printf( &logger, " Temperature: %.2f ", temperature );
    log_printf( &logger, " C \r\n" );
  
    log_printf( &logger, " Humidity: %.2f ", humidity );
    log_printf( &logger, " \r\n" );
    
    Delay_ms( 1000 );
}

void main ( void )
{
    application_init( );

    for ( ; ; )
    {
        application_task( );
    }
}

// ------------------------------------------------------------------------ END

额外支持

资源

喜欢这个项目吗?

'购买此套件' 按钮会直接带您进入购物车,您可以在购物车中轻松添加或移除产品。