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

📄 t905.c

📁 无线遥控主机程序
💻 C
字号:
/******************************************************************************************\ 
============================================================================================
* T905.c
*
* This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTYT; 
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*
* NOTES:
* This program is for the nRF905 with 232 interface;
* $write date: 2004.4.7;time 11:00$
* $Revision: 1 $
*
/*******************************************************************************************/
#include <reg51.h>
#include <intrins.h>

#define uchar unsigned char 
#define uint  unsigned int

#define WC		0x00			// Write configuration register command
#define RC		0x10 			// Read  configuration register command
#define WTP		0x20 			// Write TX Payload  command
#define RTP		0x21			// Read  TX Payload  command
#define WTA		0x22			// Write TX Address  command
#define RTA		0x23			// Read  TX Address  command
#define RRP		0x24			// Read  RX Payload  command
/*******************************************************************************************/


#define  TXBUFF_LEN 20
#define  RXBUFF_LEN	20
#define  RECEIVE_LEN_RF  0x6
#define  SEND_LEN_RF     0xc


typedef struct RFConfig
{
	uchar n;
	uchar buf[10];
}RFConfig;

code RFConfig RxTxConf =
{
	10,
	0x01, 0x0c, 0x44,RECEIVE_LEN_RF, SEND_LEN_RF, 0xcc, 0xcc, 0xcc,0xcc, 0x58	
};
// The content of this struct is nRF905's initialize data.
// CH_NO=1;433MHZ;Normal Opration,No Retrans;RX,TX Address is 4 Bytes
// RX TX Payload Width is 32 Bytes;Disable Extern Clock;Fosc=16MHZ
// 8 Bits CRC And enable
/*******************************************************************************************/
uchar idata Rs232TxBuf[TXBUFF_LEN];
uchar idata Rs232RxBuf[RXBUFF_LEN];


//uchar idata rs232txpoint_head , rs232rxpoint_head       ,rs232txpoint_tail , rs232rxpoint_tail;

uchar idata rf_send_flag,rs232_send_flag;



/*******************************************************************************************/
uchar bdata DATA_BUF;
sbit	flag	=DATA_BUF^7;
sbit	flag1	=DATA_BUF^0;
/*******************************************************************************************/
sbit	TX_EN	=P1^1;
sbit	TRX_CE	=P1^3;
sbit	PWR_UP	=P1^2;
sbit	MISO	=P1^6;
sbit	MOSI	=P1^5;
sbit	SCK		=P1^7;
sbit	CSN		=P1^4;

sbit	AM		=P3^2;
sbit	DR		=P3^3;
sbit	CD		=P3^4;

/*******************************************************************************************/
void InitIO(void);				// Initialize IO port
void Inituart(void);				// initialize 232 uart
void Config905(void);				// Config nRF905 module
void SetTxMode(void);				// Set nRF905 in Tx mode
void SetRxMode(void);				// Set nRF905 in Rx mode
void TxPacket(void);				// Send data by nRF905
void RxPacket(void);				// Recive data by nRF905
void SpiWrite(uchar);				// Write data to nRF905
uchar SpiRead(void);				// Read data to nRF905
void Delay(uchar n);				// Delay 100us
void Scankey(void);				// Scan key
//void TxData (uchar x);				// Send key_value to CRT display
void init_rxbuf(void) ;
void   rf_send_pakage(void);

void   TxPacket_special(void);
void rs232_tx_pakage(void);

/*******************************************************************************************/
//function main();
/*******************************************************************************************/


#pragma rb (0)

void main(void)
{	
	InitIO(); 				// Initialize IO port
	init_rxbuf();
	Inituart();				// initialize 232 uart
	Config905();				// Config nRF905 module
	SetTxMode();				// Set Tx Mode
//	TxPacket();				// Transmit Tx buffer data
	Delay(500);				// delay for led light	
	P0=0xff;				// led close
	SetRxMode();				// Set nRF905 in Rx mode
	while(1)				// circulation
	{	
	  if ((DR)&&(rs232_send_flag==0))				// If recive data ready... 
	  RxPacket();		// ... recive data
	  rs232_tx_pakage();
	 
	  rf_send_pakage();
	  Delay(5);	
	//	Scankey();			// Scan key
		
	}	
}
/*******************************************************************************************/
//function InitIO();
/*******************************************************************************************/
void InitIO(void)
{
	P0=0x0f;				// led light
	CSN=1;					// Spi 	disable						
	SCK=0;					// Spi clock line init high
	DR=1;					// Init DR for input
	AM=1;					// Init AM for input
	PWR_UP=1;				// nRF905 power on
	TRX_CE=0;				// Set nRF905 in standby mode
	TX_EN=0;				// set radio in Rx mode	
}
/*******************************************************************************************/
//function Inituart();
/*******************************************************************************************/
void Inituart(void)
{
	TMOD |= 0x20;				 //timer1 working mode 1
	TL1 = 0xfd;				      //f7=9600 for 16mhz Fosc,and ... 
	TH1 = 0xfd;				     //...fd=19200 for 11.0592mhz Fosc
	SCON = 0x50;				//uart mode 2,ren==1
	PCON = 0x0;				    //smod=0
	ES=1;
	EA=1;
	TR1 = 1;
  
}
/*******************************************************************************************/
//function Config905();
/*******************************************************************************************/
void Config905(void)
{
	uchar i;					
	CSN=0;					// Spi enable for write a spi command
	SpiWrite(WC);				// Write config command
	for (i=0;i<RxTxConf.n;i++)		// Write configration words
	{
		SpiWrite(RxTxConf.buf[i]);
	}
	CSN=1;					// Disable Spi
}
/*******************************************************************************************/
//function Delay100us();Delay 100us
/*******************************************************************************************/
void Delay(uchar n)
{
	uint i;
	while(n--)
	for(i=0;i<80;i++);	
}
/*******************************************************************************************/
//function SpiWrite();
/*******************************************************************************************/
void SpiWrite(uchar  byte)
{
	uchar i;	
	DATA_BUF=byte;				// Put function's parameter into a bdata variable		
	for (i=0;i<8;i++)			// Setup byte circulation bits
	{	
		
		if (flag)			// Put DATA_BUF.7 on data line
			MOSI=1;
		else
			MOSI=0;
		SCK=1;				// Set clock line high
		DATA_BUF=DATA_BUF<<1;		// Shift DATA_BUF
		SCK=0;				// Set clock line low 
	}	
}
/*******************************************************************************************/
//function SpiRead();
/*******************************************************************************************/
uchar SpiRead(void)
{
	uchar i;	
	for (i=0;i<8;i++)			// Setup byte circulation bits
	{	
		DATA_BUF=DATA_BUF<<1;		// Right shift DATA_BUF
		SCK=1;				// Set clock line high
		if (MISO)
			flag1=1;		// Read data
		else
			flag1=0;
							
		
		SCK=0;				// Set clock line low
	}
	return DATA_BUF;			// Return function parameter
}
/*******************************************************************************************/
//function TxPacket();
/*******************************************************************************************/
void TxPacket(void)
{
	uchar i;
	//Config905();				
	CSN=0;					// Spi enable for write a spi command	
	SpiWrite(WTP);				// Write payload command
	for (i=0;i<SEND_LEN_RF;i++)
	{
	    
		SpiWrite(Rs232RxBuf[i]);		// Write 32 bytes Tx data
      
	}
	CSN=1;					// Spi disable						
	Delay(1);
	CSN=0;					// Spi enable for write a spi command	
	SpiWrite(WTA);				// Write address command
	for (i=0;i<4;i++)			// Write 4 bytes address
	{
		SpiWrite(RxTxConf.buf[i+5]);
	}	
	CSN=1;					// Spi disable
	TRX_CE=1;				// Set TRX_CE high,start Tx data transmission
	Delay(1);				// while (DR!=1);
	TRX_CE=0;				// Set TRX_CE low
}

/*
void TxPacket_special(void)
{
	uchar i;
	//Config905();				
	CSN=0;					// Spi enable for write a spi command	
	SpiWrite(WTP);		   // Write payload command
	for (i=0;i<7;i++)
	{
	    
		SpiWrite(Rs232RxBuf[rs232rxpoint_tail]);		// Write 32 bytes Tx data
        rs232rxpoint_tail++;
		if(	rs232rxpoint_tail>RXBUFF_LEN)rs232rxpoint_tail=0;

	}

	  SpiWrite(0);
	   SpiWrite(0);
	    SpiWrite(0);
		 SpiWrite(0);
		  SpiWrite(0);

	CSN=1;					// Spi disable						
	Delay(1);
	CSN=0;					// Spi enable for write a spi command	
	SpiWrite(WTA);				// Write address command
	for (i=0;i<4;i++)			// Write 4 bytes address
	{
		SpiWrite(RxTxConf.buf[i+5]);
	}	
	CSN=1;					// Spi disable
	TRX_CE=1;				// Set TRX_CE high,start Tx data transmission
	Delay(1);				// while (DR!=1);
	TRX_CE=0;				// Set TRX_CE low


}

 */


/*******************************************************************************************/
//function RxPacket();
/*******************************************************************************************/
void RxPacket(void)
{
	uchar i;	
	TRX_CE=0;				// Set nRF905 in standby mode	
	CSN=0;					// Spi enable for write a spi command
	SpiWrite(RRP);				// Read payload command	
	for (i=0;i<RECEIVE_LEN_RF;i++)
	{
				
		Rs232TxBuf[i]=SpiRead();		// Read data and save to buffer		
		
	
		
	}
	rs232_send_flag=1;

	CSN=1;					// Disable spi
	while(DR||AM);
	TRX_CE=1;

   					
}





/*******************************************************************************************/
//function SetTxMode();
/*******************************************************************************************/
void SetTxMode(void)				
{	
	TX_EN=1;
	TRX_CE=0;
	Delay(1); 				// delay for mode change(>=650us)
}				
/*******************************************************************************************/
//function SetRxMode();
/*******************************************************************************************/
void SetRxMode(void)
{
	TX_EN=0;
	TRX_CE=1;
	Delay(1); 				// delay for mode change(>=650us)				
}
/*******************************************************************************************/
//Functon Scankey();
/*******************************************************************************************/	
/*
void Scankey()
{
	uchar Temp,xx;
	P0=0xff;
	Temp=P0&0x0f;				// Read key value from port P0
	if (Temp!=0x0f)
	{	Delay(10);
		Temp=P0&0x0f;			// Read key value from port P0
		if (Temp!=0x0f)
		{
			xx=Temp;	
			Temp=Temp<<4;		// Left shift 4 bits	
			TxBuf[0]=Temp;		// Turn On the LED
			P0=Temp;		// Save to Tx buffer[0]
			SetTxMode();		// Set Tx Mode
			TxPacket();		// Transmit Tx buffer data
			TxData (xx);
			Delay(500);		// Delay for LED light			
			P0=0xff;		// Close LED display
			SetRxMode();		// Set Rx Mode
			while((P0&0x0f)!=0x0f);
		 }
	}
}

 */





void   rf_send_pakage(void)
{

   if(	rf_send_flag==1)
   
   {  
   	  rf_send_flag=0;
	  EA=0;
	  SetTxMode();		// Set Tx Mode
	  TxPacket();		// Transmit Tx buffer data
	  SetRxMode();		// Set Rx Mode
	  EA=1;
	 return;
   }

  

 
}
 



/*******************************************************************************************/
//Function TxData ();
/*******************************************************************************************/

void rs232_tx_pakage(void)
{
   uchar i;
  if( rs232_send_flag==1)
  {
	 rs232_send_flag=0;	
	 TI=0;
   for (i=0;i<RECEIVE_LEN_RF;i++)	 
	   {
	 SBUF=Rs232TxBuf[i];
     while(!TI);
	 TI=0;
	
	   } 
  }
  
}





/*******************************************************************************************/
void rs232_interrupt(void)  interrupt 4	 using 1
{  int j;
   uchar flag,i,sum;
   
     if( (RI))
     {
	   RI=0;
	  if(	rf_send_flag==0)
	  {
	   Rs232RxBuf[0]=SBUF;
		sum=1;
		if(	Rs232RxBuf[0]!=0x55)
		{
		return;

		} 
		

		    sum++;
		    flag=1;
            j=0;
           while(flag==1)
            {  j++;
           if(RI!=0){flag=0;}
           if(j>=5000)
           {flag=0;}
            }
		   if(j>5000)
		   { 

				rf_send_flag=0;
				sum=0;
			 	return;
			 }	 
	 
	 	    RI=0;
		   Rs232RxBuf[1]=SBUF;
		if(	Rs232RxBuf[1]!=0xaa)
		{
		return;
		}
	   	 	 
     	for( i=2;i<SEND_LEN_RF;i++)
        {  
		   sum++;
		   flag=1;
            j=0;
           while(flag==1)
            {  j++;
           if(RI!=0){flag=0;}
           if(j>=5000)
           {flag=0;}
            }
		   if(j>5000)
		   { 

				rf_send_flag=0;
				sum=0;
			 	return;
			 }	 
	 
	 	    RI=0;
		   Rs232RxBuf[i]=SBUF;

		  }

	 	  if(sum==SEND_LEN_RF)rf_send_flag=1;
	         
                
         }
       }
       
      

}

void init_rxbuf(void)
{
  uchar i;
  rf_send_flag=0;rs232_send_flag=0;
 
  //	rs232_send_sum=0;
  for( i=0;i< TXBUFF_LEN;i++)
  {
	 Rs232TxBuf[i]=0;

   }

  for( i=0;i< RXBUFF_LEN;i++)
  {
	Rs232RxBuf[i]=0;

   }

 



}
 


⌨️ 快捷键说明

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