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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

為什么我在串口發送1,但是串口接收為82(都是HEX模式)或者為B2(發送為文本模式)

[復制鏈接]
跳轉到指定樓層
樓主
ID:71099 發表于 2016-6-24 09:57 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  1. /**********************************************************
  2. STC12C5204       
  3. 串口工作方式        :        方式1        8位UART
  4. 定時器的選取        :        T1
  5. 定時器工作方式        :        方式2
  6. 波特率選取                :        9600

  7. **********************************************************/

  8. #include "reg51.h"
  9. #include "intrins.h"

  10. typedef unsigned char BYTE;
  11. typedef unsigned int WORD;

  12. bit busy;
  13. BYTE        temp=0;
  14. BYTE        flag=0;

  15. void SendData(BYTE dat);
  16. void SendString(char *s);

  17. void main()
  18. {
  19.         SCON = 0x50;                        //方式1
  20.     TMOD = 0x20;                        //8位自動重裝載定時/計數器
  21.     TH1 = TL1 = 0xfd;                //9600
  22.     TR1 = 1;               
  23.     ES = 1;                 
  24.     EA = 1;                 
  25.     while(1)
  26.         {
  27.                 if(flag)
  28.                 {
  29.                         flag=0;
  30.                         SendData(temp+1);
  31.                 }
  32.         }
  33. }

  34. /*----------------------------
  35. UART interrupt service routine
  36. ----------------------------*/
  37. void Uart_Isr() interrupt 4 using 1
  38. {
  39.     if (RI)
  40.     {
  41.         RI = 0;             //Clear receive interrupt flag
  42.         temp = SBUF;          //P0 show UART data
  43.                 flag=1;
  44.     }
  45.     if (TI)
  46.     {
  47.         TI = 0;             //Clear transmit interrupt flag
  48.         busy = 0;           //Clear transmit busy flag
  49.     }
  50. }

  51. /*----------------------------
  52. Send a byte data to UART
  53. Input: dat (data to be sent)
  54. Output:None
  55. ----------------------------*/
  56. void SendData(BYTE dat)
  57. {
  58.     while (busy);           //Wait for the completion of the previous data is sent
  59.     busy = 1;
  60.     SBUF = dat;             //Send data to UART buffer
  61. }

  62. /*----------------------------
  63. Send a string to UART
  64. Input: s (address of string)
  65. Output:None
  66. ----------------------------*/
  67. void SendString(char *s)
  68. {
  69.     while (*s)              //Check the end of the string
  70.     {
  71.         SendData(*s++);     //Send current char and increment string ptr
  72.     }
  73. }
復制代碼


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

使用道具 舉報

沙發
ID:118380 發表于 2016-6-24 19:27 | 只看該作者
  1. /**********************************************************
  2. STC12C5204        
  3. 串口工作方式        :        方式1        8位UART
  4. 定時器的選取        :        T1
  5. 定時器工作方式        :        方式2
  6. 波特率選取                :        9600

  7. **********************************************************/

  8. #include "reg51.h"
  9. #include "intrins.h"

  10. typedef unsigned char BYTE;
  11. typedef unsigned int WORD;

  12. bit busy;
  13. BYTE        temp=0;
  14. BYTE        flag=0;

  15. void SendData(BYTE dat);
  16. void SendString(char *s);

  17. void main()
  18. {
  19.         SCON = 0x50;                        //方式1
  20.     TMOD = 0x20;                        //8位自動重裝載定時/計數器
  21.     TH1 = TL1 = 0xfd;                //9600
  22.     TR1 = 1;               
  23.     ES = 1;                 
  24.     EA = 1;                 
  25.     while(1)
  26.         {
  27.                 if(flag)
  28.                 {
  29.                         flag=0;
  30.                         SendData(temp+1);
  31.                 }
  32.         }
  33. }

  34. /*----------------------------
  35. UART interrupt service routine
  36. ----------------------------*/
  37. void Uart_Isr() interrupt 4 using 1
  38. {
  39.     if (RI)
  40.     {
  41.         RI = 0;             //Clear receive interrupt flag
  42.         temp = SBUF;          //P0 show UART data
  43.                 flag=1;
  44.     }
  45. //    if (TI)
  46. //    {
  47. //        TI = 0;             //Clear transmit interrupt flag
  48. //        busy = 0;           //Clear transmit busy flag
  49. //    }
  50. }

  51. /*----------------------------
  52. Send a byte data to UART
  53. Input: dat (data to be sent)
  54. Output:None
  55. ----------------------------*/
  56. void SendData(BYTE dat)
  57. {
  58.     while (flag);           //Wait for the completion of the previous data is sent
  59.    
  60.     SBUF = dat;  
  61. while(!TI);
  62. TI=0;        //Send data to UART buffer
  63. }

  64. /*----------------------------
  65. Send a string to UART
  66. Input: s (address of string)
  67. Output:None
  68. ----------------------------*/
  69. void SendString(char *s)
  70. {
  71.     while (*s)              //Check the end of the string
  72.     {
  73.         SendData(*s++);     //Send current char and increment string ptr
  74.     }
  75. }
復制代碼

改了一下,可以實現發送1,返回2
回復

使用道具 舉報

板凳
ID:101595 發表于 2016-6-25 23:32 來自手機 | 只看該作者
dpj蝸牛 發表于 2016-6-24 19:27
改了一下,可以實現發送1,返回2

一個簡單的程序為什么要寫這么復雜,
回復

使用道具 舉報

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

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产在线对白 | 久久久青草婷婷精品综合日韩 | 国产中文字幕网 | 毛片1 | 一区在线播放 | 精品一区在线 | 在线中文字幕日韩 | 正在播放亚洲 | 欧美一级黄色免费看 | 国产一二三区电影 | 日韩欧美国产精品 | 91天堂网| 成人依人 | 久久99精品久久久久久琪琪 | 国产午夜精品久久久久免费视高清 | 天天干天天插天天 | 中文字幕在线免费观看 | 九九热国产视频 | 欧美在线a | 毛片一区二区三区 | 欧美色专区| 精品国产视频 | 欧美在线综合 | 日韩一区二区在线观看 | 午夜爽爽爽男女免费观看 | 激情一区二区三区 | 99精品一区二区三区 | 日韩中文字幕第一页 | 久久久久久久久久久福利观看 | 欧美一级欧美三级在线观看 | 国产精华一区 | 欧美一区二区三区在线 | 国产精品久久久久久久久久久免费看 | 国产精品日韩欧美一区二区三区 | 欧美13videosex性极品 | 亚洲一二三区免费 | 国产欧美一级二级三级在线视频 | 久久国产一区二区三区 | 97久久精品午夜一区二区 | 亚洲一区二区三区在线视频 | 在线观看黄视频 |