标题: 为什么我在串口发送1,但是串口接收为82(都是HEX模式)或者为B2(发送为文本模式) [打印本页]

作者: geminiguy_07    时间: 2016-6-24 09:57
标题: 为什么我在串口发送1,但是串口接收为82(都是HEX模式)或者为B2(发送为文本模式)
  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. }
复制代码



作者: dpj蜗牛    时间: 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
作者: bx2008love    时间: 2016-6-25 23:32
dpj蜗牛 发表于 2016-6-24 19:27
改了一下,可以实现发送1,返回2

一个简单的程序为什么要写这么复杂,




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1