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

📄 interrupt.c

📁 里面包括RS232和IIC对24LC256的读写代码 CYGNAL IDE打开项目
💻 C
字号:
#define INT_ISR_C


#include "c8051F330.h"
#include "rs232.h"


char rece,count_UART,count_UART1;
char Rec_OK,Tran_OK;
char Tran_Flag;
char xdata sRecComBuf[8];
char *pointer;
void UART_Isr(void)  interrupt 4
{
	ES0 = 0x0;
	if(TI0)
		{
			SCON0 &=0xFD;
			Tran_Flag=0;
			if (count_UART1==8)
				Tran_OK=0x45;
				
		}
 	else
		{
			SCON0 &=0xFE;
			*pointer++ = SBUF0;
			count_UART++;
			if (count_UART==8)
			{
				Rec_OK=0x23;
			}
			
		}
	ES0 = 0x1;	 
} 





//------------------------------------------------------------------------------------
// Timer3 Interrupt Service Routine (ISR)
//------------------------------------------------------------------------------------
//
// A Timer3 interrupt indicates an SMBus SCL low timeout.
// The SMBus is disabled and re-enabled here
//
void Timer3_ISR (void) interrupt 14
{
   SMB0CF &= ~0x80;          // Disable SMBus
   SMB0CF |= 0x80;           // Re-enable SMBus
   TMR3CN &= ~0x80;          // Clear Timer3 interrupt-pending flag
}






//------------------------------------------------------------------------------------
// SMBus Interrupt Service Routine (ISR)
//------------------------------------------------------------------------------------
//
// SMBus ISR state machine
// - Master only implementation - no slave or arbitration states defined
// - All incoming data is written starting at the global pointer <pSMB_DATA_IN>
// - All outgoing data is read from the global pointer <pSMB_DATA_OUT>
//
void SMBus_ISR (void) interrupt 7
{
   bit FAIL = 0;                             // Used by the ISR to flag failed
                                             // transfers

   static char i;                            // Used by the ISR to count the
                                             // number of data bytes sent or
                                             // received

   static bit SEND_START = 0;                // Send a start

   switch (SMB0CN & 0xF0)                    // Status vector
   {
      // Master Transmitter/Receiver: START condition transmitted.
      case SMB_MTSTA:
         SMB0DAT = TARGET;                   // Load address of the target slave
         SMB0DAT |= SMB_RW;                  // Load R/W bit
         STA = 0;                            // Manually clear START bit
         i = 0;                              // reset data byte counter
         break;

      // Master Transmitter: Data byte (or Slave Address) transmitted
      case SMB_MTDB:
         if (ACK)                            // Slave Address or Data Byte
         {                                   // Acknowledged?
            if (SEND_START)
            {
               STA = 1;
               SEND_START = 0;
               break;
            }
            if(SMB_SENDWORDADDR)             // Are we sending the word address?
            {
               SMB_SENDWORDADDR = 0;         // Clear flag
               SMB0DAT = WORD_ADDR;          // send word's high 8bits address

              /* if (SMB_RANDOMREAD)
               {
                  SEND_START = 1;            // send a START after the next ACK cycle
                  SMB_RW = READ;
               }*/

               break;
            }

		else if(SMB_SENDWORDADDR1)             // Are we sending the word address?
            {
               SMB_SENDWORDADDR1 = 0;         // Clear flag
               SMB0DAT = WORD_ADDR1;          // send word's low 8bits address

               if (SMB_RANDOMREAD)
               {
                  SEND_START = 1;            // send a START after the next ACK cycle
                  SMB_RW = READ;
               }

               break;
            }

            if (SMB_RW==WRITE)               // Is this transfer a WRITE?
            {

               if (i < SMB_DATA_LEN)         // Is there data to send?
               {
                  SMB0DAT = *pSMB_DATA_OUT;  // send data byte
                  pSMB_DATA_OUT++;           // increment data out pointer
                  i++;                       // increment number of bytes sent
               }
               else
               {
                 STO = 1;                    // set STO to terminte transfer
                 SMB_BUSY = 0;               // clear software busy flag
               }
            }
            else {}                          // If this transfer is a READ,
                                             // then take no action. Slave
                                             // address was transmitted. A
                                             // separate 'case' is defined
                                             // for data byte recieved.
         }
         else                                // If slave NACK,
         {
            if(SMB_ACKPOLL)
            {
               STA = 1;                      // Restart transfer
            }
            else
            {
               FAIL = 1;                     // Indicate failed transfer
            }                                // and handle at end of ISR
         }
         break;

      // Master Receiver: byte received
      case SMB_MRDB:
         if ( i < SMB_DATA_LEN )             // Is there any data remaining?
         {
            *pSMB_DATA_IN = SMB0DAT;         // Store received byte
            pSMB_DATA_IN++;                  // Increment data in pointer
            i++;                             // Increment number of bytes received
            ACK = 1;                         // Set ACK bit (may be cleared later
                                             // in the code)

         }

         if (i == SMB_DATA_LEN)              // This is the last byte
         {
            SMB_BUSY = 0;                    // Free SMBus interface
            ACK = 0;                         // Send NACK to indicate last byte
                                             // of this transfer
            STO = 1;                         // Send STOP to terminate transfer
         }

         break;

      default:
         FAIL = 1;                           // Indicate failed transfer
                                             // and handle at end of ISR
         break;
   }

   if (FAIL)                                 // If the transfer failed,
   {
      SMB0CN &= ~0x40;                       // Reset communication
      SMB0CN |= 0x40;
      SMB_BUSY = 0;                          // Free SMBus
   }

   SI=0;                                     // clear interrupt flag
}



//------------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------
// EEPROM_ByteWrite ()
//------------------------------------------------------------------------------------
//
// This function writes the value in <dat> to location <addr> in the EEPROM then 
// polls the EEPROM until the write is complete.
//
void EEPROM_ByteWrite( unsigned int addr, unsigned char dat )
{
   while (SMB_BUSY);                         // Wait for SMBus to be free.
   SMB_BUSY = 1;                             // Claim SMBus (set to busy)

   // Set SMBus ISR parameters
   TARGET = EEPROM_ADDR;                     // Set target slave address
   SMB_RW = WRITE;                           // Mark next transfer as a write
   SMB_SENDWORDADDR = 1;                     // Send high 8bits Word Address after Slave Address
   SMB_SENDWORDADDR1 = 1;                     // Send low 8bits Word Address after Slave Address
   SMB_RANDOMREAD = 0;                       // Do not send a START signal after
                                             // the word address
   SMB_ACKPOLL = 1;                          // Enable Acknowledge Polling (The ISR
                                             // will automatically restart the 
                                             // transfer if the slave does not 
                                             // acknoledge its address.

   // Specify the Outgoing Data
   WORD_ADDR = addr/256;                         // Set the target address in the 
                                             // EEPROM's internal memory space

   WORD_ADDR1 = addr%256;                         // Set the target address in the 
                                             // EEPROM's internal memory space
											 
   SMB_SINGLEBYTE_OUT = dat;                 // store dat (local variable) in a global
                                             // variable so the ISR can read it after
                                             // this function exits

   pSMB_DATA_OUT = &SMB_SINGLEBYTE_OUT;      // The outgoing data pointer points to
                                             // the <dat> variable.

   SMB_DATA_LEN = 1;                         // Specify to ISR that the next transfer
                                             // will contain one data byte

   // Initiate SMBus Transfer
   STA = 1;

}





//------------------------------------------------------------------------------------
// EEPROM_ByteRead ()
//------------------------------------------------------------------------------------
//
// This function returns a single byte from location <addr> in the EEPROM then 
// polls the <SMB_BUSY> flag until the read is complete.
//
unsigned char EEPROM_ByteRead( unsigned int addr)
{
   unsigned char retval;                     // Holds the return value

   while (SMB_BUSY);                         // Wait for SMBus to be free.
   SMB_BUSY = 1;                             // Claim SMBus (set to busy)

   // Set SMBus ISR parameters
   TARGET = EEPROM_ADDR;                     // Set target slave address
   SMB_RW = WRITE;                           // A random read starts as a write
                                             // then changes to a read after
                                             // the repeated start is sent. The
                                             // ISR handles this switchover if
                                             // the <SMB_RANDOMREAD> bit is set.
   SMB_SENDWORDADDR = 1;                     // Send high 8bits Word Address after Slave Address
   SMB_SENDWORDADDR1 = 1;                     // Send low 8bits Word Address after Slave Address
   SMB_RANDOMREAD = 1;                       // Send a START after the word address
   SMB_ACKPOLL = 1;                          // Enable Acknowledge Polling


   // Specify the Incoming Data
   WORD_ADDR = addr/256;                         // Set the target address in the 
                                             // EEPROM's internal memory space

   WORD_ADDR1 = addr%256;                         // Set the target address in the 
                                             // EEPROM's internal memory space

   pSMB_DATA_IN = &retval;                   // The incoming data pointer points to
                                             // the <retval> variable.

   SMB_DATA_LEN = 1;                         // Specify to ISR that the next transfer
                                             // will contain one data byte

   // Initiate SMBus Transfer
   STA = 1;
   while(SMB_BUSY);                          // Wait until data is read

   return retval;

}

⌨️ 快捷键说明

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