⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.lst

📁 单片机做usb转串口
💻 LST
字号:
C51 COMPILER V7.06   UART                                                                  12/11/2007 01:02:43 PAGE 1   


C51 COMPILER V7.06, COMPILATION OF MODULE UART
OBJECT MODULE PLACED IN UART.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE UART.c BROWSE DEBUG OBJECTEXTEND

stmt level    source

   1          /******************************************************************
   2             本程序只供学习使用,未经作者许可,不得用于其它任何用途
   3          
   4                  欢迎访问我的USB专区:http://group.ednchina.com/93/
   5                  欢迎访问我的blog:   http://www.ednchina.com/blog/computer00
   6                                       http://computer00.21ic.org
   7          
   8          UART.C  file
   9          
  10          作者:Computer-lov
  11          建立日期: 2007.03.20
  12          修改日期: 2007.11.18
  13          版本:V1.1
  14          版权所有,盗版必究。
  15          Copyright(C) Computer-lov 2007-2017
  16          All rights reserved            
  17          *******************************************************************/
  18          
  19          #include "at89x52.H"
  20          
  21          #include "UART.h"
  22          #include "my_type.h"
  23          #include "config.h"
  24          #include "USB2UART.h"
  25          
  26          uint8 Sending;
  27          
  28          //使用T2做波特率发生器可以获得更多的波特率设置。
  29          //删除本行将使用T1作为波特率发生器,最低波特率为300bps,最高为115200bps。
  30          #define USE_T2
  31          
  32          /********************************************************************
  33          函数功能:设置串口波特率。
  34          入口参数:波特率。
  35          返    回:无。
  36          备    注:无。
  37          ********************************************************************/
  38          uint32 SetUartBitRate(uint32 BitRate)
  39          { 
  40   1       EA=0;
  41   1      #ifdef USE_T2
  42   1       if(BitRate<=230400)
  43   1       {
  44   2        RCAP2L=0x10000-(Fclk/(BitRate*32));
  45   2        RCAP2H=(0x10000-(Fclk/(BitRate*32)))>>8;
  46   2       }
  47   1       BitRate=(Fclk/32)/(0x10000-((((uint32)RCAP2H)<<8)+RCAP2L));
  48   1      #else
               if(BitRate<225)
               {
                BitRate=225;
                PCON&=~0x80;  //波特率不加倍
                TH1=256-Fclk/(BitRate*12*16*2);
                TL1=256-Fclk/(BitRate*12*16*2);
                BitRate=(Fclk/12/32)/(0x100-((uint32)TH1));
C51 COMPILER V7.06   UART                                                                  12/11/2007 01:02:43 PAGE 2   

               }
               else if(BitRate<1200)
               {
                PCON&=~0x80;  //波特率不加倍
                TH1=256-Fclk/(BitRate*12*16*2);
                TL1=256-Fclk/(BitRate*12*16*2);
                BitRate=(Fclk/12/32)/(0x100-((uint32)TH1));
               }
               else if(BitRate<=115200)
               {
                PCON|=0x80;  //波特率加倍
                TH1=256-Fclk/(BitRate*12*16);
                TL1=256-Fclk/(BitRate*12*16);
                BitRate=(Fclk/12/16)/(0x100-((uint32)TH1));
               }
               else
               {
                BitRate=115200;
                PCON|=0x80;  //波特率加倍
                TH1=256-Fclk/(BitRate*12*16);
                TL1=256-Fclk/(BitRate*12*16);
                BitRate=(Fclk/12/16)/(0x100-((uint32)TH1));
               }
              #endif
  80   1       EA=1;
  81   1       return BitRate;
  82   1      }
  83          ////////////////////////End of function//////////////////////////////
  84          
  85          
  86          /********************************************************************
  87          函数功能:串口初始化。
  88          入口参数:无。
  89          返    回:无。
  90          备    注:无。
  91          ********************************************************************/
  92          void InitUART(void)
  93          {
  94   1       EA=0;
  95   1       
  96   1      #ifndef USE_T2
               TMOD&=0x0F;
               TMOD|=0x20;    //定时器1工作在模式2
               TCON=0x05;
              #endif
 101   1      
 102   1       SCON=0x50;       //串口工作在模式1
 103   1       
 104   1       SetUartBitRate(9600);  //波特率初始化为9600
 105   1      
 106   1      #ifdef USE_T2
 107   1       T2CON=0x34;     //使用T2作为波特率发生器
 108   1      #endif
 109   1      
 110   1       ES=1;         //串行中断允许
 111   1      
 112   1      #ifndef USE_T2
               TR1=1;        //启动定时器1
              #endif
 115   1      
 116   1       REN=1;        //允许接收 
 117   1       EA=1;         //允许中断
C51 COMPILER V7.06   UART                                                                  12/11/2007 01:02:43 PAGE 3   

 118   1       Sending=0;    //允许发送
 119   1      }
 120          ////////////////////////End of function//////////////////////////////
 121          
 122            
 123          /********************************************************************
 124          函数功能:串口中断处理。
 125          入口参数:无。
 126          返    回:无。
 127          备    注:无。
 128          ********************************************************************/
 129          void UartISR(void) interrupt 4
 130          {
 131   1       if(RI)    //收到数据
 132   1        {
 133   2         RI=0;   //清中断请求
 134   2         if(InCount>64)
 135   2         {
 136   3          Overflow=1;
 137   3         }
 138   2         InBuffer[(pIn+InCount)&0x3F]=SBUF;
 139   2         InCount++;
 140   2        }
 141   1       else      //发送完一字节数据
 142   1        {
 143   2         TI=0;
 144   2         Sending=0;  //清正在发送标志
 145   2        }
 146   1      }
 147          ////////////////////////End of function//////////////////////////////
 148          
 149          /********************************************************************
 150          函数功能:往串口发送一字节数据。
 151          入口参数:d: 要发送的字节数据。
 152          返    回:无。
 153          备    注:无。
 154          ********************************************************************/
 155          void UartPutChar(uint8 d)
 156          {
 157   1       while(Sending);
 158   1       Sending=1;
 159   1       SBUF=d;
 160   1      }
 161          ////////////////////////End of function//////////////////////////////
 162          
 163          /********************************************************************
 164          函数功能:发送一个字符串。
 165          入口参数:pd:要发送的字符串指针。
 166          返    回:无。
 167          备    注:无。
 168          ********************************************************************/
 169          void Prints(uint8 * pd)
 170          {
 171   1       while((*pd)!='\0')
 172   1        {
 173   2         UartPutChar(*pd);
 174   2         pd++;
 175   2        }
 176   1      }
 177          ////////////////////////End of function//////////////////////////////
 178          
 179          /********************************************************************
C51 COMPILER V7.06   UART                                                                  12/11/2007 01:02:43 PAGE 4   

 180          函数功能:将整数转按十进制字符串发送。
 181          入口参数:x:待显示的整数。
 182          返    回:无。
 183          备    注:无。
 184          ********************************************************************/
 185          void PrintLongInt(uint32 x)
 186          {
 187   1       int8 i;
 188   1       uint8 display_buffer[10];
 189   1      
 190   1       for(i=9;i>=0;i--)
 191   1        {
 192   2         display_buffer[i]='0'+x%10;
 193   2         x/=10;
 194   2        }
 195   1       for(i=0;i<9;i++)
 196   1        {
 197   2         if(display_buffer[i]!='0')break;
 198   2        }
 199   1       for(;i<10;i++)UartPutChar(display_buffer[i]);
 200   1      }
 201          ////////////////////////End of function//////////////////////////////
 202          
 203          code uint8 HexTable[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
 204          /********************************************************************
 205          函数功能:将短整数按十六进制发送。
 206          入口参数:待发送的整数。
 207          返    回:无。
 208          备    注:无。
 209          ********************************************************************/
 210          /*void PrintShortIntHex(uint16 x)
 211          {
 212           uint8 i;
 213           uint8 display_buffer[7];
 214           display_buffer[6]=0;
 215           display_buffer[0]='0';
 216           display_buffer[1]='x';
 217           for(i=5;i>=2;i--)
 218            {
 219             display_buffer[i]=HexTable[(x&0xf)];
 220             x>>=4;
 221            }
 222           Prints(display_buffer);
 223          }*/
 224          ////////////////////////End of function//////////////////////////////
 225          
 226          /********************************************************************
 227          函数功能:将整数按十六进制发送。
 228          入口参数:待发送的整数。
 229          返    回:无。
 230          备    注:无。
 231          ********************************************************************/
 232          /*
 233          void PrintLongIntHex(uint32 x)  //
 234          {
 235           uint8 i;
 236           uint8 display_buffer[11];
 237           display_buffer[10]=0;
 238           display_buffer[0]='0';
 239           display_buffer[1]='x';
 240           for(i=9;i>=2;i--)
 241            {
C51 COMPILER V7.06   UART                                                                  12/11/2007 01:02:43 PAGE 5   

 242             display_buffer[i]=HexTable[(x&0xf)];
 243             x>>=4;
 244            }
 245           Prints(display_buffer);
 246          }
 247          */
 248          ////////////////////////End of function//////////////////////////////
 249          
 250          
 251          /********************************************************************
 252          函数功能:发送一个byte的数据。
 253          入口参数:待发送的数据。
 254          返    回:无。
 255          备    注:无。
 256          ********************************************************************/
 257          void Printc(uint8 x)
 258          {
 259   1       while(Sending);
 260   1       Sending=1;
 261   1       SBUF=x;
 262   1      }
 263          ////////////////////////End of function//////////////////////////////
 264          
 265          
 266          /********************************************************************
 267          函数功能:以HEX格式发送一个byte的数据。
 268          入口参数:待发送的数据
 269          返    回:无。
 270          备    注:无。
 271          ********************************************************************/
 272          void PrintHex(uint8 x)
 273          {
 274   1       Printc('0');
 275   1       Printc('x');
 276   1       Printc(HexTable[x>>4]);
 277   1       Printc(HexTable[x&0xf]);
 278   1       Printc(' ');
 279   1      }
 280          ////////////////////////End of function//////////////////////////////


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    477    ----
   CONSTANT SIZE    =     16    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      1      19
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -