//----------------------------------------------/
//函数功能:CC1101初始化/
//输入参数:无/
//输出参数:无/
//----------------------------------------------/
void CC1101_Init(void)
{
//uchar i;
CSN=0;
SCLK=0;
CSN=1;
delay_ms(10);
POWER_UP_RESET();
Write_Config();
SPI_Write_Burst(PATABLE,P_Config,8);
SPI_Write_Strobe(SCAL);
SPI_Write_Strobe(SIDLE);
SPI_Write_Strobe(SFRX);
//for(i=0;i<100;i++)
//SPI_Write_Strobe(SNOP);
//SPI_Write_Strobe(SIDLE);
//SPI_Write_Strobe(SRX);
}
//----------------------------------------------/
//函数功能:SPI写命令/
//输入参数:Strobe/
//输出参数:无/
//---------------------------------------------/
void SPI_Write_Strobe(uchar strobe)
{
CSN=0;
while(SO);
SPI_WR_Byte(strobe);
CSN=1;
}
uchar SPI_Read_Byte(uchar addr)
{
uchar value;
CSN=0;
while(SO);
SPI_WR_Byte(addr|0x80);
value=SPI_WR_Byte(0);
CSN=1;
return value;
}
void SPI_Write_Byte(uchar addr,uchar value)
{
CSN=0;
while(SO);
SPI_WR_Byte(addr);
SPI_WR_Byte(value);
CSN=1;
}
void SPI_Write_Burst(uchar addr,uchar *buffer,uchar count)
{
uchar i;
CSN=0;
while(SO);
SPI_WR_Byte(addr|0x40);
for(i=0;i{
SPI_WR_Byte(buffer[i]);
//LED=~LED;
delay_us(10);
}
CSN=1;
}
void SPI_Read_Burst(uchar addr,uchar *buffer,uchar count)
{
uchar i;
CSN=0;
while(SO);
SPI_WR_Byte(addr|0xc0);
for(i=0;ibuffer[i]=SPI_WR_Byte(0);
CSN=1;
}
uchar SPI_Read_Status(uchar addr)
{
uchar value;
CSN=0;
while(SO);
SPI_WR_Byte(addr|0xc0);//
value=SPI_WR_Byte(0);
CSN=1;
return value;
}
void SPI_Write_Packet(uchar *TX_Buffer,uchar size)
{
//uchar j;
SPI_Write_Byte(TX_FIFO,size);
SPI_Write_Burst(TX_FIFO,TX_Buffer,size);
SPI_Write_Strobe(STX);
while(!GDO0);
while(GDO0);
//LED1=~LED1;
SPI_Write_Strobe(SIDLE);
SPI_Write_Strobe(SFTX);
}
uchar SPI_Read_Packet(uchar *RX_Buffer,uchar length)
{
uchar status[2];
uchar size=0;
SPI_Write_Strobe(SRX);
delay_ms(10);
if(SPI_Read_Status(RXBYTES)==0) return 0;
while(GDO0)
{
SPI_Write_Strobe(SIDLE);
return 0;
}
size=SPI_Read_Byte(RX_FIFO);
if(size==length)
{
SPI_Read_Burst(RX_FIFO,RX_Buffer,size);
SPI_Read_Burst(RX_FIFO,status,2);
SPI_Write_Strobe(SFRX);
return(status[1]&0x80);
}
else
{
SPI_Write_Strobe(SIDLE);
SPI_Write_Strobe(SFRX);
return 0;
}
}
void Write_Config(void)
{
SPI_Write_Byte(IOCFG2,0x0b);//
SPI_Write_Byte(IOCFG0,0x06);//
SPI_Write_Byte(FIFOTHR,0x07);//
//SPI_Write_Byte(SYNC1,0xd3);//
//SPI_Write_Byte(SYNC0,0x91);//
SPI_Write_Byte(PKTLEN,0xff);//
SPI_Write_Byte(PKTCTRL1, 0x04);//
SPI_Write_Byte(PKTCTRL0, 0x05);//
//SPI_Write_Byte(ADDR,0x01);//
//SPI_Write_Byte(CHANNR,0x00);//
SPI_Write_Byte(FSCTRL1,0x06);//
SPI_Write_Byte(FSCTRL0,0x00);//
SPI_Write_Byte(FREQ2,0x10);//
SPI_Write_Byte(FREQ1,0xb0);//
SPI_Write_Byte(FREQ0,0x71);//
SPI_Write_Byte(MDMCFG4,0x2d);//
SPI_Write_Byte(MDMCFG3,0x3b);//
SPI_Write_Byte(MDMCFG2,0x71);//
SPI_Write_Byte(MDMCFG1,0xa2);//
SPI_Write_Byte(MDMCFG0,0xf8);//
SPI_Write_Byte(DEVIATN,0x15);//
//SPI_Write_Byte(MCSM2 ,0x07);//
SPI_Write_Byte(MCSM1 ,0x30);//
SPI_Write_Byte(MCSM0 ,0x18);//
SPI_Write_Byte(FOCCFG,0x16);//
SPI_Write_Byte(BSCFG,0x6c);//
SPI_Write_Byte(AGCCTRL2, 0x03);//
//SPI_Write_Byte(AGCCTRL1, 0x00);//
SPI_Write_Byte(AGCCTRL0, 0x91);//
//SPI_Write_Byte(WOREVT1 , 0x87);//
//SPI_Write_Byte(WOREVT0 , 0x6b);//
//SPI_Write_Byte(WORCTRL,0xf8);//
SPI_Write_Byte(FREND1,0x56);//
SPI_Write_Byte(FREND0,0x10);//
SPI_Write_Byte(FSCAL3,0xe9);//mark
SPI_Write_Byte(FSCAL2,0x0a);//mark
//SPI_Write_Byte(FSCAL1,0x00);//
SPI_Write_Byte(FSCAL0,0x1f);//mark
//SPI_Write_Byte(RCCTRL1 , 0x41);//
//SPI_Write_Byte(RCCTRL0 , 0x00);//
SPI_Write_Byte(FSTEST,0x59);//mark
//SPI_Write_Byte(PTEST ,0x7f);//
SPI_Write_Byte(TEST2,0x81);//mark
SPI_Write_Byte(TEST1,0x35);//mark
SPI_Write_Byte(TEST0,0x09);//mark
}
void main(void)
{
uchar i,j=0;
CC1101_Init();
delay_ms(100);
while(1)
{
if(SPI_Read_Packet(rx_buffer,sizeof(rx_buffer)))
{
//LED=0;
for(i=0;i{
P0=rx_buffer[i];
delay_ms(100);
}
}
//else
//SPI_Write_Strobe(SFRX);
}
}