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

📄 serial.c

📁 mcs51,2051,x86系列MCU
💻 C
📖 第 1 页 / 共 2 页
字号:
      str            Address of a zero terminated string to be transmitted
   
   Returns: 
      None

   Assumptions:
      REMAPCFG register has Expanded I/O space access enabled (ESE bit set).
      The processor Port pin are initialized separately.

   Syntax:
   
      #define SIO_0 0
             
      SerialWriteStr (SIO_0,
                   HelloString);       
   
   Real/Protected Mode
      No changes required.

*/


void SerialWriteStr(int Unit, const char far *str)
{     
   WORD TransmitPortAddr;
   WORD StatusPortAddr;

      /* Set Port base, based on serial port used */
   TransmitPortAddr = (Unit ? TBR1 : TBR0);
   StatusPortAddr =  (Unit ? LSR1 : LSR0);
   
   for( ; *str != '\0'; str++)
   {
         /* Wait until buffer is empty */
      while(!(_GetEXRegByte(StatusPortAddr) & SIO_TX_BUF_EMPTY)) ;
         /* Write Character */
      _SetEXRegByte(TransmitPortAddr,*str);
   }
}

/*
   SerialWriteMem:
   
   Description:
      Is a Polled serial port write function that will wait forever or until count characters have been
      written to the serial port.   
   
   Parameters:
      Unit        Unit number of the serial port.  0 for SIO port 0, 1 for SIO port 1.
      mem         Address of a buffer to be transmitted
      count       Number of characters in buffer to be transmitted
   
   Returns: 
      None

   Assumptions:
      REMAPCFG register has Expanded I/O space access enabled (ESE bit set).
      The processor Port pin are initialized separately.

   Syntax:
      
      #define SIO_0 0
      #define COUNT 32       
      
      char Buffer[COUNT];
      
      SerialWriteMem (SIO_0,
                      Buffer,
                      COUNT);
   
   Real/Protected Mode
      No changes required.
            
*/

void SerialWriteMem(int Unit, const char far *mem, int count)
{     
   WORD TransmitPortAddr;
   WORD StatusPortAddr;
   int i;

      /* Set Port base, based on serial port used */
   TransmitPortAddr = (Unit ? TBR1 : TBR0);
   StatusPortAddr =  (Unit ? LSR1 : LSR0);
   
   for(i=0 ; i < count; i++)
   {
         /* Wait until buffer is empty */
      while(!(_GetEXRegByte(StatusPortAddr) & SIO_TX_BUF_EMPTY)) ;
         /* Write Character */
      _SetEXRegByte(TransmitPortAddr,mem[i]);
   }
} /* SerialWriteMem */


/*

      Serial0_ISR:
      
      Description:
         Template Interrupt Service Routine for Serial Port0 interrupts.
         This function identifies the cause of the interrupt and branches 
         to the corresponding action.
         
      Parameters:
         None (Not called by user)
         
      Returns:
         None                     
         
      Assumptions:
         None
         
      Syntax:
         Not a user function.
         
      Real/Protected Mode:
         No changes required.
         
*/

void interrupt far Serial0_ISR (void)
{

 BYTE iir0, lsr0, msr0;
 
 iir0 = _GetEXRegByte(IIR0);
 
 switch ((iir0&0x06) >> 1) {
 
   case 0:
         /* modem status signal */
      
         msr0 = _GetEXRegByte(MSR0);
      
         if ((msr0&0x08) && (msr0&0x80)){
           
        /* data carrier detect has been set */
         }

         if ((msr0 & 0x04) && (msr0 & 0x40)) {
       
        /* ring indicator */
        
         }
      
         if ((msr0 & 0x02) && (msr0 & 0x20)) {
      
        /* data set ready bit has been set */
         }
      
         if ((msr0 & 0x01) && (msr0&0x10)) {
      
        /* clear to send signal has been set */
         }
      
         break;
   case 1:
         Service_TBE(); /* Routine for Interrupt driven Serial Writes */
         break;
   case 2:
         /* RBF signal */
         Service_RBF(); /* Routine specific to RBF generated interrupts */
         break;
   case 3:
         /* receive line status signal */
         
         lsr0 = _GetEXRegByte(LSR0);
         
         if (lsr0 & 0x10) {
           /* break interrupt */
         }
         
         if (lsr0 & 0x08) {
           /* framing error */
         }
         
         if (lsr0 & 0x04) {
           /* parity error */
         }
         
         if (lsr0 & 0x02) {
           /* overrun error */
         }        
         
         break;
   
 }      

 NonSpecificEOI();  /* Send End-Of-Interrupt Signal to Master */
  
}/* Serial0_ISR */



/*

      Service_RBF:
      
      Description:
         Service routine for interrupts generated by RBF signal. This 
         routine is used for Interrupt-Driven Serial Reads. It echos
         the typed character to the screen, stopping when it receives
         an ESC. 
         
      Parameters:
         None
      
      Returns:
         None
   
      Assumptions:
         None
         
      Syntax:
         Not called by user
         
      Real/Protected Mode:
         No changes required.
          
*/
void Service_RBF (void)
{                              

   
   /*  Read in contents of RBR0 */
   rec_buffer = _GetEXRegByte(RBR0);

   SerialWriteChar(SIO_0, rec_buffer); // testing   
   
   if ( rec_buffer == 0x1b ) {
   
      /* ESC character received */
      _SetEXRegByte(IER0, 0x00);
   }
      
} /* Service_RBF */  




/*
 
 Example code to show how to set up for a Serial Port interrupt. This example
 is for an interrupt on SIO_0 sourced by the Receiver Buffer Full Signal.



   SetIRQVector(Serial0_ISR, 4, INTERRUPT_ISR);  // Set vector for Interrupt 
                                                   on Master line 4 
                                                   
   Disable8259Interrupt(IR1+IR5+IR6+IR7, IR0+IR1+IR2+IR3+IR4+IR5+IR6+IR7);
   Enable8259Interrupt(IR2+IR4,0);  // Enable slave interrupt to master(IR2), Enable SIO_0 (IR4) 
   _enable(); // Enable Interrupts           
   
   _SetEXRegByte(IER0, 0x01);  // Enable interrupt on RBF signal     
  
*/


/*

   SerialWriteStr_Int:
      
   Description:
      Is an interrupt driven serial port write function.
      The NUL character ('\0') is used to indicate end of string.    
   
   Parameters:
      Unit        Unit number of the serial port.  0 for SIO port 0, 1 for SIO port 1.
      str         Address of a zero terminated string to be transmitted
   
   Returns: 
      None

   Assumptions:
      REMAPCFG register has Expanded I/O space access enabled (ESE bit set).
      The processor Port pin are initialized separately.

   Syntax:
   
      #define SIO_0 0
             
      SerialWriteStr_Int (SIO_0, HelloString);       
   
   Real/Protected Mode
      No changes required.

*/


void SerialWriteStr_Int(int Unit, const char far *str)
{
   BYTE PortIntEnable;  
   
   PortIntEnable = (Unit ? IER1 : IER0);

   PortIntEnable = PortIntEnable;      /* ???????? */
   
   strcpy (trans_buffer, str);  /* Copy string into buffer */
   Tbuffer_index = 0;
   
   /* Enable TBE interrupts */
   _SetEXRegByte(IER0,0x02);
   
 
   
}/* SerialWriteStr_Int */
                                
                                
                                
/*
                                
   Service_TBE:
   
   Description:
      Service routine for TBE generated interrupts. This function is used
      for Interrupt-Driven Serial Transmits.
      
   Parameters:
      None
      
   Assumptions:
      None
      
   Syntax:
      Not called by user.
      
   Real/Protected Mode:
      No changes required.
                                      
*/

void Service_TBE(void)
{
   if (trans_buffer[Tbuffer_index] != '\0') {
         _SetEXRegByte(TBR0, trans_buffer[Tbuffer_index]);
         Tbuffer_index++;
   }
   else {  
          /* Disable TBE interrupts */  
          _SetEXRegByte(IER0, 0x00); 
                  
   }
} /* Service_TBE */                    

/* APB_BLOCK_END */

⌨️ 快捷键说明

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