4.编写MM32F0140 UART1发送字节和ASCII字符函数: (1)MM32F0140 UART1发送字节函数如下所示: void Bsp_UART_SendByte(UART_TypeDef* uart,u8 data)
{
UART_SendData(uart, data);
while(!UART_GetFlagStatus(uart,UART_FLAG_TXEPT));
}
(2)MM32F0140 UART1发送多字节函数如下所示: void Bsp_UART_SendBytes(UART_TypeDef* uart,u8 *buf, u16 len)
{
while(len--)
{
Bsp_UART_SendByte(uart,*buf++);
}
}
[url=](3)MM32F0140 UART1发送ASCII字符串函数如下所示:[/url] void Bsp_UART_SendASCII(UART_TypeDef* uart,char *str)
{
while(*str)
{
Bsp_UART_SendByte(uart,*str++);
}
}
[url=]5.编写MM32F0140 UART1处理中断接收到的数据函数:[/url] MM32F0140 UART1处理中断接收到的数据函数如下所示,当上位机串口助手发送8字节的十六进制数据:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55;下位机MM32F0140的UART1中断接收到一帧:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55 共8字节数据后,通过UART1发送多字节函数,原样发送到串口助手。 void Bsp_UART1_Recv_Task(void)
{
if(gUART1_Rx_Flag == 1)
{
gUART1_Rx_Flag = 0;
if((gUART1_Rx_Buf[0] == 0xAA) && (gUART1_Rx_Buf[1] == 0x01) && (gUART1_Rx_Buf[2] == 0x02) && (gUART1_Rx_Buf[3] == 0x03) && \
(gUART1_Rx_Buf[4] == 0x04) && (gUART1_Rx_Buf[5] == 0x05) && (gUART1_Rx_Buf[6] == 0x06) && (gUART1_Rx_Buf[7] == 0x55))
{
Bsp_UART_SendBytes(UART1,gUART1_Rx_Buf,8);
}
gUART1_Rx_Cnt = 0;
memset(gUART1_Rx_Buf,0,sizeof(gUART1_Rx_Buf));
}
}
[url=]6.MM32F0140 UART1发送UART1中断接收到的数据到上位机串口助手:[/url] 当上位机串口助手发送8字节的十六进制数据:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55;下位机MM32F0140的UART1中断接收到一帧:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55 共8字节数据后,通过UART1发送多字节函数,原样发送到串口助手。 (1)在main函数中调用UART1 NVIC初始化函数; (2)在main函数的while(1)主循环中调用UART1处理中断接收到的数据函数,循环检测UART1的接收中断是否接收到上位机串口助手下发的数据,如有收到就原样发送到上位机串口助手上显示出来; int main(void)
{
//UART1 NVIC Init Baudrate 115200
Bsp_UART1_NVIC_Init(UART1_BAUDRATE);
while(1)
{
//Test UART1 RX TX
Bsp_UART1_Recv_Task();
}
}
[url=](3)上位机串口助手发送8字节16进制数据:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55,下位机MM32F0020 UART1中断接收到一帧数据后原样把接收到的数据发送到上位机上显示出来,如下图1所示:[/url] [url=]
[/url]
|