📄 receive多通道.txt
字号:
#include "cc2500.h"
#include "c8051f320.h"
//#include "intrins.h"
//#include "stdlib.h"
#include "stddef.h"
#include "USB.h"
//#define Buffersize 256
//USB设备描述
code const UINT USB_VID = 0x10C4;
code const UINT USB_PID = 0xEA61;
code const BYTE USB_MfrStr[] = {0x1A,0x03,'U',0,'S',0,'T',0,'C',0,' ',0,'D',0,'e',0,'p',0,'2',0,'3',0}; // Manufacturer String
code const BYTE USB_ProductStr[] = {0x0C,0x03,'T',0,'S',0,'_',0,'I',0,'n',0}; //Product Desc. String
code const BYTE USB_SerialStr[] = {0x0C,0x03,'L',0,'Z',0,'Y',0,'_',0,'1',0};
code const BYTE USB_MaxPower = 15; // Max current = 30 mA (15 * 2)
code const BYTE USB_PwAttributes = 0x80; // Bus-powered, remote wakeup not supported
code const UINT USB_bcdDevice = 0x0100; // Device release number 1.00
unsigned char pUSBBuffer;
//xdata char arrtx[44]={0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
//xdata char arrrx[44]={1,0,0,0,20,19,18,17,16,15,14,13,12,11,11,12,13,14,15,16,17,18,19,20,10,9,8,7,6,5,4,3,2,1,1,2,3,4,5,6,7,8,9,10};
//unsigned char arrtx[8]={1,2,3,4,5,6,7,8};
unsigned int TimeOutCounts[2];
unsigned long a;
unsigned char b,c,n;
typedef char INT8U;
unsigned char k=0;
unsigned xdata tem1,tem2,tem3,tem4,tem5;
unsigned char USBBuffer[44];
unsigned char *databuffer=&USBBuffer[4];
sbit gdo2=P0^7; //GDO2
//sbit sck=P0^0;
sbit v=P2^1;
//sbit spibsy=SPI0CFG^0; //SPI0 configuration register
/******************************************延时*************************************/
void delay(unsigned int n)
{
while(n--);
}
unsigned int readtimer(unsigned char n)
{
unsigned int temp;
ET0=0;
temp=TimeOutCounts[n];
ET0=1;
return temp;
}
void resettimer(unsigned char n)
{
ET0=0;
TimeOutCounts[n]=0;
ET0=1;
}
/*****************************************定时器0中断函数*************************/
void timer0_ISR(void) interrupt 1 //1ms
{
TF0=0;
TimeOutCounts[0]++;
TimeOutCounts[1]++;
v=~v;
}
/*********************************spi发送接受一个数据*************************/
unsigned char spireadwrite(unsigned char dat)
{ while(gdo2);//等待写入完成
SPI0DAT=dat;
while((SPI0CN&0x80)==0);
SPI0CN&=0x7F;
//delay(10);
return SPI0DAT;
}
/*****************************************spi写寄存器*************************/
void halSpiWriteReg(unsigned char addr,unsigned char value)
{
NSSMD0=0;
spireadwrite(addr); //写地址
spireadwrite(value);//写内容
NSSMD0=1;
}
/*******************************************spi写命令**************************/
void halspistrobe(unsigned char strobe)
{//命令寄存器只需要输入一个字节即可完成配置
NSSMD0=0;
spireadwrite(strobe);//写命令
NSSMD0=1;
}
/************************************spi连续写寄存器*********************************/
void halspiwriteburstreg(unsigned char addr,unsigned char *buffer,unsigned char count)
{//通过连续写寄存器实现连续写入
unsigned char i,temp;
temp=addr|write_burst;//+0x40连续写入
NSSMD0=0;
while(gdo2);//等待写入完成
spireadwrite(temp);
for(i=0;i<count;i++)
{spireadwrite(buffer[i]);}
NSSMD0=1;
}
/****************************************spi读寄存器********************************/
unsigned char halspireadreg(unsigned char addr)
{//单一字节读取
unsigned char value,temp;
temp=addr|read_single;//地址+读命令
NSSMD0=0;
spireadwrite(temp);
value=spireadwrite(0);
NSSMD0=1;
return value;
}
/**************************************spi连续读寄存器******************************/
void halspireadburstreg(unsigned char addr,unsigned char *buffer,unsigned char count)
{//连续读取数据
unsigned char i,temp;
temp=addr|read_burst;//地址+读命令
NSSMD0=0;
spireadwrite(temp);
for(i=0;i<count;i++)
{
buffer[i]=spireadwrite(0);
}
NSSMD0=1;
}
/**************************************spi读状态寄存器****************************/
unsigned char halspireadstatus(unsigned char addr)
{//读出状态寄存器的当前值
unsigned char value,temp;
temp=addr|read_burst;//状态寄存器地址+读命令
NSSMD0=0;
spireadwrite(temp);
value=spireadwrite(0);
NSSMD0=1;
return value;
}
/*******************************************************USB BUFFER********************/
void InsertToUSBBuffer(){
//写入USB缓存,若写满了,则发送
USBBuffer[pUSBBuffer] = tem2;
USBBuffer[pUSBBuffer + 1] = tem3;
pUSBBuffer += 2;
if(pUSBBuffer >= 256){
Block_Write(USBBuffer, 256);
pUSBBuffer = 0;
}
}
/*********************************************复位cc2500******************************/
void reset_cc2500(void)
{
NSSMD0=0;
spireadwrite(cc2500_sres);//写入重起芯片命令
NSSMD0=1;
}
/*******************************************上电复位cc2500*************************/
void power_up_reset_cc2500(void)
{
NSSMD0=1;
delay(1000);
NSSMD0=0;
delay(1000);
NSSMD0=1;
delay(10000);
reset_cc2500();//复位cc2500
}
/**************************************配置cc2500的寄存器****************************/
void halRfWriteRfSettings(void)
{
halSpiWriteReg(cc2500_FSCTRL0, 0x00); // Write register settings
halSpiWriteReg(cc2500_FSCTRL1, 0x0B); // FSCTRL1 Frequency synthesizer control. 复原值
halSpiWriteReg(cc2500_FSCTRL0, 0x00); // FSCTRL0 Frequency synthesizer control. 复原值
halSpiWriteReg(cc2500_FREQ2, 0x5e); // FREQ2 Frequency control word, high byte. (0x10) P58页
halSpiWriteReg(cc2500_FREQ1, 0xa7); // FREQ1 Frequency control word, middle byte. 复原值
halSpiWriteReg(cc2500_FREQ0, 0x62); // FREQ0 Frequency control word, low byte. 复原值
halSpiWriteReg(cc2500_MDMCFG4, 0x2d); // MDMCFG4 Modem configuration. 复原值
halSpiWriteReg(cc2500_MDMCFG3, 0x2f); // MDMCFG3 Modem configuration. 复原值
halSpiWriteReg(cc2500_MDMCFG2, 0xf2); // MDMCFG2 Modem configuration. 调制格式111;开启TX时的 16 位同步词汇发送和 RX 时的16 位同步词汇侦测
halSpiWriteReg(cc2500_MDMCFG1, 0x22); // MDMCFG1 Modem configuration. 前导码的数目为4
halSpiWriteReg(cc2500_MDMCFG0, 0xe5); // MDMCFG0 Modem configuration. 由默认值给定的信道空间为 199.951kHz
halSpiWriteReg(cc2500_CHANNR, 0x00); // CHANNR Channel number.
halSpiWriteReg(cc2500_DEVIATN, 0x00); // DEVIATN Modem deviation setting (when FSK modulation is enabled). (0x47)
halSpiWriteReg(cc2500_FREND1, 0xb6); // FREND1 Front end RX configuration. 复原值
halSpiWriteReg(cc2500_FREND0, 0x10); // FREND0 Front end RX configuration. 复原值
halSpiWriteReg(cc2500_MCSM0 , 0x18); // MCSM0 Main Radio Control State Machine configuration. 复原值
halSpiWriteReg(cc2500_FOCCFG, 0x1d); // FOCCFG Frequency Offset Compensation Configuration. 复原值
halSpiWriteReg(cc2500_BSCFG, 0x1C); // BSCFG Bit synchronization Configuration. 复原值
halSpiWriteReg(cc2500_AGCCTRL2,0xc7); // AGCCTRL2 AGC control. 复原值
halSpiWriteReg(cc2500_AGCCTRL1,0x00); // AGCCTRL1 AGC control. 复原值
halSpiWriteReg(cc2500_AGCCTRL0,0xb2); // AGCCTRL0 AGC control. 复原值
halSpiWriteReg(cc2500_FSCAL3,0xea); // FSCAL3 Frequency synthesizer calibration. (0xEA)
halSpiWriteReg(cc2500_FSCAL2,0x0A); // FSCAL2 Frequency synthesizer calibration. 复原值
halSpiWriteReg(cc2500_FSCAL1,0x00); // FSCAL1 Frequency synthesizer calibration. (0x00)
halSpiWriteReg(cc2500_FSCAL0,0x11); // FSCAL0 Frequency synthesizer calibration. (0x11)
halSpiWriteReg(cc2500_FSTEST,0x59); // FSTEST Frequency synthesizer calibration. 复原值
halSpiWriteReg(cc2500_TEST2,0x88); // TEST2 Various test settings. 复原值
halSpiWriteReg(cc2500_TEST1,0x31); // TEST1 Various test settings. 复原值
halSpiWriteReg(cc2500_TEST0,0x0B); // TEST0 Various test settings. 复原值
halSpiWriteReg(cc2500_IOCFG2,0x06); // IOCFG2 GDO2 output pin configuration. p37页
halSpiWriteReg(cc2500_IOCFG0,0x06); // IOCFG0 GDO0 output pin configuration.
//当同步词汇被送出/收到时声明,在数据包末端时反声明;对RX,当可选地址检测失败
//或RX FIFO溢出时,引脚被反声明;若TX FIFO下溢,这个引脚将同样被反声明
halSpiWriteReg(cc2500_PKTCTRL1,0x00); // PKTCTRL1 Packet automation control. 无状态字节,无地址检查
halSpiWriteReg(cc2500_PKTCTRL0,0x00); // PKTCTRL0 Packet automation control. CRC校验关闭;可变长度数据包
halSpiWriteReg(cc2500_ADDR,0x00); // ADDR Device address. 复原值
halSpiWriteReg(cc2500_PKTLEN,0x29);
//halSpiWriteReg(cc2500_FIFOTHR,0x0d);
}
unsigned char const patabel[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
//功率寄存器
/****************************************cc2500发送一组数据**********************
void halrfsendpacket(unsigned char *txbuffer,unsigned char size)
{
halSpiWriteReg(cc2500_txfifo,ID); //写入要发送的字节数
halspiwriteburstreg(cc2500_txfifo,txbuffer,size);//写入要发送的数据
halspistrobe(cc2500_stx); //进入发送模式发送数据
while(!gdo2);
while(gdo2);
halspistrobe(cc2500_sftx); //刷新TX FIFO缓冲
}
***/
/******************************************cc2500接收数据***/
unsigned char halrfreceivepacket(unsigned char *rxbuffer,unsigned char length)
{
unsigned char packetlength=0;
halspistrobe(cc2500_srx); // 进入接收状态
while(!gdo2); //接收到数据
while(gdo2); //等待接收完毕
if((halspireadstatus(cc2500_rxbytes)&bytes_in_rxfifo))
{ //bytes_in_rxfifo:接受缓冲区的有效字节数。如果接收的字节数不为0
packetlength=halspireadreg(cc2500_rxfifo);
//读取第一个字节,即该数据包数据长度
if(packetlength==length)
{ //如果所要的有效数据长度小于或等于接收到的数据包的长度
halspireadburstreg(cc2500_rxfifo,rxbuffer,packetlength);
//读出所有接收到的数据
halspistrobe(cc2500_sftx);//刷新RX FIFO缓冲
return 1;
}
else
{ //如果所要的有效数据长度>接收到的数据包的长度,发生错误
halspistrobe(cc2500_sfrx);
return 0;
}
}
else
{ //没有接受到数据包
halspistrobe(cc2500_sfrx);
return 0;
}
}
void main()
{
PCA0MD=0; //关闭看门狗
/********************************************端口初始化************************/
P0MDOUT=0x0d; //输出:SCK(p0.0),MOSI(p0.2),CSn(p0.3)
P0MDIN=0xff; //miso(p0.1)数字输入,GDO2(p0.7)数字输入
XBR0=0x02; //将SPI0配置到端口
XBR1=0x40; //交叉开关使能
/*******************************************设定内部时钟***********************/
OSCICN=0xc3; //不分频
CLKSEL=0x10; //使用内部振荡,usb时钟6M
/******************************************外部存储器XRAM接口控制初始化********/
EMI0CN=0x00; //2K地址块以取模的方式映射到整个外部数据存储器的地址空间
/**********************************************SPI初始化***********************/
SPI0CFG = 0x40;
SPI0CN = 0x0D;
SPI0CKR = 3; //F=12M/2*(3+1)=1.5M
/******************************************外部存储器XRAM接口控制初始化********/
EMI0CN=0xAA;
/******************************************初始化定时器********/
CKCON=0x02;
TMOD&=~0X0F;
TMOD|=0x02;
TH0=0x06;
TL0=TH0; //TL0装入计数值,TH0保持重载值 计数到128后产生中断
ET0=1;
TR0=0;
/**********************************************usb初始化***********************/
USB_Clock_Start(); //USB时钟使能
USB_Init(USB_VID,USB_PID,USB_MfrStr,USB_ProductStr,USB_SerialStr,USB_MaxPower,USB_PwAttributes,USB_bcdDevice); //设备初始化
RSTSRC |= 0x02;
pUSBBuffer= 0;
// CKCON=0x02; //使用系统时钟/48,
/********************************************初始化cc2500**********************/
power_up_reset_cc2500(); //上电复位cc2500
halRfWriteRfSettings(); //配置CC2500的寄存器
halspiwriteburstreg(cc2500_patable,patabel,8);//对CC2500的寄存器进行功率配置
TR0=1; //允许T0开始计数
ET0=1; //允许T0计数溢出时产生中断请求
EA=1; //允许CPU响应中断请求
halspistrobe(cc2500_sidle);
resettimer(0); //防死锁定时器清零
while(1){
n=2;
halspistrobe(cc2500_sftx); //刷新TX FIFO缓冲
halSpiWriteReg(cc2500_txfifo,0xEE);
halspistrobe(cc2500_stx); //进入发送模式发送指令
while(!gdo2);
while(gdo2);
halspistrobe(cc2500_sidle);
halspistrobe(cc2500_sfrx); //刷新RX FIFO缓冲
// delay(500);
halspistrobe(cc2500_srx); //进入接收状态
resettimer(0); //防死锁定时器清零
while(!gdo2) {
if(TimeOutCounts[0]>2)
{halspistrobe(cc2500_sidle);
break;}
}
// resettimer(0);
while(gdo2);
tem1=halspireadreg(cc2500_rxfifo);
if(tem1==0x11){
USBBuffer[0]=0x00;
halspireadburstreg(cc2500_rxfifo,databuffer,40);
halspistrobe(cc2500_sfrx); //刷新RX FIFO缓冲
Block_Write(USBBuffer,64);
}
halspistrobe(cc2500_sfrx); //刷新RX FIFO缓冲
halspistrobe(cc2500_srx); //进入接收状态
resettimer(0); //防死锁定时器清零
while(!gdo2) {
if(TimeOutCounts[0]>2)
{halspistrobe(cc2500_sidle);
break;}
}
// resettimer(0);
while(gdo2);
tem1=halspireadreg(cc2500_rxfifo);
if(tem1==0x22){
USBBuffer[0]=0x01;
halspireadburstreg(cc2500_rxfifo,databuffer,40);
halspistrobe(cc2500_sfrx); //刷新RX FIFO缓冲
Block_Write(USBBuffer,64);
}
halspistrobe(cc2500_sfrx); //刷新RX FIFO缓冲
halspistrobe(cc2500_srx); //进入接收状态
resettimer(0); //防死锁定时器清零
while(!gdo2) {
if(TimeOutCounts[0]>2)
{halspistrobe(cc2500_sidle);
break;}
}
// resettimer(0);
while(gdo2);
tem1=halspireadreg(cc2500_rxfifo);
if(tem1==0x33){
USBBuffer[0]=0x02;
halspireadburstreg(cc2500_rxfifo,databuffer,40);
halspistrobe(cc2500_sfrx); //刷新RX FIFO缓冲
Block_Write(USBBuffer,64);
} /*/* */
while(TimeOutCounts[1]<20);
resettimer(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -