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

📄 rf24e1.c

📁 基于无线单片机24e1的组网程序。在无线传感器之类的应用中的成功案例。
💻 C
字号:
//========================================================
//深圳奥特迅电力设备有限公司
//电池监控系统用无线传输模块
//文件名:RF24E1.C
//描述:无线收发模块处理,无线子系统的初始化配置;spi操作;
//	无线收发操作;子系统工作在shockburst接收模式下
//      收到无线数据,LED指示
//	喂狗操作
//By:Andylee 2006-6-13
//anssett@163.com
//=========================================================
#include <BATTERY.H>
void SendCh(char);
unsigned int OneSCnt;
bit OneSecondGain=1;
sbit P04=P0^4;					//备用
sbit led=P0^3;
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字节长,则较低的位为有效位,下同)
	0xaa, 0xbb, 0x12, 0x34, 0x83,  			//信道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表接收方式)
};
unsigned int xdata RxMsg_list[4];
struct RFConfig
{
    unsigned char n;
    unsigned char buf[15];
};

typedef struct RFConfig RFConfig;

#define ADDR_INDEX  8   // Index to address bytes in RFConfig.buf 
#define ADDR_COUNT  4   // Number of address bytes

const RFConfig tconf =
{
    15,
    RF_DATA_LENGTH * 8, RF_DATA_LENGTH * 8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xaa, 0xbb, 0x12, 0x34, 0x81, 0x4f, 0x04
};

const RFConfig rconf =
{
    15,
    RF_DATA_LENGTH * 8, RF_DATA_LENGTH * 8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xaa, 0xbb, 0x12, 0x34, 0x81, 0x4f, 0x05
};

//================================================================
//函数功能: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 SpiReadWrite(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;
}

//================================================================
//函数功能:将无线子系统初始化
//filename:SetRxMode()
//input:none
//output:none
//================================================================
void fRfInit(void){
	uchar b;
   	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
   	CS = 1;                             // RF SPI CS = 1,进行配置
	Delay100us(0);
    for(b=0;b<rconf.n;b++)
    {
        SpiReadWrite(rconf.buf[b]);
    }
    CS = 0;	  								//退出配置状态
	Delay100us(0);
	CE = 1;									//启动接收
}
/*****************************************************************************************
//函数名:RxPacket()
//输入:none
//输出:无
//功能描述:接收一组数据包
/*****************************************************************************************/
unsigned char RxPack(void)
{
    unsigned char b,jj=0;
    CE = 1;
    while(DR1 == 0);
	while(DR1)
	{
    b = SpiReadWrite(0);
	RxMsg_list[jj]=(unsigned int)b<<8;
	b=SpiReadWrite(0);
	RxMsg_list[jj++] +=b;
	}
    CE = 0;
    return jj;
}
//================================================================
//函数功能:端口初始化
//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),
       
}

/*****************************************************************************************
//函数名:SendC()
//输入:发送的数据
//输出:无
//功能描述:串口发送
/*****************************************************************************************/
void SendCh(char c)
{
	TI = 0;
	SBUF = c;
	while(!TI);
	TI = 0;
} 
/*****************************************************************************************
//函数名:show_word()
//输入:发送的数据int 
//输出:无
//功能描述:以16进制显示一个整型
/*****************************************************************************************/
void show_hex(char sByte)
{
	if(sByte>=0 && sByte<=9)
	sByte +='0';
	else
	sByte=sByte+'A'-10;
	SendCh(sByte);
}
/*****************************************************************************************
//函数名:show_word()
//输入:发送的数据int 
//输出:无
//功能描述:以16进制显示一个整型
/*****************************************************************************************/
void show_word(unsigned int dat)
{
	unsigned char char_temp1,char_temp2;
	char_temp1=dat>>8;
	char_temp2=char_temp1>>4;
	show_hex(char_temp2);
	char_temp2=char_temp1&0x0f;
	show_hex(char_temp2);
	char_temp1=dat&0x00ff;
	char_temp2=char_temp1>>4;
	show_hex(char_temp2);
	char_temp2=char_temp1&0x0f;
	show_hex(char_temp2);
}

//================================================================
//main()>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//================================================================
main()
{	unsigned char char_temp,data_show[5],jj;
	IO_Init();					//端口初始化
	InitSerial();				//串口初始化
	Timer0_Init();				//初始化定时器0
	fRfInit();					//无线子系统初始化
	while(1)
	{	Led_Show=1;								//Led off
		led=1;
		while(RxPack()!=4);						//接收错误,等待有正确的数据到来
												//接收远方4个通道的采样值,在RxMsg_list[]数组理。
			
												//收到的采样值是经过远方校正过的16位整形。
		
		
		Led_Show=0;							//LED on	
		led=0;
	/*	for(char_temp=0;char_temp<4;char_temp++)
		{	SendCh(char_temp+0x30+1);
			SendCh('#');
			data_show[0]=RxMsg_list[char_temp]/1000+0x30;
			data_show[1]='.';
			data_show[2]=(RxMsg_list[char_temp]%1000)/100+0x30;
			data_show[3]=(RxMsg_list[char_temp]%100)/10+0x30;
			data_show[4]=RxMsg_list[char_temp]%10+0x30;
			for(jj=0;jj<5;jj++)
			{
				SendCh(data_show[jj]);
			}
			SendCh('V');
		}
	*/	Delayms(5);		
	}				
	
}

//================================================================
//函数功能:TIMER0初始化函数
//filename:	Timer0_Iint()
//input: none
//output:none
//================================================================
void Timer0_Init(void)
{

	TR0 = 0;
	TMOD &= ~0x03;
	TMOD |= 0x01;                               // mode 1
	CKCON |= 0x08;                              // T0M = 1 (/4 timer clock)
	TF0 = 0;                                    // Clear any pending Timer0 interrupts
	TH0=0xb1;
	TL0=0xe0;					
	TR0 = 1;                                    // Start Timer0
	ET0 = 1;                                    // Enable Timer0 interrupt
}
//================================================================
//函数功能:TIMER0中断服务程序(实现1秒定时)
//filename:
//input: none
//output:none
//================================================================
void Timer0_isr(void) interrupt 1
{    TF0 = 0; 
	 TH0=0xb1;
	 TL0=0xe0;					//reload the counter
	 Clr_Dog=~Clr_Dog;
	 if(++OneSCnt==200)	 		//2SECOND gain
	 {
	  	OneSecondGain=1;
		OneSCnt=0;
//		Led_Show=~Led_Show;
	 }
  
}

⌨️ 快捷键说明

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