标题:
STC12单片机的UART2串口输出 源程序
[打印本页]
作者:
michaelchain
时间:
2021-8-19 22:05
标题:
STC12单片机的UART2串口输出 源程序
STC12C5A60S2系列单片机, 带内建的UART2, 可以双串口输出, 基于
HML_FwLib_STC12
封装库可以方便的初始化并调用第二串口, 使用第二串口的代码
#include "hml/hml.h"
void sys_init(void)
{
UART2_configTypeDef uc;
uc.baudrate = 115200; /* baud rate is 115200bps */
uc.brtPrescaler = RCC_BRT_prescaler_1; /* 1T mode */
uc.interruptState = ENABLE;
uc.interruptPriority = DISABLE;
uc.mode = UART_mode_1;
uc.doubleBaudrate = DISABLE;
uc.receiveState = ENABLE;
uc.pinmap = UART2_pinmap_P1;
UART2_config(&uc);
}
void main(void)
{
sys_init();
while (true)
{
/* send per 500ms */
sleep(500);
UART2_sendString("Hello, world!\r\n");
}
}
复制代码
注意: 第二串口UART2只能使用独立的BRT作为波特率发生器, 而UART1可以切换选择BRT和TIM1. 因为STC12有1T模式, 因此可以在11.0529MHz晶振下很轻松的实现115200波特率. 在启用 1T + 双倍的模式下, 可以实现345600的波特率
#include "hml/hml.h"
void sys_init(void)
{
UART2_configTypeDef uc;
uc.baudrate = 345600; /* baud rate is 345600 */
uc.brtPrescaler = RCC_BRT_prescaler_1; /* 1T mode */
uc.interruptState = ENABLE;
uc.interruptPriority = DISABLE;
uc.mode = UART_mode_1;
uc.doubleBaudrate = ENABLE;
uc.receiveState = ENABLE;
uc.pinmap = UART2_pinmap_P1;
UART2_config(&uc);
}
void main(void)
{
sys_init();
while (true)
{
/* send per 500ms */
sleep(500);
UART2_sendString("Hello, world!\r\n");
}
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1