標(biāo)題: 單片機串口1中斷收發(fā) [打印本頁]

作者: 京兆府111    時間: 2018-6-17 13:45
標(biāo)題: 單片機串口1中斷收發(fā)
  1. /*************        功能說明        **************

  2. 串口1全雙工中斷方式收發(fā)通訊程序。本例程使用22.1184MHZ時鐘,如要改變,請修改下面的"定義主時鐘"的值并重新編譯。

  3. 串口設(shè)置為:115200,8,n,1.

  4. 通過PC向MCU發(fā)送數(shù)據(jù), MCU收到后通過串口把收到的數(shù)據(jù)原樣返回.

  5. ******************************************/

  6. /*************        本地常量聲明        **************/
  7. #define MAIN_Fosc                22118400L        //定義主時鐘
  8. #define        RX1_Lenth                32                        //串口接收緩沖長度
  9. #define        BaudRate1                115200UL        //選擇波特率


  10. #define        Timer1_Reload        (65536UL -(MAIN_Fosc / 4 / BaudRate1))                //Timer 1 重裝值, 對應(yīng)300KHZ
  11. #define        Timer2_Reload        (65536UL -(MAIN_Fosc / 4 / BaudRate1))                //Timer 2 重裝值, 對應(yīng)300KHZ

  12. #include        "STC15Fxxxx.H"


  13. /*************        本地變量聲明        **************/
  14. u8        idata RX1_Buffer[RX1_Lenth];        //接收緩沖
  15. u8        TX1_Cnt;        //發(fā)送計數(shù)
  16. u8        RX1_Cnt;        //接收計數(shù)
  17. bit        B_TX1_Busy;        //發(fā)送忙標(biāo)志


  18. /*************        本地函數(shù)聲明        **************/



  19. /**********************************************/
  20. void main(void)
  21. {
  22.         B_TX1_Busy = 0;
  23.         RX1_Cnt = 0;
  24.         TX1_Cnt = 0;

  25.         S1_8bit();                                //8位數(shù)據(jù)
  26.         S1_USE_P30P31();                //UART1 使用P30 P31口        默認(rèn)
  27. //        S1_USE_P36P37();                //UART1 使用P36 P37口
  28. //        S1_USE_P16P17();                //UART1 使用P16 P17口

  29. /*
  30.         TR1 = 0;                        //波特率使用Timer1產(chǎn)生
  31.         AUXR &= ~0x01;                //S1 BRT Use Timer1;
  32.         AUXR |=  (1<<6);        //Timer1 set as 1T mode
  33.         TH1 = (u8)(Timer1_Reload >> 8);
  34.         TL1 = (u8)Timer1_Reload;
  35.         TR1  = 1;
  36. */

  37.         AUXR &= ~(1<<4);        //Timer stop                波特率使用Timer2產(chǎn)生
  38.         AUXR |= 0x01;                //S1 BRT Use Timer2;
  39.         AUXR |=  (1<<2);        //Timer2 set as 1T mode
  40.         TH2 = (u8)(Timer2_Reload >> 8);
  41.         TL2 = (u8)Timer2_Reload;
  42.         AUXR |=  (1<<4);        //Timer run enable

  43.         REN = 1;        //允許接收
  44.         ES  = 1;        //允許中斷

  45.         EA = 1;                //允許全局中斷
  46.         

  47.         while (1)
  48.         {
  49.                 if(TX1_Cnt != RX1_Cnt)                //收到過數(shù)據(jù)
  50.                 {
  51.                         if(!B_TX1_Busy)                //發(fā)送空閑
  52.                         {
  53.                                 B_TX1_Busy = 1;                //標(biāo)志發(fā)送忙
  54.                                 SBUF = RX1_Buffer[TX1_Cnt];        //發(fā)一個字節(jié)
  55.                                 if(++TX1_Cnt >= RX1_Lenth)        TX1_Cnt = 0;        //避免溢出處理
  56.                         }
  57.                 }
  58.         }
  59. }

  60. /********************* UART1中斷函數(shù)************************/
  61. void UART1_int (void) interrupt UART1_VECTOR
  62. {
  63.         if(RI)
  64.         {
  65.                 RI = 0;
  66.                 RX1_Buffer[RX1_Cnt] = SBUF;                //保存一個字節(jié)
  67.                 if(++RX1_Cnt >= RX1_Lenth)        RX1_Cnt = 0;        //避免溢出處理
  68.         }

  69.         if(TI)
  70.         {
  71.                 TI = 0;
  72.                 B_TX1_Busy = 0;                //清除發(fā)送忙標(biāo)志
  73.         }
  74. }
復(fù)制代碼

作者: 51.MCU    時間: 2021-6-27 18:28
如何實驗?




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