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

📄 rf24e1.c.bak

📁 基于无线单片机24e1的组网程序。在无线传感器之类的应用中的成功案例。
💻 BAK
字号:
//========================================================
//深圳奥特迅电力设备有限公司
//电池监控系统用无线传输模块
//文件名:RF24E1.C
//描述:无线收发模块处理,无线子系统的初始化配置;spi操作;
//	无线收发操作;子系统工作在shockburst接收模式下
//      收到无线数据,LED指示
//	喂狗操作
//By:Andylee 2006-6-13
//anssett@163.com
//=========================================================
#include <BATTERY.H>
RF_DATA rf_Received;			//该全局变量用于无线数据的接收和判断用的
unsigned int OneSCnt;
bit OneSecondGain=1;
sbit P04=P0^4;					//备用
sbit Clr_Dog=P0^5;				//喂狗用
sbit Led_Show=P0^6; 			//LED点灯
unsigned char code RxTxConfig[] = {
    	RF_DATA_LENGTH * 8, RF_DATA_LENGTH * 8,	 	//信道2和1的数据长(单位为位)
	0x00, 0x00, 0x00, 0x00, 0x00, 			//信道2的地址(如没有5字节长,则较低的位为有效位,下同)
	0x00, 0x12, 0x34, 0x56, 0x78,  			//信道1的地址
							//地址的位数(B0100_00xx),
							//CRC位数(16位Bxxxx_xx1x,8位Bxxxx_xx0x,CRC允许(Bxxxx_xxx0,0禁止,1允许)
	B1000_0001,//------------------------------------------------------------------------------------------------------------

							//位7,RX2_EN,为0只有信道1接收,为1允许2个信道同时接收
							//位6,CM,通讯方式,0:direct mode,1:ShockBurst mode,24e1只能为1
							//位5,RF DATA RATE,0:250Kbps,1:1Mbps,250Kbps时可提高接收灵敏度10dbm,1Mbps时要求16M晶体
							//位4,3,2,晶体频率,为该值+1再*4,如为2,则为12M晶体,3 ,16M
							//位1,0:发射功率:00:-20,	01:-10 10:-5 11:0dbm	 
	B0100_1111,//------------------------------------------------------------------------------------------------------------ 	 				
							//B0110_1111,1信道接收,shockBurst方式 ,1Mbps,16Mhz,0 DBm
							//B1111_111x,表示频率, 实际频率为2400+(B1111_111x >> 1)
							//位0,RX_EN,为0发送,为1接收
	B0000_0101,	 			//2402Mhz,接收方式	 (最后一位 RX_EN 为1表接收方式)
};

//================================================================
//函数功能:100us软件延时程序
//filename:Delay100us()
//input:
//output:none
//================================================================
void Delay100us(unsigned char n)
{
    	unsigned char i;
    	while(n--)
        for(i=0;i<35;i++)
            ;
}
//================================================================
//函数功能:毫秒软件延时程序
//filename:Delay100us()
//input:
//output:none
//================================================================

void Delayms(volatile unsigned char n)
{
    unsigned char j;
    while(n--)
        for(j=0;j<10;j++)
	    	Delay100us(10);

}
//================================================================
//函数功能:片内SPI接口操作
//filename:fSpiRdWr()
//input:char 读操作,输入参数0
//output:none
//================================================================
uchar fSpiRdWr(uchar dat){
	EXIF &= ~0x20;                          		// Clear SPI interrupt
   	SPI_DATA = dat;                           		// Move byte to send to SPI data register
    	while((EXIF & 0x20) == 0x00);            	// Wait until SPI hs finished transmitting
    	return SPI_DATA;
}
//================================================================
//函数功能:将无线子系统设置为接收模式
//on the spec.page26:
//If nFRF2401 subsystem is to be configured for 2 channel RX in ShockBuust
//a total of 120 bits must be shifted in during the first configuration after VDD is 
//applied.Once the wanted protocol,modus and RF channed are set ,only one bit(RXEN) is 
//shifted in to switch between RX and TX.So this function can do that like it.
//On the spec.page28
//After VDD is turned on shockburst configureation is done once and remains set whilst VDD is 
//present.during operation only the first byte for frequecy channel and Rx/Tx switch need
//to be changed!
//We can be so lasy!!
//filename:SetRxMode()
//input:none
//output:none
//================================================================
static void SetRxMode(void)
{
    	CE = 0;
    	CS = 1;
    	Delay100us(0);
    	fSpiRdWr(RxTxConfig[14] | B0000_0001);
    	CS = 0;
}
//================================================================
//函数功能:将无线子系统设置为发射模式
//filename:SetRxMode()
//input:none
//output:none
//================================================================

static void SetTxMode(void)
{
    	CE = 0;
    	CS = 1;
    	Delay100us(0);
    	fSpiRdWr(RxTxConfig[14] & B1111_1110);
    	CS = 0;
}
//================================================================
//函数功能:将无线子系统初始化
//filename:SetRxMode()
//input:none
//output:none
//================================================================
void fRfInit(void){
	uchar i;
//	CE = 0;		   								//注释掉,因为是上电的初始值
//	CS = 0;
//	PWR_UP = 1;			
//	Delay100us(30);
// 	SPI:        
    	SPICLK = 0x00;                          // Max SPICLK (=CLK/8)
    	SPI_CTRL = 0x02;                        // Connect SPI to RADIO CH1
// 	RADIO:
// 	CE = 0;
    	CS = 1;                                 // RF SPI CS = 1,进行配置
	Delay100us(0);
    	for(i=0;i<15;i++)
    	{
        	fSpiRdWr(RxTxConfig[i]);
  	}
    	CS = 0;	  				//退出配置状态
								//开放DR1中断
	EXIF &= ~0x40;				//清标志DR1
	EX4 = 1;					//允许DR1中断
	Delay100us(0);
//	SetRxMode();
	CE = 1;						//启动接收
}
//================================================================
//函数功能:端口初始化
//filename:IO_Init()
//input:none
//output:none
//================================================================
void IO_Init(void)
{
    	P0_ALT = 0x06;  					// Select alternate functions on pins P0.1 and P0.2,  TXD RXD 
    	P0_DIR = 0x00;                 		// P0 work as output
    	P0 = 0xff;                      	// all hight    
   	    P1_DIR = 0x03;		   				// P0.0, P0.3 is input(S3, S4),
        PWR_UP = 1;                     	// Turn on Radio
    	Delay100us(30);                 	// Wait > 3ms 
    	SPICLK = 0;                     	// Max SPI clock (XTAL/8)
    	SPI_CTRL = 0x02;                	// Connect internal SPI controller to Radio
}
//================================================================
//函数功能:发射一组数据
//filename:fRfSend()
//input:*addr ,*dat 目的地址和数据
//output:none
//================================================================
/*void fRfSend(uchar *addr,uchar *dat)
{
	uchar i;
	SetTxMode();
    	CE = 1;
    	Delay100us(1);
	for(i = 0;i < RF_ADDRS_LENGTH;i++)
	{  					//写地址
		fSpiRdWr(addr[i]);
	}
	for(i = 0;i < RF_DATA_LENGTH;i++)
	{					//写数据
		fSpiRdWr(dat[i]);
	}
	CE = 0;					//开始发送	
	Delay100us(200);      			// Wait until finished transmitting (200us + 256us)
	SetRxMode();				//设置为接收方式
	CE = 1; 	
}*/	
//================================================================
//函数功能:无线模块处理函数
//收到无线数据则丢给串口!
//filename:RF24E1_task()
//input: none
//output:none
//================================================================
void RF24E1_task(RF_DATA RFReceivedData)
{		
		#if _24e1Debug
		if(OneSecondGain)
		#else
		if(RFReceivedData.counter!= 0)
		#endif		
	{	OneSecondGain=0;
		Led_on(); 						//指示灯闪烁表明收到无线数据
		Delayms(2);
		Led_off();		
		#if _24e1Debug				
		PrintStr("1#电池电压:\n");
		PrintByte(RFReceivedData.AIN1_Hi);
		PrintByte(RFReceivedData.AIN1_Lo);
		PrintStr("\n");
		Delayms(100);
		PrintStr("2#电池电压:\n");
		PrintByte(RFReceivedData.AIN2_Hi);
		PrintByte(RFReceivedData.AIN2_Lo);
		PrintStr("\n");
		Delayms(100);
		PrintStr("1#电池温度:\n");
		PrintByte(RFReceivedData.TEMP1_Hi);
		PrintByte(RFReceivedData.TEMP1_Lo);
		PrintStr("\n");
		Delayms(100);
		PrintStr("2#电池温度:\n");
		PrintByte(RFReceivedData.TEMP2_Hi);
		PrintByte(RFReceivedData.TEMP2_Lo);
		PrintStr("\n");
		Delayms(100);
		RFReceivedData.counter=0;		//为了表明收到的无线数据已经被使用
		#endif

		
								
	}
}
//================================================================
//函数功能:无线模块中断处理函数
//filename:RF24E1_isr()
//input: none
//output:none
//================================================================
void RF24E1_isr(void) interrupt 8
{
	uchar *ptr,jj=1;
	ptr=&rf_Received;	
	while(DR1)
	{
		ptr[jj] = fSpiRdWr(0);	//软件不需要清除DR1 吗?答:不要的
		++jj;
	}
	rf_Received.counter=jj-1; 
	EXIF &= ~0x40;			//清标志	
}  
//================================================================
//函数功能:喂狗信号高
//filename:Dog_Hight()
//input: none
//output:none
//================================================================
void Dog_Hight(void)
{
	Clr_Dog=1;
	Delayms(10);	
}
//================================================================
//函数功能:喂狗信号低
//filename:Dog_Low()
//input: none
//output:none
//================================================================
void Dog_Low(void)
{
	Clr_Dog=0;
	Delayms(10);	
}
//================================================================
//函数功能
//filename:
//input: none
//output:none
//================================================================
void Led_on(void)
{
Led_Show=0;	
}	
//================================================================
//函数功能:
//filename:
//input: none
//output:none
//================================================================
void Led_off(void)
{
	Led_Show=1;		
}
//================================================================
//函数功能:TIMER0初始化函数
//filename:	Timer0_Iint()
//input: none
//output:none
//================================================================
void Timer0_Init(void)
{
 	 TMOD &=0xf0;
	 TMOD |=0x01;		//timer0 方式1
	 TH0=0xb1;
	 TL0=0xe0;
	 ET0=1;
	 TR0=1;
	 EA=1;
}
//================================================================
//函数功能:TIMER0中断服务程序(实现1秒定时)
//filename:
//input: none
//output:none
//================================================================
void Timer0_isr(void) interrupt 1
{
	 TH0=0xb1;
	 TL0=0xe0;			//reload the counter
	 if(++OneSCnt==2000)	 		//2SECOND gain
	 {
	  	OneSecondGain=1;
		OneSCnt=0;
	 }
  
}

⌨️ 快捷键说明

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