温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

R5F100LE串口队列的实现和UART使用心得是怎样的

发布时间:2022-01-05 19:41:33 来源:亿速云 阅读:93 作者:柒染 栏目:互联网科技

今天就跟大家聊聊有关R5F100LE串口队列的实现和UART使用心得是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

    介绍一下如何使用瑞萨的串口,当然使用串口的配置很简单只要,用代码生成器配置一下就行了。如果你也用的R78/G13这个套件,使用串口与电脑通信的话,还需要一个USB转TTL的插头,板子上没有232电平转换电路。如果没用过串口的话这有个教程R78/G13开发套件 快速入门教程。要有这里就不介绍如何配置串口了,重点写一下串口队列的实现,ardiuno的串口用起来很方便,因为他有一个串口队列,这篇文章介绍在,R5F100LE中实现一个环形串口队列。

    环形队列就是头尾相连的队列,如果放入的数据不超出定义的队列长度,就可以一直使用,不用考虑队列的长度。环形队列的实现,主要就是三个函数,putMsg()将数据放入队列中,getMsg()将数据从队列中取出,取出的是第一个放入队列中的数据,这也是队列的基本特点。testQueue()检查队列中是否有数据,有数据的话就放回1。

#define QUEUELEN 256

uint8_t queuehead = 0, queuetail = 0;//队列头尾的指示
uint8_t msg[QUEUELEN];//队列存放数组

void putMsg(uint8_t m)    //将数据放入队列
{
	msg[queuetail] = m;
	if(++queuetail >= QUEUELEN) queuetail = 0;
}

uint8_t getMsg(void)    //从队列中取出数据,这个数据是最先放入队列的那个数据
{
	uint8_t m = msg[queuehead];
	if(++queuehead >= QUEUELEN) queuehead = 0;
	return m;
}

uint8_t testQueue(void)//检查数据中是否有数据
{
	return (queuetail == queuehead) ? 0:1;
}

    在r_cg_serial_user.c里,这里改写了__interrupt static void r_uart0_interrupt_receive(void)这个中断函数。从串口接收到的数据直接放入环形队列中。

/***********************************************************************************************************************
* Function Name: r_uart0_interrupt_receive
* Description  : This function is INTSR0 interrupt service routine.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
__interrupt static void r_uart0_interrupt_receive(void)
{
    uint8_t rx_data;
   
    rx_data = RXD0;

	putMsg(rx_data);//将从串口中接收的数据直接放入环形队列中
}

实现一个函数,向串口中打印字符串。

void printStr(char const * ch)
{		
	R_UART0_Send(ch, strlen(ch));//发送字符串

	while(g_uart0_tx_count);//等待数据发送完成
}

测试主程序:作用每隔500ms向串口打印“500hello world”,并把从串口中接收到的数据发回串口。

#include "r_cg_macrodriver.h"
#include "r_cg_cgc.h"
#include "r_cg_port.h"
#include "r_cg_serial.h"
#include "r_cg_timer.h"
/* Start user code for include. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */
#include "r_cg_userdefine.h"

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
/* Start user code for global. Do not edit comment generated here */
extern uint32_t GlobalTime;    // 全局时钟变量在定时器中断函数中每ms加1
extern volatile uint16_t  g_uart0_tx_count;//串口发送数据计数,发完数据是为0
/* End user code. Do not edit comment generated here */
void R_MAIN_UserInit(void);

/***********************************************************************************************************************
* Function Name: main
* Description  : This function implements main function.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void main(void)
{    
    /* Start user code. Do not edit comment generated here */
	uint32_t nowTime=0, lastTime=0;//时间寄存
	char chBuf[32]={0}, ch=0;//数据buff
	R_MAIN_UserInit();
	R_TAU0_Channel0_Start();//定时器通道一
	R_UART0_Start();    //开启串口

    while (1U)
    {

                nowTime=GlobalTime;//更新当前时间
		if((nowTime-lastTime)>500)//每隔500ms
		{
			lastTime = GlobalTime;
			P13.0 = ~P13.0;//测试小灯
			sprintf(chBuf, "%ld", 500);//将数字转换为字符串
			strcat(chBuf, "hello world");//字符串拼接
			//R_UART2_Send(chBuf, strlen(chBuf));			
			printStr(chBuf);//从串口发出字符串
		}
		if(testQueue())//如果队列中有数据
		{
				ch = getMsg();//取出一个数据
				R_UART0_Send(&ch, 1);//发回串口
				while(g_uart0_tx_count);//等待发送完毕
		}

    }
    /* End user code. Do not edit comment generated here */
}

/***********************************************************************************************************************
* Function Name: R_MAIN_UserInit
* Description  : This function adds user code before implementing main function.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_MAIN_UserInit(void)
{
    /* Start user code. Do not edit comment generated here */
    EI();
    /* End user code. Do not edit comment generated here */
}

/* Start user code for adding. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */

如果你想从串口接收一长串数据,你最好在检测到,串口有数据之后延时一小段时间,以保证数据全部被放入队列。

接收数据之后你要做的就是解析这些数据了。接收几个有用的函数。

函数名:char *strtok(char s[], const char *delim);

功能介绍:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。

测试代码:

#include<string.h>
#include<stdio.h>
int main(void)
{
    char input[16]="123,23,12";
    char*p;
    
    p=strtok(input,",");
    if(p)
        printf("%s\n",p);               
    p=strtok(NULL,",");
    if(p)
        printf("%s\n",p);
    p=strtok(NULL,",");
    if(p)
	printf("%s\n",p);

    return 0;
}

运行结果

R5F100LE串口队列的实现和UART使用心得是怎样的

假如你输入的是PID的,KP,KI, KD的话,可以直接用atof, atoi函数将对应的数字字符串变为数字。可以在线改参数。

还有sprintf,atof,atoi这几个函数都很好用。

看完上述内容,你们对R5F100LE串口队列的实现和UART使用心得是怎样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI