找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

STC單片機 串口2 調(diào)試不處理

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:408809 發(fā)表于 2020-1-9 17:11 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
STC12C5A60S2 單片機有第二串口功能,整了好久硬是整不明白,調(diào)試不出來。懇請前輩指導(dǎo)指導(dǎo)





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

使用道具 舉報

沙發(fā)
ID:408809 發(fā)表于 2020-1-9 19:03 | 只看該作者
并且我照STC官方的  S2CON &= ~S2RI;        操作也不行。
回復(fù)

使用道具 舉報

板凳
ID:155507 發(fā)表于 2020-1-9 23:40 | 只看該作者
STC12C5A60S2單片機 串口2 中斷 要使用  interrupt 8


  1. /*------------------------------------------------------------------*/
  2. /* --- STC MCU Limited ---------------------------------------------*/
  3. /* --- STC12C5Axx Series MCU UART2 (8-bit/9-bit)Demo ---------------*/
  4. /* --- Mobile: (86)13922805190 -------------------------------------*/
  5. /* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
  6. /* --- Tel: 86-0513-55012928,55012929,55012966----------------------*/
  7. /* If you want to use the program or the program referenced in the  */
  8. /* article, please specify in which data and procedures from STC    */
  9. /*------------------------------------------------------------------*/

  10. #include "reg51.h"
  11. #include "intrins.h"

  12. typedef unsigned char BYTE;
  13. typedef unsigned int WORD;

  14. #define FOSC 11059200L      //System frequency
  15. #define BAUD 115200         //UART baudrate

  16. /*Define UART parity mode*/
  17. #define NONE_PARITY     0   //None parity
  18. #define ODD_PARITY      1   //Odd parity
  19. #define EVEN_PARITY     2   //Even parity
  20. #define MARK_PARITY     3   //Mark parity
  21. #define SPACE_PARITY    4   //Space parity

  22. #define PARITYBIT EVEN_PARITY   //Testing even parity

  23. /*Declare SFR associated with the UART2 */
  24. sfr AUXR  = 0x8e;           //Auxiliary register
  25. sfr S2CON = 0x9a;           //UART2 control register
  26. sfr S2BUF = 0x9b;           //UART2 data buffer
  27. sfr BRT   = 0x9c;           //Baudrate generator
  28. sfr IE2   = 0xaf;           //Interrupt control 2

  29. #define S2RI  0x01          //S2CON.0
  30. #define S2TI  0x02          //S2CON.1
  31. #define S2RB8 0x04          //S2CON.2
  32. #define S2TB8 0x08          //S2CON.3

  33. bit busy;

  34. void SendData(BYTE dat);
  35. void SendString(char *s);

  36. void main()
  37. {
  38. #if (PARITYBIT == NONE_PARITY)
  39.     S2CON = 0x50;           //8-bit variable UART
  40. #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
  41.     S2CON = 0xda;           //9-bit variable UART, parity bit initial to 1
  42. #elif (PARITYBIT == SPACE_PARITY)
  43.     S2CON = 0xd2;           //9-bit variable UART, parity bit initial to 0
  44. #endif

  45.     BRT = -(FOSC/32/BAUD);  //Set auto-reload vaule of baudrate generator
  46.     AUXR = 0x14;            //Baudrate generator work in 1T mode
  47.     IE2 = 0x01;             //Enable UART2 interrupt
  48.     EA = 1;                 //Open master interrupt switch

  49.     SendString("STC12C5A60S2\r\nUart2 Test !\r\n");
  50.     while(1);
  51. }

  52. /*----------------------------
  53. UART2 interrupt service routine
  54. ----------------------------*/
  55. void Uart2() interrupt 8
  56. {
  57.     if (S2CON & S2RI)
  58.     {
  59.         S2CON &= ~S2RI;     //Clear receive interrupt flag
  60.         P0 = S2BUF;         //P0 show UART data
  61.         P2 = (S2CON & S2RB8);//P2.2 show parity bit
  62.     }
  63.     if (S2CON & S2TI)
  64.     {
  65.         S2CON &= ~S2TI;     //Clear transmit interrupt flag
  66.         busy = 0;           //Clear transmit busy flag
  67.     }
  68. }

  69. /*----------------------------
  70. Send a byte data to UART
  71. Input: dat (data to be sent)
  72. Output:None
  73. ----------------------------*/
  74. void SendData(BYTE dat)
  75. {
  76.     while (busy);           //Wait for the completion of the previous data is sent
  77.     ACC = dat;              //Calculate the even parity bit P (PSW.0)
  78.     if (P)                  //Set the parity bit according to P
  79.     {
  80. #if (PARITYBIT == ODD_PARITY)
  81.         S2CON &= ~S2TB8;    //Set parity bit to 0
  82. #elif (PARITYBIT == EVEN_PARITY)
  83.         S2CON |= S2TB8;     //Set parity bit to 1
  84. #endif
  85.     }
  86.     else
  87.     {
  88. #if (PARITYBIT == ODD_PARITY)
  89.         S2CON |= S2TB8;     //Set parity bit to 1
  90. #elif (PARITYBIT == EVEN_PARITY)
  91.         S2CON &= ~S2TB8;    //Set parity bit to 0
  92. #endif
  93.     }
  94.     busy = 1;
  95.     S2BUF = ACC;            //Send data to UART2 buffer
  96. }

  97. /*----------------------------
  98. Send a string to UART
  99. Input: s (address of string)
  100. Output:None
  101. ----------------------------*/
  102. void SendString(char *s)
  103. {
  104.     while (*s)              //Check the end of the string
  105.     {
  106.         SendData(*s++);     //Send current char and increment string ptr
  107.     }
  108. }

復(fù)制代碼
回復(fù)

使用道具 舉報

地板
ID:158375 發(fā)表于 2020-1-10 09:05 | 只看該作者
樓上說得對,串口2 中斷 要使用  interrupt 8.
--------------
從你給的圖看,串口1 中斷 和 串口2 中斷 都使用 interrupt 4, 這個肯定有問題.
建議: 把串口2 中斷 改為  interrupt 8.
回復(fù)

使用道具 舉報

5#
ID:214223 發(fā)表于 2020-1-10 11:07 | 只看該作者
沙發(fā)的程序可以一試
回復(fù)

使用道具 舉報

6#
ID:679425 發(fā)表于 2020-1-10 13:11 | 只看該作者
不同入口,不同調(diào)用


interrupt 8
{
    if (S2CON & S2RI)
    {
        S2CON &= ~S2RI;     //Clear receive interrupt flag
        P0 = S2BUF;         //P0 show UART data
        P2 = (S2CON & S2RB8);//P2.2 show parity bit
    }
    if (S2CON & S2TI)
    {
        S2CON &= ~S2TI;     //Clear transmit interrupt flag
        busy = 0;           //Clear transmit busy flag
    }
}

回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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