单片机串口调试

  1 #include <reg52.h>
  2 #include <stdio.h>             
  3 #define uc    unsigned char
  4 #define uint    unsigned int
  5 
  6 uc    flag, i, k, flag_t, s[50] = "", j = 0, flag_n = 0;
  7 uc code table[] = "I get ";
  8 
  9 void init()
 10 {
 11     TMOD    = 0x20; 
 12     TH1    = 0xfd; 
 13     TL1    = 0xfd;
 14     TR1    = 1;    
 15 
 16     SM0    = 0;
 17     SM1    = 1;    
 18     REN    = 1;    
 19     EA    = 1;    
 20     ES    = 1;   
 21 }
 22 
 23 
 24 void main()
 25 {
 26     init();
 27     while ( 1 )
 28     {
 29         if ( flag == 1 )
 30         {
 31             if ( flag_n != 0 ) 
 32             {
 33                 TI = 1;
 34                 printf( "\n" );
 35                 while ( !TI )
 36                     ;
 37                 TI = 0;
 38             }
 39             for ( i = 0; i < 6; i++ )
 40             {
 41                 SBUF = table[i];
 42                 while ( !TI )
 43                     ;
 44                 TI = 0;
 45             }
 46             for ( i = 0; s[i] != '#' && i < 50; i++ )
 47             {
 48                 SBUF = s[i];
 49                 while ( !TI )
 50                     ;
 51                 TI    = 0;
 52                 SBUF    = 'b';
 53                 while ( !TI )
 54                     ;
 55                 TI = 0;
 56             }
 57             flag = 0;
 58         }
 59         if ( flag_t == 1 )              
 60         {
 61             TI = 1;                
 62             printf( "\nYou transfer %s", s );
 63             while ( !TI )
 64                 ;
 65             TI    = 0;
 66             flag_t    = 0;
 67         }
 68     }
 69 }
 70 
 71 
 72 void ser() interrupt 4
 73 {
 74     if ( RI )       
 75     {
 76         RI = 0;
 77 
 78         if ( flag == 0 && j != 0 )
 79         {
 80             flag_n++;
 81             for ( j = 0; s[j] != '#' && j < 50; j++ )
 82                 s[j] = '\0';
 83             j = 0;
 84         }
 85         s[j] = SBUF;
 86 
 87         for ( k = 0; k < 20; k++ )
 88         {
 89             SBUF = j;
 90             while ( !TI )
 91                 ;
 92             TI = 0;
 93         }
 94 
 95 
 96         flag = 1;
 97         if ( s[j] == '#' || j == 49 )   
 98             flag_t = 1;
 99         else
100             j++;
101     }
102 
103     if ( TI )                              
104     {
105     }
106 }

 

posted @ 2018-05-27 15:04  互联星空  阅读(296)  评论(0编辑  收藏  举报