使用串口3来实验串口通信。
一、硬件电路图
串口3连接的是USB-TTL芯片, 方便电脑USB连接。
二、程序代码
2.1、uart.c
<font size="3">#include "config.h"
void UART_Init(void)
{
uint16_t u16RxData;
en_result_t enRet = Ok;
uint32_t u32Fcg1Periph = PWC_FCG1_PERIPH_USART1 | PWC_FCG1_PERIPH_USART2 | \
PWC_FCG1_PERIPH_USART3 | PWC_FCG1_PERIPH_USART4;
const stc_usart_uart_init_t stcInitCfg =
{
UsartIntClkCkNoOutput,
UsartClkDiv_1,
UsartDataBits8,
UsartDataLsbFirst,
UsartOneStopBit,
UsartParityNone,
UsartSamleBit8,
UsartStartBitFallEdge,
UsartRtsEnable,
};
PWC_Fcg1PeriphClockCmd(u32Fcg1Periph, Enable);
PORT_SetFunc(USART_RX_PORT, USART_RX_PIN, USART_RX_FUNC, Disable);
PORT_SetFunc(USART_TX_PORT, USART_TX_PIN, USART_TX_FUNC, Disable);
/* Initialize UART */
enRet = USART_UART_Init(USART_CH, &stcInitCfg);
if (enRet != Ok)
{
while (1)
{
}
}
/* Set baudrate */
enRet = USART_SetBaudrate(USART_CH, USART_BAUDRATE);
if (enRet != Ok)
{
while (1)
{
}
}
/*Enable RX && TX function*/
USART_FuncCmd(USART_CH, UsartRx, Enable);
USART_FuncCmd(USART_CH, UsartTx, Enable);
}
void UartSend(uint8_t dat)
{
USART_SendData(USART_CH, dat);
while(Reset == USART_GetStatus(USART_CH, UsartTxComplete));
}
</font>
2.2、uart.h <font size="3">#ifndef __UART_H
#define __UART_H
/* USART channel definition */
#define USART_CH (M4_USART3)
/* USART baudrate definition */
#define USART_BAUDRATE (115200ul)
/* USART RX Port/Pin definition */
#define USART_RX_PORT (PortE)
#define USART_RX_PIN (Pin04)
#define USART_RX_FUNC (Func_Usart3_Rx)
/* USART TX Port/Pin definition */
#define USART_TX_PORT (PortE)
#define USART_TX_PIN (Pin05)
#define USART_TX_FUNC (Func_Usart3_Tx)
void UART_Init(void);
void UartSend(uint8_t dat);
#endif
</font>
2.3、main.c
<font size="3">int32_t main(void)
{
uint16_t cn = 0;
ClkInit();
LED_Init();
UART_Init();
while(1)
{
led_test();
cn++;
printf("HDSC-HC32F460 UART TEST! cn=%d\r\n",cn);
};
}</font>
2.4、程序源码
hc32f460_prj-20200709.rar
(3.85 MB)
三、执行结果
|