Arduino NRF24L01 应用实例(32Bytes)
发布时间:2020-06-15 17:49:00
来源:51CTO
阅读:195
作者:firswof
Arduino(NANO) NRF24L01 应用实例
软件准备:
microBox 库(https://github.com/wastel7/microBox):构建一个微型的命令行系统
Mirf 库(https://github.com/E-elektronic/Mirf):提供RF相关的核心功能
以上库请自行下载
硬件准备:
Arduino NANO板 x 2; NRF24L01 x 2; Other;
开发环境:
VS2017 + Arduino IDE(1.8.4)
硬件连接:
Server、Client 与 NRF24L01模块的连接方式完全相同。接线次序:VCC:3.3V; GND:GND; CSN: 7; CE:8; MOSI:11; SCK:13; IRQ:不接; MISO:12;
注意:
1.microBox库需要做修改方能试验本实例,修改如下:
microBox.h:
#define MAX_CMD_NUM 128 //设置合适值以正确接受输入内容
#define MAX_CMD_BUF_SIZE 128 //根据需要设置
2.经过简单性能测试,该例程可最快 50ms发收一次(发送32Byte并且接收32Byte)
3.某些情况下可能会发送失败,经测试发现是 Client端的问题,解决方法:对Client端进行复位,现以提供 “reset”指令以支持软复位
Client 代码(client.ino):
#include "microBox.h"
#pragma region UserDefined
//#define DEBUG
#undef DEBUG
#ifndef ADD_RF
#define ADD_RF
#endif // !ADD_RF
#ifdef ADD_RF
/*
* Hardware SPI:
* MISO -> 12
* MOSI -> 11
* SCK -> 13
*
* Configurable:
* CE -> 8
* CSN -> 7
*/
#include
#include "Mirf.h"
#include "nRF24L01.h"
#include "MirfHardwareSpiDriver.h"
#define ulong unsigned long
#define dword ulong
uint8_t code[32] = { 0 };
void(*ResetInternal) (void) = 0;
//CRC-8, Widh:8, Poly:0x07, Init: 0x00
//RefIn:False, RefOut:False, XorOut:0x00
byte CalcCRC8(byte * pdata, unsigned int len)
{
byte crc = 0x00;
for (int i = 0; i < len; i++)
{
crc ^= ((*pdata++) & 0xFF);
for (byte j = 0; j < 8; j++)
{
if (crc & 0x80)
{
crc <<= 1;
crc ^= 0x07;
}
else
{
crc <<= 1;
}
}
}
return crc;
}
bool RfSend_Internal(uint8_t * data)
{
int i = 0;
unsigned long time = m