新聞中心

EEPW首頁(yè) > 手機(jī)與無線通信 > 設(shè)計(jì)應(yīng)用 > 2.4G無線發(fā)射模塊nRF2402應(yīng)用

2.4G無線發(fā)射模塊nRF2402應(yīng)用

作者: 時(shí)間:2016-09-07 來源:網(wǎng)絡(luò) 收藏

  花了我一個(gè)下午的時(shí)間,終于調(diào)試好了無線發(fā)射模塊,有一點(diǎn)值得注意的,是單獨(dú)發(fā)射模塊,單通道的,具體應(yīng)用和nRF2401差不多,就是值得注意的是配置控制字(Configuration Word)不一樣。

本文引用地址:http://www.butianyuan.cn/article/201609/296667.htm

 

  模塊引腳圖如下:

  

 

  值得注意到是nRF2402配置控制字和nRF2401不一樣,具體看datasheet,我摘錄了一部分如下

  

 

  有4個(gè)字節(jié)的控制字,其中D0-D19是需要配置的,D31-D20空,可以配置為0.

  源程序:

  /***********************************************************/

  /*nRF2402學(xué)習(xí)板V1.0 */

  /*創(chuàng)建人 :鄭 文(ClimberWin) 日期:2010-02-20 */

  /*功能描述:nRF2402無線數(shù)據(jù)傳輸 */

  /*芯片 :STC89C51RC 晶振 11.0592M */

  /*版本 :V1.0 */

  /***********************************************************/

  //測(cè)試OK,功能描述:

  //8個(gè)LED接在P0口,低電平有效

  //按鍵1是LED加 按鍵2LED減 按鍵3 LED全亮 按鍵4 LED全滅

  //串口輸出數(shù)據(jù)

  //添加串口發(fā)送數(shù)據(jù)給單片機(jī)再無線發(fā)送

  #include

  #include

  #include

  #define uchar unsigned char

  #define uint unsigned int

  #define ulong unsigned long

  //端口定義

  ///////LED定義//////////

  sbit LED1 = P0^0;

  sbit LED2 = P0^1;

  sbit LED3 = P0^2;

  sbit LED4 = P0^3;

  sbit LED5 = P0^4;

  sbit LED6 = P0^5;

  sbit LED7 = P0^6;

  sbit LED8 = P0^7;

  ///////按鍵定義////////////

  sbit K1 = P3^3;

  sbit K2 = P3^4;

  sbit K3 = P3^5;

  sbit K4 = P3^6;

  sbit speaker=P3^7;//蜂鳴器輸出引腳

  /*****************************************/

  void speakertest(void); //蜂鳴器程序

  void init_rs232(void); //串口初始化

  void keytest(void); //按鍵測(cè)試程序

  /*****************************************/

  /* 串口通信初始化設(shè)置 */

  void init_rs232(void)

  {

  SCON=0x50;

  PCON=0x00;

  TH1=0xFD; //波特率 9600 11.0592MHZ 時(shí)候

  TL1=0XFD;

  TMOD=0X20;

  TR1=1;

  }

  void speakertest(void)//蜂鳴器程序

  {

  unsigned char i;

  for(i=0;i<100;i++)

  {

  speaker=0;

  delayms(1);

  speaker=1;

  delayms(1);

  }

  }

  /*****************************************/

  void keytest(void) //按鍵測(cè)試程序

  {

  ////////按鍵K1判斷程序////////

  while(K1==0)

  {

  delayms(100);//按鍵去抖

  if(K1==1)

  {

  nRF2402_flag|=0x02; //按鍵標(biāo)志位nRF2402_flag_bit1=1

  speakertest();

  nRF2402_Data[0]++;

  }

  else

  {;}

  }

  /////////////////////////////

  ////////按鍵K2判斷程序////////

  while(K2==0)

  {

  delayms(100);//按鍵去抖

  if(K2==1)

  {

  nRF2402_flag|=0x02; //按鍵標(biāo)志位nRF2402_flag_bit1=1

  speakertest();

  nRF2402_Data[0]--;

  }

  else

  {nRF2402_flag&=0xfd; } //清除按鍵標(biāo)志位nRF2402_flag_bit1=0

  }

  /////////////////////////////

  ////////按鍵K3判斷程序////////

  while(K3==0)

  {

  delayms(100);//按鍵去抖

  if(K3==1)

  {

  nRF2402_flag|=0x02; //按鍵標(biāo)志位nRF2402_flag_bit1=1

  speakertest();

  nRF2402_Data[0]=0x00;

  }

  else

  {nRF2402_flag&=0xfd; } //清除按鍵標(biāo)志位nRF2402_flag_bit1=0

  }

  /////////////////////////////

  ////////按鍵K4判斷程序////////

  while(K4==0)

  {

  delayms(100);//按鍵去抖

  if(K4==1)

  {

  nRF2402_flag|=0x02; //按鍵標(biāo)志位nRF2402_flag_bit1=1

  speakertest();

  nRF2402_Data[0]=0xff;

  }

  else

  {nRF2402_flag&=0xfd; } //清除按鍵標(biāo)志位nRF2402_flag_bit1=0

  }

  if((nRF2402_flag&0x02)==0x02)//判斷是否有按鍵按下

  {

  P0=~nRF2402_Data[0];

  nRF2402_Transmit_Mode(); //設(shè)置為發(fā)送模式

  nRF2402_Transmit_Data(nRF2402_Data); //發(fā)送數(shù)據(jù)

  nRF2402_flag&=0xfd; //清除按鍵標(biāo)志位nRF2402_flag_bit1=0

  }

  else

  {;}

  }

  void main(void)

  {

  delayms(1000);//開機(jī)延時(shí)

  nRF2402_Data[0] = 0x55;

  init_rs232(); //串口初始化

  TI=1; //串口發(fā)射有效

  nRF2402_config(); //nRF2402初始化配置

  delayms(100);

  speakertest(); //蜂鳴器叫一聲

  nRF2402_Transmit_Mode(); //設(shè)置為發(fā)送模式

  nRF2402_Transmit_Data(nRF2402_Data);//發(fā)送測(cè)試數(shù)據(jù)

  SBUF=0x55; //串口輸出0x55 測(cè)試是否有正確的串口數(shù)據(jù)

  delayms(100);

  while (1)

  {

  keytest(); //調(diào)用按鍵子程序,無線發(fā)送數(shù)據(jù)

  }

  }

  ///////////////////////////////////////////////////////////////////////////////////////////////

  頭文件:nRF2402.h

  /*************************************************************/

  /**********************中文版本*******************************/

  /*****功能描述 : nRF2402無線收發(fā)模塊應(yīng)用頭文件 *****/

  /*****調(diào)試平臺(tái) : 51單片機(jī) 11.0592M晶振 *****/

  /*****作 者 : 鄭文(ClimberWin) *****/

  /*****編寫日期 : 2010年02月20日 *****/

  /*****版本信息 : V1.0 *****/

  /*****修改日期 : *****/

  /*************************************************************/

  #ifndef __nRF2402_H__

  #define __nRF2402_H__

  #include

  #include

  #define uchar unsigned char

  #define uint unsigned int

  sbit nRF2402_PWR_UP = P1^4;

  sbit nRF2402_CE = P1^5;

  sbit nRF2402_CS = P1^3;

  //////////////////////

  sbit nRF2402_CLK = P1^6;

  sbit nRF2402_DIN = P1^2;

  //////////////////////

  //Configuration Word datasheet(page17 of 37)

  //Reserved for testing

  #define ADDR_W 2*8 //發(fā)送/接收地址寬度(單位:Bit)

  #define DATA1_W 28*8 //通道1 payload數(shù)據(jù)長(zhǎng)度設(shè)置(bit)

  //D31 MSB

  #define PLL 0x00 // D19

  #define UNUSED_bit18 0x01 //未定義 必須置1

  #define PREAMBLE_bit17 0x01 //Logic 0: No generation of Preamble |Logic 1: Preamble generation enabled (default)

  #define UNUSED_bit16 0x01 //未定義 必須置1

  #define CRC_L 0x01 //CRC 模式 0:8 位 1:16 位

  #define CRC_EN 0x01 //CRC 校驗(yàn) 0:禁用 1:啟用

  #define CM 0x01 //0:Direct mode 1:ShockBurst mode

  #define RFDR_SB 0x00 //傳輸速率0:250kbps 1:1Mbps(requires 16MHZ crystal)

  #define XO_F 0x03 //晶振頻率 D11-D9(000:4MHZ 001:8MHZ 010:12MHZ 011:16MHZ 100:20MHZ)

  #define RF_PWR 0x03 //發(fā)射模式的輸出功率D8-D7(00:-20dBm 01:-10dBm 10:-5dBm 11:0dBm)

  #define RF_CH 0x02 //Channel 發(fā)射頻率設(shè)置Channel=2400MHz+RF_CH * 1.0MHz datasheet(page22 of 37)

  uchar code nRF2402_Config_Word[4] =

  {

  0x00,0x07,0xe7,0x82

  };

  uchar code Channel_Addr[]={0x34,0x12,0x00,0x00,0x00};//設(shè)置通道1地址,發(fā)射和接收必須匹配

  uchar nRF2402_Data[32]; //定義nRF2402數(shù)據(jù)發(fā)送接收臨時(shí)存儲(chǔ)地址

  uchar nRF2402_flag;//定義標(biāo)志位,bit7-bit2 暫時(shí)保留 | bit0:是否有接收數(shù)據(jù)標(biāo)志位 1:有 0:無

  //| bit1:是否有按鍵數(shù)據(jù)標(biāo)志位 1:有 0:無

  void delayms(uint count); //延時(shí)程序

  uchar nRF2402_Read_Byte(void); //讀字節(jié)

  void nRF2402_Write_Byte(uchar Send_Byte);//寫字節(jié)

  void nRF2402_config(void); //參數(shù)配置

  void nRF2402_Transmit_Mode(void); //設(shè)置為發(fā)射模式

  void nRF2402_Transmit_Data(uchar TxBuf[]); //nRF2402發(fā)送數(shù)據(jù)子程序

  /*************延時(shí)子程序start*****************/

  void delayms(unsigned int count)

  {

  unsigned int i,j;

  for(i=0;i for(j=0;j<120;j++);

  }

  /**************延時(shí)子程序end****************/

  /************讀數(shù)據(jù)子程序***********/

  uchar nRF2402_Read_Byte(void)

  {

  uchar i,j,temp;

  temp=0x00;

  for (i=0;i<8;i++)

  {

  nRF2402_CLK=1;

  _nop_();

  if (nRF2402_DIN==1) j=1;

  else j=0;

  temp=(temp<<1)|j;

  nRF2402_CLK=0;

  }

  return temp;

  }

  /************寫數(shù)據(jù)子程序***********/

  void nRF2402_Write_Byte(uchar Send_Byte)

  {

  uchar i;

  for (i=0;i<8;i++)

  {

  Send_Byte=Send_Byte<<1;

  nRF2402_DIN=CY; //讀進(jìn)位數(shù)據(jù)

  nRF2402_CLK=1;

  _nop_();

  nRF2402_CLK=0;

  }

  }

  /*********配置參數(shù)子程序***********/

  void nRF2402_config(void)

  {

  uchar i;

  //////stand by/////////////

  nRF2402_PWR_UP=1;

  nRF2402_CE=0;

  nRF2402_CS=0;

  ///////////////////////////// datasheet(page24 of 37)

  delayms(3);// PWR_DWN -> ST_BY_mode | MAX 3ms | name: Tpd2sby |

  nRF2402_CS=1; //進(jìn)入配置模式Configuration

  ////4個(gè)字節(jié)的配置字////////////// datasheet(page17 of 37)

  for (i=0;i<4;i++)

  {

  nRF2402_Write_Byte(nRF2402_Config_Word[i]);

  }

  delayms(1);

  nRF2402_CS=0; //nRF2402_CS 置低使配置有效 datasheet(page26 of 37);

  }

  /*********設(shè)置發(fā)射模式子程序*********/

  void nRF2402_Transmit_Mode(void)

  {

  //設(shè)置為Active(Tx)模式

  nRF2402_PWR_UP=1;

  nRF2402_CE=1;

  nRF2402_CS=0;

  delayms(1);

  }

  /********發(fā)送數(shù)據(jù)子程序*********/

  void nRF2402_Transmit_Data(uchar temp[]) //datasheet(page22 of 37);

  {

  uchar i;

  nRF2402_CE=1;

  delayms(1);

  for (i=0;i<(ADDR_W/8);i++)//寫入接收地址

  {

  nRF2402_Write_Byte(Channel_Addr[i]);

  }

  for (i=0;i<(DATA1_W/8);i++)//寫入需要發(fā)送的數(shù)據(jù)

  {

  nRF2402_Write_Byte(temp[i]);

  }

  nRF2402_CE=0; //nRF2402_CE 置低使發(fā)送有效

  delayms(1);

  }

  #endif



關(guān)鍵詞: 2.4G nRF2402

評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉