极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 85240|回复: 67

认真的nrf24l01库 5路接收通道 2月6号_更新用户模式选择

  [复制链接]
发表于 2014-11-6 16:20:37 | 显示全部楼层 |阅读模式
本帖最后由 mylife1213 于 2015-2-6 13:57 编辑


   
        
        更新的库基本重写了,加入软SPI,可以使用任意引脚驱动,也可以选择硬件spi;
        加入数据接收是否启用中断;
        不再是杂乱无章的测试代码,有规划的一个库;
        各个功能实现库分开,下次更新不需要改变函数传入参数;
        支持5路通道接收数据;
        忘记把uint8_t 改成 byte了,uint8_t = byte; 不改也可以用,有uint8_t 的地方都可以用byte替换

        2月6号更新
         加入用户界面基于字符串模式选择
         通过   int set_reg(String reg, String data);进行操作,例句:nrf.set_reg("Rate","2M");这个是设置nrf24l01为2M速率,如果输入错误的模式与命令会返回"1000"(模式错误) ,"1001"(命令错误)
        库已更新-版本号:v1.1

      "Mode"-> "Receive" or "Send" //模式     "Power"-> "up" or "Down"     //电源模式
     "CRC"-> "16bit" or "8bit"   //CRC校验模式
     "RF_Power"->"0dBm"or"-6dBm"or"-12dBm"or"-18dBm"  //功率
     "Rate"->"1M"or"2M"or"250k"   //速率
     "RF_Channel"->数值    //信道


int set_reg(String reg, String data);源代码


  1. int Multiple::set_reg(String reg, String data)
  2. {
  3.   uint8_t tmp;

  4. //-------------命令检查模块-------------     
  5.   
  6.   boolean cmd_tmp=false;
  7.   boolean data_tmp=false;
  8.   boolean cmd_RF_Channel=false;

  9.   String cmd[]={"Mode","Power","CRC","RF_Power","Rate","RF_Channel","NULL"};

  10.   String cmd_data[]={"Receive","Send","up","Down","16bit","8bit","0dBm","-6dBm","-12dBm","-18dBm","1M","2M","250k","NULL"};

  11. for(uint8_t i=0;cmd[i]!="NULL";i++)
  12. {
  13.   if(reg==cmd[i])
  14.      {cmd_tmp=true;}
  15.   if(reg=="RF_Channel")
  16.      {cmd_RF_Channel=true;}
  17. }

  18. if(!cmd_tmp)
  19.    { return 1000;}

  20. for(uint8_t i=0;cmd_data[i]!="NULL";i++)
  21. {
  22.    if(data==cmd_data[i])
  23.      {data_tmp=true;}
  24. }  

  25. if(!cmd_RF_Channel && !cmd_RF_Channel)
  26.   { return 1001;}

  27. //-------------命令检查模块---------------//
  28.   

  29. digitalWrite(_CE,LOW);
  30.   if(reg=="Mode")
  31.     {
  32.        tmp=Read(0x00);
  33.        if(data=="Receive")
  34.            {tmp |=0x01;}
  35.        else if(data=="Send")
  36.            {tmp &=0xfe;}
  37.       
  38.        RW_Reg(WRITE_REG + 0x00,tmp);
  39.      }
  40.   if(reg=="Power")
  41.     {
  42.        tmp=Read(0x00);
  43.        if(data=="up")
  44.           { tmp |=0x02;}
  45.        else if(data=="Down")
  46.           { tmp &=0xfd;}
  47.       
  48.        RW_Reg(WRITE_REG + 0x00,tmp);
  49.      }   
  50.   if(reg=="CRC")
  51.     {
  52.        tmp=Read(0x00);
  53.        if(data=="16bit")
  54.            {tmp |=0x04;}
  55.        else if(data=="8bit")
  56.            {tmp &=0xfb;}
  57.       
  58.        RW_Reg(WRITE_REG + 0x00,tmp);
  59.      }
  60. //-----------------CONFIG--------------

  61.   if(reg=="RF_Power")
  62.     {
  63.        tmp=Read(0x06);
  64.        if(data=="0dBm")
  65.            {tmp |=0x07;}
  66.        else if(data=="-6dBm")
  67.          {  tmp &=0xf9;
  68.            tmp |=0x04; }
  69.        else if(data=="-12dBm")
  70.          {  tmp &=0xf9;
  71.            tmp |=0x02; }
  72.        else if(data=="-18dBm")
  73.          {  tmp &=0xf9;
  74.            tmp |=0x00; }     
  75.        RW_Reg(WRITE_REG + 0x06,tmp);
  76.       }
  77.   if(reg=="Rate")
  78.     {
  79.        tmp=Read(0x06);
  80.        if(data=="1M")
  81.          {  tmp &=0xd7;
  82.            tmp |=0x00; }
  83.        else if(data=="2M")
  84.          {  tmp &=0xd7;
  85.            tmp |=0x08; }
  86.        else if(data=="250k")
  87.          {  tmp &=0xd7;
  88.            tmp |=0x20; }
  89.        RW_Reg(WRITE_REG + 0x06,tmp);
  90.      }
  91.   if(reg=="RF_Channel")
  92.     {
  93.        tmp=atof(data.c_str());

  94.        RW_Reg(WRITE_REG + 0x05,tmp);
  95.      }
  96.   
  97.   
  98. digitalWrite(_CE,HIGH);
  99. return tmp;

  100. }
复制代码





        面向用户开放的库头文件函数:
  1. /**
  2. * Copyright (c) 2015 by Mylife1213 <[url=mailto:[email protected]][email protected][/url]>
  3. * Nrf24l01P library for Arduino.
  4. * This file is free software; you can redistribute it and/or modify
  5. * it under the terms of either the GNU General Public License version 2
  6. * or the GNU Lesser General Public License version 2.1, both as
  7. * published by the Free Software Foundation.
  8. * version v1.0
  9. **/

  10. #ifndef Multiple_H_
  11. #define Multiple_H_

  12. #include "nrf_base.h"

  13. #define uint8_t unsigned char

  14. class Multiple : public nrf_base
  15. {
  16. public:
  17.     /**
  18.      构建函数,使用硬件spi
  19.     **/
  20.     Multiple(uint8_t ce, uint8_t csn);
  21.     /**
  22.      构建函数,使用SOFT SPI
  23.      **/
  24.     Multiple(uint8_t ce, uint8_t csn,uint8_t clk,uint8_t mosi,uint8_t miso);
  25.      /**
  26.      设置成读取模式,参数: p1接收主地址,p2_p5其余接收地址首位
  27.      **/
  28.      uint8_t read_mode(uint8_t* p1, uint8_t* p2_p5);
  29.      /**
  30.       设置使用中断模式
  31.       **/
  32.      void Interrup(uint8_t pin,void (*func)() );
  33.      /**
  34.       中断模式下的接收数据读取,channel通道,data读取的数组
  35.      **/
  36.      uint8_t Interrup_read(uint8_t channel,uint8_t* data);
  37.      /**
  38.       普通模式下的数据读取,channel通道,data数据数组
  39.       返回值 1:收到数据  0:没有数据
  40.       **/
  41.      uint8_t read(uint8_t channel,uint8_t* data);
  42.      /**
  43.       设置发送模式,参数p0:发送的地址数组
  44.       **/
  45.      uint8_t send_mode(uint8_t* p0);
  46.      /**
  47.       发送数据,addr:发送地址  data:发送数据数组
  48.       **/
  49.      uint8_t send(uint8_t* addr, uint8_t* data);
  50.      /**
  51.       手动修改寄存器,reg:修改的寄存器 data:数值
  52.      **/
  53.      uint8_t set_reg(uint8_t reg, uint8_t data);
  54.       
  55.      /**
  56.       寄存器调试变量,需设置模式前启用
  57.       **/

  58.       uint8_t _EN_AA;
  59.       uint8_t _EN_RXADDR;
  60.       uint8_t _SETUP_RETR;
  61.       uint8_t _RF_CH;
  62.       uint8_t _RF_SETUP;
  63.       uint8_t _SETUP_AW;
  64.       uint8_t _CONFIG;
  65.       uint8_t _RX_PW;

  66.       uint8_t _TX_EN_AA;
  67.       uint8_t _TX_EN_RXADDR;
  68.       uint8_t _TX_SETUP_RETR;
  69.       uint8_t _TX_RF_CH;
  70.       uint8_t _TX_RF_SETUP;
  71.       uint8_t _TX_SETUP_AW;
  72.       uint8_t _TX_CONFIG;
  73.       uint8_t _TX_PW;


  74. private:
  75.    
  76. };


  77. #endif
复制代码



已带不同场景例程,全部有进行测试



例程代码:
Spi_send 使用硬件spi进行发送
  1. /**
  2. * Copyright (c) 2015 by Mylife1213 <[url=mailto:[email protected]][email protected][/url]>
  3. * Nrf24l01P library for Arduino.
  4. * This file is free software; you can redistribute it and/or modify
  5. * it under the terms of either the GNU General Public License version 2
  6. * or the GNU Lesser General Public License version 2.1, both as
  7. * published by the Free Software Foundation.
  8. * version v1.0
  9. **/
  10.   
  11.   
  12. /**
  13. nrf24l01   arduino
  14. csk->      csk
  15. mosi->     mosi
  16. miso->     miso
  17. ce->      用户设置   
  18. csn->     用户设置
  19. **/

  20. #include"Multiple.h"

  21. Multiple nrf(9,8);//构建函数,参数分别为CE,CSN

  22. uint8_t tx_addr[] = { 0x03, 0xFF, 0xFF, 0xFF, 0xFF};//发送地址
  23. uint8_t tx_data[32]={1,2,3,4};//发送的数据,32位

  24. void setup() {
  25.   nrf.send_mode(tx_addr);//设置为发送模式,参数为发送地址
  26. }

  27. void loop() {
  28.    
  29.   tx_data[0]++;
  30.   nrf.send(tx_addr,tx_data);//发送数据,参数:发送地址,数据
  31.   delay(5000);

  32. }
复制代码




例程Soft_spi_send使用软SPI进行发送
  1. /**
  2. * Copyright (c) 2015 by Mylife1213 <[url=mailto:[email protected]][email protected][/url]>
  3. * Nrf24l01P library for Arduino.
  4. * This file is free software; you can redistribute it and/or modify
  5. * it under the terms of either the GNU General Public License version 2
  6. * or the GNU Lesser General Public License version 2.1, both as
  7. * published by the Free Software Foundation.
  8. * version v1.0
  9. **/

  10. /**
  11. nrf24l01   arduino
  12. csk->     用户设置
  13. mosi->    用户设置
  14. miso->    用户设置
  15. ce->      用户设置   
  16. csn->     用户设置
  17. **/

  18. #include"Multiple.h"

  19. Multiple nrf(9,8,3,4,5);//构建函数,参数分别为CE,CSN,CLK,MOSI,MISO

  20. uint8_t tx_addr[] = { 0x03, 0xFF, 0xFF, 0xFF, 0xFF};//发送地址
  21. uint8_t tx_data[32]={1,2,3,4};//发送的数据,32位

  22. void setup() {
  23.   nrf.send_mode(tx_addr);//设置为发送模式,参数为发送地址
  24. }

  25. void loop() {
  26.   
  27.   tx_data[0]++;
  28.   nrf.send(tx_addr,tx_data);//发送数据,参数:发送地址,数据
  29.   delay(5000);

  30. }
复制代码




例程Read_no_interrup_mode不使用中断接收
  1. /**
  2. * Copyright (c) 2015 by Mylife1213 <[url=mailto:[email protected]][email protected][/url]>
  3. * Nrf24l01P library for Arduino.
  4. * This file is free software; you can redistribute it and/or modify
  5. * it under the terms of either the GNU General Public License version 2
  6. * or the GNU Lesser General Public License version 2.1, both as
  7. * published by the Free Software Foundation.
  8. * version v1.0
  9. **/

  10. /**
  11. nrf24l01   arduino
  12. csk->      csk
  13. mosi->     mosi
  14. miso->     miso
  15. ce->      用户设置   
  16. csn->     用户设置
  17. **/


  18. #include"Multiple.h"

  19. Multiple nrf(9,8);//构建函数,参数分别为CE,CSN

  20. uint8_t rx_addr_p1[6]={ 0x01, 0xFF, 0xFF, 0xFF, 0xFF};//接收地址
  21. uint8_t rx_addr_p2_5[6]={0x02,0x03,0x04,0x05}; /**
  22.                                               通道2至5地址,各地址=各个数
  23.                                               组元素+rx_addr_p1后4位**/

  24. uint8_t rx_data[32];//接收数据存储
  25. uint8_t channel;//接收通道
  26. uint8_t tmp;

  27. void setup() {
  28.   Serial.begin(9600);
  29.   nrf.read_mode(rx_addr_p1, rx_addr_p2_5);//设置为接收模式,参数:通道1地址,通道2-5地址

  30. }

  31. void loop() {
  32.    
  33.    
  34.    
  35.    tmp=nrf.read(channel,rx_data);//读取接收数据,有数据返回1,参数:通道,数据
  36.    if(tmp==1)
  37.    {
  38.      Serial.print("Channel= ");
  39.      Serial.print(channel);
  40.      Serial.print(" rx_data[0]= ");
  41.      Serial.println(rx_data[0]);
  42.    }
  43.    
  44. }
复制代码




例程Read_interrup_mode使用中断进行数据接收
  1. /**
  2. * Copyright (c) 2015 by Mylife1213 <[url=mailto:[email protected]][email protected][/url]>
  3. * Nrf24l01P library for Arduino.
  4. * This file is free software; you can redistribute it and/or modify
  5. * it under the terms of either the GNU General Public License version 2
  6. * or the GNU Lesser General Public License version 2.1, both as
  7. * published by the Free Software Foundation.
  8. * version v1.0
  9. **/
  10.   
  11.   /**
  12. nrf24l01   arduino
  13. csk->      csk
  14. mosi->     mosi
  15. miso->     miso
  16. ce->      用户设置   
  17. csn->     用户设置
  18. isr->     用户设置中断引脚
  19. **/


  20. #include"Multiple.h"

  21. Multiple nrf(9,8);//构建函数,参数分别为CE,CSN

  22. uint8_t rx_addr_p1[6]={ 0x01, 0xFF, 0xFF, 0xFF, 0xFF};//接收地址
  23. uint8_t rx_addr_p2_5[6]={0x02,0x03,0x04,0x05}; /**
  24.                                               通道2至5地址,各地址=各个数
  25.                                               组元素+rx_addr_p1后4位**/

  26. uint8_t rx_data[32];//接收数据存储
  27. uint8_t channel;//接收通道

  28. void setup() {
  29.   Serial.begin(9600);
  30.   nrf.read_mode(rx_addr_p1, rx_addr_p2_5);//设置为接收模式,参数:通道1地址,通道2-5地址
  31.   nrf.Interrup(0, r_data);//设置为中断接收,参数:中断号,中断函数
  32. }

  33. void loop() {

  34. }

  35. void r_data()
  36. {
  37.   nrf.Interrup_read(channel, rx_data);//中断模式下读取数据,参数:通道,接收数据
  38.   Serial.print("Channel: ");
  39.   Serial.print(channel);
  40.   Serial.print("rx_data[0]=");
  41.   Serial.println(rx_data[0]);
  42. }
复制代码




库文件版本v1.1








本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2014-11-6 16:21:43 | 显示全部楼层
转载请注明转载链接
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-6 16:51:46 | 显示全部楼层
原装nrf24l01+带PA空地测试700米
回复 支持 反对

使用道具 举报

发表于 2014-11-6 17:16:27 | 显示全部楼层
这个库能否实现多发一收,或者一发多收,或者既能接收也能发送
回复 支持 反对

使用道具 举报

发表于 2014-11-6 17:25:26 | 显示全部楼层
就这些功能了?能用多块nrf24l01么?一收一发。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-6 17:28:34 | 显示全部楼层
LINK~ 发表于 2014-11-6 17:16
这个库能否实现多发一收,或者一发多收,或者既能接收也能发送

可以多发一收,库文件有详细说明,配置p2-p5通道地址就行,一发多收也可以实现,但是需要关闭ACK,那样就没有接收状态了,建议你要发几个nrf24l01 ,就顺序发送接收地址
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-6 17:32:06 | 显示全部楼层
ourutopia 发表于 2014-11-6 17:25
就这些功能了?能用多块nrf24l01么?一收一发。

nrf24l01不支持全双工,可以发完以后切换成接收模式,然后接收的那个切换成发送!主要是硬件不允许,只能软件来实现,在下一版可能会加入这个功能,还有nrf24l01+ 独有的ack带数据确认码,原理差不多就是你发一个数据过去,接收端会发回一个已接收ACK和接收端发给你的数据,硬件实现的
回复 支持 反对

使用道具 举报

发表于 2014-11-6 17:35:00 | 显示全部楼层
mylife1213 发表于 2014-11-6 17:32
nrf24l01不支持全双工,可以发完以后切换成接收模式,然后接收的那个切换成发送!主要是硬件不允许,只能软件 ...

我要实现全双工通信,所以要接两块nrf24l01,一块专职发送,一块专职接收~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-6 17:39:56 | 显示全部楼层
ourutopia 发表于 2014-11-6 17:35
我要实现全双工通信,所以要接两块nrf24l01,一块专职发送,一块专职接收~

你数据量很大吗?切换接收和发送模式在数据发送速度应该可以满足大多数应用啊,下一版库再加入类似硬件全双工,基本和两块板子实现没什么差别
回复 支持 反对

使用道具 举报

发表于 2014-11-6 19:10:04 | 显示全部楼层
mylife1213 发表于 2014-11-6 17:39
你数据量很大吗?切换接收和发送模式在数据发送速度应该可以满足大多数应用啊,下一版库再加入类似硬件全双 ...

数据量不大,但是有一路要求信号不能间断,做控制用啊,所以只能再单独开一路回传数据。
回复 支持 反对

使用道具 举报

发表于 2014-11-7 08:01:00 | 显示全部楼层
好东西。。。。。。。。
回复 支持 反对

使用道具 举报

发表于 2014-11-7 09:15:22 | 显示全部楼层
Serial.print(Channel);   Channel怎么关联的??不理解啊
回复 支持 反对

使用道具 举报

发表于 2014-11-7 09:17:17 | 显示全部楼层
原来在库里面定义的啊
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-7 09:20:53 | 显示全部楼层
lm4766 发表于 2014-11-7 09:17
原来在库里面定义的啊

嗯嗯,在.h 头文件定义了全局变量 extern unsigned char Channel; 等到中断启动后读取的都是新数据
回复 支持 反对

使用道具 举报

发表于 2014-11-7 15:39:02 | 显示全部楼层
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-25 23:21 , Processed in 0.044606 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表