【全能小网关|CH32V208】--2.串口打印
<p> 本篇讲述开发板串口打印功能。串口打印可以辅助开发阶段调试和测验。</p><p><strong><span style="color:#0000ff;">一.硬件原理与准备</span></strong></p>
<p> 查阅datasheet可看到PA9、PA10可配置成串口USART1_TX、USART1_RX,如下图</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图1:串口引脚选择</div>
<p> 查阅原理图串口部分接口如下图2。同时,安装好WCH-LinkE具有串口功能,硬件连接好如下图3</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图2:硬件连接原理</div>
<div style="text-align: center;">
<div style="text-align: center;"></div>
<div style="text-align: center;">图3:串口连接</div>
<p> </p>
</div>
<p><strong><span style="color:#0000ff;">二.代码准备</span></strong></p>
<p>1.初始化串口UART1,配置波特率115200.</p>
<pre>
<code>/* UART Printf Definition */
#define DEBUG_UART1 1
#define DEBUG_UART2 2
#define DEBUG_UART3 3
/* DEBUG UATR Definition */
#ifndef DEBUG
#define DEBUG DEBUG_UART1
#endif
void USART_Printf_Init(uint32_t baudrate)
{
GPIO_InitTypeDefGPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
#if(DEBUG == DEBUG_UART1)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#elif(DEBUG == DEBUG_UART2)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#elif(DEBUG == DEBUG_UART3)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
#endif
USART_InitStructure.USART_BaudRate = baudrate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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_Tx;
#if(DEBUG == DEBUG_UART1)
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
#elif(DEBUG == DEBUG_UART2)
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
#elif(DEBUG == DEBUG_UART3)
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
#endif
}
</code></pre>
<p>2.main函数打印系统时钟、芯片ID</p>
<pre>
<code>int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
SystemCoreClockUpdate();
Delay_Init();
USART_Printf_Init(115200);
printf("Uart Init\r\n");
printf("SystemClk:%d\r\n", SystemCoreClock);
printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
printf("Run printf success\r\n");
while(1)
{
}
}</code></pre>
<p>3.编译烧录</p>
<p> </p>
<p><strong><span style="color:#0000ff;">三.测试</span></strong></p>
<p> 烧录后复位,可看到串口打印如下</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图4:串口打印</div>
<p> 至此,实现串口打印功能。</p>
页:
[1]