2318|3

1239

帖子

68

TA的资源

纯净的硅(中级)

楼主
 

【先楫HPM6750EVK2测评】硬件I2C驱动温湿度传感器SHT40 [复制链接]

 

盛思锐官方提供了SHT40的驱动参考:

链接已隐藏,如需查看请登录或者注册

将驱动文件添加到i2c master poll例程里面:

首先修改i2c驱动管脚:

驱动实现如下:

主要是利用SDK提供的i2c_init_master、i2c_master_write和i2c_master_read函数实现i2c初始化、数据读写:

#include "sensirion_i2c_hal.h"
#include "sensirion_common.h"
#include "sensirion_config.h"

#include <stdio.h>
#include "board.h"
#include "hpm_clock_drv.h"
#include "hpm_i2c_drv.h"

#define TEST_I2C                BOARD_APP_I2C_BASE
#define TEST_I2C_CLOCK_NAME     BOARD_APP_I2C_CLK_NAME

int16_t sensirion_i2c_hal_select_bus(uint8_t bus_idx) 
{
    /* TODO:IMPLEMENT or leave empty if all sensors are located on one single
     * bus
     */
    return NO_ERROR;
}

void sensirion_i2c_hal_init(void) 
{
    hpm_stat_t stat;
    i2c_config_t config;
    uint32_t freq;
        init_i2c_pins(TEST_I2C);
    config.i2c_mode = i2c_mode_normal;
    config.is_10bit_addressing = false;
    freq = clock_get_frequency(TEST_I2C_CLOCK_NAME);
    stat = i2c_init_master(TEST_I2C, freq, &config);
    if (stat != status_success) 
    {
        return stat;
    }
}

void sensirion_i2c_hal_free(void) 
{
    /* TODO:IMPLEMENT or leave empty if no resources need to be freed */
}

/**
 * Execute one read transaction on the I2C bus, reading a given number of bytes.
 * If the device does not acknowledge the read command, an error shall be
 * returned.
 *
 * @param address 7-bit I2C address to read from
 * @param data    pointer to the buffer where the data is to be stored
 * @param count   number of bytes to read from I2C and store in the buffer
 * @returns 0 on success, error code otherwise
 */
int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) 
{
    if (status_success != i2c_master_read(TEST_I2C, address, data,count)) 
    {
        printf("Master read failed\n");
        while (1);
    }
    return NO_ERROR;
}

/**
 * Execute one write transaction on the I2C bus, sending a given number of
 * bytes. The bytes in the supplied buffer must be sent to the given address. If
 * the slave device does not acknowledge any of the bytes, an error shall be
 * returned.
 *
 * @param address 7-bit I2C address to write to
 * @param data    pointer to the buffer containing the data to write
 * @param count   number of bytes to read from the buffer and send over I2C
 * @returns 0 on success, error code otherwise
 */
int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t* data, uint16_t count) 
{
  if (status_success != i2c_master_write(TEST_I2C, address, data, count)) 
  {
    printf("Master write failed");
    while (1);
  }
  return NO_ERROR;
}

/**
 * Sleep for a given number of microseconds. The function should delay the
 * execution for at least the given time, but may also sleep longer.
 *
 * Despite the unit, a <10 millisecond precision is sufficient.
 *
 * @param useconds the sleep time in microseconds
 */
void sensirion_i2c_hal_sleep_usec(uint32_t useconds) 
{
   clock_cpu_delay_us(useconds);
}

测试程序:

#include <stdio.h>

#include "sensirion_common.h"
#include "sensirion_i2c_hal.h"
#include "sht4x_i2c.h"

int sht40_test(void) 
{
    int16_t error = 0;

    sensirion_i2c_hal_init();

    uint32_t serial_number;
    error = sht4x_serial_number(&serial_number);
    if (error) 
    {
        printf("Error executing sht4x_serial_number(): %i\n", error);
    } 
    else 
    {
        printf("Serial number: %u\n", serial_number);
    }

    // Start Measurement
    for (;;) 
    {
        int32_t temperature;
        int32_t humidity;
        // Read Measurement
        error = sht4x_measure_high_precision(&temperature, &humidity);
        if (error) 
        {
            printf("Error executing sht4x_measure_high_precision(): %i\n",error);
        } 
        else 
        {
            printf("Temperature: %0.2f °C Humidity: %0.2f %%RH\n",temperature / 1000.0f, humidity / 1000.0f);
        }

        sensirion_i2c_hal_sleep_usec(1000000);
    }

    return 0;
}

主函数里面调用测试程序:

/*
 * Copyright (c) 2022 HPMicro
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#include <stdio.h>
#include "board.h"
#include "hpm_clock_drv.h"
#include "hpm_i2c_drv.h"

extern int sht40_test(void);
int main(void)
{
    board_init();
    sht40_test();
    return 0;
}

将传感器连接到i2c接口上

效果:

 

 

最新回复

这个为什么要先修改i2c驱动管脚   详情 回复 发表于 2023-4-27 22:43
点赞 关注(1)
 
 

回复
举报

6450

帖子

9

TA的资源

版主

沙发
 

用硬件IIC总会遇到从机应答状态异常     

个人签名

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

回复

1700

帖子

0

TA的资源

五彩晶圆(初级)

板凳
 

这个为什么要先修改i2c驱动管脚

点评

例程里面不是用的扩展排针  详情 回复 发表于 2023-4-28 11:51
 
 
 

回复

1239

帖子

68

TA的资源

纯净的硅(中级)

4
 
火辣西米秀 发表于 2023-4-27 22:43 这个为什么要先修改i2c驱动管脚

例程里面不是用的扩展排针

 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表