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

📄 code.c

📁 nRF905无线FSK芯片在51单片机上工作的代码
💻 C
字号:
/*******************************************************************************************
科创四第15小组FSK通信控制程序
/*******************************************************************************************/
#include <reg51.h>
#include <intrins.h>
#include <absacc.h>
#include "set.h"

#define SB 8 //sent bytes

sbit retest=P2^0;
int trsec;
uchar jj=0;
uchar r_flag=1;// flag of receiving	指示芯片处于接受状态的flag
uchar retrieving_flag=0;  //高则指示单片机从芯片取数据的那段时间

code RFConfig RxTxConf =
{
 10,
 0x01,
 0x2c, 
 0x44, 
 0x08, 
 0x08, 
 0xcc, 0xcc, 0xcc,0xcc,    // 4 bytes address
 0x5c 
};
// 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 SB Bytes;Disable Extern Clock;Fosc=16MHZ
// 8 Bits CRC And enable

//四个按键对应的动作
Key1()//
{
	  trsec	= 410;
}
Key2()//
{	  uchar i,flag=1;

 CSN=0;PB8255=PB;
	SpiWrite(RC);
  for (i=0;i<10;i++) // Write configration words  写放配置字
 {
  temp.buf[i]=SpiRead();
 }
 CSN=1;	PB8255=PB;
 for(i=0;i<10;i++)
 {
 	if ( temp.buf[i] == RxTxConf.buf[i]) flag++;
 }
 sec_dis=flag;
}
Key3()//
{  
		int i;
		for(i=0;i<SB;i++)
		TxBuf[i]=i+1;
  		SetTxMode(); // Set Tx Mode
		for (i=0;i<SB;i++) TxData(TxBuf[i]);
		Delay(100);
		
      	TxPacket();    // Transmit Tx buffer data
		SetRxMode();
   
}
Key4()//

{ 	   
		trsec	= 0;  SetRxMode();
}

/**** T0时钟中断服务程序
	在本例中,每5ms被执行一次
*****/

timer0() interrupt 1 using 0         
{
//EA=0;
	int i;
	/* 重新对计数器赋初值,并启动定时计数 */
	TH0=V_TH0;
    TL0=V_TL0;
    TR0=1;
    
    if(p_counter<30) p_counter++;

	/* 检测到按键被按下(0)时,相应的指示灯亮(0) */
	if (KEY1==0)
	{if (p_counter==30)  {p_counter=0;Key1();} }
	if (KEY2==0) 
	{if (p_counter==30)  {p_counter=0;Key2();} }
	if (KEY3==0) 
    {if (p_counter==30)  {p_counter=0;Key3();} }
	if (KEY4==0) 
	{if (p_counter==30)  {p_counter=0;Key4();} }
				
		//the next section is about display
	Display();

//	if(r_flag>0) r_flag--;

/*	if(trsec>0)
	{
		if (trsec%400==0)
		{ SetTxMode();
		for(i=0;i<SB;i++)
		TxBuf[i]=trsec/400;
		TxPacket();
		SetRxMode();
		}
		trsec--;
	}  */


	if(trsec>0 && (!retrieving_flag))
	{
		if (trsec%300==0)
		{ 
			SetTxMode();
			for(i=0;i<SB;i++)
			TxBuf[i]=jj;
			jj++;
			if(jj>15) jj=0;
			TxPacket();
			SetRxMode();
		}
		trsec--;
		if (trsec==0) trsec=320;
	}
		// EA=1;
}

/*******************************************************************************************/
void main()
{

    SCON=0x50;  //模式1
    TMOD=0x21;  //定时模式2
    IE=0x90;

    //=====9600=======设置串口通讯寄存器
    PCON=0x00;   //波特率9600BPS,晶振11.0592MHZ
    TH1=0xFD;  //
    TL1=0xFD;  //计数器初值
	TH0=V_TH0;	  //5ms		键盘显示中断
    TL0=V_TL0;


	// 8255初始化 
    RST8255=0;
	COM8255=0x81;
    //================NRF905初始化
 CSN=1;      // Spi  disable      
 SCK=0;      // Spi clock line init high
 DR=1;      // Init DR for input
 AM=1; 
// TEST=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 
 ///////////////////////////////////////////
    TI = 0;//串行中断标志位清零
    TR1 = 1;//打开计数器1 
	TR0 = 1;//开计数器0
	ET0=1;		  //允许计数器T0中断
	PB8255=PB;



	trsec=0;
 //================
 Config905();    // Config nRF905 module

 
 SetRxMode();    // Set nRF905 in Rx mode
 retest=0; 
while(1)
{
	  retest=1;
         //=====================等待NRF905接收
	
    if (r_flag && DR)   // Ifr recive data ready...
   {//Delay(500); 
   //if(DR)
   //retest=1;
   r_flag=0;
   retrieving_flag=1;
   RxPacket();   // ... recive data
	fir_dis = RxBuf[1]; //jsq++;	 
	sec_dis = RxBuf[2];
	thi_dis = RxBuf[3];
	for_dis	= jsq;if (jsq<15)jsq++; else jsq=0;

//	Config905();
	SetRxMode();
	Delay(1000);
	retrieving_flag=0;
	 r_flag=1;
	
	}  
	 retest=0;
  //=====================


}
}

/*******************************************************************************************/
/*******************************************************************************************/
//function Config905();
/*******************************************************************************************/
void Config905(void)
{
 uchar i,flag=1;    
 CSN=0;  PB8255=PB;    // 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;  PB8255=PB;    // Disable Spi



 CSN=0;PB8255=PB;
	SpiWrite(RC);
  for (i=0;i<RxTxConf.n;i++) // Write configration words  写放配置字
 {
  temp.buf[i]=SpiRead();
 }
 CSN=1;	PB8255=PB;
 for(i=0;i<RxTxConf.n;i++)
 {
 	if ( temp.buf[i] == RxTxConf.buf[i]) flag++;
 }
 fir_dis=flag;
}

/*******************************************************************************************/
void FixConfig(void)	//fix the configuration command if it goes wrong
{
 uchar i,flag=1;
 TRX_CE=0;PB8255=PB;Delay(100);
 CSN=0;	PB8255=PB;
SpiWrite(RC);
  for (i=0;i<RxTxConf.n;i++) // Write configration words  写放配置字
 {
  temp.buf[i]=SpiRead();
 }
 CSN=1;	PB8255=PB;
 for(i=0;i<RxTxConf.n;i++)
 {
 	if ( temp.buf[i] == RxTxConf.buf[i]) flag++;
 }
 if (flag!=(RxTxConf.n+1))
 {
 for_dis=10;

 	TRX_CE=0;PB8255=PB;Delay(100);

	Config905();


 }
}
/*******************************************************************************************/

//function SpiWrite();

/*******************************************************************************************/
//function TxPacket();
/*******************************************************************************************/
void TxPacket(void)
{
 uchar i;
 TRX_CE=0;
 //Config905();    
 CSN=0;    PB8255=PB;  // Spi enable for write a spi command 
 SpiWrite(WTP);    // Write payload command
 for (i=0;i<SB;i++)
 {
  SpiWrite(TxBuf[i]);  // Write 32 bytes Tx data
 }
 CSN=1;    PB8255=PB;  // Spi disable      
 Delay(1);
 CSN=0;   PB8255=PB;   // Spi enable for write a spi command 
 SpiWrite(WTA);    // Write address command
 for (i=0;i<(RxTxConf.n-6);i++)   // Write 4 bytes address
 {
  SpiWrite(RxTxConf.buf[i+5]);
 } 
 CSN=1;      // Spi disable
 TRX_CE=1;  PB8255=PB;   // Set TRX_CE high,start Tx data transmission
 Delay(1000);     // while (DR!=1);
 TRX_CE=0;  PB8255=PB;   // Set TRX_CE low
}
/*******************************************************************************************/
//function RxPacket();
/*******************************************************************************************/
void RxPacket(void)
{
 uchar i; 
 TRX_CE=0;  PB8255=PB;    // Set nRF905 in standby mode 
 Delay(100);
 CSN=0;   PB8255=PB;   // Spi enable for write a spi command
 SpiWrite(RRP);    // Read payload command 
 for (i=0;i<SB;i++)
 {
  RxBuf[i]=SpiRead();  // Read data and save to buffer  
 }
 CSN=1;   PB8255=PB;   // Disable spi
 while(DR||AM) _nop_(); 
 //P0= RxBuf[0];    // Buffer[0] output from P0 port...
 TRX_CE=1;	 PB8255=PB;
/* if (!(RxBuf[0]==0&&RxBuf[1]==0)  )
  for (i=0;i<SB;i++)
 {
  TmpBuf[i]=RxBuf[i];  // Read data and save to buffer  
 }	 */
 //xx=(RxBuf[0]>>4)&0x0f;
 //for (i=0;i<SB;i++) TxData(TmpBuf[i]);
   
}
/*******************************************************************************************/
//function SetTxMode();
/*******************************************************************************************/
void SetTxMode(void)    
{ 
 TX_EN=1;
 TRX_CE=0; PB8255=PB;
 Delay(100);      // delay for mode change(>=650us)
 r_flag=0;
}    
/*******************************************************************************************/
//function SetRxMode();
/*******************************************************************************************/
void SetRxMode(void)
{

TRX_CE=0;  PB8255=PB;
Delay(100);
Config905();
//FixConfig();
Delay(100);
 TX_EN=0;
 TRX_CE=1;	PB8255=PB;
 Delay(100);      // delay for mode change(>=650us)  
  r_flag=1;  
 
}

⌨️ 快捷键说明

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