找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3097|回复: 3
打印 上一主题 下一主题
收起左侧

有人遇到过STC12C5A60S2串口2无法使用的问题吗?

[复制链接]
跳转到指定楼层
楼主
ID:523982 发表于 2019-4-30 11:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有人遇到过STC12C5A60S2串口2无法使用的问题吗?串口1正常能收发数据,用的官网提供的例程,百度发现很多人都遇到这个问题,目前还没找到解决办法。下载过http://www.51hei.com/bbs/dpj-133507-1.html也无法使用。
  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. /* --- Web: www.STCMCU.com -----------------------------------------*/
  8. /* --- Web: www.GXWMCU.com -----------------------------------------*/
  9. /* If you want to use the program or the program referenced in the  */
  10. /* article, please specify in which data and procedures from STC    */
  11. /*------------------------------------------------------------------*/

  12. #include "reg51.h"
  13. #include "intrins.h"

  14. typedef unsigned char BYTE;
  15. typedef unsigned int WORD;

  16. #define FOSC 11059200L      //System frequency
  17. #define BAUD 115200         //UART baudrate

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

  24. #define PARITYBIT EVEN_PARITY   //Testing even parity

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

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

  35. bit busy;

  36. void SendData(BYTE dat);
  37. void SendString(char *s);

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

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

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

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

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

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

复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:110606 发表于 2019-4-30 16:32 | 只看该作者
我之前用官网的历程发现有问题,后来就不用了
回复

使用道具 举报

板凳
ID:155507 发表于 2019-5-1 10:51 | 只看该作者
你可以试试这个

  1. #include "reg51.h"

  2. #define FOSC 11059200L
  3. #define BAUD 115200

  4. #define NONE_PARITY     0   //无校验位
  5. #define ODD_PARITY      1   //奇校验
  6. #define EVEN_PARITY     2   //偶校验
  7. #define MARK_PARITY     3   //标记校验
  8. #define SPACE_PARITY    4   //空校验

  9. #define PARITYBIT NONE_PARITY

  10. #define S2RI  0x01
  11. #define S2TI  0x02
  12. #define S2RB8 0x04
  13. #define S2TB8 0x08

  14. sfr AUXR  = 0x8e;
  15. sfr S2CON = 0x9a;
  16. sfr S2BUF = 0x9b;
  17. sfr BRT   = 0x9c;
  18. sfr IE2   = 0xaf;

  19. bit busy;

  20. void SendData(char dat);
  21. void SendString(char *s);

  22. void main()
  23. {
  24. #if (PARITYBIT == NONE_PARITY)
  25.     S2CON = 0x5a;               //8位可变波特率 (无校验位)
  26. #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
  27.     S2CON = 0xda;               //9位可变波特率,校验位初始为1
  28. #elif (PARITYBIT == SPACE_PARITY)
  29.     S2CON = 0xd5;               //9位可变波特率,校验位初始为0
  30. #endif

  31.     BRT = -(FOSC/32/BAUD);      //设置独立波特率发生器的重载初值
  32.     AUXR = 0x14;                //独立波特率发生器工作在1T模式
  33.     IE2 = 0x01;                 //使能串口2中断
  34.     EA = 1;                     //开总中断

  35.     SendString("STC12C5A60S2\r\nUart2 Test !\r\n");
  36.     while(1);
  37. }

  38. void Uart2() interrupt 8 using 1
  39. {
  40.     if (S2CON & S2RI)
  41.     {
  42.         S2CON &= ~S2RI;         //清除接收完成标志
  43.         P0 = S2BUF;             //P0显示串口数据
  44.         P2 = (S2CON & S2RB8);   //P2.2显示校验位
  45.     }
  46.     if (S2CON & S2TI)
  47.     {
  48.         S2CON &= ~S2TI;         //清除发送完成标志
  49.         busy = 0;
  50.     }
  51. }

  52. void SendData(char dat)
  53. {
  54.     while (busy);               //等待上个数据发送完成
  55.     ACC = dat;                  //取得偶校验位P
  56.     if (P)                                                //根据P来设置串口数据的校验位
  57.     {
  58. #if (PARITYBIT == ODD_PARITY)
  59.         S2CON &= ~S2TB8;        //置校验位为0
  60. #elif (PARITYBIT == EVEN_PARITY)
  61.         S2CON |= S2TB8;         //置校验位为1
  62. #endif
  63.     }
  64.     else
  65.     {
  66. #if (PARITYBIT == ODD_PARITY)
  67.         S2CON |= S2TB8;         //置校验位为1
  68. #elif (PARITYBIT == EVEN_PARITY)
  69.         S2CON &= ~S2TB8;        //置校验位为0
  70. #endif
  71.     }
  72.     busy = 1;
  73.     S2BUF = ACC;                //发送数据
  74. }

  75. void SendString(char *s)
  76. {
  77.     while (*s)                  //判断字符串结束标志
  78.     {
  79.         SendData(*s++);         //发送字符
  80.     }
  81. }
复制代码
回复

使用道具 举报

地板
ID:319557 发表于 2020-5-14 17:05 | 只看该作者
angmall 发表于 2019-5-1 10:51
你可以试试这个

对我很有用。十分感谢!!!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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