当前位置:网站首页>rt-thread i2c 使用教程
rt-thread i2c 使用教程
2022-07-06 12:14:00 【duapple】
rt-thread i2c 使用教程
rt-thread studio
1. 创建基础工程
使用芯片级的基础工程作为环境。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eGeqJ6Zv-1657039855097)(images/markdown/rtthread_i2c使用教程/image-20220706004456178.png)]](/img/04/4cc560867d8188ad961a6df1011153.png)
2. 配置i2c
打开i2c。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-r0pjOOJ3-1657039855097)(images/markdown/rtthread_i2c使用教程/image-20220706004526762.png)]](/img/a5/8c510bad5d014d45ce1277e6958112.png)
配置驱动。

CTRL+S保存配置后,会重新生成工程。
修改board.h文件。取消 BSP_USING_I2C1 的注释。
/** if you want to use i2c bus(soft simulate) you can use the following instructions. * * STEP 1, open i2c driver framework(soft simulate) support in the RT-Thread Settings file * * STEP 2, define macro related to the i2c bus * such as #define BSP_USING_I2C1 * * STEP 3, according to the corresponding pin of i2c port, modify the related i2c port and pin information * such as #define BSP_I2C1_SCL_PIN GET_PIN(port, pin) -> GET_PIN(C, 11) * #define BSP_I2C1_SDA_PIN GET_PIN(port, pin) -> GET_PIN(C, 12) */
#define BSP_USING_I2C1
#ifdef BSP_USING_I2C1
#define BSP_I2C1_SCL_PIN GET_PIN(B, 10)
#define BSP_I2C1_SDA_PIN GET_PIN(B, 11)
#endif
3. 代码
使用i2c 读取BMP280温湿度计。使用rt_i2c_transfer来读取和写入数据到传感器芯片。这里的温湿度驱动是我自己实现的。
#include <rtthread.h>
#include <rtdevice.h>
#include <stdio.h>
#include "bme280_i2c.h"
#define BMP280_I2C_BUS_NAME "i2c1"
#define BME280_ADDR 0X76
static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
static rt_bool_t initialized = RT_FALSE;
static int write_bytes(struct rt_i2c_bus_device *bus, uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t len)
{
uint8_t buffer[128] = {
0 };
buffer[0] = reg_addr;
memcpy(buffer + 1, data, len);
struct rt_i2c_msg msgs;
msgs.addr = BME280_ADDR;
msgs.flags = RT_I2C_WR;
msgs.buf = buffer;
msgs.len = len + 1;
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
{
return RT_EOK;
}
else
return -RT_ERROR;
}
static int read_bytes(struct rt_i2c_bus_device *bus, uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t len)
{
struct rt_i2c_msg msgs[2];
msgs[0].addr = BME280_ADDR;
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = ®_addr;
msgs[0].len = 1;
msgs[1].addr = BME280_ADDR;
msgs[1].flags = RT_I2C_RD;
msgs[1].buf = data;
msgs[1].len = len;
if (rt_i2c_transfer(bus, msgs, 2) == 2)
{
return RT_EOK;
}
else
return -RT_ERROR;
}
static int write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *bytes, uint32_t len)
{
return write_bytes(i2c_bus, BME280_ADDR, reg_addr, bytes, len);
}
static int read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *bytes, uint32_t len)
{
return read_bytes(i2c_bus, BME280_ADDR, reg_addr, bytes, len);
}
static void delay_us(uint32_t us)
{
rt_thread_mdelay(us / 1000);
}
static int i2c_test(void)
{
i2c_bus = (struct rt_i2c_bus_device *) rt_device_find(BMP280_I2C_BUS_NAME);
if (i2c_bus == RT_NULL)
{
rt_kprintf("can't find %s device!\n", BMP280_I2C_BUS_NAME);
return RT_ERROR;
}
bme280_init(write, read, delay_us, NULL, 0);
int32_t temperature = 0;
bme280_read_temperature(&temperature);
rt_kprintf("temp: %d\n", temperature);
return RT_EOK;
}
MSH_CMD_EXPORT(i2c_test, i2c test);
4. 测试
使用i2c_test 进行demo测试,读取温度为27.28℃。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a2PYPUyI-1657039855098)(images/markdown/rtthread_i2c使用教程/image-20220706004857734.png)]](/img/c3/4872ff2c6eb0539672dc39894b4189.png)
KEIL
暂时无。
边栏推荐
- 颜色(color)转换为三刺激值(r/g/b)(干股)
- Finally, there is no need to change a line of code! Shardingsphere native driver comes out
- VMware virtual machine cannot open the kernel device "\.\global\vmx86"
- 腾讯T2大牛亲自讲解,跳槽薪资翻倍
- Information System Project Manager - Chapter VIII project quality management
- Cesium Click to draw a circle (dynamically draw a circle)
- Test Li hi
- Example of applying fonts to flutter
- Cesium 两点之间的直线距离
- 350. 两个数组的交集 II
猜你喜欢
Classic 100 questions of algorithm interview, the latest career planning of Android programmers

PowerPivot——DAX(初识)

《数字经济全景白皮书》保险数字化篇 重磅发布

【云原生与5G】微服务加持5G核心网
Tencent Android development interview, basic knowledge of Android Development
![[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.](/img/09/106adc222c06cbd2f4f66cf475cce2.jpg)
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.

Understand yolov1 Part II non maximum suppression (NMS) in prediction stage
算法面试经典100题,Android程序员最新职业规划

Redisson bug analysis
![[infrastructure] deployment and configuration of Flink / Flink CDC (MySQL / es)](/img/1e/b270a81c8457f1eae34f55c004a01a.png)
[infrastructure] deployment and configuration of Flink / Flink CDC (MySQL / es)
随机推荐
腾讯安卓开发面试,android开发的基础知识
转让malloc()该功能后,发生了什么事内核?附malloc()和free()实现源
Microservice architecture debate between radical technologists vs Project conservatives
(3) Web security | penetration testing | basic knowledge of network security construction, IIS website construction, EXE backdoor generation tool quasar, basic use of
Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
Tencent architects first, 2022 Android interview written examination summary
Alibaba数据源Druid可视化监控配置
After solving 2961 user feedback, I made such a change
JVM_常见【面试题】
Hudi vs Delta vs Iceberg
Tencent Android interview must ask, 10 years of Android development experience
夏志刚介绍
颜色(color)转换为三刺激值(r/g/b)(干股)
Tencent Android development interview, basic knowledge of Android Development
Li Kou 101: symmetric binary tree
MySQL information schema learning (II) -- InnoDB table
Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022
Wonderful coding [hexadecimal conversion]
腾讯T2大牛亲自讲解,跳槽薪资翻倍
HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I