Make it My Home Page  |  Bookmark
Now everyone can do wireless. Responsibility promise future.
   
Home About US Products Technology Contact Us Chinese
   
Catalogy
 
Wireless Sensor
RF Module
Bluetooth2.0
Bluetooth audio Module
Bluetooth4.0 Module
Customize Project Show
RFID Active Tag
DMX512 RF Module
Wireless Audio Module
Anti-Losing Solution
Wireless SKYPE
Weather Station
WI-FI FINDER
Wireless Video
AXSEM
Remote Keyless entry
Key Finder Solution
Develop Tools
Wireless Accessory
Remoter
Wireless Mouse/Keyboard
Remote motor
DOCUMENT
 
 
 
 
Home » Technology
 
Technology
 
» SI4432,SI4431,SI4430 Source Code

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME: U8 SpiReadRegister(U8 reg)
  +
  + DESCRIPTION:   This function reads the registers
  +      
  + INPUT:    U8 reg - register address  
  +
  + RETURN:        SPI1DAT - the register content
  +
  + NOTES:         none
  + SI4432, SI4431, SI4430 Source code
  + SI4432,SI4432,SI4430 ²Î¿¼´úÂë,Ô´´úÂë,Ô´³ÌÐò,ÊÕ·¢³ÌÐò
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
u8  SpiRfReadRegister (u8 reg)
{
 u8 tData;
 OpenSpi(); 
 WriteByte(reg);
 tData= ReadByte();
 CloseSpi();
   return tData;
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME: void SpiWriteRegister(U8 reg, U8 value)
  +
  + DESCRIPTION:   This function writes the registers
  +      
  + INPUT:    U8 reg - register address  
  +      U8 value - value write to register 
  +
  + RETURN:        None
  +
  + NOTES:         Write uses a Double buffered transfer
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void SpiRfWriteRegister (u8 reg, u8 value)
{                          
 OpenSpi(); 
  WriteByte(reg|0x80);                      
 WriteByte(value);                                                    
 CloseSpi();
}
//******************************************************************************************
//º¯ÊýÃû:void SpiRfWriteBurst(UINT8 add, UINT8* reg, UINT8 counter)
//¹¦ÄÜ:sipд×Ö·û´®
//²ÎÊý: ÎÞ
// + SI4432, SI4431, SI4430 Source code
//  + SI4432,SI4432,SI4430 ²Î¿¼´úÂë,Ô´´úÂë,Ô´³ÌÐò
//*******************************************************************************************
#define REG_READ (0x00)
#define REG_WRITE (0x80)
void SpiRfWriteBurst(u8 add, u8 *reg, u8 counter)
{
 u8 i;
 OpenSpi(); 
 WriteByte(REG_WRITE|add);
 for(i =0; i<counter;i++)
 {
  WriteByte(reg[i]);
 }
 CloseSpi();
}
//*****************************************************************************************
//º¯ÊýÃû:void SpiRfReadBurest(UINT8 add, UINT8* p, UINT8 counter )
//¹¦ÄÜ:sip¶ÁÈ¡×Ö·û´®
//²ÎÊý: ÎÞ
// + SI4432, SI4431, SI4430 Source code
//  + SI4432,SI4432,SI4430 ²Î¿¼´úÂë,Ô´´úÂë,Ô´³ÌÐò,ÊÕ·¢³ÌÐò
//******************************************************************************************
void SpiRfReadBurest(u8 add, u8 *p, u8 counter )
{
 u8 i;
 OpenSpi(); 
 WriteByte(add);
 for(i =0; i<counter;i++)
 {
  p[i] =  ReadByte();
 }
 CloseSpi();
}

//*************************************************************
//º¯ÊýÃû:void RfChipInit(void)
//¹¦ÄÜ:оƬ³õʼ»¯
//²ÎÊý: ÎÞ
// + SI4432, SI4431, SI4430 Source code
//  + SI4432,SI4432,SI4430 ²Î¿¼´úÂë,Ô´´úÂë,Ô´³ÌÐò,ÊÕ·¢³ÌÐò
//*************************************************************

void RfChipInit(void)
{                 
  SDN = 0;         
  Wait_Time(200);
  ItStatus1 = SpiRfReadRegister(0x03);  //read the Interrupt Status1 register
  ItStatus2 = SpiRfReadRegister(0x04);  //read the Interrupt Status2 register
  SpiRfWriteRegister(0x07, 0x80);   //write 0x80 to the Operating & Function Control1 register
  while ( RF_NIRQ_PIN == 1);
  ItStatus1 = SpiRfReadRegister(0x03);   //read the Interrupt Status1 register
  ItStatus2 = SpiRfReadRegister(0x04);   //read the Interrupt Status2 register 
  while ( RF_NIRQ_PIN == 1); 
  ItStatus1 = SpiRfReadRegister(0x03);  
  ItStatus2 = SpiRfReadRegister(0x04); 

/*ÉèÖÃRF ²ÎÊý:ÖжÏƵÂÊ£¨center frequency£©¡¢·¢ÉäÊý¾ÝËÙÂÊ£¨transmit data rate£©ºÍ·¢ÉäÆ«²î£¨transmit deviation£©*/
/*set the center frequency to 433 MHz*/
    SpiRfWriteRegister(0x75, 0x53);
    SpiRfWriteRegister(0x76, 0x4b);
    SpiRfWriteRegister(0x77, 0x00);
/*set the center frequency to 868 MHz*/
    //SpiRfWriteRegister(0x75, 0x73);
    //SpiRfWriteRegister(0x76, 0x64);
    //SpiRfWriteRegister(0x77, 0x00);
/*set the desired TX data rate 4.8K*/
  SpiRfWriteRegister(0x6E, 0x27);
  SpiRfWriteRegister(0x6F, 0x52);
  SpiRfWriteRegister(0x70, 0x24);

/*set the desired TX deviation */
  SpiRfWriteRegister(0x72, 0x18);

/*½ÓÊÕµÄGFSK µ÷ÖÆÊý¾ÝÅäÖõ÷Öƽâµ÷Æ÷²ÎÊý*/
  SpiRfWriteRegister(0x1C, 0x1D);
  SpiRfWriteRegister(0x20, 0xA1);
  SpiRfWriteRegister(0x21, 0x20);
  SpiRfWriteRegister(0x22, 0x4E);
  SpiRfWriteRegister(0x23, 0xA5);
  SpiRfWriteRegister(0x24, 0x00);
  SpiRfWriteRegister(0x25, 0x1B);
  SpiRfWriteRegister(0x1D,0x40);
/*set the TX power to MAX*/
  SpiRfWriteRegister(0x6D, 0x0f);  //20DBM

/*ÅäÖýÓÊÕÊý¾Ý°ü´¦ÀíÆ÷£¨packet handler£©*/
  SpiRfWriteRegister(0x35, 0x28); //write 0x28 to the Preamble Detection Control register
  SpiRfWriteRegister(0x34, 0x0A); //write 0x0A to the Preamble Length register

/*½ûÖ¹èåÍ·×Ö½Ú£¨±¾Ê¾ÀýÖÐδÓã©£¬ÉèÖÃͬ²½×Ö³¤¶ÈΪÁ½¸ö×Ö½Ú*/
  SpiRfWriteRegister(0x33, 0x02); //write 0x02 to the Header Control2 register
/*Disable the receive header filters*/
  SpiRfWriteRegister(0x32, 0x00 ); //write 0x00 to the Header Control1 register

/*ÉèÖÃͬ²½×ÖÑùʽ£¨pattern£©Îª0x2DD4*/
/*Set the sync word pattern to 0x2DD4 */
  SpiRfWriteRegister(0x36, 0x2D); //write 0x2D to the Sync Word 3 register
  SpiRfWriteRegister(0x37, 0xD4); //write 0xD4 to the Sync Word 2 register

/*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,SI4432,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,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 ²Î¿¼´úÂë,Ô´´úÂë,Ô´³ÌÐò,ÊÕ·¢³ÌÐò
//*************************************************************
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 Êý¾Ý


  if(buff[0]==0x55)     //È·¶¨ÊDz»ÊǶԷ½·¢³öÀ´µÄ
         { P_2_LED = ~P_2_LED; }
    }
  }
}

 


/* Private functions ---------------------------------------------------------*/
// + SI4432, SI4431, SI4430 Source code
//  + SI4432,SI4432,SI4430 ²Î¿¼´úÂë,Ô´´úÂë,Ô´³ÌÐò,ÊÕ·¢³ÌÐò

#define C_SWITCH_PA_DELAY 2
#define C_PA_ACTIVE
void main(void)
{

   #ifdef C_PA_ACTIVE
    RF_TXON=C_RF_TX_DIS;
   RF_RXON=C_RF_RX_DIS;
    #endif
        
   /*Time Initial*/
   Timer0Ini(); 
 /*RF Initial*/
   RfChipInit();
  
   while (1)
   { 
  #ifdef C_SEND_PROGARM

  #ifdef C_PA_ACTIVE
    RF_TXON=C_RF_TX_EN;
   RF_RXON=C_RF_RX_DIS;
    Wait_Time(C_SWITCH_PA_DELAY);
    #endif
  
  RFTransmitMessage(TxBuffer,10);
  #ifdef C_PA_ACTIVE
    RF_TXON=C_RF_TX_DIS;
   RF_RXON=C_RF_RX_DIS;
    #endif
  Wait_Time(500);  //500ms 
  #else
  #ifdef C_PA_ACTIVE
    RF_TXON=C_RF_TX_DIS;
   RF_RXON=C_RF_RX_EN;
    #endif
            RFReceiveReady();    //RF Receive Stauts
          RFGetBuffer(RxBuffer);   //Get data frm Buffer
  #endif
 }
}

 
» NRF24L01+ Source code

/**************************************************
Function: SPI_RW();
Description:
  Writes one byte to nRF24L01, and return the byte read
  from nRF24L01 during write, according to SPI protocol
NRF24L01 Source code,NRF24L01+ Source Code
NRF24L01 源代码,参考代码,源程序,收发程序,NRF24L01+ 源代码,参考代码,源程序,收发程序
/**************************************************/
uchar SPI_RW(uchar byte)
{
 uchar bit_ctr;
    for(bit_ctr=0;bit_ctr<8;bit_ctr++)   // output 8-bit
    {
     MOSI = (byte & 0x80);         // output 'byte', MSB to MOSI
     byte = (byte << 1);           // shift next bit into MSB..
     SCK = 1;                      // Set SCK high..
     byte |= MISO;           // capture current MISO bit
     SCK = 0;                // ..then set SCK low again
    }
    return(byte);               // return read byte
}
/**************************************************/

/**************************************************
Function: SPI_RW_Reg();

Description:
  Writes value 'value' to register 'reg'
/**************************************************/
uchar SPI_RW_Reg(BYTE reg, BYTE value)
{
 uchar status;

   CSN = 0;                   // CSN low, init SPI transaction
   status = SPI_RW(reg);      // select register
   SPI_RW(value);             // ..and write value to it..
   CSN = 1;                   // CSN high again

   return(status);            // return nRF24L01 status byte
}
/**************************************************/

/**************************************************
Function: SPI_Read();
Description:
  Read one byte from nRF24L01 register, 'reg'
NRF24L01 Source code,NRF24L01+ Source Code
NRF24L01 源代码,参考代码,源程序,收发程序,NRF24L01+ 源代码,参考代码,源程序,收发程序
/**************************************************/
BYTE SPI_Read(BYTE reg)
{
 BYTE reg_val;

   CSN = 0;                // CSN low, initialize SPI communication...
   SPI_RW(reg);            // Select register to read from..
   reg_val = SPI_RW(0);    // ..then read registervalue
   CSN = 1;                // CSN high, terminate SPI communication

   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

   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..

   CE = 1; // Set CE pin high to enable RX 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

   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..
 CE=1;

}

 
» How to get support from RF world?

Pls send your question and your contact to tech@mcurf.com

 
» What is Wireless, Radio, RF Modules?

Radio module consists of a radio IC and a subsystem MCU. It shielding complicate impedance matching or soldering fine pitch radio technical details as well as sophisticate wireless protocol from application engineering. A well designed radio module will provide simple HW and SW interface for design engineer to easily integrate the wireless solution with their application.

 
 
 
RF Module | Wireless module | 2.4G Radio module | FSK RF Module | BLE4.0 module| WIFI module| GPRS module COPYRIGHT © 2006-2013 RFWORLD All Rights Reserved. Keyword: Wireless Module,RF Module, RFID, IPAD IPHONE BLE Control , DMX512 , Data transmit,BLE4.0,Bluetooth 4.0 module, Bluetooth 2.0 module Contact : Sales@mcurf.com