久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3676|回復: 3
打印 上一主題 下一主題
收起左側

有人遇到過STC12C5A60S2串口2無法使用的問題嗎?

[復制鏈接]
跳轉到指定樓層
樓主
ID:523982 發表于 2019-4-30 11:22 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
有人遇到過STC12C5A60S2串口2無法使用的問題嗎?串口1正常能收發數據,用的官網提供的例程,百度發現很多人都遇到這個問題,目前還沒找到解決辦法。下載過http://www.zg4o1577.cn/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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲精品永久免费 | 91 视频网站 | 亚洲精品一区av在线播放 | 99久久婷婷国产精品综合 | 国产精品福利一区二区三区 | 欧美一区二区三区视频在线播放 | 久草资源网站 | 久久成人国产 | 日韩成人免费视频 | 欧美在线看片 | 视频一区在线观看 | 日韩欧美一区二区三区免费观看 | 国产精品久久久久久久久污网站 | 国色天香成人网 | 视频一区在线观看 | 九九九精品视频 | 亚洲欧美一区在线 | 精品国产综合 | 国产免费一区二区 | av在线黄| 国产精品久久久乱弄 | 久久久精品国产 | 国产欧美日韩综合精品一区二区 | 东方伊人免费在线观看 | 亚洲成人av | 久久av一区 | 日韩国产在线 | 欧美日韩精品在线免费观看 | 天天色图| 国产真实精品久久二三区 | 久久免费精品 | 久久无毛| 亚洲午夜精品 | 国产一区二区精品 | 天天干狠狠操 | 成年免费在线观看 | 国产亚洲精品久久久久动 | 成人免费视频 | 国产成人午夜电影网 | www.久久精品视频 | 久草新在线 |