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

📄 pc_o.c

📁 使用Keil uVision2下C语言开发的防盗报警系统
💻 C
字号:
/*------------------------------------------------------------------*-

   PC_O.C (v1.00)

  ------------------------------------------------------------------

   Core files for simple write-only PC link library for 8051 family
   [Sends data to PC - cannot receive data from PC]

   Uses the UART, and Pin 3.1 (Tx) 

   See text for details (Chapter 9).

   COPYRIGHT
   ---------

   This code is associated with the book:

   EMBEDDED C by Michael J. Pont 
   [Pearson Education, 2002: ISBN: 0-201-79523-X].

   This code is copyright (c) 2001 by Michael J. Pont.
 
   See book for copyright details and other information.

-*------------------------------------------------------------------*/

#include "Main.h"
#include "PC_O.h"

// ------ Public variable definitions ------------------------------

tByte Out_written_index_G;  // Data in buffer that has been written 
tByte Out_waiting_index_G;  // Data in buffer not yet written

// ------ Private function prototypes ------------------------------

static void PC_LINK_O_Send_Char(const char);

// ------ Private constants ----------------------------------------

// The transmit buffer length
#define TRAN_BUFFER_LENGTH 20

// ------ Private variables ----------------------------------------

static tByte Tran_buffer[TRAN_BUFFER_LENGTH];

static tByte Time_count_G = 0;


/*------------------------------------------------------------------*-

  PC_LINK_O_Update()

  Sends next character from the software transmit buffer

  NOTE: Output-only library (Cannot receive chars)

  Uses on-chip UART hardware.

-*------------------------------------------------------------------*/
void PC_LINK_O_Update(void)
   {
   // Deal with transmit bytes here
   //
   // Are there any data ready to send?
   if (Out_written_index_G < Out_waiting_index_G)
      {
      PC_LINK_O_Send_Char(Tran_buffer[Out_written_index_G]);     

      Out_written_index_G++;
      }
   else
      {
      // No data to send - just reset the buffer index
      Out_waiting_index_G = 0;
      Out_written_index_G = 0;
      }
   }

/*------------------------------------------------------------------*-

  PC_LINK_O_Write_String_To_Buffer()

  Copies a (null terminated) string to the character buffer.  
  (The contents of the buffer are then passed over the serial link)

-*------------------------------------------------------------------*/
void PC_LINK_O_Write_String_To_Buffer(const char* const STR_PTR)
   {
   tByte i = 0;

   while (STR_PTR[i] != '\0')
      {
      PC_LINK_O_Write_Char_To_Buffer(STR_PTR[i]);
      i++;
      }
   }

/*------------------------------------------------------------------*-

  PC_LINK_O_Write_Char_To_Buffer()

  Stores a character in the 'write' buffer, ready for 
  later transmission
 
-*------------------------------------------------------------------*/
void PC_LINK_O_Write_Char_To_Buffer(const char CHARACTER)
   {
   // Write to the buffer *only* if there is space
   // (No error reporting in this simple library...)
   if (Out_waiting_index_G < TRAN_BUFFER_LENGTH)
      {
      Tran_buffer[Out_waiting_index_G] = CHARACTER;
      Out_waiting_index_G++;     
      }
   }

/*------------------------------------------------------------------*-

  PC_LINK_O_Send_Char()

  Based on Keil sample code, with added (loop) timeouts.
  Implements Xon / Off control.

  Uses on-chip UART hardware.

-*------------------------------------------------------------------*/
void PC_LINK_O_Send_Char(const char CHARACTER)
   {
   tLong Timeout1 = 0;

   if (CHARACTER == '\n')  
      {
      Timeout1 = 0;
      while ((++Timeout1) && (TI == 0));  

      if (Timeout1 == 0)
         {
         // UART did not respond - error
         // No error reporting in this simple library...
         return;
         } 

      TI = 0;
      SBUF = 0x0D;  // Output CR  
      }
  
   Timeout1 = 0;
   while ((++Timeout1) && (TI == 0));  

   if (Timeout1 == 0)
      {
      // UART did not respond - error
      // No error reporting in this simple library...
      return;
      } 

   TI = 0;

   SBUF = CHARACTER;
   }

/*------------------------------------------------------------------*-
  ---- END OF FILE -------------------------------------------------
-*------------------------------------------------------------------*/

⌨️ 快捷键说明

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