pc_io_t1.c

来自「Embedded C 这本书的范例光碟程式」· C语言 代码 · 共 81 行

C
81
字号
/*------------------------------------------------------------------*-

   PC_IO_T1.C (v1.00)

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

   PC link library. Bidirectional. T1 used for baud rate generation.

   Uses the UART, and Pins 3.1 (Tx) and 3.0 (Rx) 

   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_IO_T1.h"

// ------ Public variable declarations -----------------------------

extern tByte In_read_index_G;     
extern tByte In_waiting_index_G;  

extern tByte Out_written_index_G;  
extern tByte Out_waiting_index_G;  

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

  PC_LINK_IO_Init_T1()

  This (generic) version uses T1 for baud rate generation.

-*------------------------------------------------------------------*/
void PC_LINK_IO_Init_T1(const tWord BAUD_RATE)
   {
   PCON &= 0x7F;   // Set SMOD bit to 0 (don't double baud rates)

   //  Receiver enabled.
   //  8-bit data, 1 start bit, 1 stop bit, 
   //  Variable baud rate (asynchronous)
   //  Receive flag will only be set if a valid stop bit is received
   //  Set TI (transmit buffer is empty)
   SCON = 0x72;

   TMOD |= 0x20;   // T1 in mode 2, 8-bit auto reload

   TH1 = (256 - (tByte)((((tLong)OSC_FREQ / 100) * 3125) 
            / ((tLong) BAUD_RATE * OSC_PER_INST * 1000)));

   TL1 = TH1;  
   TR1 = 1;  // Run the timer
   TI = 1;   // Send first character (dummy)

   // Set up the buffers for reading and writing
   In_read_index_G = 0;
   In_waiting_index_G = 0;
   Out_written_index_G = 0;
   Out_waiting_index_G = 0;

   // Interrupt *NOT* enabled
   ES = 0;
   }

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



⌨️ 快捷键说明

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