PIC32MX470 温湿度计+RTOS+GUI:串口打印温湿度数据解析

串口可用之后,很多debug信息就可以通过串口打印输出了,所以我打算先把读到的温湿度数据通过串口打印出来,然后再调oled显示,之后再将数据通过oled屏显示,一步一步来。
首先还是在板子自带的information sheet上找到i2c的管脚,有i2c1和i2c2,但还是由于硬件老白的原因,只能用x32接口上的i2c2了。
看了下温湿度的资料,最高耐压5.5v,先接到3.3v上试一下,按如下方式连接x32和温湿度计
接下来依然是通过mhc来使能i2c驱动
1. 打开mhc的options选项卡,找到i2c对应的driver选项打开并做相应的配置,我的配置如下
2. 打开mhc的pin settings,将rf4和rf5设置为i2c2的sda和scl
3. 然后生成代码,主要包含以下几个源文件,我为了添加callback以及封装dht12的驱动,又添加了bsp_i2c.c和bsp_dht12.c两个文件
4. 分析i2c驱动代码后可知在sys_initialize中已经根据用户的配置调用了i2c相关的初始化函数,所以使用时只需要在我们的封装层里直接调用drv_i2c_mapping.c中的其他api就可以了,我添加的bsp_i2c.c和bsp_dht12.c中的代码如下,dht12的数据还没有做校验
bsp_i2c.c
#include “system/common/sys_common.h”
#include “system_config.h”
#include “system_definitions.h”
#include “driver/i2c/drv_i2c.h”
#include
#include
#define bsp_i2c_buf_size 0x10
typedef struct bsp_i2c_dev {
drv_handle i2chandle;
os_sem semlock; /* i2c exclusive access sempahore */
os_sem semwait; /* transfer complete signal */
cpu_int08u txbuf[bsp_i2c_buf_size]; /* the transfer data area */
cpu_int08u rxbuf[bsp_i2c_buf_size]; /* the receive data area */
} bsp_i2c_dev;
static bsp_i2c_dev bsp_i2c_devtbl[bsp_i2c_nbr_max];
static void bsp_i2c2_callback (drv_i2c_buffer_event event,
drv_i2c_buffer_handle bufferhandle, uintptr_t context);
cpu_boolean bsp_i2c_init (cpu_int08u i2c_id,
cpu_int08u i2c_mode,
cpu_int32u bit_rate)
{
os_err err;
bsp_i2c_dev *p_i2c_dev;
switch (i2c_id) {
case bsp_i2c_id_i2c2:
p_i2c_dev = (bsp_i2c_dev *)&bsp_i2c_devtbl[0];
break;
default:
return (def_fail);
}
/* setup the i2c handle */
p_i2c_dev-》i2chandle = drv_i2c_open(drv_i2c_index_0, 0);
/* -------------- create os semaphores ------------- */
ossemcreate((os_sem *)&(p_i2c_dev-》semwait), “i2c wait”, 0, &err);
ossemcreate((os_sem *)&(p_i2c_dev-》semlock), “i2c lock”, 1, &err);
switch (i2c_id) {
case bsp_i2c_id_i2c2:
drv_i2c_buffereventhandlerset(p_i2c_dev-》i2chandle, bsp_i2c2_callback, null);
break;
default:
return (def_fail);
}
return (def_ok);
}
cpu_boolean bsp_i2c_wrrd (cpu_int08u i2c_id,
cpu_int08u i2c_addr,
cpu_int08u *offset_buf,
cpu_int08u offset_len,
cpu_int08u *p_buf,
cpu_int16u nbr_bytes)
{
os_err err;
bsp_i2c_dev *p_i2c_dev;
if ((offset_buf == (cpu_int08u *)0) || (p_buf == (cpu_int08u *)0)) {
return (def_fail);
}
if ((nbr_bytes 《 1) ||
((offset_len + 1) 》 bsp_i2c_buf_size) ||
((nbr_bytes + 1) 》 bsp_i2c_buf_size)) {
return (def_fail);
}
switch (i2c_id) {
case bsp_i2c_id_i2c2:
p_i2c_dev = (bsp_i2c_dev *)&bsp_i2c_devtbl[0];
break;
default:
return (def_fail);
}
/* lock the i2c peripheral */
ossempend(&(p_i2c_dev-》semlock), 0, os_opt_pend_blocking, 0, &err);
/* do master write transfer */
drv_i2c_transmitthenreceive(p_i2c_dev-》i2chandle, i2c_addr,
offset_buf, offset_len, p_buf, nbr_bytes, null);
/* wait until the transfer completes */
ossempend(&(p_i2c_dev-》semwait), 1000, os_opt_pend_blocking, 0, &err);
ossempost(&(p_i2c_dev-》semlock), os_opt_post_1, &err); /* release the i2c peripheral */
return def_ok;
}
static void bsp_i2c2_callback (drv_i2c_buffer_event event,
drv_i2c_buffer_handle bufferhandle, uintptr_t context)
{
os_err err;
bsp_i2c_dev *p_i2c_dev;
p_i2c_dev = (bsp_i2c_dev *)&bsp_i2c_devtbl[0];
ossempost(&(p_i2c_dev-》semwait), os_opt_post_1, &err); /* post to the sempahore */
}
bsp_dht12.c
#include “system/common/sys_common.h”
#include “system_config.h”
#include “system_definitions.h”
#include
#include
cpu_boolean bsp_dht12_read (cpu_int08u *hum_high,
cpu_int08u *hum_low,
cpu_int08u *temp_high,
cpu_int08u *temp_low)
{
cpu_boolean ret;
cpu_int08u byte_addr = 0;
cpu_int08u data_buf[4];
ret = bsp_i2c_wrrd(bsp_i2c_id_i2c2, 0xb8, &byte_addr, 1, data_buf, 4);
if (ret) {
*hum_high = data_buf[0];
*hum_low = data_buf[1];
*temp_high = data_buf[2];
*temp_low = data_buf[3];
}
return ret;
}
5. 在_sys_tasks任务中添加我们自己代码的初始化
6. 最后在app_tasks中添加读温湿度数据的处理,每秒读一次并通过串口打印出来
串口打印输出如下
串口定时地将温湿度数据打印出来,也算是一个简陋的温湿度计吧。下一步就是调试spi和oled屏,给我们的温湿度计做一个好看一点的输出界面,毕竟这是一个实(kao)力(lian)说(chi)话(fan)的时代。

美媒称阿里不可能创造100万岗位 表示不相信
福建物构所单晶异质结偏振光探测研究获进展
为何要引入去耦电容?在选择去耦电容的容值时应该考虑哪些因素?
智能门锁的应用场景广阔 但信息安全问题很明显
魅蓝note6使用P20?魅族硬生生让使用高通处理器成为卖点
PIC32MX470 温湿度计+RTOS+GUI:串口打印温湿度数据解析
Altera Cyclone IV GX系列的特性及FPGA开发套件的设计方案介绍
网约出租电动汽车市场怎么了 还有前景吗
基于Zynq平台的动态智能家居系统的设计
截至2020年12月,我国IPv4地址数量为38923万个
边缘计算应用中的SSD如何保持高数据安全性和完整性
英特尔正在测试新型量子芯片,其计算能力要将比当前全球最快的超级电脑要强大得多
5G网络部署正在全球各地如火如荼地开展
随着电子产品的繁荣发展,智能照明行业将迎来黄金期
今年一季度京东方持续稳居全球半导体显示龙头地位
FCI推出多端口和单端口RJ45模块插座连接器
鲁大师2023年Q1手机报告:魅族 20 PRO沉默中爆发,登顶整机流畅度榜单!
剖析电池包的直接降价空间有多少
2018年全球智能音箱Q3出货量达2270万台,中国成最大赢家
基于RaspberryPi Zero W的机器人的制作