您的当前位置:首页51单片机cc1101模块程序

51单片机cc1101模块程序

2022-01-18 来源:爱问旅游网
基于CC1101与STC89C52无线收发程序(发射程序)

#include #include

#define IOCFG2 0x00 #define IOCFG1 0x01 #define IOCFG0 0x02

#define FIFOTHR 0x03//rx和tx FIFO门限 #define SYNC1 0x04//同步词汇,高字节 #define SYNC0 0x05//同步词汇,低字节 #define PKTLEN 0x06//数据包长度

#define PKTCTRL1 0x07//数据包自动控制 #define PKTCTRL0 0x08//数据包自动控制 #define ADDR 0x09//设备地址 #define CHANNR 0x0a//信道数

#define FSCTRL1 0x0b//频率合成控制器

#define FSCTRL0 0x0c//频率控制词汇,高字节 #define FREQ2 0x0d//频率控制词汇,中间字节 #define FREQ1 0x0e//频率控制词汇,低字节 #define FREQ0 0x0f//调置器配置 #define MDMCFG4 0x10//调置器配置 #define MDMCFG3 0x11//调置器配置 #define MDMCFG2 0x12//调置器配置 #define MDMCFG1 0x13//调置器配置 #define MDMCFG0 0x14//调置器配置

#define DEVIATN 0x15//主通信控制状态机配置 #define MCSM2 0x16//主通信控制状态机配置 #define MCSM1 0x17//主通信控制状态机配置 #define MCSM0 0x18//频率偏移补偿配置 #define FOCCFG 0x19//位同步配置 #define BSCFG 0x1a//AGC控制 #define AGCCTRL2 0x1b//AGC控制 #define AGCCTRL1 0x1c//AGC控制

#define AGCCTRL0 0x1d//高字节时间0暂停 #define WOREVT1 0x1e//低字节时间0暂停 #define WOREVT0 0x1f//电磁波激活控制 #define WORCTRL 0x20//前末端RX配置 #define FREND1 0x21//前末端TX配置 #define FREND0 0x22//频率合成器校准 #define FSCAL3 0x23//频率合成器校准 #define FSCAL2 0x24//频率合成器校准 #define FSCAL1 0x25//频率合成器校准

#define FSCAL0 0x26//RC振荡器配置 #define RCCTRL1 0x27//RC振荡器配置

#define RCCTRL0 0x28//频率合成器校准控制 #define FSTEST 0x29//产品测试 #define PTEST 0x2a//AGC测试

#define AGCTEST 0x2b//不同的测试设置 #define TEST2 0x2c//不同的测试设置 #define TEST1 0x2d//不同的测试设置 #define TEST0 0x2e//

#define SRES 0x30//重启芯片

#define SFSTXON 0x31//开启和校准频率合成器 #define SXOFF 0x32//关闭晶体振荡器

#define SCAL 0x33//校准频率合成器并判断 #define SRX 0x34//启用SRX

#define STX 0x35//空闲状态:启用STX

#define SIDLE 0x36//离开RX/TX,关断频率合成器 #define SAFC 0x37//频率合成器AFC调节

#define SWOR 0x38//自动RX选举序列(电磁波激活) #define SPWD 0x39//当CSN为高时进入功率降低模式 #define SFRX 0x3a//冲洗RX FIFO缓存 #define SFTX 0x3b//冲洗TX FIFO缓存

#define SWORRST 0x3c//重新设置真实时间时钟 #define SNOP 0x3d//无操作

#define PARTNUM 0x30//CC2550组成部分数目 #define VERSION 0x31//当前版本数 #define FREQEST 0x32//频率偏移估计

#define LQI 0x33//连接质量的解调器估计 #define RSSI 0x34//接收信号强度指示 #define MARCSTATE 0x35//控制状态机状态 #define WORTIME1 0x36//WOR计时器高字节 #define WORTIME0 0x37//WOR计时器低字节

#define PKTSTATUS 0x38//当前GDOx状态和数据包状态 #define VCO_VC_DAC 0x39//PLL校准模块的当前设定 #define TXBYTES 0x3a//TX FIFO中的下溢和比特数 #define RXBYTES 0x3b//RX FIFO中的下溢和比特数 #define PATABLE 0x3e #define TX_FIFO 0x3f #define RX_FIFO 0x3f #define data_len 8

typedef unsigned int uint; typedef unsigned char uchar;

uchar rx_buffer[data_len]={0,0,0,0,0,0,0,0};//rx data //uchar RX_data[data_len]; uchar code

tx_buffer[data_len]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//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 key=P2^5; 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); }

//------------------------------------------------/ //函数功能:通过SPI写读一字

节 / //输入参

数:value / //输出参

数:B_value /

//-----------------------------------------------/ uchar SPI_WR_Byte(uchar value) {

uchar i,B_value=0; SCLK=0;

for(i=0;i<8;i++) {

if(value&0x80) SI=1; else SI=0; value<<=1; SCLK=1;

_nop_();_nop_(); B_value<<=1; if(SO)

B_value|=0x01; SCLK=0;

_nop_();_nop_();

}

return B_value; }

//-------------------------------------------------/ //函数功能:重置

CC1101 //输入参数:

无 /

/

//输出参数:

无 //----------------------------------------------/ void CC1101_RESET(void) {

SPI_Write_Strobe(SIDLE); CSN=0; while(SO);

SPI_WR_Byte(SRES); while(SO); CSN=1; }

//-----------------------------------------------/ //函数功能:CC1101上电重

置 / //输入参数:

无 /

//输出参数:

无 //--------------------------------------------/ void POWER_UP_RESET(void) {

CSN=1;

delay_us(1); CSN=0;

delay_us(1); CSN=1;

delay_us(41); CC1101_RESET(); }

//-----------------------------------------------/ //函数功能:CC1101初始

/

/

化 / //输入参数:

无 /

//输出参数:

无 /

//--------------------------------------------/ void CC1101_Init(void) {

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(SFTX); // 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;iSPI_WR_Byte(buffer[i]); 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); LED=1;

while(GDO0); LED=0;

SPI_Write_Strobe(SIDLE); SPI_Write_Strobe(SFTX); }

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);// SPI_Write_Byte(FSCAL2, 0x0a);// // SPI_Write_Byte(FSCAL1, 0x00);// SPI_Write_Byte(FSCAL0, 0x1f);// // SPI_Write_Byte(RCCTRL1 , 0x41);// // SPI_Write_Byte(RCCTRL0 , 0x00);//

SPI_Write_Byte(FSTEST, 0x59);// // SPI_Write_Byte(PTEST , 0x7f);// SPI_Write_Byte(TEST2, 0x81);// SPI_Write_Byte(TEST1, 0x35);// SPI_Write_Byte(TEST0, 0x09);// }

uchar SPI_Read_Packet(uchar *RX_Buffer,uchar length) {

uchar status[2]; uchar size=0;

SPI_Write_Strobe(SRX); delay_ms(1);

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 main(void) {

CC1101_Init(); delay_ms(100); while(1) {

SPI_Write_Packet(tx_buffer,sizeof(tx_buffer)); } }

因篇幅问题不能全部显示,请点此查看更多更全内容