本期测评我来讲解展示一下使用串口助手上位机软件发送指令来实现RA4E2开发板上的LED1 LED2的点灯操作。替代传统的按键点灯。
话不多说,直接上干货。
1.打开
选择RA4E2




- 先配置LED1和LED2
查看原理图

LED1为P207
LED2为P113


2.配置串口
查看原理图

窗口引脚为P109 P110



点击生成代码
3, 打开KEIL

添加一下代码
#include "hal_data.h"
#include <stdio.h>
/********* 参数宏定义 *********/
#define USART_REC_LEN 50 //定义最大接收字节数 50
#define First_String "Receive:" //返回给PC端的起始字符串
#define First_String_num strlen(First_String) //起始字符串长度
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
/*作用:串口9初始化,P109--TXD,P110--RXD
*传入参数:无
*返回参数:无
*/
void UART9_Init(void)
{
fsp_err_t err = FSP_SUCCESS;
err = R_SCI_UART_Open(&g_uart0_ctrl,&g_uart0_cfg);
assert(err == FSP_SUCCESS);
}
volatile bool uart_send_complete_flag = false; // 发送完成标志
uint8_t USART_RX_BUF[USART_REC_LEN] = First_String; //接收缓冲,最大USART_REC_LEN个字节.起始字符串为First_String的宏定义
/*
USART_RX_STA
=============================================================================================================
bit15 | bit14 | bit13--bit0
接收完成标志位(0x0A,换行键) | 接收到0x0d(回车键) | 接收到的有效数据个数
=============================================================================================================
*/
uint16_t USART_RX_STA = First_String_num; //接收状态标记,根据起始字符串来初始化起始字符位置
/********* 函数宏定义 *********/
#define LED1_lighting_off R_IOPORT_PinWrite(&g_ioport_ctrl,BSP_IO_PORT_02_PIN_07,BSP_IO_LEVEL_LOW)
#define LED1_lighting_up R_IOPORT_PinWrite(&g_ioport_ctrl,BSP_IO_PORT_02_PIN_07,BSP_IO_LEVEL_HIGH)
#define LED2_lighting_off R_IOPORT_PinWrite(&g_ioport_ctrl,BSP_IO_PORT_01_PIN_13,BSP_IO_LEVEL_LOW)
#define LED2_lighting_up R_IOPORT_PinWrite(&g_ioport_ctrl,BSP_IO_PORT_01_PIN_13,BSP_IO_LEVEL_HIGH)
/*函数说明:led1闪烁
*传入参数:无
*返回参数:无
*/
void led_1_flicker(void)
{
LED1_lighting_up;
R_BSP_SoftwareDelay(1,BSP_DELAY_UNITS_SECONDS);
LED1_lighting_off;
R_BSP_SoftwareDelay(1,BSP_DELAY_UNITS_SECONDS);
}
/*函数说明:led2闪烁
*传入参数:无
*返回参数:无
*/
void led_2_flicker(void)
{
LED2_lighting_up;
R_BSP_SoftwareDelay(1,BSP_DELAY_UNITS_SECONDS);
LED2_lighting_off;
R_BSP_SoftwareDelay(1,BSP_DELAY_UNITS_SECONDS);
}
static volatile uint8_t falg = 0;
/*用于串口9的接收和发送中断
*传入参数:
*p_args:这个不需要我们管,系统自动会将数据传入
*返回参数:无
/
void UART9_callback(uart_callback_args_t * p_args)
{
switch (p_args->event)
{
//如果是串口接收中断
case UART_EVENT_RX_CHAR:
{
/ 根据字符指令控制RGB彩灯颜色 */
switch (p_args->data)
{
case '1':
LED1_lighting_up;
falg = 1;
break;
case '2':
LED1_lighting_off;
falg = 2;
break;
case '3':
LED2_lighting_up;
falg = 3;
break;
case '4':
LED2_lighting_off;
falg = 4;
break;
case '5':
LED1_lighting_up; LED2_lighting_up;
falg = 5;
break;
case '6':
LED1_lighting_off; LED2_lighting_off;
falg = 6;
break;
default:
break;
}
break;
}
//如果是串口发送中断
case UART_EVENT_TX_COMPLETE:
{
uart_send_complete_flag = true;
break;
}
default:
break;
}
}
/* 重定向 printf 输出 */
#if defined GNUC && !defined clang
int _write(int fd, char *pBuffer, int size); //防止编译警告
int _write(int fd, char *pBuffer, int size)
{
(void)fd;
R_SCI_UART_Write(&g_uartx_ctrl, (uint8_t *)pBuffer, (uint32_t)size);
while(uart_send_complete_flag == false);
uart_send_complete_flag = false;
return size;
}
#else
int fputc(int ch, FILE *f)
{
(void)f;
R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&ch, 1);
while(uart_send_complete_flag == false);
uart_send_complete_flag = false;
return ch;
}
#endif

LED宏定义

串口接收点灯

串口重定向

主函数

输入1 代表 LED1亮
输入2 代表 LED1灭
输入3 代表 LED2亮
输入4 代表 LED2灭
输入5 代表 LED1亮 LED2亮
输入6 代表 LED1灭 LED2灭