当前位置:网站首页>rt-thread i2c 使用教程
rt-thread i2c 使用教程
2022-07-06 12:14:00 【duapple】
rt-thread i2c 使用教程
rt-thread studio
1. 创建基础工程
使用芯片级的基础工程作为环境。
2. 配置i2c
打开i2c
。
配置驱动。
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℃。
KEIL
暂时无。
边栏推荐
- Tensorflow2.0 self defined training method to solve function coefficients
- Period compression filter
- 激进技术派 vs 项目保守派的微服务架构之争
- Vscode debug run fluent message: there is no extension for debugging yaml. Should we find yaml extensions in the market?
- Teach you to learn JS prototype and prototype chain hand in hand, a tutorial that monkeys can understand
- mod_wsgi + pymssql通路SQL Server座
- BUUCTF---Reverse---easyre
- Tencent T2 Daniel explained in person and doubled his job hopping salary
- Poj1149 pigs [maximum flow]
- 从sparse.csc.csr_matrix生成邻接矩阵
猜你喜欢
【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
《数字经济全景白皮书》保险数字化篇 重磅发布
Tencent T3 Daniel will teach you hand-in-hand, the internal information of the factory
A5000 vGPU显示模式切换
腾讯T2大牛亲自讲解,跳槽薪资翻倍
Redisson bug analysis
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
PowerPivot——DAX(初识)
企业精益管理体系介绍
Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022
随机推荐
Analysis of rainwater connection
MySQL information schema learning (II) -- InnoDB table
POJ 3207 Ikki&#39;s Story IV – Panda&#39;s Trick (2-SAT)
从sparse.csc.csr_matrix生成邻接矩阵
Understand yolov1 Part II non maximum suppression (NMS) in prediction stage
Interpretation of Dagan paper
Cesium Click to draw a circle (dynamically draw a circle)
Leetcode brush first_ Maximum Subarray
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
广州首个数据安全峰会将在白云区开幕
LeetCode_ Gray code_ Medium_ 89. Gray code
HDU 1026 Ignatius and the Princess I 迷宫范围内的搜索剪枝问题
Redisson bug analysis
It's enough to read this article to analyze the principle in depth
腾讯Android面试必问,10年Android开发经验
Pay attention to the partners on the recruitment website of fishing! The monitoring system may have set you as "high risk of leaving"
青龙面板白屏一键修复
Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)
A5000 vgpu display mode switching
PHP与EXCEL PHPExcel