Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
I2C_IIC
软件i2c库
I2C传感器运行时显示Send Address Fail
发布于 2022-05-20 00:04:18 浏览:704
订阅该版
我在仿照官方例程编写HDC1080的I2C驱动,编译时没有发现错误,但是在烧到板子上运行时出现I2C:timeout,Send Address Fail.如下图所示。 我这边编写的代码如下,请大神们帮忙指点一下,谢谢了。``` ``` #define hdc1080_I2C_BUS_NAME "i2c1" /* 传感器连接的I2C总线设备名称 */ #define hdc1080_ADDR 0x40 /* 从机地址 */ #define T_MEASUREMENT 0x00 /* Temperature measurement output */ #define RH_MEASUREMENT 0x01 /* Relative Humidity measurement output */ #define CONFIGURATION 0x02 /* HDC1080 configuration and status */ #define FIRST_SID 0xFB /* First 2 bytes of the serial ID of the part */ #define MID_SID 0xFC /* Mid 2 bytes of the serial ID of the part */ #define LAST_SID 0xFD /* Last byte bit of the serial ID of the part */ #define MANUFACTURE_ID 0xFE /* ID of Texas Instruments,0x5449 */ #define DEVICE_ID 0xFF /* ID of the device,0x1050 */ /* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2022-05-18 4DX the first version */ #include "hdc1080.h" #include "finsh.h" #include "drivers\i2c.h" struct rt_i2c_bus_device *i2c_bus = RT_NULL; /* I2C总线设备句柄 */ static rt_bool_t initialized = RT_FALSE; /* 传感器初始化状态 */ /* 写传感器寄存器 */ static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data) { rt_uint8_t buf[3]; struct rt_i2c_msg msgs; rt_uint32_t buf_size = 1; buf[0] = reg; //cmd if (data != RT_NULL) { buf[1] = data[0]; buf[2] = data[1]; buf_size = 3; } msgs.addr = hdc1080_ADDR; msgs.flags = RT_I2C_WR; msgs.buf = buf; msgs.len = buf_size; /* 调用I2C设备接口传输数据 */ if (rt_i2c_transfer(bus, &msgs, 1) == 1) { return RT_EOK; } else { return -RT_ERROR; } } /* 读传感器寄存器数据 */ static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf) { struct rt_i2c_msg msgs; msgs.addr = hdc1080_ADDR; msgs.flags = RT_I2C_RD; msgs.buf = buf; msgs.len = len; /* 调用I2C设备接口传输数据 */ if (rt_i2c_transfer(bus, &msgs, 1) == 1) { return RT_EOK; } else { return -RT_ERROR; } } static void hdc1080_init() { rt_uint8_t IDBuffer[2]={0}; rt_uint8_t InitSetup[2]={0x90,0x00}; rt_uint32_t SID[3]={0},MID=0,DID=0; /* 查找I2C总线设备,获取I2C总线设备句柄 */ i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(hdc1080_I2C_BUS_NAME); if (i2c_bus == RT_NULL) { rt_kprintf("can't find %s device!\n", hdc1080_I2C_BUS_NAME); } else { write_reg(i2c_bus, CONFIGURATION, InitSetup); rt_thread_mdelay(400); write_reg(i2c_bus, DEVICE_ID, RT_NULL); read_regs(i2c_bus,2,IDBuffer); DID = (((rt_uint32_t)IDBuffer[0])<<8 | IDBuffer[1]); rt_kprintf("The DeviceID is 0x%x\n\r",DID); write_reg(i2c_bus, MANUFACTURE_ID, RT_NULL); read_regs(i2c_bus,2,IDBuffer); MID = (((rt_uint32_t)IDBuffer[0])<<8 | IDBuffer[1]); rt_kprintf("The ManufatureID is 0x%x\n\r",MID); write_reg(i2c_bus, FIRST_SID, RT_NULL); read_regs(i2c_bus,2,IDBuffer); SID[0] = (((rt_uint32_t)IDBuffer[0])<<8 | IDBuffer[1]); write_reg(i2c_bus, MID_SID, RT_NULL); read_regs(i2c_bus,2,IDBuffer); SID[1] = (((rt_uint32_t)IDBuffer[0])<<8 | IDBuffer[1]); write_reg(i2c_bus, LAST_SID, RT_NULL); read_regs(i2c_bus,2,IDBuffer); SID[2] = (((rt_uint32_t)IDBuffer[0])<<8 | IDBuffer[1]); rt_kprintf("The First bytes of the serial ID of the part is 0x%x\n\r",SID[0]); rt_kprintf("The MID bytes of the serial ID of the part is 0x%x\n\r",SID[1]); rt_kprintf("The Last bytes of the serial ID of the part is 0x%x\n\r",SID[2]); rt_kprintf("\n\r"); initialized = RT_TRUE; } } static void read_temp_humi(float *cur_temp, float *cur_humi) { rt_uint8_t temp[4]; rt_uint16_t buf_temp,buf_humi; write_reg(i2c_bus, T_MEASUREMENT, RT_NULL); /* 发送命令 */ rt_thread_mdelay(20); read_regs(i2c_bus, 4, temp); /* 获取传感器数据 */ /* 温度数据转换 */ buf_temp = ((( rt_uint16_t)temp[0])<<8 | temp[1]); *cur_temp = (float)(buf_temp)/(65536)*165-40; /* 湿度数据转换 */ buf_humi = ((( rt_uint16_t)temp[2])<<8 | temp[3]); *cur_humi = (float)(buf_humi)/(65536)*100; } static void hdc1080(int argc, char *argv[]) { float humidity, temperature; char name[RT_NAME_MAX]; humidity = 0.0; temperature = 0.0; if (argc == 2) { rt_strncpy(name, argv[1], RT_NAME_MAX); } else { rt_strncpy(name, hdc1080_I2C_BUS_NAME, RT_NAME_MAX); } if (!initialized) { /* 传感器初始化 */ hdc1080_init(name); } if (initialized) { /* 读取温湿度数据 */ read_temp_humi(&temperature, &humidity); rt_kprintf("read hdc1080 sensor humidity : %.2f%%\n\r", humidity); rt_kprintf("read hdc1080 sensor temperature: %.2f°C\n\r", temperature); } else { rt_kprintf("initialize sensor failed!\n"); } } MSH_CMD_EXPORT(hdc1080, read temperature and humidity); ```
查看更多
2
个回答
默认排序
按发布时间排序
aozima
2022-05-20
调网络不抓包,调I2C等时序不上逻辑分析仪,就像电工不用万用表!多用整理的好的文字,比截图更省流量,还能在整理过程中思考。
上逻辑分析仪,检查上拉电阻
ghetto
2022-05-20
开源交流分享,共同学习进步
cubemx 有配置吗
撰写答案
登录
注册新账号
关注者
0
被浏览
704
关于作者
Cainhu
新手,望指教!
提问
1
回答
0
被采纳
0
关注TA
发私信
相关问题
1
NXP的I2C应该比ST的好用吧
2
Use of I2C device driver
3
关于I2C 驱动问题请教
4
我如何知道这个iic的io配置和我电路设计的是一致的?
5
I2C模拟读操作失败,不知道问什么进不去读函数
6
RTT的I2C有官方文档资料没有
7
求 STM32F103 IIC 自定义IO初始化 代码
8
报一个LPC4008代码中I2C的bug
9
RTOS IIC总线使用
10
关于在RTT中使用STM32 I2C的疑问
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
RT-Thread CI编译产物artifacts自动上传功能介绍
2
STM32G030移植RT-Thread
3
CubeMX & RT-Thread Studio 联合开发说明
4
RT-Thread动态模块
5
RT-Thread项目助手v0.3 | Ubuntu与MacOS平台的RT-Thread Env
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
ESP8266
UART
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
GD32
定时器
ADC
flashDB
编译报错
socket
中断
rt_mq_消息队列_msg_queue
keil_MDK
Debug
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
lchnu
3
个答案
2
次被采纳
张世争
1
个答案
2
次被采纳
a1012112796
9
个答案
1
次被采纳
三世执戟
8
个答案
1
次被采纳
聚散无由
5
个答案
1
次被采纳
本月文章贡献
jinchanchan
10
篇文章
13
次点赞
ssdd45555
3
篇文章
2
次点赞
聚散无由
1
篇文章
4
次点赞
RTT_逍遥
1
篇文章
3
次点赞
hywing
1
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部