当前位置:网站首页>RT thread I2C tutorial
RT thread I2C tutorial
2022-07-06 20:09:00 【duapple】
rt-thread i2c Use the tutorial
rt-thread studio
1. Create basic engineering
Use chip level basic engineering as the environment .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-eGeqJ6Zv-1657039855097)(images/markdown/rtthread_i2c Use the tutorial /image-20220706004456178.png)]](/img/04/4cc560867d8188ad961a6df1011153.png)
2. To configure i2c
open i2c.
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-r0pjOOJ3-1657039855097)(images/markdown/rtthread_i2c Use the tutorial /image-20220706004526762.png)]](/img/a5/8c510bad5d014d45ce1277e6958112.png)
Configure driver .

CTRL+S After saving the configuration , The project will be regenerated .
modify board.h file . Cancel BSP_USING_I2C1 Notes .
/** 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. Code
Use i2c Read BMP280 Temperature and humidity meter . Use rt_i2c_transfer To read and write data to the sensor chip . The temperature and humidity drive here is implemented by myself .
#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. test
Use i2c_test Conduct demo test , The reading temperature is 27.28℃.
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-a2PYPUyI-1657039855098)(images/markdown/rtthread_i2c Use the tutorial /image-20220706004857734.png)]](/img/c3/4872ff2c6eb0539672dc39894b4189.png)
KEIL
Temporarily none .
边栏推荐
- 某东短信登录复活 安装部署教程
- Period compression filter
- DOM operation
- Transformer model (pytorch code explanation)
- [calculating emotion and thought] floor sweeper, typist, information panic and Oppenheimer
- 2022年6月语音合成(TTS)和语音识别(ASR)论文月报
- It's enough to read this article to analyze the principle in depth
- Oceanbase Community Edition OBD mode deployment mode stand-alone installation
- Color is converted to tristimulus value (r/g/b) (dry stock)
- Recursive implementation of department tree
猜你喜欢

Redisson bug analysis

PowerPivot——DAX(初识)

5. Nano - Net in wireless body: Top 10 "is it possible?" Questions

Tencent T3 teaches you hand in hand. It's really delicious

系统与应用监控的思路和方法

爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解

Cesium Click to draw a circle (dynamically draw a circle)

The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance

Microservice architecture debate between radical technologists vs Project conservatives
Tencent T2 Daniel explained in person and doubled his job hopping salary
随机推荐
信息系统项目管理师---第八章 项目质量管理
Microservice architecture debate between radical technologists vs Project conservatives
Ideas and methods of system and application monitoring
Example of applying fonts to flutter
8086指令码汇总表(表格)
PowerPivot——DAX(初识)
PHP and excel phpexcel
rt-thread i2c 使用教程
PowerPivot - DAX (first time)
PHP与EXCEL PHPExcel
Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
方法关键字Deprecated,ExternalProcName,Final,ForceGenerate
beegfs高可用模式探讨
夏志刚介绍
js实现力扣71题简化路径
Speech recognition (ASR) paper selection: talcs: an open source Mandarin English code switching corps and a speech
Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
golang的超时处理使用技巧
范式的数据库具体解释