<p><iframe name="ifd" src="https://mnifdv.cn/resource/cnblogs/ML307R_OPEN" frameborder="0" scrolling="auto" width="100%" height="1500"></iframe></p>
'custom_main/src/uart.c',
'custom_main/src/sht3x.c',
current_dir + '/custom_main',
#include "custom_main.h"
#include "stdio.h"
#include "stdlib.h"
#include "cm_fs.h"
#include "cm_mem.h"
#include "cm_sys.h"
#include "cm_sim.h"
#include "cm_rtc.h"
#include "cm_os.h"
#include "cm_i2c.h"
#include "cm_gpio.h"
#include "cm_iomux.h"
#include "cm_uart.h"
#include "uart.h"
#include "sht3x.h"
/********************************************uart0**************************************************/
uartStruct uart0={0};//定义串口结构体变量
void uartRecvData(uint32_t flags, char *data, int len)
{
uartSendData(&uart0, data, len);//把接收的数据返回
}
/********************************************Sht30**************************************************/
osThreadId_t osThreadIdSht30;//用于记录任务的句柄(ID码),可以用来停止任务
static void osThreadIdSht30Fun(void *param)
{
cm_i2c_cfg_t config = {
CM_I2C_ADDR_TYPE_7BIT,
CM_I2C_MODE_MASTER,
CM_I2C_CLK_100KHZ
};
int32_t ret=0;
sht3x_init(&sht3x, SHT3X_DEV_ADDR);
char print_buff[100];
int len=0;
while (1)
{
//启动i2c
if (cm_i2c_open(SHT3X_I2C_ID, &config)!=0)
{
cm_log_printf(0,"i2c init err, ret = %d\n", ret);
osDelay(1000/5);//延时
cm_i2c_close(SHT3X_I2C_ID);
osDelay(20/5);//延时
}
else
{
cm_log_printf(0,"i2c init ok\n");
// 启动传感器
osDelay(20/5);//延时
ret = sht3x_begin(&sht3x);
if (ret != 0)
{
cm_log_printf(0,"SHT3x begin failed\n");
osDelay(1000/5);//延时
cm_i2c_close(SHT3X_I2C_ID);
osDelay(20/5);//延时
}
else
{
osDelay(20);//延时
while (1)
{
ret = sht3x_measure(&sht3x);// 读取传感器数据
if (ret != 0)
{
cm_log_printf(0,"Failed to read from SHT3x\n");
break;
}
else
{
//打印温湿度数据
len = snprintf(print_buff, sizeof(print_buff), "Temperature: %.2f C, Humidity: %.2f %%\n", sht3x.temperature, sht3x.humidity);
uartSendData(&uart0, print_buff, len);
}
osDelay(100); //延时读取一次数据
}
}
}
}
}
int cm_opencpu_entry(void *param)
{
(void)param;
//配置串口
uart0.uartId = 0;//配置串口号
uart0.uartRecvCb = uartRecvData;//设置接收数据函数
uart0.config.baudrate = 115200;//波特率
uart0.config.byte_size = CM_UART_BYTE_SIZE_8;//数据位数
uart0.config.flow_ctrl = CM_UART_FLOW_CTRL_NONE;//硬件流控
uart0.config.parity = CM_UART_PARITY_NONE;//奇偶校验
uart0.config.stop_bit = CM_UART_STOP_BIT_ONE;//停止位
uart0.config.is_lpuart = 0;//若要配置为低功耗模式可改为1
if (uart_init(&uart0) !=0)//初始化串口
{
return -1;
}
//配置任务sht30
osThreadAttr_t app_task_attr = {0};
app_task_attr.name = "osThreadIdSht30";//任务名字-随意
app_task_attr.stack_size = 4096 * 2;//任务使用栈大小-写这个就可以
app_task_attr.priority = osPriorityNormal;//任务优先级-普通优先级
//返回任务句柄 任务函数 给任务函数的参数 任务配置
osThreadIdSht30 = osThreadNew((osThreadFunc_t)osThreadIdSht30Fun, NULL, &app_task_attr);
return 0;
}
.