標(biāo)題: STM32的5個(gè)串口初始化的使用程序 [打印本頁]

作者: DENGJIAQI    時(shí)間: 2021-6-29 15:51
標(biāo)題: STM32的5個(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)



作者: herohuang    時(shí)間: 2021-7-6 21:11
回個(gè)頂下,如果有函數(shù)版的就好了

作者: herohuang    時(shí)間: 2021-7-6 21:12
好例子啊,謝謝樓主,有個(gè)好例子




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1