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

📄 send多通道.txt

📁 用c8051f320单片机控制CC2500无线收发芯片进行多通道数据采集和无线传输
💻 TXT
字号:
#include "cc2500.h"
#include "c8051f320.h"
#include "stddef.h"
#include "USB.h"

unsigned char pbuffer;
xdata char adcbuffer[2][40]; 	
const int  ID=0x33;
//xdata char arrtx[6]={0,2,4,6,8,10}; 
unsigned long a;   
unsigned char b,c,m;
unsigned char n=0;

unsigned char k=0; 
unsigned int TimeOutCounts[2]; 
unsigned char tem1; 

sbit gdo2=P0^6;                 //GDO2   
sbit V=P2^1;               


/******************************************延时*************************************/
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;
}
/*********************************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;
}



/*********************************************复位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,0x03);
}




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缓冲
  halspistrobe(cc2500_sidle);
}
void InsertToBuffer(){
	//写入缓存
	adcbuffer[m][pbuffer] = b;
	adcbuffer[m][pbuffer + 1] =c;
	pbuffer += 2;
}
 
/************************************************ADC转换**************************/
void getADC(void)
{
  // unsigned long result;
   //REF0CN|=0x03;    //开启内部偏压和电压基准
  // AD0EN=1;         //enable ADC0
  // EA=0;
  // AD0INT=0;        //清ADC中断标志
  // AD0WINT=0;       //清ADC窗口比较中断标志
  // delay(10);
   AD0BUSY=1;       //启动A/D转换
   while(!AD0INT);  //等待转换结束
  // REF0CN&=~0x03;   //关闭内部偏压和电压基准
 // AD0EN=0;
   b=ADC0L;
   c=ADC0H;//得到的值
  // EA=1;
  // return result;   //返回两个字节
}

//********************************定时器0中断处理程序**************************
void Timer0_ISR( void ) interrupt 1{		//T0的中断号为1 
//    TF0=0;
//	TL0+=tl0s;			//TL0装入计数值,TH0保持重载值 计数到128后产生中断
//	TH0=th0s;
//   n++;
 TimeOutCounts[0]++; 

    getADC();
	InsertToBuffer();
//	V=~V;
//	if(n==40){  n=0;
//	            m = (m ++) & 0x01;
//				pbuffer = 0;
         //       TR0=1;    }                      //允许T0开始计数   
 
}   


void main()
{
  PCA0MD=0;        //关闭看门狗
/********************************************端口初始化************************/
  P0MDOUT=0x0d;    //输出:SCK(p0.0),MOSI(p0.2),CSn(p0.3)
  P0MDIN=0xff;     //miso(p0.1)数字输入,GDO2(p0.6)数字输入
  P2MDOUT=0xfb;    //P2.2模拟输入
  P2MDIN=0xfb;     //P2.2模拟输入
  P2SKIP=0x04;     //P2.2模拟输入 
  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;
/*****************************************初始化ADC转换***********************/
   AMX0P=0x0A;               //AD输入P2.2
   AMX0N=0x1F;              //GND被选择为负输入,单端输入
   ADC0CF=0x80;            //右对齐
   ADC0CN=0x80;           //ADC0允许,正常方式,向AD0BUSY写1时启动ADC0
   REF0CN=0x08;          //VDD为参考电压,温度传感器关闭,偏压和电压基准关闭
  // EIE1=0x08;	        //允许AD0INT标志的中断请求,禁止USB0标志的中断请求
/********************************************初始化cc2500**********************/
  power_up_reset_cc2500();                      //上电复位cc2500
  halRfWriteRfSettings();                       //配置CC2500的寄存器
  halspiwriteburstreg(cc2500_patable,patabel,8);//对CC2500的寄存器进行功率配置
/******************************************初始化定时器********/
	CKCON=0x02;			//使用系统时钟/48,
	TMOD=(TMOD&0xF0)|0x02;		
	TH0=0x06;
	TL0=TH0;			//TL0装入计数值,TH0保持重载值 计数到128后产生中断
	ET0=1;      //允许T0计数溢出时产生中断请求   
 	TR0=0;      //允许T0开始计数   
	m=0;
	EA=1;       //允许CPU响应中断请求    
    halspistrobe(cc2500_sidle);
    TR0=1; 
    while(1){
    
	  halspistrobe(cc2500_sfrx);//刷新RX FIFO缓冲
	   halspistrobe(cc2500_srx);     //进入接收状态
	   while(!gdo2);
       while(gdo2);
        tem1=halspireadreg(cc2500_rxfifo);
        if(tem1==0xEE) /*/**/
	    {
		     TR0=0;
		     m = (m ++) & 0x01;
		     pbuffer = 0; 
		     TR0=1; 

	   //delay(8000);
          halrfsendpacket(adcbuffer[(m ++) & 0x01],40);//发送数据
	     // delay(10000);
			V=~V;

	}
    //   halspistrobe(cc2500_sidle);
    //            halspistrobe(cc2500_sfrx);//刷新RX FIFO缓冲
    //       halspistrobe(cc2500_srx);     //进入接收状态
    //          while(!gdo2);
    //          while(gdo2);
    //       tem1=halspireadreg(cc2500_rxfifo);
    //        if(tem1==0xEE)
    //      TR0=0;                        //禁止T0计数   
	//   halrfsendpacket(arrtx,6); //发送数据 
  }
}

⌨️ 快捷键说明

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