本帖最后由 740071911 于 2022-7-20 21:51 编辑
刚拿到板子的时候把玩过串口,然后时隔时隔20天,就忘记怎么用了,写个帖子作为备忘录还是很好的,还不占地方。
串口实验结果:
1:系统初始化时,打印系统时钟频率
2:串口定时打印信息
硬件连接:
(1)需要注意板载的串口模块CH340,并未与MCU的串口相连接,同时CH340的电源也需要自己选择。
看原理图,CN9排插线,VDDU,RX,TX都是悬空的,用板载串口模块时,需要用杜邦线与MCU的串口相连。
(2)板载串口模块CH340的电源VDDU的选择不同,J5跳帽的位置也要相应的变动,见手册CH340N有记录,
所以,这里也要有所注意。
实物连接:串口的GND与MCU的gnd是连接的,如果只打印信息,可以只用两根线,
VDDU--->VDD,PCRXD--->PB08即可。
代码实现:
/******************************************************************************
* Include files
******************************************************************************/
#include "Uart.h"
/******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
//UARTx
#define DEBUG_USARTx CW_UART1
#define DEBUG_USART_BaudRate 9600
#define DEBUG_USART_UclkFreq 64000000
//UARTx GPIO
#define DEBUG_USART_GPIO_CLK RCC_AHB_PERIPH_GPIOB
#define DEBUG_USART_TX_GPIO_PORT CW_GPIOB
#define DEBUG_USART_TX_GPIO_PIN GPIO_PIN_8
#define DEBUG_USART_RX_GPIO_PORT CW_GPIOB
#define DEBUG_USART_RX_GPIO_PIN GPIO_PIN_9
//GPIO AF
#define DEBUG_USART_AFTX PB08_AFx_UART1TXD()
#define DEBUG_USART_AFRX PB09_AFx_UART1RXD()
/******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
/******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
/******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
void MX_UART_Init(void)
{
GPIO_InitTypeDef ioConfig;
USART_InitTypeDef uartConfig;
RCC_AHBPeriphClk_Enable(RCC_AHB_PERIPH_GPIOB, ENABLE);
RCC_APBPeriphClk_Enable2(RCC_APB2_PERIPH_UART1, ENABLE);
//UART TX RX 复用
#if 0
DEBUG_USART_AFTX;
DEBUG_USART_AFRX;
#endif
PB08_AFx_UART1TXD();
PB09_AFx_UART1RXD();
ioConfig.Pins = DEBUG_USART_TX_GPIO_PIN;
ioConfig.Mode = GPIO_MODE_OUTPUT_PP;
ioConfig.Speed = GPIO_SPEED_HIGH;
GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &ioConfig);
ioConfig.Pins = DEBUG_USART_RX_GPIO_PIN;
ioConfig.Mode = GPIO_MODE_INPUT_PULLUP;
GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &ioConfig);
uartConfig.USART_BaudRate = DEBUG_USART_BaudRate;
uartConfig.USART_Over = USART_Over_16;
uartConfig.USART_Source = USART_Source_PCLK;
uartConfig.USART_UclkFreq = DEBUG_USART_UclkFreq;
uartConfig.USART_StartBit = USART_StartBit_FE;
uartConfig.USART_StopBits = USART_StopBits_1;
uartConfig.USART_Parity = USART_Parity_No ;
uartConfig.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
uartConfig.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(DEBUG_USARTx, &uartConfig);
}
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Retargets the C library printf function to the USART.
*
*/
PUTCHAR_PROTOTYPE
{
USART_SendData_8bit(CW_UART1, (uint8_t)ch);
while (USART_GetFlagStatus(CW_UART1, USART_FLAG_TXE) == RESET);
return ch;
}
demo是在上一个工程基础上添加的,下面是系统时钟的获取打印
/**
*\*\name PrintfClockInfo.
*\*\fun Printf clock information.
*\*\param none
*\*\return none
**/
void PrintfClockInfo(const char* msg)
{
u32 nHclk_Freq;
u32 nPclk_Freq;
/* reinit after sysclk changed */
MX_UART_Init();
/* Wait for the configuration to succeed */
Delay(0xFFFF);
printf("--------------------------------\n");
printf("%s:\n", msg);
nHclk_Freq = RCC_Sysctrl_GetHClkFreq();
nPclk_Freq = RCC_Sysctrl_GetPClkFreq();
printf("SystemCoreClock: %d MHz\n", SystemCoreClock/1000000);
printf("HCLK: %d MHz\n", nHclk_Freq/1000000);
printf("PCLK: %d MHz\n", nPclk_Freq/1000000);
/*
--------------------------------
clock acquisition, wait a minute...:
*/
}
代码附件:
最后附上代码
cw32-startkit-v1.1.zip
(215.42 KB)
|