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

📄 serial.lst

📁 嵌入式WEB程序
💻 LST
字号:
C51 COMPILER V7.01  SERIAL                                                                 04/30/2003 15:22:11 PAGE 1   


C51 COMPILER V7.01, COMPILATION OF MODULE SERIAL
OBJECT MODULE PLACED IN Serial.OBJ
COMPILER INVOKED BY: D:\SOFT\KEIL\C51\BIN\C51.EXE Serial.c BROWSE DEBUG OBJECTEXTEND

stmt level    source

   1          //-----------------------------------------------------------------------------
   2          // Copyright (c) 2002 Jim Brady
   3          // Do not use commercially without author's permission
   4          // Last revised August 2002
   5          // Net SERIAL.C
   6          //
   7          // This module handles RS-232 messages and associated tasks
   8          //-----------------------------------------------------------------------------
   9          #include "C8051f.h"
  10          #include "intrins.h"
  11          #include "net.h"
  12          #include "serial.h"
  13          
  14          bit CommRecDataOverflowFlag,FlagRecComm,SendItComm;
  15          
  16          unsigned char CommSendBufferHead, CommSendBufferTail;
  17          unsigned char CommRecBufferHead, CommRecBufferTail;
  18          
  19          unsigned char xdata CommSendBuffer[DB_SENDMAXSIZE] _at_ 0 ; //串行口缓冲区定位在内部4K XRAM中
  20          unsigned char xdata CommRecBuffer[DB_RECMAXSIZE] _at_ DB_SENDMAXSIZE; 
  21           
  22          void ClearCommRecBuffer(void)
  23          {
  24   1              unsigned char i;
  25   1              CommRecBufferHead=CommRecBufferTail=0;
  26   1              CommSendBufferHead=CommSendBufferTail=0;
  27   1              FlagRecComm=0;
  28   1              for (i=0;i<DB_SENDMAXSIZE;i++)
  29   1              {
  30   2                      CommSendBuffer[i]=0;
  31   2              }
  32   1              for (i=0;i<DB_RECMAXSIZE;i++)
  33   1              {
  34   2                      CommRecBuffer[i]=0;
  35   2              }
  36   1      }
  37          
  38          void init_serial(void)
  39          {
  40   1              ClearCommRecBuffer();
  41   1              OpenComm();
  42   1      }
  43          
  44          void OpenComm(void) 
  45          {
  46   1              PCON |= 0x80;           // SMOD=1 (HW_UART uses Timer 1 overflow with no divide down).
  47   1              TMOD |= 0x20;           // Configure Timer 1 for use by UART0
  48   1              CKCON |= 0x10;          // Timer 1 derived from SYSCLK
  49   1      
  50   1              RCAP2H=(65536-(SYSCLK/BAUDRATE0/32))/256;
  51   1              RCAP2L=(65536-(SYSCLK/BAUDRATE0/32))%256;
  52   1              TH2=RCAP2H;TL2=RCAP2L;
  53   1              CT2=0;                          //T2:timer mode 
  54   1              TR2=1;
  55   1              TCLK=1;RCLK=1;          //说明:52,对于SIO0,可选择T1(TCLK=0,RCLK=0)或T2(TCLK=1,RCLK=1)作为波特率发生器
C51 COMPILER V7.01  SERIAL                                                                 04/30/2003 15:22:11 PAGE 2   

  56   1                                                  //            SIO1只能用T1作为波特率发生器
  57   1                                                  //baud=OSC/(32*(65536-[RCAP2H,RCAP2L])
  58   1              CommSendBufferHead=CommSendBufferTail=0; // set the head and tail to the base of the ring buffer
  59   1              CommRecBufferHead=CommRecBufferTail=0;
  60   1              FlagRecComm=0;
  61   1              RI0=0;                                  // Clear HW_UART receive and transmit
  62   1              TI0=0;                                  // complete indicators.
  63   1              SCON0 = 0x50;                   // Configure UART0 for mode 1, receiver enabled.
  64   1              ES0=1;                                  // allow the serial interrupt
  65   1              SendItComm=1;
  66   1      }
  67          /*
  68          void SendCommChar(char ch)
  69          {
  70                  CommSendBuffer[CommSendBufferTail]=ch; 
  71                  CommSendBufferTail++;
  72                  if (CommSendBufferTail==DB_SENDMAXSIZE)
  73                  {       
  74                          CommSendBufferTail=0;
  75                  }
  76                  if (SendItComm)
  77                  {        
  78                          SendItComm=0;
  79                          SBUF0=CommSendBuffer[CommSendBufferHead]; 
  80                  }
  81                  while (CommSendBufferHead!=CommSendBufferTail);
  82                  return ;
  83          }
  84          
  85          code unsigned char hex[]="0123456789ABCDEF";
  86          void SendCommHex(unsigned char senddata)//往串口发送hex码 表示的一个字符 例如senddata=0x3A那么将向串口发送
             -两个字符'3','A'hex[]为转换表,在前面有定义
  87          {
  88                  unsigned char ch;
  89                  ch=senddata>>4;
  90                  SendCommChar(hex[ch]);
  91                  ch=senddata&0x0F;
  92                  SendCommChar(hex[ch]);
  93          }
  94          void SendCommWord(unsigned int asciiword)
  95          //向串口发送一个int型的 hex码表示的字符 例如:asciiword=0x124D 将向串口发送4个字符:'1','2','4','D'
  96          {
  97                  unsigned char ascii;
  98                  ascii=asciiword>>8;
  99                  SendCommHex(ascii);
 100                  ascii=asciiword&0xff;
 101                  SendCommHex(ascii);
 102          }
 103          
 104          void SendCommLong(unsigned long asciilong)
 105          {
 106                  SendCommWord(asciilong>>16);
 107                  SendCommWord(asciilong&0xffff);
 108          }
 109          */
 110          void serial_send(unsigned char *base)
 111          {
 112   1              SendCommString(base);
 113   1      }
 114          
 115          void SendCommString(unsigned char *base) 
 116          {
C51 COMPILER V7.01  SERIAL                                                                 04/30/2003 15:22:11 PAGE 3   

 117   1      unsigned char i=0;
 118   1              if (base[0]==0) return;
 119   1              for (;;)
 120   1              {       
 121   2                      if (base[i]==0) break;
 122   2                      CommSendBuffer[CommSendBufferTail]=base[i]; 
 123   2                      CommSendBufferTail++; 
 124   2                      if (CommSendBufferTail==DB_SENDMAXSIZE)
 125   2                      {                                               
 126   3                              CommSendBufferTail=0;
 127   3                      }
 128   2                      i++;
 129   2              }
 130   1              if (SendItComm)
 131   1              {                                                       
 132   2                      SendItComm=0;
 133   2                      SBUF0=CommSendBuffer[CommSendBufferHead];
 134   2              }
 135   1              while (CommSendBufferHead!=CommSendBufferTail);
 136   1      }
 137          /*
 138          void SendCommBuffer(unsigned char *base, unsigned char size) 
 139          {
 140          unsigned char i=0;
 141                  if (!size) { return; }  
 142                  while (i<size) 
 143                  {        
 144                          CommSendBuffer[CommSendBufferTail]=base[i]; 
 145                          i++;
 146                          CommSendBufferTail++; 
 147                          if (CommSendBufferTail==DB_SENDMAXSIZE)
 148                          { 
 149                                  CommSendBufferTail=0;
 150                          }
 151                  }
 152                  if (SendItComm)
 153                  {        
 154                          SendItComm=0;
 155                          SBUF0=CommSendBuffer[CommSendBufferHead]; 
 156                  }
 157          }
 158          */
 159          void CommISR(void) interrupt 4
 160          {
 161   1              if (_testbit_(TI0))
 162   1              {
 163   2                      TI0=0;
 164   2                      CommSendBufferHead++;   
 165   2                      if (CommSendBufferHead==DB_SENDMAXSIZE)
 166   2                      {        
 167   3                              CommSendBufferHead=0;
 168   3                      }
 169   2                      if (CommSendBufferHead!=CommSendBufferTail)
 170   2                      {        
 171   3                              SBUF0=CommSendBuffer[CommSendBufferHead]; // send the next byte
 172   3                              SendItComm=0;
 173   3                      }
 174   2                      else
 175   2                      {
 176   3                              SendItComm=1;
 177   3                      }
 178   2              }
C51 COMPILER V7.01  SERIAL                                                                 04/30/2003 15:22:11 PAGE 4   

 179   1              if (_testbit_(RI0))     
 180   1              {        
 181   2                      RI0=0;
 182   2                      if (CommRecBufferTail==CommRecBufferHead)
 183   2                      {
 184   3                              CommRecDataOverflowFlag=1;                              //接收缓冲区溢出
 185   3                      }
 186   2                      CommRecBuffer[CommRecBufferTail]=SBUF0;     //receive data           
 187   2                  CommRecBufferTail++;
 188   2                  if (CommRecBufferTail==DB_RECMAXSIZE)
 189   2                  {
 190   3                      CommRecBufferTail=0;
 191   3                  }
 192   2                      FlagRecComm=1;
 193   2              }
 194   1      }
 195          
 196          //从接收缓冲区读数据 ,无数据返回0,有数据返回1
 197          bit GetCommChar(unsigned char idata *ch)      
 198          { 
 199   1              if (CommRecBufferTail==CommRecBufferHead) return 0;     
 200   1              *ch=CommRecBuffer[CommRecBufferHead];
 201   1              CommRecBufferHead++;
 202   1              if (CommRecBufferHead==DB_RECMAXSIZE)
 203   1              {
 204   2                      CommRecBufferHead=0;
 205   2              }
 206   1              if (CommRecBufferTail==CommRecBufferHead) FlagRecComm=0;
 207   1              return 1;
 208   1      }
 209          /*
 210          //在T(0-255)毫秒内从接收缓冲区读数据 ,无数据返回0,有数据返回1
 211          bit GetCommCharWait(unsigned char idata *ch,unsigned char T)  //T ms    
 212          { 
 213                  Count1ms=T;*ch=0;
 214                  while (Count1ms)
 215                  {
 216                          if (CommRecBufferTail!=CommRecBufferHead) break;
 217                  }
 218                  if (Count1ms==0) return 0;
 219                  *ch=CommRecBuffer[CommRecBufferHead];
 220                  CommRecBufferHead++;
 221                  if (CommRecBufferHead==DB_RECMAXSIZE)
 222                  {
 223                          CommRecBufferHead=0;
 224                  }
 225                  return 1;
 226          }
 227          */
 228          
 229          //------------------------------------------------------------------------
 230          // This function converts an integer to an ASCII string.  It is a 
 231          // normally provided as a standard library function but the Keil
 232          // libraries do not include it.  Caution: The string passed to this
 233          // must be at least 12 bytes long
 234          //------------------------------------------------------------------------
 235          char * itoa(UINT value, char * buf, UCHAR radix)
 236          {
 237   1              UINT i;
 238   1              char * ptr;
 239   1              char * temphold;
 240   1      
C51 COMPILER V7.01  SERIAL                                                                 04/30/2003 15:22:11 PAGE 5   

 241   1              temphold = buf;
 242   1              ptr = buf + 12;
 243   1              *--ptr = 0;             // Insert NULL char
 244   1              do
 245   1              {
 246   2                 // First create string in reverse order
 247   2                 i = (value % radix) + 0x30;
 248   2                      if(i > 0x39) i += 7;
 249   2                      *--ptr = i;
 250   2            value = value / radix;
 251   2              } while(value != 0);
 252   1      
 253   1              // Next, move the string 6 places to the left
 254   1              // Include NULL character
 255   1              for( ; (*buf++ = *ptr++); );    
 256   1              return(temphold);
 257   1      }
 258          
 259          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    489    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      4      17
   IDATA SIZE       =   ----    ----
   BIT SIZE         =      3    ----
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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