标题: STC12单片机的UART2串口输出 源程序 [打印本页]

作者: michaelchain    时间: 2021-8-19 22:05
标题: STC12单片机的UART2串口输出 源程序
STC12C5A60S2系列单片机, 带内建的UART2, 可以双串口输出, 基于 HML_FwLib_STC12 封装库可以方便的初始化并调用第二串口, 使用第二串口的代码


  1. #include "hml/hml.h"

  2. void sys_init(void)
  3. {
  4.     UART2_configTypeDef uc;
  5.     uc.baudrate                    = 115200;                         /* baud rate is 115200bps */
  6.     uc.brtPrescaler                = RCC_BRT_prescaler_1;            /* 1T mode */
  7.     uc.interruptState              = ENABLE;
  8.     uc.interruptPriority           = DISABLE;
  9.     uc.mode                        = UART_mode_1;
  10.     uc.doubleBaudrate              = DISABLE;
  11.     uc.receiveState                = ENABLE;
  12.     uc.pinmap                      = UART2_pinmap_P1;
  13.     UART2_config(&uc);
  14. }


  15. void main(void)
  16. {
  17.     sys_init();

  18.     while (true)
  19.     {
  20.         /* send per 500ms */
  21.         sleep(500);
  22.         UART2_sendString("Hello, world!\r\n");
  23.     }
  24. }
复制代码

注意: 第二串口UART2只能使用独立的BRT作为波特率发生器, 而UART1可以切换选择BRT和TIM1. 因为STC12有1T模式, 因此可以在11.0529MHz晶振下很轻松的实现115200波特率. 在启用 1T + 双倍的模式下, 可以实现345600的波特率

  1. #include "hml/hml.h"

  2. void sys_init(void)
  3. {
  4.     UART2_configTypeDef uc;
  5.     uc.baudrate                    = 345600;                         /* baud rate is 345600 */
  6.     uc.brtPrescaler                = RCC_BRT_prescaler_1;            /* 1T mode */
  7.     uc.interruptState              = ENABLE;
  8.     uc.interruptPriority           = DISABLE;
  9.     uc.mode                        = UART_mode_1;
  10.     uc.doubleBaudrate              = ENABLE;
  11.     uc.receiveState                = ENABLE;
  12.     uc.pinmap                      = UART2_pinmap_P1;
  13.     UART2_config(&uc);
  14. }

  15. void main(void)
  16. {
  17.     sys_init();

  18.     while (true)
  19.     {
  20.         /* send per 500ms */
  21.         sleep(500);
  22.         UART2_sendString("Hello, world!\r\n");
  23.     }
  24. }
复制代码








欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1