/*Enable the receive packet handler and CRC-16 (IBM) check*/
SpiRfWriteRegister(0x30, 0x8D); //write 0x8D to the Data Access Control register
/*enable FIFO mode and GFSK modulation*/
SpiRfWriteRegister(0x71, 0x63);//write 0x63 to the Modulation Mode Control 2 register
/*set the GPIO's according the testcard type*/
SpiRfWriteRegister(0x0C, 0x12); //write 0x12 to the GPIO1 Configuration(set the TX state)
SpiRfWriteRegister(0x0D, 0x15); //write 0x15 to the GPIO2 Configuration(set the RX state)
/*ÉèÖÃAGC ºÍADC ²ÎÊý*/
SpiRfWriteRegister(0x6A, 0x0B); //write 0x0B to the AGC Override 2 register
SpiRfWriteRegister(0x68, 0x04);
SpiRfWriteRegister(0x1F, 0x03);
/*set Crystal Oscillator Load Capacitance register*/
SpiRfWriteRegister(0x09, 0x68); //write 0x68 to the CrystalOscillatorLoadCapacitance register
}
//*************************************************************
//º¯ÊýÃû:void RFTransmitMessage(u8 *DataMessage,u8 Length)
//¹¦ÄÜ: ·¢ËÍÒ»¸öÊý¾Ý½á¹¹
//²ÎÊý: MESSAGE ½á¹¹Ìå
// + SI4432, SI4431, SI4430 Source code
// SI4432,SI4431,SI4430源程序,源代码,参考代码,收发程序 //*************************************************************
void RFTransmitMessage(u8 *DataMessage,u8 Length)
{
//SET THE CONTENT OF THE PACKET
SpiRfWriteRegister(0x3E, Length); //write 8 to the Transmit Packet Length register
//fill the payload into the transmit FIFO
SpiRfWriteBurst(0x7F, DataMessage, Length);
//Disable all other interrupts and enable the packet sent interrupt only.
//This will be used for indicating the successfull packet transmission for the MCU
SpiRfWriteRegister(0x05, 0x04); //write 0x04 to the Interrupt Enable 1 register
SpiRfWriteRegister(0x06, 0x00); //write 0x00 to the Interrupt Enable 2 register
//Read interrupt status regsiters. It clear all pending interrupts and the nIRQ pin goes back to high.
ItStatus1 = SpiRfReadRegister(0x03); //read the Interrupt Status1 register
ItStatus2 = SpiRfReadRegister(0x04); //read the Interrupt Status2 register
//enable transmitter
//The radio forms the packet and send it automatically.
SpiRfWriteRegister(0x07, 0x09);//write 0x09 to the Operating Function Control 1 register
//enable the packet sent interupt only
SpiRfWriteRegister(0x05, 0x04); //write 0x04 to the Interrupt Enable 1 register
//wait for the packet sent interrupt
//The MCU just needs to wait for the 'ipksent' interrupt.
while(RF_NIRQ_PIN == 1);
//read interrupt status registers to release the interrupt flags
ItStatus1 = SpiRfReadRegister(0x03); //read the Interrupt Status1 register
ItStatus2 = SpiRfReadRegister(0x04); //read the Interrupt Status2 register
//wait a bit for showing the LED a bit longer
P_1_LED = ~P_1_LED;
Wait_Time(200);
}
//*************************************************************
//º¯ÊýÃû:void RFReceiveReady(void)
//¹¦ÄÜ: ½øÈë½ÓÊÕÊý¾Ý״̬
//²ÎÊý: ÎÞ
// + SI4432, SI4431, SI4430 Source code
// SI4432,SI4431,SI4430源程序,源代码,参考代码,收发程序 // + SI4432,SI4432,SI4430 ²Î¿¼´úÂë,Ô´´úÂë,Ô´³ÌÐò,ÊÕ·¢³ÌÐò
//*************************************************************
void RFReceiveReady(void)
{
//enable receiver chain
SpiRfWriteRegister(0x07, 0x05);//write 0x05 to the Operating Function Control 1 register
//Enable two interrupts:
// a) one which shows that a valid packet received: 'ipkval'
// b) second shows if the packet received with incorrect CRC: 'icrcerror'
SpiRfWriteRegister(0x05, 0x03); //write 0x03 to the Interrupt Enable 1 register
SpiRfWriteRegister(0x06, 0x00); //write 0x00 to the Interrupt Enable 2 register
//read interrupt status registers to release all pending interrupts
ItStatus1 = SpiRfReadRegister(0x03);//read the Interrupt Status1 register
ItStatus2 = SpiRfReadRegister(0x04);//read the Interrupt Status2 register
}
//*************************************************************
//º¯ÊýÃû:void RFGetBuffer(u8 * buff)
//¹¦ÄÜ: ¶ÔÈ¡buff Êý¾Ý
//²ÎÊý: ×Ö·û´®Ö¸Õë
// + SI4432, SI4431, SI4430 Source code
// + SI4432,SI4432,SI4430 ²Î¿¼´úÂë,Ô´´úÂë,Ô´³ÌÐò,ÊÕ·¢³ÌÐò
// SI4432,SI4431,SI4430源程序,源代码,参考代码,收发程序 //*************************************************************
void RFGetBuffer(u8 *buff)
{
u8 k;
//wait for the packet sent interrupt
//The MCU just needs to wait for the 'ipksent' interrupt.
while(RF_NIRQ_PIN == 1);
//wait for the interrupt event
if( RF_NIRQ_PIN == 0 )
{
//read interrupt status registers
ItStatus1 = SpiRfReadRegister(0x03);//read the Interrupt Status1 register
ItStatus2 = SpiRfReadRegister(0x04);//read the Interrupt Status2 register
//packet received interrupt occurred
if( (ItStatus1 & 0x02) == 0x02 )
{
//disable the receiver chain
SpiRfWriteRegister(0x07, 0x01);//write 0x01 to the Operating Function Control 1 register
//Read the length of the received payload
k= SpiRfReadRegister(0x4B) ; //¶ÁÈ¡Êý¾Ý³¤¶È
//Get the received payload from the RX FIFO
SpiRfReadBurest(0x7F, buff, k); //¶ÁÈ¡fifo Êý¾Ý
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
NRF24L01 Source code,NRF24L01+ Source Code
NRF24L01 源代码,参考代码,源程序,收发程序,NRF24L01+ 源代码,参考代码,源程序,收发程序
/**************************************************/
uchar SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
uchar status,byte_ctr;
CSN = 0; // Set CSN low, init SPI tranaction
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
CSN = 1; // Set CSN high again
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
NRF24L01 Source code,NRF24L01+ Source Code
NRF24L01 源代码,参考代码,源程序,收发程序,NRF24L01+ 源代码,参考代码,源程序,收发程序
/**************************************************/
uchar SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
uchar status,byte_ctr;
CSN = 0; // Set CSN low, init SPI tranaction
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)
SPI_RW(*pBuf++);
CSN = 1; // Set CSN high again
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.
NRF24L01 Source code,NRF24L01+ Source Code
NRF24L01 源代码,参考代码,源程序,收发程序,NRF24L01+ 源代码,参考代码,源程序,收发程序
/**************************************************/
void RX_Mode(void)
{
CE=0;
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
// 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.
NRF24L01 Source code,NRF24L01+ Source Code
NRF24L01 源代码,参考代码,源程序,收发程序,NRF24L01+ 源代码,参考代码,源程序,收发程序
/**************************************************/
void TX_Mode(void)
{
CE=0;
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, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload
ISM频段(Industrial Scientific Medical Band)主要是开放给工业、科学和医用3个主要机构使用的频段。ISM频段属于无许可(Free License)频段,使用者无需许可证,没有所谓使用授权的限制。ISM频段允许任何人随意地传输数据,但是对所有的功率进行限制,使得发射与接收之间只能是很短的距离,因而不同使用者之间不会相互干扰。