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

📄 cc1000ppv2.c

📁 Communication with chipcon cc1000.
💻 C
📖 第 1 页 / 共 2 页
字号:
//#include "cc1000pp.h"
#define modtager 1 //angiver om der er en modtagelse igang
#define sender   2 //          -          forsendelse  - 

#define TXmode 1
#define RXmode 2
#define idle 3
#define preambmax 3// (24 bit) antal preamb's der skal skal modtages for at synkronisere - se datablad for cc1000.
#define SOF 0xD3


#define CC1000_MAIN            0x00
#define CC1000_FREQ_2A         0x01
#define CC1000_FREQ_1A         0x02
#define CC1000_FREQ_0A         0x03
#define CC1000_FREQ_2B         0x04
#define CC1000_FREQ_1B         0x05
#define CC1000_FREQ_0B         0x06
#define CC1000_FSEP1           0x07
#define CC1000_FSEP0           0x08
#define CC1000_CURRENT         0x09
#define CC1000_FRONT_END       0x0A
#define CC1000_PA_POW          0x0B
#define CC1000_PLL             0x0C
#define CC1000_LOCK            0x0D
#define CC1000_CAL             0x0E
#define CC1000_MODEM2          0x0F
#define CC1000_MODEM1          0x10
#define CC1000_MODEM0          0x11
#define CC1000_MATCH           0x12
#define CC1000_FSCTRL          0x13
#define CC1000_FSHAPE7         0x14
#define CC1000_FSHAPE6         0x15
#define CC1000_FSHAPE5         0x16
#define CC1000_FSHAPE4         0x17
#define CC1000_FSHAPE3         0x18
#define CC1000_FSHAPE2         0x19
#define CC1000_FSHAPE1         0x1A
#define CC1000_FSDELAY         0x1B
#define CC1000_PRESCALER       0x1C
#define CC1000_TEST6           0x40
#define CC1000_TEST5           0x41
#define CC1000_TEST4           0x42
#define CC1000_TEST3           0x43
#define CC1000_TEST2           0x44
#define CC1000_TEST1           0x45
#define CC1000_TEST0           0x46

#define  LOCK_NOK         0x00
#define  LOCK_OK          0x01
#define  LOCK_RECAL_OK    0x02

/* Pin usage definitions */
#define PORT        PORTC
#define PORTDDR     DDRC
#define PDATA       1   // PORTc, pin 3
#define PCLK        0   // PORTc, pin 4
#define PALE        2   // PORTc, pin 5
#define DIO         4   // PORTb, pin 1
#define DCLK        3   // PORTD, pin 2

#define CAL_TIMEOUT   0xfFFE
#define LOCK_TIMEOUT  0xfFFE

/* Contents of CURRENT register for TX and RX, use SmartRF(R) Studio */
/* to find values for your application */   
 
#define TX_CURRENT 0xF3
#define RX_CURRENT 0x8C
#define TX_PLL 0x30
#define RX_PLL 0x40

#define PA_VALUE 0x80  

unsigned char CC1000status;
unsigned char CC1000mode;
unsigned char datamodtaget;
unsigned char SOFmodtaget;

unsigned char sendCC1000(unsigned char id, unsigned char *bytes, unsigned char size){
  if((CC1000status&sender) == sender)
	return 0;
  else{	
	if(CC1000mode == RXmode)
	  SetupCC1000TX(TX_CURRENT,TX_PLL);
	TXid = id;	
	TXbytes = bytes;
	TXsize = size;
	CC1000status = sender;//bloker for modtagelse
  }
  return 1;
}
unsigned char receiveCC1000(unsigned char *bytes){
  if(((CC1000status&modtager) == modtager) || ((CC1000status&sender) == sender))
	return 0;  
  else{
	if(CC1000mode == TXmode)
	  SetupCC1000RX(RX_CURRENT,RX_PLL);		
	RXbytes = bytes;
	RXsize = 1;
	CC1000status |= modtager;
	SOFmodtaget=0;
	datamodtaget=0;
  }
  return 1;
}
unsigned char getCC1000status(void){ 
  return CC1000status;	
}
unsigned char getCC1000mode(void){ 
  return CC1000mode;	
}
unsigned char CC1000datamodtaget(void){
  return datamodtaget;
}
void initCC1000(void){
  CC1000status = 0;
  CC1000mode = 0;
  datamodtaget=0;
  //I/O ops鎡ning:
  PORTDDR |= (1<<PALE) | (1<<PCLK) | (1<<PDATA);
  //f鴕st reset:  
  ResetCC1000();
  //ops鎡ning af registre (udregnet med smartRF)
  WriteToCC1000Register(CC1000_MAIN,      0x11);  
  WriteToCC1000Register(CC1000_FREQ_2A,   0x75);
  WriteToCC1000Register(CC1000_FREQ_1A,   0xA0);  
  WriteToCC1000Register(CC1000_FREQ_0A,   0x00);
  WriteToCC1000Register(CC1000_FREQ_2B,   0x58);
  WriteToCC1000Register(CC1000_FREQ_1B,   0x33);
  WriteToCC1000Register(CC1000_FREQ_0B,   0x13);  
  WriteToCC1000Register(CC1000_FSEP1,     0x01);
  WriteToCC1000Register(CC1000_FSEP0,     0xAB);
  WriteToCC1000Register(CC1000_CURRENT,   0x8c);//F3);    
  WriteToCC1000Register(CC1000_FRONT_END, 0x32);
  WriteToCC1000Register(CC1000_PA_POW,    0x80);
  WriteToCC1000Register(CC1000_PLL,       0x40);//30);
  WriteToCC1000Register(CC1000_LOCK,      0x10);
  WriteToCC1000Register(CC1000_CAL,       0x26);
  WriteToCC1000Register(CC1000_MODEM2,    0x90);
  WriteToCC1000Register(CC1000_MODEM1,    0x6F);//6F
  WriteToCC1000Register(CC1000_MODEM0,    0x27);//47=9.6, 57=19.2 kbaud
  WriteToCC1000Register(CC1000_MATCH,     0x10);
  WriteToCC1000Register(CC1000_FSCTRL,    0x01);
  WriteToCC1000Register(CC1000_PRESCALER, 0x00);
  //WriteToCC1000Register(CC1000_TEST6,     0x10);
  //WriteToCC1000Register(CC1000_TEST5,     0x08);
  WriteToCC1000Register(CC1000_TEST4,     0x25);
  //WriteToCC1000Register(CC1000_TEST3,     0x04);
  //WriteToCC1000Register(CC1000_TEST2,     0x17);
  //WriteToCC1000Register(CC1000_TEST1,     0x07);
  //WriteToCC1000Register(CC1000_TEST0,     0x07);
  SetupCC1000RX(RX_CURRENT,RX_PLL);
  //Calibrering skal foretages f鴕ste gang (power up)
  CalibrateCC1000();
  SetupCC1000TX(TX_CURRENT,TX_PLL);  
  CalibrateCC1000();
  
}


/****************************************************************************/
/*  This routine sends new configuration data to the CC1000                 */
/*  Based on bit bashing (general I/O pin use)                              */
/****************************************************************************/

void ConfigureCC1000(char Count, short Configuration[])
{
  char  BitCounter;
  char  WordCounter;
  short Data;
 
 
   
  PORT|=(1<<PALE); //PALE=1
  
  for (WordCounter=0;WordCounter<Count;WordCounter++)
  {
    Data=Configuration[WordCounter];
    PORT&=~(1<<PALE); // PALE=0
    
    /* Send address bits */
    for (BitCounter=0;BitCounter<7;BitCounter++)
    {
      PORT|=(1<<PCLK); // PCLK=1
      if ((Data&0x8000)==0) {
        PORT&=~(1<<PDATA); // PDATA=0
      }
      else {
        PORT|=(1<<PDATA); // PDATA=1
      }
      Data=Data<<1;
      PORT&=~(1<<PCLK); //PCLK=0;
    }
    
    /* Send read/write bit */
    /* Ignore bit in data, always use 1 */
    
    PORT|=(1<<PCLK); //PCLK=1
    PORT|=(1<<PDATA);  //PDATA=1
    PORT&=~(1<<PCLK); //PCLK=0
    Data=Data<<1;
    PORT|=(1<<PCLK); //PCLK=1
    PORT|=(1<<PALE); //PALE=1
    
    /* Send data bits */
    
    for (BitCounter=0;BitCounter<8;BitCounter++)
    {
      PORT|=(1<<PCLK); //PCLK=1
      if ((Data&0x8000)==0) {
        PORT&=~(1<<PDATA); // PDATA=0
      }
      else {
        PORT|=(1<<PDATA); // PDATA=1
      }
      Data=Data<<1;
      PORT&=~(1<<PCLK); //PCLK=0
     }
  PORT|=(1<<PCLK); //PCLK=1
  }   /* Finished with word */   
}

/****************************************************************************/
/*  This routine writes to a single CC1000 register                         */
/****************************************************************************/

void WriteToCC1000Register(char addr, char data)
{
  short val;
  
  val=((short)addr&0x7F)<<9 | ((short)data&0xFF);                 
  ConfigureCC1000(1,&val);
}

/****************************************************************************/
/*  This routine writes to a single CC1000 register, with data and address  */
/*  given in the same variable                                              */
/****************************************************************************/

void WriteToCC1000RegisterWord(short addranddata)
{
 
  ConfigureCC1000(1,&addranddata);
}

/****************************************************************************/
/*  This routine reads from a single CC1000 register                        */
/****************************************************************************/

char ReadFromCC1000Register(char addr)
{
  char BitCounter;
  char Data;
  char Debug;
  
    
  PORT|=(1<<PALE); //PALE=1
  
  Data=addr<<1;
  PORT&=~(1<<PALE);
    
  /* Send address bits */
  for (BitCounter=0;BitCounter<7;BitCounter++)
  {
    PORT|=(1<<PCLK); // PCLK=1
    if ((Data&0x80)==0) {
      PORT&=~(1<<PDATA); // PDATA=0
    }
    else {
      PORT|=(1<<PDATA); // PDATA=1
    }
    Data=Data<<1;
    PORT&=~(1<<PCLK); //PCLK=0;
  }
  
  /* Send read/write bit */
  /* Ignore bit in data, always use 0 */
  
  PORT|=(1<<PCLK); //PCLK=1
  PORT&=~(1<<PDATA); //PDATA=0
  PORT&=~(1<<PCLK); //PCLK=0
 
 
  PORT|=(1<<PCLK); //PCLK=1
  PORT|=(1<<PALE); //PALE=1
    
  /* Receive data bits */
  
  PORT|=(1<<PDATA); //PDATA=1
   
  PORTDDR&=~(1<<PDATA); /* Set up PDATA as an input */
    
  for (BitCounter=0;BitCounter<8;BitCounter++)
  {
    PORT&=~(1<<PCLK); //PCLK=0
    Data=Data<<1;
    Debug=(1<<PDATA);
    if ((PINC&Debug)==0) {
      Data&=0xFE;
    } else {
      Data|=0x01;
    }
    PORT|=(1<<PCLK); //PCLK=1
  }
  
  
  PORTDDR|=(1<<PDATA); /* Set up PDATA as an output again */
  
  return Data;
}
  
/****************************************************************************/
/*  This routine resets the CC1000, clearing all registers.                 */
/****************************************************************************/  

void ResetCC1000(void)
{
  char MainValue;
  
  MainValue=ReadFromCC1000Register(CC1000_MAIN);
  WriteToCC1000Register(CC1000_MAIN,MainValue & 0xFE);         // Reset CC1000
  WriteToCC1000Register(CC1000_MAIN,MainValue | 0x01);         // Bring CC1000 out of reset
}


/****************************************************************************/
/*  This routine calibrates the CC1000                                      */
/*  Returns 0 if calibration fails, non-zero otherwise. Checks the LOCK     */

⌨️ 快捷键说明

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