fuart库用于接入单片机的UART通信。
使用时,需要按照平台封装好UART相关的函数,并且传入初始化结构体。
MCU:R5F10BGGC
根据所使用的平台,先封装好uart相关的函数(阻塞函数), 以RL78平台为例。
externvolatilecharuart0_tx_flag;
externvolatilecharuart0_rx_flag;
voiduart0_Send(uint8_t*consttx_buf, uint16_ttx_num)
{
uint16_tlen=strlen(tx_buf);
uart0_tx_flag=R_UART0_Send((uint8_t*)tx_buf, len);
while(uart0_tx_flag==0);
}
uint8_tuart0_Receive()
{
uint8_tbuf;
uart0_rx_flag=0;
uart0_rx_flag=R_UART0_Receive(&buf, 1);
while(uart0_rx_flag==0);
returnbuf;
}中断设置如下:
volatilecharuart0_tx_flag;
volatilecharuart0_rx_flag;
staticvoidr_uart0_callback_sendend(void)
{
/* Start user code. Do not edit comment generated here */uart0_tx_flag=1;
/* End user code. Do not edit comment generated here */
}
staticvoidr_uart0_callback_receiveend(uint8_terr_type)
{
/* Start user code. Do not edit comment generated here */uart0_rx_flag=1;
/* End user code. Do not edit comment generated here */
}首先需要初始化fuart设备。这需要提供一个fuart_InitTypeDef类型的结构体,其中包含了设备的发送函数和接收函数。然后,调用fuart_Init函数进行初始化,并获取到一个设备句柄。
fuart_InitTypeDeffuart_InitStruct;
fuart_tfuart;
fuart_InitStruct.Send=&uart0_Send; // 发送数据的函数指针fuart_InitStruct.Receive=&uart0_Receive; // 接收数据的函数指针fuart_Init(&fuart_InitStruct, &fuart);使用fuart_printf函数发送字符串。可以使用格式化字符串和可变参数。
intdata=114514;
fuart_printf(fuart, "data = %d!\r\n", buf); // 发送一个带有参数的字符串使用fuart_scanf函数接收字符串。可以使用格式化字符串和可变参数。
intbuf=0;
fuart_scanf(fuart, "%d", &buf); // 从串口接收一个整数最后,当我们不再需要使用fuart设备时,可以调用fuart_DeInit函数来释放设备句柄。
fuart_DeInit(fuart);在使用本库时,请确保你的平台上已经正确实现了UART通讯函数,并能够正常工作。