上次使用keil开发的I2C接口读取传感器数据 https://bbs.21ic.com/icview-3071262-1-1.html。这次试了下使用RTT-Studio开发I2C设备方式读取传感器。
在RT-Thread Studio打开新建基于RT-Thread的工程。
在RT-Thread Studio中配置组件,这里主要添加了软件模拟I2C组件读取传感器。
使能软件模拟I2C。
软件模拟I2C的引脚PB8和PB9。
初始化I2C,和I2C读写函数。
/* 查找I2C总线设备,获取I2C总线设备句柄 */
i2c_bus = (struct rt_i2c_bus_device *)rt_device_find("i2c1");
if (i2c_bus == RT_NULL)
{
rt_kprintf("can't find i21 device!\n");
}
void I2C_WriteByte(unsigned char dev_addr, unsigned int addr,unsigned char Byte)
{
rt_uint8_t buf[4];
struct rt_i2c_msg msgs;
buf[0] = addr&0xff; //cmd
buf[1] = Byte;
msgs.addr = dev_addr;
msgs.flags = RT_I2C_WR;
msgs.buf = buf;
msgs.len = 2;
/* 调用I2C设备接口传输数据 */
rt_i2c_transfer(i2c_bus, &msgs, 1);
}
unsigned char I2C_ReadByte(unsigned char dev_addr, unsigned int addr)
{
rt_uint8_t buf[4];
struct rt_i2c_msg msgs[2];
buf[0] = addr&0xff; //cmd
msgs[0].addr = dev_addr;
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = buf;
msgs[0].len = 1;
msgs[1].addr = dev_addr;
msgs[1].flags = RT_I2C_RD;
msgs[1].buf = buf;
msgs[1].len = 1;
/* 调用I2C设备接口传输数据 */
rt_i2c_transfer(i2c_bus, msgs, 2);
return buf[0];
}
void I2C_PageWrite(unsigned char dev_addr,unsigned int addr,unsigned char *str,unsigned int num)
{
rt_uint8_t buf[4];
struct rt_i2c_msg msgs;
buf[0] = addr&0xff; //cmd
msgs.addr = dev_addr;
msgs.flags = RT_I2C_WR;
msgs.buf = buf;
msgs.len = 1;
/* 调用I2C设备接口传输数据 */
rt_i2c_transfer(i2c_bus, &msgs, 1);
msgs.addr = dev_addr;
msgs.flags = RT_I2C_WR;
msgs.buf = str;
msgs.len = num;
/* 调用I2C设备接口传输数据 */
rt_i2c_transfer(i2c_bus, &msgs, 1);
}
void I2C_PageRead(unsigned char dev_addr,unsigned int addr,unsigned char *str,unsigned int num)
{
// rt_uint8_t buf[4];
// struct rt_i2c_msg msgs;
//
// buf[0] = addr&0xff; //cmd
// msgs.addr = dev_addr;
// msgs.flags = RT_I2C_WR;
// msgs.buf = buf;
// msgs.len = 1;
// /* 调用I2C设备接口传输数据 */
// rt_i2c_transfer(i2c_bus, &msgs, 1);
// msgs.addr = dev_addr;
// msgs.flags = RT_I2C_RD;
// msgs.buf = str;
// msgs.len = num;
// /* 调用I2C设备接口传输数据 */
// rt_i2c_transfer(i2c_bus, &msgs, 1);
rt_uint8_t buf[4];
struct rt_i2c_msg msgs[2];
buf[0] = addr&0xff; //cmd
msgs[0].addr = dev_addr;
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = buf;
msgs[0].len = 1;
msgs[1].addr = dev_addr;
msgs[1].flags = RT_I2C_RD;
msgs[1].buf = str;
msgs[1].len = num;
/* 调用I2C设备接口传输数据 */
rt_i2c_transfer(i2c_bus, msgs, 2);
}
main主函数读取传感器数据:
#include <rtthread.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include <rtdevice.h>
#include "gpio_i2c.h"
#include "RPR-0521RS.h"
#include "BM1422AGMV.h"
#include "BM1383AGLV.h"
#ifndef LED2_PIN_NUM
#define LED2_PIN_NUM 61 /* PD13 */
#endif
#ifndef LED3_PIN_NUM
#define LED3_PIN_NUM 62 /* PD14 */
#endif
#ifndef LED4_PIN_NUM
#define LED4_PIN_NUM 63 /* PD15 */
#endif
int main(void)
{
uint8_t utbuff[6];
float press; float temp;
uint16_t ps; float als;
i2c_gpio_init();
rt_pin_mode(LED2_PIN_NUM, PIN_MODE_OUTPUT);
rt_pin_write(LED2_PIN_NUM, PIN_HIGH);
rt_pin_mode(LED3_PIN_NUM, PIN_MODE_OUTPUT);
rt_pin_write(LED3_PIN_NUM, PIN_HIGH);
rt_pin_mode(LED4_PIN_NUM, PIN_MODE_OUTPUT);
rt_pin_write(LED4_PIN_NUM, PIN_HIGH);
if(RPR0521RS_Init() == 0)
{
rt_kprintf("RPR0521RS_Init OK\r\n");
}
if(BM1422AGMV_Init() == 0)
{
rt_kprintf("BM1422AGMV_Init OK\r\n");
}
if(BM1383AGLV_Init() == 0)
{
rt_kprintf("BM1383AGLV_Init OK\r\n");
}
while (1)
{
//LOG_D("Hello RT-Thread!");
rt_thread_mdelay(500);
rt_pin_write(LED4_PIN_NUM, PIN_HIGH);
rt_pin_write(LED2_PIN_NUM, PIN_LOW);
rt_thread_mdelay(500);
rt_pin_write(LED2_PIN_NUM, PIN_HIGH);
rt_pin_write(LED3_PIN_NUM, PIN_LOW);
rt_thread_mdelay(500);
rt_pin_write(LED3_PIN_NUM, PIN_HIGH);
rt_pin_write(LED4_PIN_NUM, PIN_LOW);
RPR0521RS_get_psalsval(&ps,&als);
rt_kprintf("RPR0521RS:Lx=%u\t\tPs=%d\n",(uint16_t)als,ps);
BM1383AGLV_get_val(&press,&temp);
rt_kprintf("BM1383AGLV:P=%u\t\tTem=%u\n",(uint16_t)press,(uint16_t)temp);
BM1422AGMV_get_rawdata(utbuff);
rt_kprintf("BM1422AGMV:xx=%u\t\tyy=%u\t\tzz=%u\r\n\n",(uint16_t)(utbuff[1]<<8)|utbuff[0],
(uint16_t)(utbuff[3]<<8)|utbuff[2],
(uint16_t)(utbuff[5]<<8)|utbuff[4]);
}
return RT_EOK;
}
通过串口输出的数据:
代码工程:
|