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

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2735|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

STM32的5個(gè)串口初始化的使用程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:656155 發(fā)表于 2021-6-29 15:51 | 只看該作者 回帖獎(jiǎng)勵(lì) |正序?yàn)g覽 |閱讀模式
一個(gè)復(fù)雜的項(xiàng)目往往會(huì)用到多個(gè)串口
一般的串口1、2、3都有例程可用
這里主要分享串口4、5的調(diào)用

單片機(jī)源程序如下:
  1. #include "sys.h"
  2. #include "usart.h"               
  3. #include "delay.h"       

  4. #include "led.h"     
  5. #include "usart2.h"
  6. #include "usart3.h"
  7. #include "UART4.h"          
  8. #include "UART5.h"       
  9. void u1_Tx(void)
  10. {
  11.                 USART1->DR=0x44; delay_ms(1);                       
  12.                 USART1->DR=0x53; delay_ms(1);                       
  13.                 USART1->DR=0x54; delay_ms(1);
  14.                 USART1->DR=0x31; delay_ms(1);
  15.        
  16.                 USART1->DR=0x0d; delay_ms(1);               
  17.                 USART1->DR=0x0a; delay_ms(1);       

  18. }
  19. void u2_Tx(void)
  20. {
  21.                 USART2->DR=0x44; delay_ms(1);                       
  22.                 USART2->DR=0x53; delay_ms(1);                       
  23.                 USART2->DR=0x54; delay_ms(1);
  24.                 USART2->DR=0x32; delay_ms(1);
  25.        
  26.                 USART2->DR=0x0d; delay_ms(1);               
  27.                 USART2->DR=0x0a; delay_ms(1);       
  28. }
  29. void u3_Tx(void)
  30. {
  31.                 USART3->DR=0x44; delay_ms(1);                       
  32.                 USART3->DR=0x53; delay_ms(1);                       
  33.                 USART3->DR=0x54; delay_ms(1);
  34.                 USART3->DR=0x33; delay_ms(1);
  35.        
  36.                 USART3->DR=0x0d; delay_ms(1);               
  37.                 USART3->DR=0x0a; delay_ms(1);       
  38. }

  39. void u4_Tx(void)
  40. {
  41.                 UART4->DR=0x44; delay_ms(1);                       
  42.                 UART4->DR=0x53; delay_ms(1);                       
  43.                 UART4->DR=0x54; delay_ms(1);
  44.                 UART4->DR=0x34; delay_ms(1);
  45.        
  46.                 UART4->DR=0x0d; delay_ms(1);               
  47.                 UART4->DR=0x0a; delay_ms(1);       
  48. }
  49.        
  50. void u5_Tx(void)
  51. {
  52.                 UART5->DR=0x44; delay_ms(1);                       
  53.                 UART5->DR=0x53; delay_ms(1);                       
  54.                 UART5->DR=0x54; delay_ms(1);
  55.                 UART5->DR=0x35; delay_ms(1);
  56.        
  57.                 UART5->DR=0x0d; delay_ms(1);               
  58.                 UART5->DR=0x0a; delay_ms(1);       
  59. }


  60. int main(void)
  61. {                 
  62.                
  63.   Stm32_Clock_Init(9);//系統(tǒng)時(shí)鐘設(shè)置
  64.         delay_init(72);                //延時(shí)初始化
  65.        
  66.         uart_init(72,9600); //串口1初始化  
  67.         usart2_Init(36,9600);        //串口2 初始化為 波特率:9600
  68.         usart3_init(36,9600); //串口3 初始化為 初始化為9600
  69.         uart4_init(36,9600);               
  70.         uart5_init(36,9600);

  71. //        PD2 串口5-RX 與LED1 沖突       
  72. //        LED_Init();        
  73.        

  74.         while(1)
  75.         {
  76.                 delay_ms(3000);
  77.                 u1_Tx();
  78.                 u2_Tx();
  79.                 u3_Tx();
  80.                 u4_Tx();
  81.                 u5_Tx();
  82.         }
  83.                

  84.        
  85. }
復(fù)制代碼

所有程序51hei提供下載:
串口1 串口2 串口3 串口4 串口5 初始化程序(寄存器版).7z (472.38 KB, 下載次數(shù): 12)


評(píng)分

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

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

板凳
ID:101305 發(fā)表于 2021-7-6 21:12 | 只看該作者
好例子啊,謝謝樓主,有個(gè)好例子
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:101305 發(fā)表于 2021-7-6 21:11 | 只看該作者
回個(gè)頂下,如果有函數(shù)版的就好了
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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