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

📄 f360_cc1100.c

📁 使用C8051F360高速单片机加无线收发芯片CC1100实现的无线语音通信的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
               if((READREG(STATUS) & 0x10) != 0)
               {
                   SETREG(PA_POWER,0xFF);

                   RXTX_StateMachine = TX_SwitchTime;

                   SPI_TX();           // set MISO pin to push-pull
               }
               else
               {
                  RXTX_StateMachine = Switch_ToTX02;
               }
          }
          break;

          case(Switch_ToRX01):

             SPI0DAT = 0xFF;
             _nop_();

             if(TXBMT) SPI0DAT = 0xFF;

             // set RF transceiver to lowest output power setting
             SETREG(PA_POWER, 0x00);

             // configure RF transceiver to RX mode
             SETREG(MAIN,0x01);
             RXTX_StateMachine = Switch_ToRX02;

          break;

          case(Switch_ToRX02):

            SPI0DAT = 0xFF;
            if(SPI_Timer >= SPI_CalibrationWaitTime)
            {
               if((READREG(STATUS) & 0x10) != 0)
               {
                  // route antenna to the input pin of the RF transceiver
                  SETREG(INTERFACE,I_RX);

                  RXTX_StateMachine = RX_Preamble;

                  SPI_RX();            // disconnect MISO pin by configuring it
                                       // to analog input
               }
            }
            else
            {
               RXTX_StateMachine = Switch_ToRX02;
            }

            break;

      }// end switch(RXTX_StateMachine)

   }// end if(SPIF)



   // this section of code increments the SPI_Timer, if enabled
   if(SPI_TimerEnable == TRUE)
   {
      SPI_Timer++;

      if(SPI_Timer == SPI_PacketTime)
      {
        SPI_TimeOutEvent = TRUE;
        SPI_Timer = 0;
      }
   }
   else
   {
      SPI_Timer = 0;
      SPI_TimeOutEvent = FALSE;
   }
}

//-----------------------------------------------------------------------------
// RF State Machine Functions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// RXTX_InitMaster(void)
//-----------------------------------------------------------------------------
//
// This function switches the RF transceiver to transmit and sets variables
// to indicate that this endpoint is designated as master.
//
void RXTX_InitMaster(void)
{

   SPIEN = 0;                          // disable SPI
   SPIF = 0;                           // clear any pending interrupt flag

   CC1100_SwitchToTX();                // configure system to transmit

   RXTX_ResetVariables = TRUE;         // signal RF state machine to
                                       // reset variables

   RXTX_StateMachine = TX_InitPreamble;// set state machine to transmit
                                       // initial preamble

   RXTX_MasterSelect = RXTX_Master;    // designate endpoint as master

   SPIEN = 1;                          // re-enable SPI

   // pad SPI0DAT with known values to be shifted out
   SPI0DAT = 0x00;

   _nop_();

   if(TXBMT) SPI0DAT = 0x00;


}

//-----------------------------------------------------------------------------
// RXTX_InitSlave(void)
//-----------------------------------------------------------------------------
//
// This function switches the RF transceiver to receive and sets variables
// to indicate that the endpoint is designated as slave.
//

void RXTX_InitSlave(void)
{

   SPIEN = 0;                          // disable SPI
   SPIF = 0;                           // clear any pending interrupt flags

   CC1100_SwitchToRX();                // configure system to receive

   RXTX_RunInitSlave = FALSE;          // clear signal flag

   RXTX_ResetVariables = TRUE;         // signal state machine to reset
                                       // variables

   // set RF state machine to search for a transmitting master endpoint
   RXTX_StateMachine = RX_SearchForMaster;

   RXTX_MasterSelect = RXTX_Searching; // designate endpoint as neither master
                                       // or slave

   SPIEN = 1;                          // re-enable SPI

   SPI0DAT = 0x00;                     // pad SPI0DAT with known values

   _nop_();

   if(TXBMT) SPI0DAT = 0x00;

}


//-----------------------------------------------------------------------------
// CC1100 Functions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// SETREG
//-----------------------------------------------------------------------------
// Writing registers in the CC1100 transceiver is accomplished using the
// SPI interface.  Two bytes are sent, an address byte and a data byte.
// First PALE is driven low.  The address byte contains the 7-bit address,
// and then a logic '1', signifying "write mode".  Then PALE is brought
// high and the data byte is transmitted.
//
void SETREG(unsigned char address, unsigned char value)
{
   unsigned char output,count;


   output = address << 1 | 0x01;       // 7 address bits + WRITE

   PCLK = 0;

   PALE = 0;                           // selects the RF transceiver


   // bit-banged interface sends the register address to the RF transceiver
   for (count = 0;count<8;count++)
   {
      PCLK = 0;                        // data is clocked into the RF
                                       // transceiver on the rising edge
                                       // of the clock

      if(output & 0x80) PDI = 1;       // set or clear the config data line
      else PDI = 0;                    // to transmit data MSB first

      PCLK = 1;                        // clock in the data bit
      output = output<<1;              // shift to read the next data bit
   }

   output = value;                     // prepare to shift out new register
                                       // value

   for (count = 0;count<8;count++)
   {
      PCLK = 0;

      if(output & 0x80) PDI = 1;       // set or clear config data line
      else PDI = 0;                    // with data out MSB first

      PCLK = 1;                        // clock out data

      output = output<<1;              // shift to prepare next bit's output
   }

   PCLK = 0;


   PALE = 1;                           // deselect the RF transceiver

}


//-----------------------------------------------------------------------------
// READREG
//-----------------------------------------------------------------------------
// Reading registers is accomplished by sending the address of the
// register to be read and then receiving a data byte in return.
// PALE is driven low, and a byte consisting of the 7-bit address
// plus a bit set to '0' signifying "read mode" is sent.  PALE is
// then raised high and a data byte is read in through SPI.
//
unsigned char READREG(unsigned char address)
{

   unsigned char output,input,count;

   output = address<<1;                // 7 address bits + READ

   PCLK = 0;

   PALE = 0;                           // select the RF transceiver

   for(count = 0;count<8;count++)
   {
      PCLK = 0;

      if(output & 0x80) PDI = 1;       // shift out register address
      else PDI = 0;                    // MSB first


      PCLK = 1;

      output = output<<1;              // prepare next bit to shift out
   }

   for(count = 0;count<8;count++)
   {
      PCLK = 0;

      _nop_();
      _nop_();
      _nop_();
      _nop_();

      PCLK = 1;                        // clock in register value MSB first

      // save bit of register value
      if(PDO == 1)
      {
         input = (input<<1) | 1;
      }
      else
      {
         input<<=1;
      }
   }

   PCLK = 0;

   PALE = 1;                           // deselect the RF transceiver

   return input;

}

//-----------------------------------------------------------------------------
// CC1100_Init
//-----------------------------------------------------------------------------
// This function will initialize the transceiver and calibrate it to
// transmit and receive on a defined frequency.
//
unsigned char cal_complete;

unsigned int idata count;

unsigned char CC1100_Init(void)
{
   unsigned char temp_reg;
   unsigned char RegAddress;

   if(CC1100_StartUpCall)
   {
      SETREG(MAIN,0x0E);
      SETREG(MAIN,0x0F);
      SETREG(INTERFACE,I_RX);
      SETREG(RESETT,0xFF);
      SETREG(SEQUENCING,0x8F);


   }

      // Configure receive frequency of 908 MHz
      SETREG(FREQ_2A,0x3c);
      SETREG(FREQ_1A,0xCE);
      SETREG(FREQ_0A,0x8E);

      // Configure transmit frequency of 908 MHz
      SETREG(FREQ_2B,0x3c);
      SETREG(FREQ_1B,0xd3);
      SETREG(FREQ_0B,0xe3);

   if(CC1100_StartUpCall)
   {

      // CLOCK_A is not located in a space contiguous with the
      // other registers to be programmed
      SETREG(CLOCK_A,0x25);

      // this loop writes to each register of the CC1100 a
      // corresponding value found in RegValue
      for(RegAddress = CLOCK_B;RegAddress != TEST7 + 1;)
      {
         temp_reg = RegValue[RegAddress - CLOCK_B];
         SETREG(RegAddress,RegValue[RegAddress - CLOCK_B]);
         RegAddress++;
      }

      SETREG(MAIN,0x0B);
      WaitMS_360 (5*200);                  // wait for oscillator to stabilize
      SETREG(MAIN,0x09);
      WaitMS_360 (1*200);                  // wait for bias generator to stabilize
      SETREG(MAIN,0x01);


   }


   // Calibration Routine
   count = 0;                          // reset counter
   cal_complete = 0;                   // clear temporary storage variable
   SETREG(MAIN,0x11);
   SETREG(CALIBRATE,0x85);

   // check for calibration complete
   do
   {
      count++;
      cal_complete = READREG(STATUS);
   } while (!(cal_complete & 0x80) && (count != 0xFFFF));

   // if the count is too high or too low, an error occurred during
   // calibration, exit the initialization and re-run
   if(!(cal_complete & 0x80))
   {
      return 0;
   }

   // now check for lock
   count = 0;                          // reset counter
   do
   {
      count++;                         // increment counter
      cal_complete = READREG (STATUS);
      WaitUS(1);
      // spin until a lock is detected
   } while (!(cal_complete & 0x10) && (count != 0xFFFE));


   // if count timed out, exit function
   if (!(cal_complete & 0x10))
   {//|| (count < 2)) {
      return 0;
   }



   count = 0;                          // reset counter
   SETREG(MAIN,0xD1);

   SETREG(CALIBRATE,0x85);
   do
   {
      count++;          // increment counter
      cal_complete = READREG(STATUS);
   } while (!(cal_complete & 0x80) && (count != 0xFFFF));

   // if the count is too high or too low, an error occurred during
   // calibration, exit the initialization and re-run
   if((count < 10) || (!(cal_complete & 0x80)))
   {
      return 0;
   }


   // now check for lock
   count = 

⌨️ 快捷键说明

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