CC1101與STC89C52無(wú)線通訊接收程序:#include
#include
#define IOCFG2 0x00
#define IOCFG10x01
#define IOCFG00x02
#define FIFOTHR0x03//rx和tx FIFO門限
#define SYNC10x04//同步詞匯,高字節(jié)
#define SYNC00x05//同步詞匯,低字節(jié)
#define PKTLEN0x06//數(shù)據(jù)包長(zhǎng)度
#define PKTCTRL10x07//數(shù)據(jù)包自動(dòng)控制
#define PKTCTRL00x08//數(shù)據(jù)包自動(dòng)控制
#define ADDR0x09//設(shè)備地址
#define CHANNR0x0a//信道數(shù)
#define FSCTRL10x0b//頻率合成控制器
#define FSCTRL00x0c//頻率控制詞匯,高字節(jié)
#define FREQ20x0d//頻率控制詞匯,中間字節(jié)
#define FREQ10x0e//頻率控制詞匯,低字節(jié)
#define FREQ00x0f//調(diào)置器配置
#define MDMCFG40x10//調(diào)置器配置
#define MDMCFG30x11//調(diào)置器配置
#define MDMCFG20x12//調(diào)置器配置
#define MDMCFG10x13//調(diào)置器配置
#define MDMCFG00x14//調(diào)置器配置
#define DEVIATN0x15//主通信控制狀態(tài)機(jī)配置
#define MCSM20x16//主通信控制狀態(tài)機(jī)配置
#define MCSM10x17//主通信控制狀態(tài)機(jī)配置
#define MCSM00x18//頻率偏移補(bǔ)償配置
#define FOCCFG0x19//位同步配置
#define BSCFG0x1a//AGC控制
#define AGCCTRL20x1b//AGC控制
#define AGCCTRL10x1c//AGC控制
#define AGCCTRL00x1d//高字節(jié)時(shí)間0暫停
#define WOREVT10x1e//低字節(jié)時(shí)間0暫停
#define WOREVT00x1f//電磁波激活控制
#define WORCTRL0x20//前末端RX配置
#define FREND10x21//前末端TX配置
#define FREND00x22//頻率合成器校準(zhǔn)
#define FSCAL30x23//頻率合成器校準(zhǔn)
#define FSCAL20x24//頻率合成器校準(zhǔn)
#define FSCAL10x25//頻率合成器校準(zhǔn)
#define FSCAL00x26//RC振蕩器配置
#define RCCTRL10x27//RC振蕩器配置
#define RCCTRL00x28//頻率合成器校準(zhǔn)控制
#define FSTEST0x29//產(chǎn)品測(cè)試
#define PTEST0x2a//AGC測(cè)試
#define AGCTEST0x2b//不同的測(cè)試設(shè)置
#define TEST20x2c//不同的測(cè)試設(shè)置
#define TEST10x2d//不同的測(cè)試設(shè)置
#define TEST00x2e//
#define SRES0x30//重啟芯片
#defineSFSTXON0x31//開(kāi)啟和校準(zhǔn)頻率合成器
#define SXOFF0x32//關(guān)閉晶體振蕩器
#define SCAL0x33//校準(zhǔn)頻率合成器并判斷
#define SRX0x34//啟用SRX
#define STX0x35//空閑狀態(tài):啟用STX
#define SIDLE0x36//離開(kāi)RX/TX,關(guān)斷頻率合成器
#define SAFC0x37//頻率合成器AFC調(diào)節(jié)
#define SWOR0x38//自動(dòng)RX選舉序列(電磁波激活)
#define SPWD0x39//當(dāng)CSN為高時(shí)進(jìn)入功率降低模式
#define SFRX0x3a//沖洗RX FIFO緩存
#define SFTX0x3b//沖洗TX FIFO緩存
#define SWORRST0x3c//重新設(shè)置真實(shí)時(shí)間時(shí)鐘
#define SNOP0x3d//無(wú)操作
#define PARTNUM0x30//CC2550組成部分?jǐn)?shù)目
#define VERSION0x31//當(dāng)前版本數(shù)
#define FREQEST0x32//頻率偏移估計(jì)
#define LQI0x33//連接質(zhì)量的解調(diào)器估計(jì)
#define RSSI0x34//接收信號(hào)強(qiáng)度指示
#define MARCSTATE0x35//控制狀態(tài)機(jī)狀態(tài)
#define WORTIME10x36//WOR計(jì)時(shí)器高字節(jié)
#define WORTIME00x37//WOR計(jì)時(shí)器低字節(jié)
#define PKTSTATUS0x38//當(dāng)前GDOx狀態(tài)和數(shù)據(jù)包狀態(tài)
#define VCO_VC_DAC0x39//PLL校準(zhǔn)模塊的當(dāng)前設(shè)定
#define TXBYTES0x3a//TX FIFO中的下溢和比特?cái)?shù)
#define RXBYTES0x3b//RX FIFO中的下溢和比特?cái)?shù)
#define PATABLE0x3e
#define TX_FIFO0x3f
#define RX_FIFO0x3f
#define data_len8
本文引用地址:http://m.butianyuan.cn/article/201611/322553.htmtypedef unsigned intuint;
typedef unsigned charuchar;
uchar rx_buffer[data_len]={0,0,0,0,0,0,0,0};//rx data
//uchar const code TX_Buffer[data_len]={0x02,0x55};//tx data
uchar data_length=0;
uchar code P_Config[]={0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0};//功率配置
sbit CSN=P2^1;
sbit SCLK=P2^7;
sbit SI=P2^2;
sbit SO=P2^0;
sbit GDO0=P2^6;
sbit LED=P0^0;
sbit LED1=P0^6;
void CC1101_Init(void);
void CC1101_RESET(void);
void POWER_UP_RESET(void);
uchar SPI_WR_Byte(uchar value);
void SPI_Write_Byte(uchar addr,uchar value);
uchar SPI_Read_Byte(uchar addr);
void SPI_Write_Strobe(uchar strobe);
void SPI_Write_Burst(uchar addr,uchar *buffer,uchar count);
void SPI_Read_Burst(uchar addr,uchar *buffer,uchar count);
void SPI_Write_Packet(uchar *TX_Buffer,uchar size);
uchar SPI_Read_Packet(uchar *RX_Buffer,uchar length);
void CC1101_Set_TX(void);
void CC1101_Set_RX(void);
void Write_Config(void);
void delay_ms(uint x)
{
uint i;
while(x--)for(i=0;i<120;i++);
}
void delay_us(uint x)
{
do {
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
} while (--x);
}
//------------------------------------------------/
//函數(shù)功能:通過(guò)SPI寫讀一字節(jié)/
//輸入?yún)?shù):value/
//輸出參數(shù):B_value/
//------------------------------------------------/
uchar SPI_WR_Byte(uchar value)
{
uchar i,status=0;
SCLK=0;
for(i=0;i<8;i++)
{
if(value&0x80)
SI=1;
else
SI=0;
value<<=1;
SCLK=1;
status<<=1;
_nop_();_nop_();
if(SO)
status|=0x01;
SCLK=0;
_nop_();_nop_();
}
return status;
}
//--------------------------------------------------/
//函數(shù)功能:重置CC1101/
//輸入?yún)?shù):無(wú)/
//輸出參數(shù):無(wú)/
//--------------------------------------------/
void CC1101_RESET(void)
{
SPI_Write_Strobe(SIDLE);
CSN=0;
while(SO);
SPI_WR_Byte(SRES);
while(SO);
CSN=1;
}
//-----------------------------------------------/
//函數(shù)功能:CC1101上電重置/
//輸入?yún)?shù):無(wú)/
//輸出參數(shù):無(wú)/
//--------------------------------------------/
void POWER_UP_RESET(void)
{
CSN=1;
delay_us(1);
CSN=0;
delay_us(1);
CSN=1;
delay_us(41);
CC1101_RESET();
}
//----------------------------------------------/
//函數(shù)功能:CC1101初始化/
//輸入?yún)?shù):無(wú)/
//輸出參數(shù):無(wú)/
//----------------------------------------------/
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);
}
//----------------------------------------------/
//函數(shù)功能:SPI寫命令/
//輸入?yún)?shù):Strobe/
//輸出參數(shù):無(wú)/
//---------------------------------------------/
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);
}
}
評(píng)論