【匠芯创D133CBS】 RS485通信测试
<p><span style="font-size:16px;">测试板卡上的RS485通信,通过命令来控制蜂鸣器开关。</span></p><p> </p>
<p><span style="font-size:16px;"><strong>一、硬件电路图</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">RS485部分电路图</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><span style="font-size:16px;"><strong>二、外设配置</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">RS485采用2线制通信,在串口配置成RS485方式,配置如下:</span></p>
<p><span style="font-size:16px;">2.1、使能串口1</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><span style="font-size:16px;">2.2、选择RS485两线模式</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><strong><span style="font-size:16px;">三、程序</span></strong></p>
<p> </p>
<p><span style="font-size:16px;">3.1、uart.c</span></p>
<pre>
<code>//uart.c
#include <getopt.h>
#include <string.h>
#include <rtthread.h>
#include <aic_core.h>
#include "beep.h"
#define SAMPLE_UART_NAME "uart1"
#define TIMEOUT_NONE -4096
static struct rt_semaphore rx_sem;
static rt_device_t serial;
static char str_send[] = "https://www.eeworld.com.cn/\n";
uint8_t g_recv_max = 128;
static int g_uart_test_result = 1;
static int g_exit = 0;
static int g_timeout = TIMEOUT_NONE;
static rt_timer_tuart1_rx_timeout = RT_NULL;
char str_receive = {0};
typedef struct
{
uint8_t buf;
uint8_t len;
uint8_t rx_over;
} uart_rx_dat_t;
uart_rx_dat_t uart1_rx_dat = { 0 };
static void uart1_rx_timeout_cb(void *p)
{
rt_timer_stop(uart1_rx_timeout);
rt_kprintf("read_length: %d \n", uart1_rx_dat.len);
uart1_rx_dat.rx_over = RT_TRUE;
}
rt_err_t uart1_input(rt_device_t dev, rt_size_t size)
{
char ch;
//rt_sem_release(&rx_sem);
if(rt_device_read(serial, -1, &ch, 1)==1)
{
uart1_rx_dat.buf = ch;
rt_timer_start(uart1_rx_timeout);
}
return RT_EOK;
}
void serial_uart1_thread_entry(void *parameter)
{
uint8_t jx=0;
char ch;
int ret = 0;
int cnt = 0;
static int timeout_time = 0;
static uint8_t rx_one_byte_flag = 1;
while (1)
{
if(uart1_rx_dat.rx_over == RT_TRUE)
{
uart1_rx_dat.rx_over = RT_FALSE;
rt_kprintf("read_dat: ");
for(jx=0; jx<uart1_rx_dat.len; jx++)
{
rt_kprintf("%02x", uart1_rx_dat.buf);
}
rt_kprintf("\n");
uart1_rx_dat.len=0;
if( uart1_rx_dat.buf==0xff &uart1_rx_dat.buf==0x2a)
{
if(uart1_rx_dat.buf==0x01)
{
beep_on();
}
else if(uart1_rx_dat.buf==0x00)
{
beep_off();
}
}
}
rt_thread_mdelay(5);
}
}
int test_uart_sample(int argc, char *argv[])
{
int c = 0;
rt_err_t ret = RT_EOK;
static rt_uint8_t open_cnt = 0;
//查找串口设备
serial = rt_device_find(SAMPLE_UART_NAME);
if (!serial)
{
rt_kprintf("find %s failed!\n", SAMPLE_UART_NAME);
return -RT_ERROR;
}
//初始化信号量
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
//打开串口设备
ret = rt_device_open(serial, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
if (ret != RT_EOK)
{
rt_kprintf("open %s failed : %d !\n", SAMPLE_UART_NAME, ret);
return -RT_ERROR;
}
//设置接收回调函数
rt_device_set_rx_indicate(serial, uart1_input);
rt_device_write(serial, 0, str_send, (sizeof(str_send) - 1));
//创建 serial 线程
rt_thread_t thread = rt_thread_create("serial", serial_uart1_thread_entry, RT_NULL, 1024*2, 26, 10);
if (thread != RT_NULL) {
rt_thread_startup(thread);
} else {
rt_device_close(serial);
return -RT_ERROR;
}
//创建接收超时定时器
uart1_rx_timeout = rt_timer_create("serial_timer", uart1_rx_timeout_cb, RT_NULL, 50, RT_TIMER_FLAG_PERIODIC);
return ret;
}
MSH_CMD_EXPORT(test_uart_sample,Uart Test);
</code></pre>
<p> </p>
<p><strong><span style="font-size:16px;">四、程序运行</span></strong></p>
<p> </p>
<p><span style="font-size:16px;">下载程序后,串口发送控制命令</span></p>
<p><span style="font-size:16px;">4.1、执行测试命令</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><span style="font-size:16px;">4.2、打开蜂鸣器</span></p>
<p></p>
<p> </p>
<p><span style="font-size:16px;">4.3、关闭蜂鸣器</span></p>
<p></p>
<p> </p>
<p>大佬,这个例程我也试了一下,但是用串口ttl发送test_uart_sample提示我没有这个命令,是我这个函数没包含到主程序之中吗?是不是将uart.c复制到工程之中就可以呀?正常创建rtt线程是不是就算是包含到工程之中呢?</p>
yangjiaxu 发表于 2024-9-23 00:20
大佬,这个例程我也试了一下,但是用串口ttl发送test_uart_sample提示我没有这个命令,是我这个函数没包含 ...
<p>默认例程是没有打开的,需要在menuconfig中,打开此例程。</p>
TL-LED 发表于 2024-9-23 09:36
默认例程是没有打开的,需要在menuconfig中,打开此例程。
<p>也就是进入到me,配置串口1为rs485 compact io 即可是么?这个我配置成功了,并且我用的是官方的原helloworld例程,原bsp文件夹下有个uart.c文件,我替换成你提供的c文件,编译没报错,但是发送test_uart_sample ,提示没找到该命令。正常操作是将你的uart.c复制到工程里就行吧?</p>
yangjiaxu 发表于 2024-9-23 09:40
也就是进入到me,配置串口1为rs485 compact io 即可是么?这个我配置成功了,并且我用的是官方的原hellow ...
<p>我是复制例程的代码,在此基础上修改,重新命名的。</p>
页:
[1]