找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1405|回复: 5
收起左侧

STC单片机 串口2 调试不处理

[复制链接]
ID:408809 发表于 2020-1-9 17:11 | 显示全部楼层 |阅读模式
STC12C5A60S2 单片机有第二串口功能,整了好久硬是整不明白,调试不出来。恳请前辈指导指导
第一串口.png 第一串口的中断.png

第二串口.png 第二串口的中断.png


回复

使用道具 举报

ID:408809 发表于 2020-1-9 19:03 | 显示全部楼层
并且我照STC官方的  S2CON &= ~S2RI;        操作也不行。
回复

使用道具 举报

ID:155507 发表于 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. }

复制代码
回复

使用道具 举报

ID:158375 发表于 2020-1-10 09:05 | 显示全部楼层
楼上说得对,串口2 中断 要使用  interrupt 8.
--------------
从你给的图看,串口1 中断 和 串口2 中断 都使用 interrupt 4, 这个肯定有问题.
建议: 把串口2 中断 改为  interrupt 8.
回复

使用道具 举报

ID:214223 发表于 2020-1-10 11:07 | 显示全部楼层
沙发的程序可以一试
回复

使用道具 举报

ID:679425 发表于 2020-1-10 13:11 | 显示全部楼层
不同入口,不同调用


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
    }
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表