找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 1985|回復(fù): 0
收起左側(cè)

STC12單片機(jī)的UART2串口輸出 源程序

[復(fù)制鏈接]
ID:912806 發(fā)表于 2021-8-19 22:05 | 顯示全部樓層 |閱讀模式
STC12C5A60S2系列單片機(jī), 帶內(nèi)建的UART2, 可以雙串口輸出, 基于 HML_FwLib_STC12 封裝庫(kù)可以方便的初始化并調(diào)用第二串口, 使用第二串口的代碼


  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. }
復(fù)制代碼

注意: 第二串口UART2只能使用獨(dú)立的BRT作為波特率發(fā)生器, 而UART1可以切換選擇BRT和TIM1. 因?yàn)镾TC12有1T模式, 因此可以在11.0529MHz晶振下很輕松的實(shí)現(xiàn)115200波特率. 在啟用 1T + 雙倍的模式下, 可以實(shí)現(xiàn)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. }
復(fù)制代碼



評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表