RT-Thread 串口的使用

简单测试串口打印

主函数前定义


#define SAMPLE_UART_NAME "uart2" /* 串 口 设 备 名 称 */
static rt_device_t serial; /* 串 口 设 备 句 柄 */
char str[] = "hello RT-Thread!\r\n"; /* 要发送的数据 */

主函数内


struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 配 置 默 认 参 数 */

/* 查 找 串 口 设 备 */
serial = rt_device_find(SAMPLE_UART_NAME);
/* 以 中 断 接 收 及 轮 询 发 送 模 式 打 开 串 口 设 备 */
rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);

//通过config结构体来配置 串口参数
config.baud_rate = BAUD_RATE_115200;
config.data_bits = DATA_BITS_8;
config.stop_bits = STOP_BITS_2;
config.parity = PARITY_NONE;

/* 打 开 设 备 后 才 可 修 改 串 口 配 置 参 数 */
rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
	

while内

rt_device_write(serial, 0, str, (sizeof(str) - 1));

另一种方法

直接添加rt_kprintf函数就行

rt_kprintf("wave_diff value:%d::%d\n",p[0],p[2]);

我的工程

/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-5      SummerGift   first version
 */
 
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "dht11.h"
#include "usart1.h"
#include "drv_usart.h"

#define LED1_PIN    GET_PIN(B, 5)

#define SAMPLE_UART_NAME "uart2" /* 串 口 设 备 名 称 */
	static rt_device_t serial; /* 串 口 设 备 句 柄 */
	char str[] = "hello RT-Thread!\r\n"; /* 要发送的数据 */


	int p[5] = {11};
	int temp;
	int humi;
int main(void)
{
    int count = 1;
	
	
	struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 配 置 默 认 参 数 */

	/* 查 找 串 口 设 备 */
	serial = rt_device_find(SAMPLE_UART_NAME);
	/* 以 中 断 接 收 及 轮 询 发 送 模 式 打 开 串 口 设 备 */
	rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);

	//通过config结构体来配置 串口参数
	config.baud_rate = BAUD_RATE_115200;
	config.data_bits = DATA_BITS_8;
	config.stop_bits = STOP_BITS_2;
	config.parity = PARITY_NONE;

	/* 打 开 设 备 后 才 可 修 改 串 口 配 置 参 数 */
	rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
		
		
    /* set LED0 pin mode to output */
    rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);

	Dht11_Init();

    while (1)
    {
        rt_pin_write(LED1_PIN, PIN_HIGH);//LED状态翻转
        rt_thread_mdelay(500);
        rt_pin_write(LED1_PIN, PIN_LOW);
        rt_thread_mdelay(500);
		
		Mcu_Control_Dht11(p);//DHT11温湿度采集
		rt_device_write(serial, 0, str, (sizeof(str) - 1));//方法1
//		rt_kprintf("wave_diff value:%d::%d\n",p[0],p[2]); //方法2
    }

    return RT_EOK;
}

正在学习,不断更新中。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值