2.4G应用
发射端:
/*********************************************************************
巫妖王V3.0开发板例程
朱有鹏物联网大讲堂出品(www.zhulaoshi.org)
技术支持和学习讨论请加朱有鹏单片机学习1群 214959925获取
**********************************************************************
模块名称:nRF24L01
模块功能: 2401无线收发模块
硬件接线:
注意事项:(1)
版 本:V1.00
作 者:朱有鹏(AstonZhu)
时 间:2018.10.30
**********************************************************************/
/*
**********************************************************************
* 头文件包含
**********************************************************************
*/
#include "2401.h"
/*
**********************************************************************
* 本地全局变量
**********************************************************************
*/
sbit CE = P1^0;
sbit CSN = P1^1;
sbit SCK = P1^2;
sbit MOSI = P1^3;
sbit MISO = P1^4;
sbit IRQ = P1^5;
u8 TX_ADDRESS[TX_ADR_WIDTH]= {0xE7,0xE7,0xE7,0xE7,0xE7}; //本地地址
u8 RX_ADDRESS[RX_ADR_WIDTH]= {0xE7,0xE7,0xE7,0xE7,0xE7}; //接收地址
/*
**********************************************************************
* 内部函数原型声明
**********************************************************************
*/
/********************底层驱动函数源码******************************/
//----------------------------------------------
//发送一字节[MOSI和MISO数据传递]
//----------------------------------------------
u8 NRF24L01SpiSendByte(u8 dat)
{
u8 i;
for (i=0; i<8; i++) //output 8-bit
{
//写入1位数据
MOSI = (dat & 0x80); //output 'uchar', MSB to MOSI
dat <<= 1; // shift next bit into MSB..
//读取1位数据
SCK = 1; // Set SCK high..
if (MISO)
{
dat |= 1;
}
else
{ // capture current MISO bit
dat &= 0xFE;
}
SCK = 0; // ..then set SCK low again
}
return (dat); // return read uchar
}
//----------------------------------------------
//延时n个10us
//----------------------------------------------
void DelayN10us(u16 n)
{
for (; n>0; n--)
{
u8 a,b;
for (b=1; b>0; b--)
for (a=2; a>0; a--);
}
}
//----------------------------------------------
//NRF24L01检测是否存在
//----------------------------------------------
u8 NRF24L01Check(void)
{
u8 bu[5] = {0XA5, 0XA5, 0XA5, 0XA5, 0XA5};
u8 bu1[5];
u8 i;
SpiWriteBuf(WRITE_REG+TX_ADDR, bu, 5); //写入5个字节的地址.
SpiReadBuf(TX_ADDR, bu1, 5); //读出写入的地址
for (i=0; i<5; i++)
if (bu1[i] != 0XA5)
break;
if (i != 5)
return 1; //NRF24L01不在位
return 0; //NRF24L01在位
}
//----------------------------------------------
//NRF24L01初始化
//----------------------------------------------
void InitNRF24L01(void)
{
u8 buf[5] = {0};
DelayN10us(10);
CE = 0; // chip enable
CSN= 0; // Spi disable
SpiReadBuf(TX_ADDR, buf, TX_ADR_WIDTH);//debug 测试原来的本地地址:复位值是:0xE7 0xE7 0xE7 0xE7 0xE7
}
//----------------------------------------------
//函数:uchar SPI_Read(uchar reg)
//功能:NRF24L01的SPI时序,读取指定寄存器的内容
//----------------------------------------------
u8 SpiReadReg(u8 reg)
{
u8 reg_val;
CSN = 0; // CSN low, initialize SPI communication...
NRF24L01SpiSendByte(reg); // Select register to read from..
reg_val = NRF24L01SpiSendByte(0); // ..then read registervalue
CSN = 1; // CSN high, terminate SPI communication
return (reg_val); // return register value
}
//----------------------------------------------
//函数:uchar SPI_Write(uchar reg)
//功能:NRF24L01的SPI时序,写入指定寄存器的内容
//----------------------------------------------
u8 SpiWriteReg(u8 reg, u8 value)
{
u8 status = 0;
CSN = 0; // CSN low, init SPI transaction
status = NRF24L01SpiSendByte(reg); // select register
NRF24L01SpiSendByte(value); // ..and write value to it..
CSN = 1; // CSN high again
return (status); // return nRF24L01 status uchar
}
//----------------------------------------------
//函数:uint SpiReadBuf
//功能: 用于读数据
//参数:reg:为寄存器地址
// pBuf:为待读出数据地址
// uchars:读出数据的个数
//----------------------------------------------
u8 SpiReadBuf(u8 reg, u8 *pBuf, u8 Len)
{
u16 status, i;
CSN = 0; // Set CSN low, init SPI tranaction
status = NRF24L01SpiSendByte(reg); // Select register to write to and read status uchar
for (i=0; i<Len; i++)
{
pBuf[i] = NRF24L01SpiSendByte(0);
}
CSN = 1;
return (status); // return nRF24L01 status uchar
}
//----------------------------------------------
//函数:uint SpiWriteBuf
//功能: 用于写数据
//参数:reg:为寄存器地址
// pBuf:为待写入数据地址
// uchars:写入数据的个数
//----------------------------------------------
u8 SpiWriteBuf(u8 reg, u8 *pBuf, u8 Len)
{
u16 status, i;
CSN = 0; //SPI使能
status = NRF24L01SpiSendByte(reg);
for (i=0; i<Len; i++)
{
NRF24L01SpiSendByte(*pBuf);
pBuf++;
}
CSN = 1; //关闭SPI
return (status);
}
//----------------------------------------------
//函数:RxMode
//功能:数据接收配置
//----------------------------------------------
void RxMode(void)
{
u8 buf[5] = {0};
CE = 0;
SpiReadBuf(TX_ADDR, buf, TX_ADR_WIDTH); //debug 测试原来的本地地址:复位值是:0xE7 0xE7 0xE7 0xE7 0xE7
//SPI_WR_Reg(WRITE_REG + CONFIG, 0x03);//SPI_WR_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC ,主接收
//SPI_Write_Buf(WRITE_REG + TX_ADDR,