NRF24L01数据通信C程序

2023-05-16

NRF24l01

产品性能:

1) 2.4GHZ全球开放ISM频段免许可使用

2) 最高工作速率2Mbps,GFSK高效调制

3) 125个频道满足多点通讯和跳频通讯需求

4) 1.9-3.6V工作,低功耗,待机模式仅1uA.

5) 双通道数据接收,内置环行天线,体积仅17*34mm,通信距离在100m之内,软件编简单。

7) 内置硬件8/16位CRC校验,收发中断标志,每次可发28字节

备注:1。免费提供开发源代码

           2。提供廉价开发套件

          3。提供全面技术支持和即使方案

         4。任何单片机都可实现对NRF系列模块的数据收发控制,

#define uchar unsigned char

#define TX_ADR_WIDTH    5   // 5 bytes TX(RX) address width

#define TX_PLOAD_WIDTH  20  // 20 bytes TX payload

uchar const TX_ADDRESS[TX_ADR_WIDTH]  = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address

uchar rx_buf[TX_PLOAD_WIDTH];     //接收缓冲区

uchar tx_buf[TX_PLOAD_WIDTH];      //发送缓冲区

uchar flag;

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

//管脚定义

sbit CE =  P1^0;

sbit CSN=  P1^1;

sbit SCK=  P1^2;

sbit MOSI= P1^3;

sbit MISO= P1^4;

sbit IRQ = P1^5;

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

uchar  bdata sta;

sbit RX_DR =sta^6;

sbit TX_DS =sta^5;

sbit MAX_RT =sta^4;

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

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

Function: init_io();

Description:

  flash led one time,chip enable(ready to TX or RX Mode),

  Spi disable,Spi clock line init high

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

#define KEY 0xaa

void init_io(void)

{

 P0=KEY;  // led light

 CE=0;   // chip enable

 CSN=1;   // Spi disable

 SCK=0;   // Spi clock line init high

 P0=0xff;  // led close

}

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

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

Function: Inituart();

Description:

  set uart working mode

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

void Inituart(void)

{

 TMOD = 0x20;    //timer1 working mode 1

 TL1 = 0xfd;     //f7=9600 for 16mhz Fosc,and ...

 TH1 = 0xfd;     //...fd=19200 for 11.0592mhz Fosc

 SCON = 0xd8;    //uart mode 3,ren==1

 PCON = 0x80;    //smod=0

 TR1 = 1;     //start timer1

}

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

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

Function: init_int0();

Description:

  enable int0 interrupt;

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

void init_int0(void)

{

 EA=1;

 EX0=1;      // Enable int0 interrupt.

}

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

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

Function: delay100();

Description:

  delay 100ms

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

void delay(uchar )

{

 uchar  x;

 uchar  y;

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

 {

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

  _nop_();

 }

}

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

void delay_ms(unsigned int x)

{

    unsigned int i,j;

    i=0;

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

    {

       j=108;

           ;

       while(j--);

    }

}

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

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

Function: SPI_RW();

Description:

  Writes one byte to nRF24L01, and return the byte read

  from nRF24L01 during write, according to SPI protocol

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

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'

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

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

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

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

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

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.

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

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.

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

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;

}

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

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

Function: check_ACK();

Description:

  check if have "Data sent TX FIFO interrupt",if TX_DS=1,

  all led light and after delay 100ms all led close

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

void check_ACK()

{

 uchar test;

 test=SPI_Read(READ_REG+STATUS); // read register STATUS's

 test=test&0x20;     // check if have Data sent TX FIFO interrupt (TX_DS=1)

 if(test==0x20)     // TX_DS =1

 {

  P0=0x00;     // turn on all led

     delay100();     // delay 100ms

  P0=0xff;

 }

}

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

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

Function: TxData();

Description:

  write data x to SBUF

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

void TxData (uchar x)

{

 SBUF=x;   // write data x to SBUF

 while(TI==0);

  TI=0;

}

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

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

Function: CheckButtons();

Description:

  check buttons ,if have press,read the key values,

  turn on led and transmit it;  after transmition,

  if received ACK, clear TX_DS interrupt and enter RX Mode;

  turn off the led

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

void CheckButtons()

{

 uchar Temp,xx,Tempi;

 P0=0xff;

 Temp=P0&KEY;            //read key value from port P0

 if (Temp!=KEY)

 {

  delay_ms(10);

  Temp=P0&KEY;    // read key value from port P0

  if (Temp!=KEY)

  {

    xx=Temp;

    Tempi=Temp>>1;  // Left shift 4 bits

    P0=Tempi;      // Turn On the led

    tx_buf[0]=Tempi; // Save to tx_buf[0]

    TX_Mode();   // set TX Mode and transmitting

    TxData(xx);   // send data to uart

    //check_ACK();  // if have acknowledgment from RX device,turn on all led

    SPI_RW_Reg(WRITE_REG+STATUS,SPI_Read(READ_REG+STATUS)); // clear interrupt flag(TX_DS)

    delay_ms(500);

    P0=0xff;   // Turn off the led   

    RX_Mode();   // set receive mode

    while((P0&KEY)!=KEY);

  }

 }

}

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

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

Function: main();

Description:

  control all subprogrammes;

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

void main(void)

{

 uchar xx;

 init_io();  // Initialize IO port

 Inituart();  // initialize 232 uart

 //init_int0(); // enable int0 interrupt

 RX_Mode();  // set RX mode

 while(1)

 {

  CheckButtons(); // scan key value and transmit

  sta=SPI_Read(STATUS); // read register STATUS's value

     if(RX_DR)    // if receive data ready (RX_DR) interrupt

       {

     SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer

     flag=1;

          }

     if(MAX_RT)

       {

     SPI_RW_Reg(FLUSH_TX,0);

          }

  SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag

  if(flag)  // finish received

  {

   flag=0;  // set flag=0

   P0=rx_buf[0]; // turn on led

   delay_ms(500);

     P0=0xff;  // turn off led

   xx=rx_buf[0]>>1;// right shift 4 bits

   TxData(xx);  // send data to uart

  }

 }

}

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

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

Function: ISR_int0() interrupt 0;

Description:

  if RX_DR=1 or TX_DS or MAX_RT=1,enter this subprogram;

  if RX_DR=1,read the payload from RX_FIFO and set flag;

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

void ISR_int0(void) interrupt 0

{

 sta=SPI_Read(STATUS); // read register STATUS's value

 if(RX_DR)    // if receive data ready (RX_DR) interrupt

 {

  SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer

  flag=1;

 }

 if(MAX_RT)

 {

  SPI_RW_Reg(FLUSH_TX,0);

 }

  SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag

}

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NRF24L01数据通信C程序 的相关文章

  • Requests.request()方法分享【一】

    最近参加了一次新公司测试团队技术分享会 xff0c 有大佬分享了关于接口自动化框架 python 43 requests 43 ddt 43 unittest 43 jenkins xff0c 印象很深刻的是他的脚本测试用例的设计和requ
  • 指针 Swap交换函数

    64 努力的张张 的C 练习 数组 指针地址传递 Swap函数 首先 xff0c 我们先来看一下普通值传递和地址传递的区别 函数间普通值传递 上代码 xff1a span class token macro property span cl
  • 用两个栈实现一个队列【C语言】

    问题描述 xff1a 考虑用两个栈实现队列这样的特殊结构 问题分析 xff1a 我们靠两个栈实现队列 xff0c 肯定是一个用来存放入队的数据 xff0c 一个用来出栈 xff0c 在这里我们主要关注这个样几个问题 xff1a 什么时候队列
  • 数据库安全 --- 创建登录名 用户+配置权限【笔记】

    项目场景 xff1a 创建用户和给用户授权 解决方案 xff1a 1 创建用户 至此用户才创建成功 xff1a 2 配置权限 把查询Student表权限授给用户test xff1a 把对Student表和Course表的全部权限授予用户U2

随机推荐

  • Visual C++6.0 一些编译链接报错解决

    01 VC 43 43 编写图形化界面链接时出现 LIBCD lib crt0 obj error LNK2001 unresolved external symbol main 的解决方案 在我使用VC 43 43 编写一个图形化显示界面
  • lambda表达式【C++】

    lambda表达式 lambda表达式是C 43 43 11最重要也是最常用的一个特性 lambda来源于函数式编程的概念 优点 xff1a 声明式编程风格 xff1a 就地匿名定义目标函数或函数对象 xff0c 不需要额外写一个命名函数或
  • Qt学习笔记 day_03

    目录 十三 自定义代理类的实现1 基于QSpinBox的自定义代理类的实现2 自定义代理类的使用3 xff09 setItemDelegateForColumn 函数的使用注意 十三 自定义代理类的实现 1 基于QSpinBox的自定义代理
  • 版本控制软件SVN

    SVN学习 1 版本控制软件定义及用途 版本控制软件是为适应软件配置管理的需要 xff0c 控制软件的修改 xff0c 减少混乱 xff0c 提高软件生产效率 xff0c 其是软件质量保证的重要环节软件配置管理是对软件修改进行标识 组织和控
  • 螺旋桨的制作图文教程

    一 螺旋桨的一些基础概念 当我们把螺旋桨看成是一个一面旋转一面前进的机翼时 xff0c 就能借助已知的空气动力学常识 xff0c 直观地理解螺旋桨的基本工作原理 1 xff0e 桨距 动力桨距和几何桨距 桨距 xff1a 从广义而言 xff
  • 自制2.4G ELRS接收机,不需要打板,容易制作

    制作难度 xff1a 中等 xff0c 主要是器件太小 xff0c 焊接需要耐心 一 硬件材料 1 LoRa射频模块 xff0c sx1280 xff1a E28 2G4M12S 2 MCU Wifi模块 xff1a ESP 01F 3 各
  • Qt学习笔记 【C++】(4)

    目录 一 Qt中的C 43 43 11标准二 Explicit Linking 和 Implicit Linking三 自动生成的ui xxx ui文件四 常用快捷键 一 Qt中的C 43 43 11标准 Qt 5 中开启C 43 43 1
  • 串口发送接收字符串的C语言代码参考

    通过串口把字符串数据从单片机U1发送到单片机U2 xff0c 通过U2的LCD602显示出来 LCD602显示代码是用的一个比较不错的现成的显示代码 单片机串口传字符串 xff0c 主要是利用字符串的格式的特点 xff0c 在传输中结束串口
  • HTTP协议解析

    HTTP概述 HTTP 全称为 34 超文本传输协议 34 是一种应用非常广泛的应用层协议 我们平时打开一个网站 就是通过 HTTP 协议来传输数据的 HTTP工作过程 xff1a 当我们在浏览器中输入一个 34 网址 34 xff0c 此
  • 《算法导论》学习心得

    第四章 分治策略 xff08 1 xff09 Master Method中case 3中 正则条件 的含义 xff1a 保证f n 在每次递归后都比上一层小 xff08 非递增 xff09 否则显然T n gt f n xff08 2 xf
  • 《算法导论》 第11章部分答案

    注 xff1a 以下答案均为自己思考 xff0c 难免有误 xff0c 欢迎指正 11 3 1 xff1a 将长度为n的链表进行排序 xff0c 将关键值的散列值相同的元素排为相邻 而散列表有点类似于链接法解决冲突的散列表 11 3 2 x
  • 算法刷题心得:动态规划 scramble-string

    牛客网 gt 在线编程 gt letcode gt scramble string Given a string s1 we may represent it as a binary tree by partitioning it to t
  • POJ 1635 Subway tree systems

    题目 xff1a Some major cities have subway systems in the form of a tree i e between any pair of stations there is one and o
  • Openwrt添加定制一个软件包

    我深知前路风雨 xff0c 但我依然微笑前行 Openwrt的Makefile流程异于一般常用的Makefile xff0c 阅读起来难度太大 但是我么可以先通过如何使用 修改Makefile开始 xff0c 从Makefile的某个局部开
  • 无人机通信(WiFI/3G/4GLTE)

    无人机通信 xff08 WiFI 3G 4GLTE xff09 DJI 大疆创新的无人机可实时操控执行各项任务 Phantom3 还内置了全新的 Lightbridge 高清图传系统 xff0c 使飞机所拍摄的实时图像可远距离传输到移动设备
  • realsense D430 python采集深度图像,并保存为txt及pcd点云,用于open3D后处理

    D430点云是x y z 将realsense D430保存的点云文件 pcd 需要对数据进行处理 废话不多说 直接上代码 span class token comment coding utf 8 span span class toke
  • 隐藏符号 __dso_handle 问题

    这几天要给项目做移植 xff0c 重写了下Makfile 项目原是使用autoconf配置的 xff0c 但在新环境下对autoconf的支持不好 Makefile编写基本按autoconf生成的Makefile来的 xff0c 编译选项等
  • 类模板成员函数

    模板类的头文件 span class token macro property span class token directive hash span span class token directive keyword ifndef s
  • C++ 在.h文件中包含头文件和在.cpp文件中包含头文件的原则

    1 第一个原则 xff1a 如果可以不包含头文件 xff0c 那就不要包含了 xff0c 这时候前置声明可以解决问题 如果使用的仅仅是一个类的指针 xff0c 没有使用这个类的具体对象 xff08 非指针 xff09 xff0c 也没有访问
  • NRF24L01数据通信C程序

    NRF24l01 产品性能 xff1a 1 xff09 2 4GHZ全球开放ISM频段免许可使用 2 xff09 最高工作速率2Mbps GFSK高效调制 3 xff09 125个频道满足多点通讯和跳频通讯需求 4 xff09 1 9 3