今天晚上来搞一下串口,由于回来比较晚,所以今天的分享些简单点,孩子睡觉了,也不敢打字太多,串口是设置的串口3,和STM32的有所不同,串口1 在PA8和PA9上,而PA9和PA10是串口3,底板上PA8是LED的,所以我打算使用PA9和PA10(串口3)来做测试,
直接还是不多说,直接上代码,
#include "DRV_UART.h"
void UART_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APBPeriphClk_Enable1(RCC_APB1_PERIPH_UART3, ENABLE);
RCC_AHBPeriphClk_Enable( RCC_AHB_PERIPH_GPIOA, ENABLE);
PA09_AFx_UART1RXD();
PA09_AFx_UART1RXD();
PA09_AFx_UART3TXD();
PA10_AFx_UART3RXD() ;
GPIO_InitStructure.Pins = GPIO_PIN_9;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
GPIO_Init(CW_GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pins = GPIO_PIN_10;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
GPIO_Init(CW_GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_Over = USART_Over_16;
USART_InitStructure.USART_Source = USART_Source_PCLK;
USART_InitStructure.USART_UclkFreq = 64000000;
USART_InitStructure.USART_StartBit = USART_StartBit_FE;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(CW_UART3, &USART_InitStructure);
}
void Uart_TestDemo(void)
{
unsigned char t=0;
unsigned char dat[30]={"This is CW32 UART3 Demo\r\n"};
for(t=0;t<30;t++)
{
USART_SendData(CW_UART3,dat[t]);
while(USART_GetFlagStatus(CW_UART3,USART_FLAG_TXE)==RESET);
USART_ClearFlag(CW_UART3,USART_FLAG_TXE);
}
}
在定时器内部1s时置位发送标志,主函数内部清零,并完成测试代码的发送,由于没有做中断接收,所以测试实际上只用到了PA9,连线需要如图来。
这样连接就可以保证及连接了DAP,又可以串口收信号。
好了今天的分享就到这里,然后还是上个视屏吧,代码也在下面,大家可以下载一下玩玩;
测试代码:*附件:CW32_Demo-20230510.rar
好了下面上个视屏:
TimUART
|