标题: STM32的5个串口初始化的使用程序 [打印本页]

作者: DENGJIAQI    时间: 2021-6-29 15:51
标题: STM32的5个串口初始化的使用程序
一个复杂的项目往往会用到多个串口
一般的串口1、2、3都有例程可用
这里主要分享串口4、5的调用

单片机源程序如下:
  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);//系统时钟设置
  64.         delay_init(72);                //延时初始化
  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. }
复制代码

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



作者: herohuang    时间: 2021-7-6 21:11
回个顶下,如果有函数版的就好了

作者: herohuang    时间: 2021-7-6 21:12
好例子啊,谢谢楼主,有个好例子




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