找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

帖子
查看: 3721|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

有人遇到過(guò)STC12C5A60S2串口2無(wú)法使用的問(wèn)題嗎?

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:523982 發(fā)表于 2019-4-30 11:22 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
有人遇到過(guò)STC12C5A60S2串口2無(wú)法使用的問(wèn)題嗎?串口1正常能收發(fā)數(shù)據(jù),用的官網(wǎng)提供的例程,百度發(fā)現(xiàn)很多人都遇到這個(gè)問(wèn)題,目前還沒(méi)找到解決辦法。下載過(guò)http://www.torrancerestoration.com/bbs/dpj-133507-1.html也無(wú)法使用。
  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. }

復(fù)制代碼


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

使用道具 舉報(bào)

沙發(fā)
ID:110606 發(fā)表于 2019-4-30 16:32 | 只看該作者
我之前用官網(wǎng)的歷程發(fā)現(xiàn)有問(wèn)題,后來(lái)就不用了
回復(fù)

使用道具 舉報(bào)

板凳
ID:155507 發(fā)表于 2019-5-1 10:51 | 只看該作者
你可以試試這個(gè)

  1. #include "reg51.h"

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

  4. #define NONE_PARITY     0   //無(wú)校驗(yàn)位
  5. #define ODD_PARITY      1   //奇校驗(yàn)
  6. #define EVEN_PARITY     2   //偶校驗(yàn)
  7. #define MARK_PARITY     3   //標(biāo)記校驗(yàn)
  8. #define SPACE_PARITY    4   //空校驗(yàn)

  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位可變波特率 (無(wú)校驗(yàn)位)
  26. #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
  27.     S2CON = 0xda;               //9位可變波特率,校驗(yàn)位初始為1
  28. #elif (PARITYBIT == SPACE_PARITY)
  29.     S2CON = 0xd5;               //9位可變波特率,校驗(yàn)位初始為0
  30. #endif

  31.     BRT = -(FOSC/32/BAUD);      //設(shè)置獨(dú)立波特率發(fā)生器的重載初值
  32.     AUXR = 0x14;                //獨(dú)立波特率發(fā)生器工作在1T模式
  33.     IE2 = 0x01;                 //使能串口2中斷
  34.     EA = 1;                     //開(kāi)總中斷

  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;         //清除接收完成標(biāo)志
  43.         P0 = S2BUF;             //P0顯示串口數(shù)據(jù)
  44.         P2 = (S2CON & S2RB8);   //P2.2顯示校驗(yàn)位
  45.     }
  46.     if (S2CON & S2TI)
  47.     {
  48.         S2CON &= ~S2TI;         //清除發(fā)送完成標(biāo)志
  49.         busy = 0;
  50.     }
  51. }

  52. void SendData(char dat)
  53. {
  54.     while (busy);               //等待上個(gè)數(shù)據(jù)發(fā)送完成
  55.     ACC = dat;                  //取得偶校驗(yàn)位P
  56.     if (P)                                                //根據(jù)P來(lái)設(shè)置串口數(shù)據(jù)的校驗(yàn)位
  57.     {
  58. #if (PARITYBIT == ODD_PARITY)
  59.         S2CON &= ~S2TB8;        //置校驗(yàn)位為0
  60. #elif (PARITYBIT == EVEN_PARITY)
  61.         S2CON |= S2TB8;         //置校驗(yàn)位為1
  62. #endif
  63.     }
  64.     else
  65.     {
  66. #if (PARITYBIT == ODD_PARITY)
  67.         S2CON |= S2TB8;         //置校驗(yàn)位為1
  68. #elif (PARITYBIT == EVEN_PARITY)
  69.         S2CON &= ~S2TB8;        //置校驗(yàn)位為0
  70. #endif
  71.     }
  72.     busy = 1;
  73.     S2BUF = ACC;                //發(fā)送數(shù)據(jù)
  74. }

  75. void SendString(char *s)
  76. {
  77.     while (*s)                  //判斷字符串結(jié)束標(biāo)志
  78.     {
  79.         SendData(*s++);         //發(fā)送字符
  80.     }
  81. }
復(fù)制代碼
回復(fù)

使用道具 舉報(bào)

地板
ID:319557 發(fā)表于 2020-5-14 17:05 | 只看該作者
angmall 發(fā)表于 2019-5-1 10:51
你可以試試這個(gè)

對(duì)我很有用。十分感謝!��!
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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