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

📄 cc1020pic.c

📁 CC1020的源代码,详细介绍CC1020的配置和开发,
💻 C
📖 第 1 页 / 共 2 页
字号:
  char Byte;

  PSEL=1;
  
  Byte=addr<<1;
  PSEL=0;
    
  // Send address bits
  for (BitCounter=0;BitCounter<7;BitCounter++)
  {
    PCLK=0;
    PDI=((Byte&0x80)>>7);
    Byte=Byte<<1;
    PCLK=1;
  }
  // Send read/write bit 
  // Ignore bit in data, always use 0 
  
  PCLK=0;
  PDI=0;
  PCLK=1;
 
  PCLK=0;
    
  // Receive data bits 
  
  PDI=1;
   
  TRISC|=0x20; // Set up PDATA as an input
    
  for (BitCounter=0;BitCounter<8;BitCounter++)
  {
    PCLK=1;
    Byte=Byte<<1;
    Byte=Byte|PDO;
    PCLK=0;
  }
  
  TRISC&=~0x20; // Set up PDATA as an output again
  
  PSEL=1;

  return Byte;
}

#endif
  
/****************************************************************************/
/*  This routine resets the CC1020, clearing all registers.                 */
/****************************************************************************/  

void ResetCC1020(void)
{
  // Reset CC1020
  WriteToCC1020Register(CC1020_MAIN, 0x0F&~0x01);

  // Bring CC1020 out of reset
  WriteToCC1020Register(CC1020_MAIN, 0x1F);
}


/****************************************************************************/
/*  This routine calibrates the CC1020                                      */
/*  Returns 0 if calibration fails, non-zero otherwise. Checks the LOCK     */
/*  to check for success.                                                   */
/****************************************************************************/

char CalibrateCC1020(char PA_POWER)
{
  volatile int TimeOutCounter;

  // Turn off PA to avoid spurs during calibration in TX mode
  WriteToCC1020Register(CC1020_PA_POWER,0x00);

  // Start calibration
  WriteToCC1020Register(CC1020_CALIBRATE,0xB5);
  
  // Monitor calibration
  for(TimeOutCounter=CAL_TIMEOUT; ((ReadFromCC1020Register(CC1020_STATUS)&0x80)==0)&&(TimeOutCounter>0); TimeOutCounter--);

  // Monitor lock
  for(TimeOutCounter=LOCK_TIMEOUT; ((ReadFromCC1020Register(CC1020_STATUS)&0x10)==0)&&(TimeOutCounter>0); TimeOutCounter--);

  // Restore PA setting
  WriteToCC1020Register(CC1020_PA_POWER, PA_POWER);

  // Return state of LOCK_CONTINUOUS bit
  return ((ReadFromCC1020Register(CC1020_STATUS)&0x10)==0x10);
}

/****************************************************************************/
/*  This routine puts the CC1020 into RX mode (from TX). When switching to  */
/*  RX from PD, use WakeupC1020ToRX first                                   */
/****************************************************************************/

char SetupCC1020RX(char RXANALOG, char PA_POWER)
{
  volatile int TimeOutCounter;
  char lock_status;

  // Switch into RX, switch to freq. reg A
  WriteToCC1020Register(CC1020_MAIN,0x11);

  // Setup bias current adjustment
  WriteToCC1020Register(CC1020_ANALOG,RXANALOG);

  // Monitor LOCK
  for(TimeOutCounter=LOCK_TIMEOUT; ((ReadFromCC1020Register(CC1020_STATUS)&0x10)==0)&&(TimeOutCounter>0); TimeOutCounter--);

  // If PLL in lock
  if((ReadFromCC1020Register(CC1020_STATUS)&0x10)==0x10){
    // Indicate PLL in LOCK
    lock_status = LOCK_OK;
  // Else (PLL out of LOCK)
  }else{
    // If recalibration ok
    if(CalibrateCC1020(PA_POWER)){
      // Indicate PLL in LOCK
      lock_status = LOCK_RECAL_OK;
    // Else (recalibration failed)
    }else{
      // Indicate PLL out of LOCK
      lock_status = LOCK_NOK;
    }
  }

  // Switch RX part of CC1020 on
  WriteToCC1020Register(CC1020_MAIN,0x01);

  // Return LOCK status to application
  return (lock_status);
}

/****************************************************************************/
/*  This routine puts the CC1020 into TX mode (from RX). When switching to  */
/*  TX from PD, use WakeupCC1020ToTX first                                  */
/****************************************************************************/

char SetupCC1020TX(char TXANALOG, char PA_POWER)
{
  volatile int TimeOutCounter;
  char lock_status;

  // Turn off PA to avoid frequency splatter
  WriteToCC1020Register(CC1020_PA_POWER,0x00);

  // Setup bias current adjustment
  WriteToCC1020Register(CC1020_ANALOG,TXANALOG);

  // Switch into TX, switch to freq. reg B
  WriteToCC1020Register(CC1020_MAIN,0xC1);

  // Monitor LOCK
  for(TimeOutCounter=LOCK_TIMEOUT; ((ReadFromCC1020Register(CC1020_STATUS)&0x10)==0)&&(TimeOutCounter>0); TimeOutCounter--);

  // If PLL in lock
  if((ReadFromCC1020Register(CC1020_STATUS)&0x10)==0x10){
    // Indicate PLL in LOCK
    lock_status = LOCK_OK;
  // Else (PLL out of LOCK)
  }else{
    // If recalibration ok
    if(CalibrateCC1020(PA_POWER)){
      // Indicate PLL in LOCK
      lock_status = LOCK_RECAL_OK;
    // Else (recalibration failed)
    }else{
      // Indicate PLL out of LOCK
      lock_status = LOCK_NOK;
    }
  }

  // Restore PA setting
  WriteToCC1020Register(CC1020_PA_POWER,PA_POWER);

  // Turn OFF DCLK squelch in TX
  WriteToCC1020Register(CC1020_INTERFACE,ReadFromCC1020Register(CC1020_INTERFACE)&~0x10);

  // Return LOCK status to application
  return (lock_status);
}

/****************************************************************************/
/*  This routine puts the CC1020 into power down mode. Use WakeUpCC1020ToRX */
/*  followed by SetupCC1020RX or WakeupCC1020ToTX followed by SetupCC1020TX */
/*  to wake up from power down                                              */
/****************************************************************************/

void SetupCC1020PD(void)
{
  // Put CC1020 into power-down
  WriteToCC1020Register(CC1020_MAIN,0x1F);

  // Turn off PA to minimise current draw
  WriteToCC1020Register(CC1020_PA_POWER,0x00);
}

/****************************************************************************/
/*  This routine wakes the CC1020 up from PD mode to RX mode                */
/****************************************************************************/

void WakeUpCC1020ToRX(char RXANALOG)
{
  volatile int i;

  // Turn on xtal oscillator core
  WriteToCC1020Register(CC1020_MAIN,0x1B);

  // Setup bias current adjustment
  WriteToCC1020Register(CC1020_ANALOG,RXANALOG);

  // Insert wait routine here, must wait for xtal oscillator to stabilise, 
  // typically takes 2-5ms.
  for (i=0x0260; i > 0; i--);

  // Turn on bias generator
  WriteToCC1020Register(CC1020_MAIN,0x19);

  // Wait for 150 usec
  for (i=0x0010; i > 0; i--);

  // Turn on frequency synthesiser
  WriteToCC1020Register(CC1020_MAIN,0x11);
}

/****************************************************************************/
/*  This routine wakes the CC1020 up from PD mode to TX mode                */
/****************************************************************************/

void WakeUpCC1020ToTX(char TXANALOG)
{
  volatile int i;

  // Turn on xtal oscillator core
  WriteToCC1020Register(CC1020_MAIN,0xDB);

  // Setup bias current adjustment
  WriteToCC1020Register(CC1020_ANALOG,TXANALOG);

  // Insert wait routine here, must wait for xtal oscillator to stabilise, 
  // typically takes 2-5ms. 
  for (i=0x0260; i > 0; i--);

  // Turn on bias generator
  WriteToCC1020Register(CC1020_MAIN,0xD9);

  // Wait for 150 usec
  for (i=0x0010; i > 0; i--);

  // Turn on frequency synthesiser
  WriteToCC1020Register(CC1020_MAIN,0xD1);
}


/****************************************************************************/
/*  This routine releases the AGC level                                     */
/****************************************************************************/
void AGCLevelReleaseCC1020(void){
  WriteToCC1020Register(CC1020_VGA1, ReadFromCC1020Register(CC1020_VGA1)&~0x18);
}


/****************************************************************************/
/*  This routine freezes the AGC level                                     */
/****************************************************************************/
void AGCLevelFreezeCC1020(void){
  WriteToCC1020Register(CC1020_VGA1, ReadFromCC1020Register(CC1020_VGA1)|0x1C);
}

⌨️ 快捷键说明

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