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

📄 pc_io.lst

📁 时间触发嵌入式系统设计模式:使用8051系列微控制器开发可靠应用
💻 LST
字号:
C51 COMPILER V6.10  PC_IO                                                                  04/18/2001 16:10:03 PAGE 1   


C51 COMPILER V6.10, COMPILATION OF MODULE PC_IO
OBJECT MODULE PLACED IN .\PC_IO.OBJ
COMPILER INVOKED BY: C:\KEIL\C51\BIN\C51.EXE .\PC_IO.c OPTIMIZE(6,SIZE) BROWSE DEBUG OBJECTEXTEND

stmt level    source

   1          /*------------------------------------------------------------------*-
   2          
   3             PC_IO.C (v1.01)
   4          
   5            ------------------------------------------------------------------
   6          
   7             Core files for simple PC link library for 8051 family
   8          
   9             Uses the USART, and Pins 3.1 (Tx) and 3.0 (Rx) 
  10          
  11             See text for details (Chapter 18).
  12          
  13          
  14             COPYRIGHT
  15             ---------
  16          
  17             This code is from the book:
  18          
  19             PATTERNS FOR TIME-TRIGGERED EMBEDDED SYSTEMS by Michael J. Pont 
  20             [Pearson Education, 2001; ISBN: 0-201-33138-1].
  21          
  22             This code is copyright (c) 2001 by Michael J. Pont.
  23           
  24             See book for copyright details and other information.
  25          
  26          -*------------------------------------------------------------------*/
  27          
  28          #include "Main.h"
  29          #include "PC_IO.h"
  30          
  31          // ------ Public variable definitions ------------------------------
  32          
  33          tByte In_read_index_G;     // Data in buffer that has been read 
  34          tByte In_waiting_index_G;  // Data in buffer not yet read
  35          
  36          tByte Out_written_index_G;  // Data in buffer that has been written 
  37          tByte Out_waiting_index_G;  // Data in buffer not yet written
  38          
  39          // ------ Public variable declarations -----------------------------
  40          
  41          // The error code variable
  42          //
  43          // See Main.H for port on which error codes are displayed
  44          // and for details of error codes
  45          extern tByte Error_code_G;
  46          
  47          // ------ Private function prototypes ------------------------------
  48          
  49          void PC_LINK_IO_Send_Char(const char);
  50          
  51          // ------ Private constants ----------------------------------------
  52          
  53          // The receive buffer length
  54          #define RECV_BUFFER_LENGTH 8
  55          
C51 COMPILER V6.10  PC_IO                                                                  04/18/2001 16:10:03 PAGE 2   

  56          // The transmit buffer length
  57          #define TRAN_BUFFER_LENGTH 50
  58          
  59          #define XON  0x11
  60          #define XOFF 0x13
  61          
  62          // ------ Private variables ----------------------------------------
  63          
  64          static tByte Recv_buffer[RECV_BUFFER_LENGTH];
  65          static tByte Tran_buffer[TRAN_BUFFER_LENGTH];
  66          
  67          
  68          /*------------------------------------------------------------------*-
  69          
  70            PC_LINK_IO_Update()
  71          
  72            Checks for character in the UART (hardware) receive buffer
  73            Sends next character from the software transmit buffer
  74            
  75          -*------------------------------------------------------------------*/
  76          void PC_LINK_IO_Update(void)
  77             {
  78   1         // Deal with transmit bytes here
  79   1      
  80   1         // Is there any data ready to send?
  81   1         if (Out_written_index_G < Out_waiting_index_G)
  82   1            {
  83   2            PC_LINK_IO_Send_Char(Tran_buffer[Out_written_index_G]);     
  84   2      
  85   2            Out_written_index_G++;
  86   2            }
  87   1         else
  88   1            {
  89   2            // No data to send - just reset the buffer index
  90   2            Out_waiting_index_G = 0;
  91   2            Out_written_index_G = 0;
  92   2            }
  93   1      
  94   1         // Only dealing with received bytes here
  95   1         // -> Just check the RI flag
  96   1         if (RI == 1)
  97   1            {
  98   2            // Flag only set when a valid stop bit is received, 
  99   2            // -> data ready to be read into the received buffer
 100   2      
 101   2            // Want to read into index 0, if old data has been read
 102   2            // (simple ~circular buffer)
 103   2            if (In_waiting_index_G == In_read_index_G)
 104   2               { 
 105   3               In_waiting_index_G = 0;
 106   3               In_read_index_G = 0;
 107   3               } 
 108   2            
 109   2            // Read the data from USART buffer   
 110   2            Recv_buffer[In_waiting_index_G] = SBUF;
 111   2      
 112   2            if (In_waiting_index_G < RECV_BUFFER_LENGTH)
 113   2               {
 114   3               // Increment without overflowing buffer
 115   3               In_waiting_index_G++;
 116   3               }
 117   2          
C51 COMPILER V6.10  PC_IO                                                                  04/18/2001 16:10:03 PAGE 3   

 118   2            RI = 0;  // Clear RT flag
 119   2            }
 120   1         }
 121          
 122          /*------------------------------------------------------------------*-
 123          
 124            PC_LINK_IO_Write_Char_To_Buffer()
 125          
 126            Stores a character in the 'write' buffer, ready for 
 127            later transmission
 128           
 129          -*------------------------------------------------------------------*/
 130          void PC_LINK_IO_Write_Char_To_Buffer(const char CHARACTER)
 131             {
 132   1         // Write to the buffer *only* if there is space
 133   1         if (Out_waiting_index_G < TRAN_BUFFER_LENGTH)
 134   1            {
 135   2            Tran_buffer[Out_waiting_index_G] = CHARACTER;
 136   2            Out_waiting_index_G++;     
 137   2            }
 138   1         else
 139   1            {
 140   2            // Write buffer is full
 141   2            // Increase the size of TRAN_BUFFER_LENGTH
 142   2            // or increase the rate at which UART 'update' function is called 
 143   2            // or reduce the amount of data sent to PC
 144   2            Error_code_G = ERROR_USART_WRITE_CHAR;
 145   2            }
 146   1         }
 147          
 148          
 149          /*------------------------------------------------------------------*-
 150          
 151            PC_LINK_IO_Write_String_To_Buffer()
 152          
 153            Copies a (null terminated) string to the character buffer.  
 154            (The contents of the buffer are then passed over the serial link)
 155          
 156            STR_PTR - Pointer to the NULL-TERMINATED string.  
 157          
 158          -*------------------------------------------------------------------*/
 159          void PC_LINK_IO_Write_String_To_Buffer(const char* const STR_PTR)
 160             {
 161   1         tByte i = 0;
 162   1      
 163   1         while (STR_PTR[i] != '\0')
 164   1            {
 165   2            PC_LINK_IO_Write_Char_To_Buffer(STR_PTR[i]);
 166   2            i++;
 167   2            }
 168   1         }
 169          
 170          /*------------------------------------------------------------------*-
 171          
 172            PC_LINK_IO_Get_Char_From_Buffer()
 173          
 174            Retrieves a character from the (software) buffer, if available
 175          
 176            The character from the buffer is returned, or - if no 
 177            data are available - PC_LINK_IO_NO_CHAR is returned.
 178          
 179          -*------------------------------------------------------------------*/
C51 COMPILER V6.10  PC_IO                                                                  04/18/2001 16:10:03 PAGE 4   

 180          char PC_LINK_IO_Get_Char_From_Buffer(void)
 181             {
 182   1         char Ch = PC_LINK_IO_NO_CHAR;
 183   1      
 184   1         // If there is new data in the buffer
 185   1         if (In_read_index_G < In_waiting_index_G)
 186   1            {
 187   2            Ch = Recv_buffer[In_read_index_G];
 188   2      
 189   2            if (In_read_index_G < RECV_BUFFER_LENGTH)
 190   2               {
 191   3               In_read_index_G++;
 192   3               }
 193   2            }
 194   1         
 195   1         return Ch;
 196   1         }
 197          
 198          /*------------------------------------------------------------------*-
 199          
 200            PC_LINK_IO_Send_Char()
 201          
 202            Based on Keil sample code, with added (loop) timeouts.
 203            Implements Xon / Off control.
 204          
 205            Uses on-chip UART hardware.
 206          
 207          -*------------------------------------------------------------------*/
 208          void PC_LINK_IO_Send_Char(const char CHARACTER)
 209             {
 210   1         tLong Timeout1 = 0;
 211   1         tLong Timeout2 = 0;
 212   1      
 213   1         if (CHARACTER == '\n')  
 214   1            {
 215   2            if (RI)  
 216   2               {
 217   3               if (SBUF == XOFF)  
 218   3                  {
 219   4                  Timeout2 = 0;
 220   4                  do {
 221   5                     RI = 0;
 222   5                      
 223   5                     // Wait for uart (with simple timeout)
 224   5                     Timeout1 = 0;
 225   5                     while ((++Timeout1) && (RI == 0));  
 226   5      
 227   5                     if (Timeout1 == 0)
 228   5                        {
 229   6                        // USART did not respond - error
 230   6                        Error_code_G = ERROR_USART_TI;
 231   6                        return;
 232   6                        } 
 233   5      
 234   5                     } while ((++Timeout2) && (SBUF != XON));
 235   4      
 236   4                  if (Timeout2 == 0)
 237   4                     {
 238   5                     // uart did not respond - error
 239   5                     Error_code_G = ERROR_USART_TI;
 240   5                     return;
 241   5                     } 
C51 COMPILER V6.10  PC_IO                                                                  04/18/2001 16:10:03 PAGE 5   

 242   4      
 243   4                  RI = 0; 
 244   4                  }
 245   3               }
 246   2      
 247   2            Timeout1 = 0;
 248   2            while ((++Timeout1) && (TI == 0));  
 249   2      
 250   2            if (Timeout1 == 0)
 251   2               {
 252   3               // uart did not respond - error
 253   3               Error_code_G = ERROR_USART_TI;
 254   3               return;
 255   3               } 
 256   2      
 257   2            TI = 0;
 258   2            SBUF = 0x0d;  // output CR  
 259   2            }
 260   1        
 261   1         if (RI)  
 262   1            {
 263   2            if (SBUF == XOFF)  
 264   2               {
 265   3               Timeout2 = 0;
 266   3      
 267   3               do {
 268   4                  RI = 0;
 269   4      
 270   4                  // Wait for USART (with simple timeout)
 271   4                  Timeout1 = 0;
 272   4                  while ((++Timeout1) && (RI == 0));  
 273   4      
 274   4                  if (Timeout1 == 0)
 275   4                     {
 276   5                     // USART did not respond - error
 277   5                     Error_code_G = ERROR_USART_TI;
 278   5                     return;
 279   5                     } 
 280   4      
 281   4                  } while ((++Timeout2) && (SBUF != XON));
 282   3            
 283   3               RI = 0; 
 284   3               }
 285   2           }
 286   1      
 287   1         Timeout1 = 0;
 288   1         while ((++Timeout1) && (TI == 0));  
 289   1      
 290   1         if (Timeout1 == 0)
 291   1            {
 292   2            // USART did not respond - error
 293   2            Error_code_G = ERROR_USART_TI;
 294   2            return;
 295   2            } 
 296   1      
 297   1         TI = 0;
 298   1      
 299   1         SBUF = CHARACTER;
 300   1         }
 301          
 302          
 303          /*------------------------------------------------------------------*-
C51 COMPILER V6.10  PC_IO                                                                  04/18/2001 16:10:03 PAGE 6   

 304            ---- END OF FILE -------------------------------------------------
 305          -*------------------------------------------------------------------*/


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    457    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     62      12
   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 + -