Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the realization of R5F100LE serial port queue and the experience of using UART?

2025-03-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about the implementation of R5F100LE serial port queue and the experience of using UART. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Introduce how to use Renesas serial port, of course, the configuration of using serial port is very simple, just configure it with code generator. If you also use the R78/G13 kit and use the serial port to communicate with the computer, you also need a USB to TTL plug. There is no 232level conversion circuit on the board. If you have not used the serial port, here is a tutorial to get started with the R78/G13 development kit. This article will not introduce how to configure the serial port here, but focus on the implementation of the serial port queue. Ardiuno's serial port is very convenient to use because he has a serial port queue. This article introduces the implementation of a circular serial port queue in R5F100LE.

A ring queue is an end-to-end queue, which can be used all the time if the data does not exceed the defined queue length, regardless of the length of the queue. The implementation of the ring queue mainly consists of three functions: putMsg () puts the data into the queue, getMsg () takes the data out of the queue and takes out the data that is the first to be put into the queue, which is also the basic feature of the queue. TestQueue () checks to see if there is any data in the queue, and if so, put it back to 1.

# define QUEUELEN 256uint8_t queuehead = 0, queuetail = 0 QUEUELEN / the indication at the beginning and end of the queue [QUEUELEN]; / / the queue stores the array void putMsg (uint8_t m) / / puts the data into the queue {msg [queuetail] = m; if (+ + queuetail > = QUEUELEN) queuetail = 0 } uint8_t getMsg (void) / / fetches the data from the queue, which is the data {uint8_t m = msg [queuehead]; if (+ + queuehead > = QUEUELEN) queuehead = 0; return m;} uint8_t testQueue (void) / / check whether there is data {return (queuetail = = queuehead)? 0uint8_t testQueue;}

In r_cg_serial_user.c, the interrupt function _ _ interrupt static void r_uart0_interrupt_receive (void) is rewritten here. The data received from the serial port is directly put into the ring queue.

/ * * Function Name: 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); / / put the data received from the serial port directly into the ring queue}

Implement a function to print a string to the serial port.

Void printStr (char const * ch) {R_UART0_Send (ch, strlen (ch)); / / send the string while (g_uart0_tx_count); / / wait for the data to be sent}

Test main program: print "500hello world" to the serial port every 500ms, and send the data received from the serial port back to the serial port.

# 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; / / the global clock variable counts the data per ms plus 1extern volatile uint16_t / serial port in the timer interrupt function. After sending the data, it is 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;// time register char chBuf [32] = {0}, ch=0;// data buff R_MAIN_UserInit (); R_TAU0_Channel0_Start (); / timer channel-R_UART0_Start (); / / Open serial port while (1U) {nowTime=GlobalTime / / update the current time if ((nowTime-lastTime) > 500) / / every 500ms {lastTime = GlobalTime; P13.0 = ~ P13.0 / Test small lamp sprintf (chBuf, "% ld", 500) / / convert a number to a string strcat (chBuf, "hello world"); / / string concatenation / / R_UART2_Send (chBuf, strlen (chBuf)); printStr (chBuf) / / send the string} if (testQueue ()) from the serial port / / if there is data {ch = getMsg () in the queue; / / take out a data R_UART0_Send (& ch, 1) / / send back the serial port while (g_uart0_tx_count); / / wait for the transmission to be completed}} / * End user code. Do not edit comment generated here * /} / * * Function Name: 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 * /

If you want to receive a long string of data from the serial port, you'd better delay for a short period of time after detecting that there is data in the serial port to ensure that all the data is put into the queue.

After receiving the data, all you have to do is parse the data. Receive several useful functions.

Function name: char * strtok (char s [], const char * delim)

Function introduction: decompose a string into a set of strings. S is the string to be decomposed and delim is the delimiter string. On the first call, s points to the string to be decomposed, and then it is called again to set s to NULL.

Test the code:

# include#includeint main (void) {char input [16] = "123 input,"; 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;}

Running result

If you enter PID, KP,KI, KD, you can directly use atof, atoi function to change the corresponding number string into a number. The parameters can be changed online.

And sprintf,atof,atoi these functions are very easy to use.

After reading the above, do you have any further understanding of the implementation of R5F100LE serial port queue and the experience of using UART? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report