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

📄 2.4g+̤

📁 2.4G无线模块资料
💻 4G+̤
📖 第 1 页 / 共 2 页
字号:
	delay();
  	return(reg_val);        // return register value
}
/**************************************************
Function: SPI_Read_Buf();

Description:
  Reads 'bytes' #of bytes from register 'reg'
  Typically used to read RX payload, Rx/Tx address */
/**************************************************/
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
	unsigned char status,byte_ctr;
	nRF24L01_CSNL;
	delay();
  	status = SPI_RW(reg);       		// Select register to write to and read status byte
	for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
    	pBuf[byte_ctr] = SPI_RW(0);    // Perform SPI_RW to read byte from nRF24L01
	nRF24L01_CSNH;
	delay();
  	return(status);                    // return nRF24L01 status byte
}
/**************************************************
Function: SPI_Write_Buf();

Description:
  Writes contents of buffer '*pBuf' to nRF24L01
  Typically used to write TX payload, Rx/Tx address */
/**************************************************/
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
	unsigned char status,byte_ctr;
	nRF24L01_CSNL;                   		// Set nRF24L01_CSN low, init SPI tranaction
  	delay();
	status = SPI_RW(reg);    					// Select register to write to and read status byte
  	for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
    	status = SPI_RW(*pBuf++);            
	nRF24L01_CSNH; 								// Set nRF24L01_CSN high again
	delay();
  	return(status);          					// return nRF24L01 status byte
}
/**************************************************
Function: RX_Mode();

Description:
  This function initializes one nRF24L01 device to
  RX Mode, set RX address, writes RX payload width,
  select RF channel, datarate & LNA HCURR.
  After init, CE is toggled high, which means that
  this device is now ready to receive a datapacket. */
/**************************************************/
void RX_Mode(void)
{
	nRF24L01_CEL;
	delay();
  	SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device

  	SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
  	SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
  	SPI_RW_Reg(WRITE_REG + RF_CH, 40);        // Select RF channel 40
  	SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
  	SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
  	SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
  	nRF24L01_CEH;
	delay();
	//  This device is now ready to receive one packet of 16 bytes payload from a TX device sending to address
  //  '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.

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

/**************************************************
Function: TX_Mode();

Description:
  This function initializes one nRF24L01 device to
  TX mode, set TX address, set RX address for auto.ack,
  fill TX payload, select RF channel, datarate & TX pwr.
  PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.

  ToDo: One high pulse(>10us) on CE will now send this
  packet and expext an acknowledgment from the RX device. */
/**************************************************/
void TX_Mode(void)
{
	nRF24L01_CEL;
	delay();
  	SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);    // Writes TX_Address to nRF24L01
  	SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack
  	SPI_Write_Buf(WR_TX_PLOAD, Buffer, TX_PLOAD_WIDTH); // Writes data to TX payload

  	SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
  	SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
  	SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
  	SPI_RW_Reg(WRITE_REG + RF_CH, 40);        // Select RF channel 40
  	SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
  	SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX_DS enabled..
	nRF24L01_CEH;
	delay();
	
}
//------------------------------------------------------
void timer0_init(void)
{
 TCCR0B = 0x00; //stop
 TCNT0 = 0x06; //set count
 TCCR0A = 0x00; 
 TCCR0B = 0x02; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)		// 5ms
{

 	TCNT0 = 0x06; //reload counter value

 	if((--t20ms)==0)
 		{
	 	set(flag,bit20ms);
		t20ms=t20msC;
		if(PIND& key1)
		{
		 	key1_pulse<<=1;
    		key1_pulse&=0xfe;
		}
		if(!(PIND& key1))
		{
			key1_pulse<<=1;
    		key1_pulse|=0x01;
		}
		if(key1_pulse==0x0f)
		{
		 	key1_flag=1;
			send_flag=1;	
			accept_time=0;
		}
		if(key1_pulse==0xff)
		{
		    key1_time++;
			if(key1_time>10)
			{
			  key1_time=0;
			  key1_flag=1;
			  send_flag=1;
			  accept_time=0;
			}
		}
		if(PIND& key2)
		{
		 	key2_pulse<<=1;
    		key2_pulse&=0xfe;
		}
		if(!(PIND& key2))
		{
			key2_pulse<<=1;
    		key2_pulse|=0x01;
		}
		if(key2_pulse==0x0f)
		{
		 	key2_flag=1;
			send_flag=1;	
			accept_time=0;
		}
		if(key2_pulse==0xff)
		{
		    key2_time++;
			if(key2_time>10)
			{
			  key2_time=0;
			  key2_flag=1;
			  send_flag=1;
			  accept_time=0;
			}
		}
		if(PIND& key3)
		{
		 	key3_pulse<<=1;
    		key3_pulse&=0xfe;
		}
		if(!(PIND& key3))
		{
			key3_pulse<<=1;
    		key3_pulse|=0x01;
		}
		if(key3_pulse==0x0f)
		{
		 	key3_flag=1;
			send_flag=1;	
			accept_time=0;
		}
		if(key3_pulse==0xff)
		{
		    key3_time++;
			if(key3_time>10)
			{
			  key3_time=0;
			  key3_flag=1;
			  send_flag=1;
			  accept_time=0;
			}
		}
		if(PINC& key4)
		{
		 	key4_pulse<<=1;
    		key4_pulse&=0xfe;
		}
		if(!(PINC& key4))
		{
			key4_pulse<<=1;
    		key4_pulse|=0x01;
		}
		if(key4_pulse==0x0f)
		{
		 	key4_flag=1;
			send_flag=1;	
			accept_time=0;
		}
		if(key4_pulse==0xff)
		{
		    key4_time++;
			if(key4_time>10)
			{
			  key4_time=0;
			  key4_flag=1;
			  send_flag=1;
			  accept_time=0;
			}
		}
		if(PINC& key5)
		{
		 	key5_pulse<<=1;
    		key5_pulse&=0xfe;
		}
		if(!(PINC& key5))
		{
			key5_pulse<<=1;
    		key5_pulse|=0x01;
		}
		if(key5_pulse==0x0f)
		{
		 	key5_flag=1;
			send_flag=1;	
			accept_time=0;
		}
		if(key5_pulse==0xff)
		{
		    key5_time++;
			if(key5_time>10)
			{
			  key5_time=0;
			  key5_flag=1;
			  send_flag=1;
			  accept_time=0;
			}
		}
		if(PINC& key6)
		{
		 	key6_pulse<<=1;
    		key6_pulse&=0xfe;
		}
		if(!(PINC& key6))
		{
			key6_pulse<<=1;
    		key6_pulse|=0x01;
		}
		if(key6_pulse==0x0f)
		{
		 	key6_flag=1;
			send_flag=1;	
			accept_time=0;
		}
		if(key6_pulse==0xff)
		{
		    key6_time++;
			if(key6_time>10)
			{
			  key6_time=0;
			  key6_flag=1;
			  send_flag=1;
			  accept_time=0;
			}
		}
		if(PINC& key7)
		{
		 	key7_pulse<<=1;
    		key7_pulse&=0xfe;
		}
		if(!(PINC& key7))
		{
			key7_pulse<<=1;
    		key7_pulse|=0x01;
		}
		if(key7_pulse==0x0f)
		{
		 	key7_flag=1;
			send_flag=1;	
			accept_time=0;
		}
		if(key7_pulse==0xff)
		{
		    key7_time++;
			if(key7_time>10)
			{
			  key7_time=0;
			  key7_flag=1;
			  send_flag=1;
			  accept_time=0;
			}
		}
		if(PINC& key8)
		{
		 	key8_pulse<<=1;
    		key8_pulse&=0xfe;
		}
		if(!(PINC& key8))
		{
			key8_pulse<<=1;
    		key8_pulse|=0x01;
		}
		if(key8_pulse==0x0f)
		{
//		 	key8_flag=1;
//			send_flag=1;	
			accept_time=0;
		}
		if(key8_pulse==0xff)
		{
		    key8_time++;
			if(key8_time>50)
			{
			  key8_time=0;
			  key8_long_flag=1;
			  send_flag=0;
			  accept_time=0;
			}
		}
//=================================
 		}
}

⌨️ 快捷键说明

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