Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
OLED显示屏
英飞凌Infineon
【英飞凌创意创客大赛】基于Infineon实现信息采集系统
发布于 2023-07-16 02:13:12 浏览:474
订阅该版
**项目简介** 此次项目主要是基于PSOC6 英飞凌 PSoC™62 with CAPSENSE™ evaluation kit 实现OLED 显示温度、气压室内信息。 整体框架如下: ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230716/2ce2965cfd003c43fe91d6e9a26106a7.png) 1.通过板载的dap 和uart 进行串口log 打印及程序刷写 2.通过I2C 进行BMP280 的驱动 3.通过I2C 进行oled SSD1306的显示 **实现步骤** 1.rtt 中添加 软件包 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230716/6f8d97234aab70bbe6a1758210c89583.png) 使能sensor 驱动 iic 驱动 uart驱动 2.默认的bmp280 包不能使用,由于使用最新的rt版本 不兼容,进行驱动层修改 ```c #ifndef SENSOR_BS_BMP280_H__ #define SENSOR_BS_BMP280_H__ #include "drivers/sensor.h" #include "bmp280.h" #define BMP280_ADDR_DEFAULT BMP280_I2C_ADDR_SEC #define BMP280_I2CBUS_NAME "i2c1" int rt_hw_bmp280_init(const char *name, struct rt_sensor_config *cfg); #endif ``` 定义I2C bus名称 定义BMP280 默认地址 硬件初始化 ```c static struct rt_sensor_ops sensor_ops = { _bmp280_fetch_data, _bmp280_control }; int rt_hw_bmp280_init(const char *name, struct rt_sensor_config *cfg) { int result; rt_sensor_t sensor_pres = RT_NULL, sensor_temp = RT_NULL; result = _rt_bmp280_init(&cfg->intf); if (result != RT_EOK) { LOG_E("_rt_bmp280_init err code: %d", result); return -RT_ERROR; } else { sensor_pres = rt_calloc(1, sizeof(struct rt_sensor_device)); if (sensor_pres == RT_NULL) { LOG_E("rt_calloc error"); return -RT_ERROR; } sensor_pres->info.type = RT_SENSOR_TYPE_BARO; sensor_pres->info.vendor = RT_SENSOR_VENDOR_BOSCH; sensor_pres->info.name = "bmp280_pres"; sensor_pres->info.unit = RT_SENSOR_UNIT_PA; sensor_pres->info.intf_type = RT_SENSOR_INTF_I2C; sensor_pres->info.scale.range_max = SENSOR_PRES_RANGE_MAX; sensor_pres->info.scale.range_min = SENSOR_PRES_RANGE_MIN; //sensor_pres->info.period_min = 0; rt_memcpy(&sensor_pres->config, cfg, sizeof(struct rt_sensor_config)); sensor_pres->ops = &sensor_ops; result = rt_hw_sensor_register(sensor_pres, name, RT_DEVICE_FLAG_RDWR, (void *)RT_NULL); if (result != RT_EOK) { LOG_E("device register err code: %d", result); goto __exit; } sensor_temp = rt_calloc(1, sizeof(struct rt_sensor_device)); if (sensor_temp == RT_NULL) { LOG_E("rt_calloc error"); goto __exit; } sensor_temp->info.type = RT_SENSOR_TYPE_TEMP; sensor_temp->info.vendor = RT_SENSOR_VENDOR_BOSCH; sensor_temp->info.name = "bmp280_temp"; sensor_temp->info.unit = RT_SENSOR_UNIT_CELSIUS; sensor_temp->info.intf_type = RT_SENSOR_INTF_I2C; sensor_temp->info.scale.range_max = SENSOR_TEMP_RANGE_MAX; sensor_temp->info.scale.range_min = SENSOR_TEMP_RANGE_MIN; //sensor_temp->info.period_min = 0; rt_memcpy(&sensor_temp->config, cfg, sizeof(struct rt_sensor_config)); sensor_temp->ops = &sensor_ops; result = rt_hw_sensor_register(sensor_temp, name, RT_DEVICE_FLAG_RDWR, RT_NULL); if (result != RT_EOK) { LOG_E("device register err code: %d", result); goto __exit; } } LOG_I("bmp280_sensor init success"); return RT_EOK; __exit: if (sensor_pres) rt_free(sensor_pres); if (sensor_temp) rt_free(sensor_temp); return -RT_ERROR; } ``` rt_sensor_info 结构体改变了很多,需要根据sensor 驱动进行相应的修改 设置 bmp 280 更新速度 ```c static rt_err_t _bmp280_set_odr(rt_sensor_t sensor, rt_uint32_t args) { int8_t rslt; struct bmp280_config conf; if(args==1||args==2||args==4||args==8||args==16||args==2048||args==BMP280_ODR_2000_MS||args==BMP280_ODR_4000_MS) { rslt = bmp280_get_config(&conf, &bmp); if(rslt!=BMP280_OK) { print_rslt(" bmp280_get_config status", rslt); return -RT_ERROR; } switch(args) { case 1 : conf.odr = BMP280_ODR_1000_MS; break; case 2 : conf.odr = BMP280_ODR_500_MS; break; case 4 : conf.odr = BMP280_ODR_250_MS; break; case 8 : conf.odr = BMP280_ODR_125_MS; break; case 16 : conf.odr = BMP280_ODR_62_5_MS; break; case 2048 : conf.odr = BMP280_ODR_0_5_MS; break; case BMP280_ODR_2000_MS : conf.odr = BMP280_ODR_2000_MS; break; case BMP280_ODR_4000_MS : conf.odr = BMP280_ODR_4000_MS; break; default: return -RT_ERROR; } rslt = bmp280_set_config(&conf, &bmp); if(rslt!=BMP280_OK) { print_rslt(" bmp280_set_config status", rslt); return -RT_ERROR; } return RT_EOK; } else { // LOG_E("only 1,2,4,8,16,2048,BMP280_ODR_2000_MS,BMP280_ODR_4000_MS could set"); return -RT_ERROR; } } ``` 最新版本的sensor control 去掉了好多控制,做了相应的注释 ```c static rt_err_t _bmp280_control(struct rt_sensor_device *sensor, int cmd, void *args) { rt_err_t result = RT_EOK; switch (cmd) { case RT_SENSOR_CTRL_GET_ID: result = _bmp280_get_id(sensor,args); break; #if 0 case RT_SENSOR_CTRL_SET_RANGE: result = -RT_ERROR; break; case RT_SENSOR_CTRL_SET_ODR: result = _bmp280_set_odr(sensor,(rt_uint32_t)args & 0xffff); break; case RT_SENSOR_CTRL_SET_MODE: break; case RT_SENSOR_CTRL_SET_POWER: result = _bmp280_set_POWER(sensor,(rt_uint32_t)args & 0xff); break; #endif case RT_SENSOR_CTRL_SELF_TEST: result = -RT_ERROR; break; default: // LOG_E("only RT_SENSOR_CTRL_GET_ID,RT_SENSOR_CTRL_SET_POWER,RT_SENSOR_CTRL_SET_ODR could set"); return -RT_ERROR; } return result; } ``` **3.bmp280 app 初始化** ```c #include
#include
#include
#include "sensor_bs_bmp280.h" #define BMP_NAME "bmp280" int bmp280_port(void) { struct rt_sensor_config cfg; cfg.intf.dev_name = BMP280_I2CBUS_NAME; rt_hw_bmp280_init("bmp280", &cfg); return 0; } INIT_APP_EXPORT(bmp280_port); ``` ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230716/de0234385603adab798370ef844c25cd.png) 上电log 打印可以看到device 已经注册成功 4.oled 显示驱动 oled 显示通过u8g2进行驱动,这里参考ssd1306 I2C 初始化过程 代码如下: ```c u8g2_t u8g2; // Initialization u8g2_Setup_ssd1306_i2c_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_gpio_and_delay_rtthread); u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_I2C_CLOCK, OLED_I2C_PIN_SCL); u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_I2C_DATA, OLED_I2C_PIN_SDA); ``` 显示初始化前 ,先寻找驱动 ```c rt_device_t temp_dev =RT_NULL; rt_device_t baro_dev =RT_NULL; rt_size_t res =0; temp_dev = rt_device_find(BMP_TEMP); if(temp_dev == RT_NULL) { rt_kprintf("can not find bmp280 temp\n"); return ; } baro_dev = rt_device_find(BMP_BARO); if(temp_dev == RT_NULL) { rt_kprintf("can not find bmp280 baro\n"); return ; } if (rt_device_open(temp_dev, RT_DEVICE_FLAG_RDWR) != RT_EOK) { rt_kprintf("open device failed!\n"); return; } if (rt_device_open(baro_dev, RT_DEVICE_FLAG_RDWR) != RT_EOK) { rt_kprintf("open device failed!\n"); return; } ``` 然后显示线程的主循环中按一定的间隔进行sensor 数据读取,然后更新显示到oled 上 ```c res = rt_device_read(temp_dev, 0, &temp_data, 1); if (res != 1) { rt_kprintf("read data failed!size is %d\n", res); rt_device_close(temp_dev); return; } res = rt_device_read(baro_dev, 0, &baro_data, 1); if (res != 1) { rt_kprintf("read data failed!size is %d\n", res); rt_device_close(baro_dev); return; } u8g2_ClearBuffer(&u8g2); u8g2_DrawStr(&u8g2, 12, 12, "psoc6 demo"); temp_int =temp_data.data.temp; //sprintf(buf,"temp:%.2f C",temp_data.data.temp); sprintf(buf,"temp:%d.%d C",temp_int /10,temp_int %10); u8g2_DrawStr(&u8g2, 32, 40, buf); sprintf(buf,"baro:%.0fPa",baro_data.data.baro ); u8g2_DrawStr(&u8g2, 32, 56, buf); u8g2_SendBuffer(&u8g2); rt_thread_mdelay(100); ``` 整体实现效果如下: ![screenshot_efc2743f81624cb1240854fd5ea1865.jpg](https://oss-club.rt-thread.org/uploads/20230716/1497865cfacf64d7c61d6e2e05831032.jpg.webp) [实现代码链接](https://gitee.com/xinshuwei/psoc6-collects-data "实现代码链接")
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
xinshuwei
这家伙很懒,什么也没写!
文章
8
回答
0
被采纳
0
关注TA
发私信
相关文章
1
_ansi.h 谁有这个头文件地址或者源码呀
2
我想在OLED显示屏上显示温湿度数据,
3
u8g2 驱动1.3寸oled乱码
4
F407zgoled编译报错
5
关于使用oled的时候,print报错
6
rt_thread studio
7
ART PI怎么样驱动oled屏
8
u8g2软件包中详细配置examples的软件无效设置
9
u8g2的软件包详细设置中勾选exmple无效
10
RTT提示没有drv_spi.h
推荐文章
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组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
UART
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
SFUD
msh
rt_mq_消息队列_msg_queue
keil_MDK
ulog
MicroPython
C++_cpp
本月问答贡献
出出啊
1517
个答案
342
次被采纳
小小李sunny
1443
个答案
289
次被采纳
张世争
805
个答案
174
次被采纳
crystal266
547
个答案
161
次被采纳
whj467467222
1222
个答案
148
次被采纳
本月文章贡献
出出啊
1
篇文章
4
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
1
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部