加入星计划,您可以享受以下权益:

  • 创作内容快速变现
  • 行业影响力扩散
  • 作品版权保护
  • 300W+ 专业用户
  • 1.5W+ 优质创作者
  • 5000+ 长期合作伙伴
立即加入

【智能家居挑战赛】+ 智障小助手之使用RT1010驱动SHT30读取温湿度的程序

05/17 08:23
2822
服务支持:
技术交流群

完成交易后在“购买成功”页面扫码入群,即可与技术大咖们分享疑惑和经验、收获成长和认同、领取优惠和红包等。

虚拟商品不可退

当前内容为数字版权作品,购买后不支持退换且无法转移使用。

加入交流群
扫码加入
获取工程师必备礼包
参与热点资讯讨论
放大
电路板图(3)
相关方案
  • 方案介绍
  • 推荐器件
  • 相关推荐
  • 电子产业图谱
申请入驻 产业图谱

今天先来一篇使用RT1010驱动SHT30读取温湿度的程序。

其中通信方式使用的是IIC,框图如下:

其中命令是16位长度。

比较核心的命令是数值查询。发送指令0x2C06,返回为6字节的数组:

1[温度高八位]

2[温度第八位]

3[温度crc校验]

4[湿度高八位]

5[湿度第八位]

6[湿度crc校验]

手册也给出了温湿度单位转换方式。

 

IO初始化部分

  IOMUXC_SetPinMux(IOMUXC_GPIO_01_LPI2C1_SDA,1U);
                                       /* GPIO_01 is configured as LPI2C1_SDA */
                                       /* Software Input On Field: Force input path of pad GPIO_01 */
  IOMUXC_SetPinMux(IOMUXC_GPIO_02_LPI2C1_SCL,1U); 
                                       /* GPIO_02 is configured as LPI2C1_SCL */
                                       /* Software Input On Field: Force input path of pad GPIO_02 */
  IOMUXC_SetPinConfig(IOMUXC_GPIO_01_LPI2C1_SDA,0xD8B0U);
                                       /* GPIO_01 PAD functional properties : */
                                       /* Slew Rate Field: Slow Slew Rate
                                          Drive Strength Field: R0/6
                                          Speed Field: fast(150MHz)
                                          Open Drain Enable Field: Open Drain Enabled
                                          Pull / Keep Enable Field: Pull/Keeper Enabled
                                          Pull / Keep Select Field: Keeper
                                          Pull Up / Down Config. Field: 22K Ohm Pull Up
                                          Hyst. Enable Field: Hysteresis Disabled */
  IOMUXC_SetPinConfig(IOMUXC_GPIO_02_LPI2C1_SCL,0xD8B0U);
                                       /* GPIO_02 PAD functional properties : */
                                       /* Slew Rate Field: Slow Slew Rate
                                          Drive Strength Field: R0/6
                                          Speed Field: fast(150MHz)
                                          Open Drain Enable Field: Open Drain Enabled
                                          Pull / Keep Enable Field: Pull/Keeper Enabled
                                          Pull / Keep Select Field: Keeper
                                          Pull Up / Down Config. Field: 22K Ohm Pull Up
                                          Hyst. Enable Field: Hysteresis Disabled */

IIC初始化部分

const lpi2c_master_config_t ACCEL_I2C_masterConfig = {
    .enableMaster            = true,
    .enableDoze              = true,
    .debugEnable             = false,
    .ignoreAck               = false,
    .pinConfig               = kLPI2C_2PinOpenDrain,
    .baudRate_Hz             = 100000,
    .busIdleTimeout_ns       = 0,
    .pinLowTimeout_ns        = 0,
    .sdaGlitchFilterWidth_ns = 0,
    .sclGlitchFilterWidth_ns = 0,
    .hostRequest             = {
    .enable = false, .source = kLPI2C_HostRequestExternalPin, .polarity = kLPI2C_HostRequestPinActiveHigh}};
lpi2c_master_handle_t ACCEL_I2C_masterHandle;

        LPI2C_MasterInit(LPI2C1, &ACCEL_I2C_masterConfig, 60000000UL);
    LPI2C_MasterTransferCreateHandle(LPI2C1, &ACCEL_I2C_masterHandle, NULL, NULL);

IIC读写函数

status_t BOARD_LPI2C_Send(LPI2C_Type *base,
                          uint8_t deviceAddress,
                          uint32_t subAddress,
                          uint8_t subAddressSize,
                          uint8_t *txBuff,
                          uint8_t txBuffSize)
{
    lpi2c_master_transfer_t xfer;

    xfer.flags          = kLPI2C_TransferDefaultFlag;
    xfer.slaveAddress   = deviceAddress;
    xfer.direction      = kLPI2C_Write;
    xfer.subaddress     = subAddress;
    xfer.subaddressSize = subAddressSize;
    xfer.data           = txBuff;
    xfer.dataSize       = txBuffSize;

    return LPI2C_MasterTransferBlocking(base, &xfer);
}

status_t BOARD_LPI2C_Receive(LPI2C_Type *base,
                             uint8_t deviceAddress,
                             uint32_t subAddress,
                             uint8_t subAddressSize,
                             uint8_t *rxBuff,
                             uint8_t rxBuffSize)
{
    lpi2c_master_transfer_t xfer;

    xfer.flags          = kLPI2C_TransferDefaultFlag;
    xfer.slaveAddress   = deviceAddress;
    xfer.direction      = kLPI2C_Read;
    xfer.subaddress     = subAddress;
    xfer.subaddressSize = subAddressSize;
    xfer.data           = rxBuff;
    xfer.dataSize       = rxBuffSize;

    return LPI2C_MasterTransferBlocking(base, &xfer);
}

主函数

SDK_DelayAtLeastUs(1000*1000, 600*1000*1000);
BOARD_LPI2C_Receive(LPI2C1, 0x44, 0x2C06, 2, sht30_buff, 6);

uint16_t tem = ((sht30_buff[0]<<8) | sht30_buff[1]);//温度拼接                        
float Temperature= (175.0*(float)tem/65535.0-45.0) ;// T = -45 + 175 * tem / (2^16-1)/*转换实际温度*/

uint16_t hum = ((sht30_buff[3]<<8) | sht30_buff[4]);//湿度拼接
float Humidity= (100.0*(float)hum/65535.0);

uint8_t shi = Temperature/10;
uint8_t ge = Temperature - shi*10;
uint8_t sound[] = {'0'+shi, '0'+ge, 'C', ' '};

shi = Humidity/10;
ge = Humidity - shi*10;
uint8_t sound1[] = {'0'+shi, '0'+ge, '%', 'r', 'n'};

LPUART_WriteBlocking(LPUART1, sound, sizeof(sound) / sizeof(sound[0]));
LPUART_WriteBlocking(LPUART1, sound1, sizeof(sound1) / sizeof(sound1[0]));

效果

推荐器件

更多器件
器件型号 数量 器件厂商 器件描述 数据手册 ECAD模型 风险等级 参考价格 更多信息
TJA1042TK/3,118 1 NXP Semiconductors TJA1042 - High-speed CAN transceiver with Standby mode SON 8-Pin

ECAD模型

下载ECAD模型
$1.3 查看
ABS07AIG-32.768KHZ-1-T 1 Abracon Corporation CRYSTAL 32.768KHZ 12.5PF SMD

ECAD模型

下载ECAD模型
$1.9 查看
AFBR-5715APZ 1 Broadcom Limited Transceiver, 830nm Min, 860nm Max, 1250Mbps(Tx), LC Connector, Panel Mount, ROHS COMPLIANT
暂无数据 查看

相关推荐

电子产业图谱