找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3750|回复: 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 单片机教程网

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