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

📄 f02x_spi0_master.c

📁 芯科原厂所有c8051fxx程序的例子。
💻 C
📖 第 1 页 / 共 2 页
字号:
         Error_Flag = 1;
      }

      // When the Master first enters the ISR, the SPIF flag should be set from
      // sending the Command byte.  This ISR handles the remaining steps of the
      // SPI transfer process.
      // <state> == 0: writing or reading 1 byte of data
      // <state> == 1: for READ commands (first time, only a dummy byte is
      //               sent but the second time, the data must be read from
      //               SPI0DAT)
      // <state> == 2: NSS = 1 to end the transfer, final byte read
      if (state == 0)
      {
         switch (Command)
         {
            case SLAVE_LED_ON:
            case SLAVE_LED_OFF:
               // Not expecting any data back

               Transfer_In_Progress = 0; // Allow a new transfer to begin

               break;

            case SPI_WRITE:
               SLAVE_SEL = 0;          // Reselect the slave

               SPI0DAT = SPI_Data;

               state = 2;              // Advance to the final state (only
                                       // writing one byte)

               break;

            case SPI_READ:
               SLAVE_SEL = 0;          // Reselect the slave

               SPI0DAT = 0xFF;         // Send a dummy byte so the Slave can
                                       // send the data

               state = 2;              // Advance to the final state (only
                                       // reading one byte)

               break;

            case SPI_WRITE_BUFFER:
               array_index = 0;        // Clear the data counter

               SLAVE_SEL = 0;          // Reselect the slave

               SPI0DAT = SPI_Data_Array[array_index]; // Write the bytes in
                                       // the array to the Slave

               array_index++;

               state = 1;

               break;

            case SPI_READ_BUFFER:
               array_index = 0;        // Clear the data counter

               SLAVE_SEL = 0;          // Reselect the slave

               SPI0DAT = 0xFF;         // Send a dummy byte so the Slave can
                                       // start sending the data

               state = 1;              // Advance to the next state where the
                                       // data can be received
                                       // The data from the slave is not
                                       // available until after the second
                                       // transfer is completed.
                                       // The dummy byte allows the slave to
                                       // send data, since the Master controls
                                       // SCK.

               break;

            default:
               state = 2;              // Any errors in the Command parsing
                                       // should go to state 2 where NSSMD0
                                       // is de-asserted
         }
      }
      else if (state == 1)             // This state is for ARRAY
      {                                // commands.
                                       // For READ_ARRAY, the data must be read
                                       // after the first dummy byte is sent.
                                       // For WRITE_ARRAY, the data counter
                                       // must be cleared before sending the
                                       // first byte.
         switch (Command)
         {
            case SPI_WRITE_BUFFER:
               SLAVE_SEL = 0;          // Reselect the slave

               SPI0DAT = SPI_Data_Array[array_index]; // Write the bytes in
                                       // the array to the Slave

               array_index++;

               if (array_index == MAX_BUFFER_SIZE)
               {
                  state = 2;           // Advance to the final state when
                                       // all bytes are written
               }

               break;

            case SPI_READ_BUFFER:
               SPI_Data_Array[array_index] = SPI0DAT;

               SLAVE_SEL = 0;          // Reselect the slave

               SPI0DAT = 0xFF;

               array_index++;

               if (array_index == (MAX_BUFFER_SIZE-1))
               {
                  state = 2;
               }

               break;
            default:
               state = 2;              // Any errors in the Command parsing
                                       // should go to state 2 where NSSMD0
                                       // is de-asserted
         }
      }
      else if (state == 2)
      {
         switch (Command)
         {
            case SPI_READ:
               SPI_Data = SPI0DAT;     // Read the data from the slave

               break;

            case SPI_READ_BUFFER:
               SPI_Data_Array[array_index] = SPI0DAT; // Read the last data
                                                      // without sending a
                                                      // dummy byte

               break;
         }

         Transfer_In_Progress = 0;     // Allow a new transfer to begin

         state = 0;                    // Reset the state
      }

      SPIF = 0;                        // Clear the SPIF flag
   }
}

//-----------------------------------------------------------------------------
// Support Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// SPI_LED_On
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Turns the LED on the SPI Slave on.  The slave does not respond to this
// command, so the command consists of:
//
// Command = SLAVE_LED_ON
// Length = 1 byte (the command itself)
//
//-----------------------------------------------------------------------------
void SPI_LED_On (void)
{
   while (Transfer_In_Progress);       // Wait until the SPI is free, in case
                                       // it's already busy

   Transfer_In_Progress = 1;           // Indicate a transfer is ongoing

   SLAVE_SEL = 0;                      // Select the slave

   Command = SLAVE_LED_ON;

   SPI0DAT = Command;

   // The rest of this command will be handled by the SPI ISR, which will
   // trigger when SPIF is set from sending the Command
}

//-----------------------------------------------------------------------------
// SPI_LED_Off
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Turns the LED on the SPI Slave off.  The slave does not respond to this
// command, so the command consists of:
//
// Command = SLAVE_LED_OFF
// Length = 1 byte (the command itself)
//
//-----------------------------------------------------------------------------
void SPI_LED_Off (void)
{
   while (Transfer_In_Progress);       // Wait until the SPI is free, in case
                                       // it's already busy

   Transfer_In_Progress = 1;           // Indicate a transfer is ongoing

   SLAVE_SEL = 0;                      // Select the slave

   Command = SLAVE_LED_OFF;

   SPI0DAT = Command;

   // The rest of this command will be handled by the SPI ISR, which will
   // trigger when SPIF is set from sending the Command
}

//-----------------------------------------------------------------------------
// SPI_Byte_Write
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Note: SPI_Data must contain the data to be sent before calling this
// function.
//
// Writes a single byte to the SPI Slave.  The slave does not respond to this
// command, so the command consists of:
//
// Command = SPI_WRITE
// Length = 1 byte of command, 1 byte of data
//
//-----------------------------------------------------------------------------
void SPI_Byte_Write (void)
{
   while (Transfer_In_Progress);       // Wait until the SPI is free, in case
                                       // it's already busy

   Transfer_In_Progress = 1;           // Indicate a transfer is ongoing

   SLAVE_SEL = 0;                      // Select the slave

   Command = SPI_WRITE;

   SPI0DAT = Command;

   // The rest of this command will be handled by the SPI ISR, which will
   // trigger when SPIF is set from sending the Command
}

//-----------------------------------------------------------------------------
// SPI_Byte_Read
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Note: SPI_Data will contain the data received after calling this function.
//
// Reads a single byte from the SPI Slave.  The command consists of:
//
// Command = SPI_READ
// Length = 1 byte of command, 1 byte of data
//
//-----------------------------------------------------------------------------
void SPI_Byte_Read (void)
{
   while (Transfer_In_Progress);       // Wait until the SPI is free, in case
                                       // it's already busy

   Transfer_In_Progress = 1;           // Indicate a transfer is ongoing

   SLAVE_SEL = 0;                      // Select the slave

   Command = SPI_READ;

   SPI0DAT = Command;

   // The rest of this command will be handled by the SPI ISR, which will
   // trigger when SPIF is set from sending the Command
}

//-----------------------------------------------------------------------------
// SPI_Array_Write
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Note: SPI_Data_Array must contain the data to be sent before calling this
// function.
//
// Writes an array of values of size MAX_BUFFER_SIZE to the SPI Slave.  The
// command consists of:
//
// Command = SPI_WRITE_BUFFER
// Length = 1 byte of command, MAX_BUFFER_SIZE bytes of data
//
//-----------------------------------------------------------------------------
void SPI_Array_Write (void)
{
   while (Transfer_In_Progress);       // Wait until the SPI is free, in case
                                       // it's already busy

   Transfer_In_Progress = 1;           // Indicate a transfer is ongoing

   SLAVE_SEL = 0;                      // Select the slave

   Command = SPI_WRITE_BUFFER;

   SPI0DAT = Command;

   // The rest of this command will be handled by the SPI ISR, which will
   // trigger when SPIF is set from sending the Command
}

//-----------------------------------------------------------------------------
// SPI_Array_Read
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Note: SPI_Data_Array will contain the data received after calling this
// function.
//
// Reads a single byte from the SPI Slave.  The command consists of:
//
// Command = SPI_READ_BUFFER
// Length = 1 byte of command, MAX_BUFFER_SIZE bytes of data
//
//-----------------------------------------------------------------------------
void SPI_Array_Read (void)
{
   while (Transfer_In_Progress);       // Wait until the SPI is free, in case
                                       // it's already busy

   Transfer_In_Progress = 1;           // Indicate a transfer is ongoing

   SLAVE_SEL = 0;                      // Select the slave

   Command = SPI_READ_BUFFER;

   SPI0DAT = Command;

   // The rest of this command will be handled by the SPI ISR, which will
   // trigger when SPIF is set from sending the Command
}

//-----------------------------------------------------------------------------
// Delay
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Delay for little while (used for blinking the LEDs)
//
//-----------------------------------------------------------------------------
void Delay (void)
{
   unsigned long count;

   for (count = 200000; count > 0; count--);
}

//-----------------------------------------------------------------------------
// Short_Delay
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Delay for little while (used between transfers with the Slave)
//
//-----------------------------------------------------------------------------
void Short_Delay (void)
{
   unsigned char count;

   for (count = 200; count > 0; count--);
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

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